From a5b3ea61a6257f131a5017094e05ae2501f59b73 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 7 Apr 2020 20:47:03 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=9A=A7=20Add=20session=20provider=20implem?= =?utf8?q?entation?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/web/FreenetSessionProvider.kt | 50 ++++++++ .../sone/web/FreenetSessionProviderTest.kt | 126 +++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/FreenetSessionProvider.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/FreenetSessionProviderTest.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/web/FreenetSessionProvider.kt b/src/main/kotlin/net/pterodactylus/sone/web/FreenetSessionProvider.kt new file mode 100644 index 0000000..80bacdf --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/FreenetSessionProvider.kt @@ -0,0 +1,50 @@ +/** + * Sone - FreenetSessionProvider.kt - Copyright © 2020 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.web + +import freenet.clients.http.SessionManager +import freenet.clients.http.ToadletContext +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.database.SoneProvider +import java.util.UUID +import javax.inject.Inject + +/** + * [SoneProvider] implementation based on Freenet’s [SessionManager]. + */ +class FreenetSessionProvider @Inject constructor(private val soneProvider: SoneProvider, private val sessionManager: SessionManager) : SessionProvider { + + override fun getCurrentSone(toadletContext: ToadletContext): Sone? = + soneProvider.localSones.singleOrNull() + ?: sessionManager.useSession(toadletContext) + ?.let { it.getAttribute("Sone.CurrentSone") as? String } + ?.let(soneProvider.soneLoader) + ?.takeIf { it.isLocal } + + override fun setCurrentSone(toadletContext: ToadletContext, sone: Sone?) { + if (sone == null) { + sessionManager.useSession(toadletContext) + ?.removeAttribute("Sone.CurrentSone") + } else { + (sessionManager.useSession(toadletContext) + ?: sessionManager.createSession(UUID.randomUUID().toString(), toadletContext)) + ?.setAttribute("Sone.CurrentSone", sone.id) + } + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/FreenetSessionProviderTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/FreenetSessionProviderTest.kt new file mode 100644 index 0000000..835b14d --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/FreenetSessionProviderTest.kt @@ -0,0 +1,126 @@ +/** + * Sone - FreenetSessionProviderTest.kt - Copyright © 2020 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.web + +import com.google.inject.Guice +import freenet.clients.http.SessionManager +import freenet.clients.http.ToadletContext +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.data.impl.IdOnlySone +import net.pterodactylus.sone.database.SoneProvider +import net.pterodactylus.sone.test.deepMock +import net.pterodactylus.sone.test.eq +import net.pterodactylus.sone.test.getInstance +import net.pterodactylus.sone.test.isProvidedByMock +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.notNullValue +import org.hamcrest.Matchers.nullValue +import org.hamcrest.Matchers.sameInstance +import org.junit.Test +import org.mockito.ArgumentMatchers.anyString +import org.mockito.Mockito.never +import org.mockito.Mockito.verify + +/** + * Unit test for FreenetSessionProviderTest. + */ +class FreenetSessionProviderTest { + + private var soneProvider: SoneProvider = DelegatingSoneProvider(mock()) + private val sessionManager: SessionManager = deepMock() + private val provider by lazy { FreenetSessionProvider(soneProvider, sessionManager) } + private val toadletContext = mock() + + @Test + fun `provider returns null for current sone if no sone exists`() { + assertThat(provider.getCurrentSone(toadletContext), nullValue()) + } + + @Test + fun `provider returns singular sone if one sone exists`() { + val localSone: Sone = IdOnlySone("local") + soneProvider = object : DelegatingSoneProvider(mock()) { + override val localSones: Collection = listOf(localSone) + } + assertThat(provider.getCurrentSone(toadletContext), sameInstance(localSone)) + } + + @Test + fun `provider returns null if more than one sones exist but none is stored in the session`() { + soneProvider = object : DelegatingSoneProvider(mock()) { + override val localSones: Collection = listOf(IdOnlySone("1"), IdOnlySone("2")) + } + assertThat(provider.getCurrentSone(toadletContext), nullValue()) + } + + @Test + fun `provider returns sone if more than one sones exist and one is stored in the session`() { + val localSone = object : IdOnlySone("1") { + override fun isLocal() = true + } + soneProvider = object : DelegatingSoneProvider(mock()) { + override val localSones: Collection = listOf(localSone, IdOnlySone("2")) + override val soneLoader: (String) -> Sone? get() = { id -> localSone.takeIf { id == "1" } } + } + whenever(sessionManager.useSession(toadletContext).getAttribute("Sone.CurrentSone")).thenReturn("1") + assertThat(provider.getCurrentSone(toadletContext), equalTo(localSone)) + } + + @Test + fun `provider sets sone ID in existing session`() { + val localSone: Sone = IdOnlySone("local") + provider.setCurrentSone(toadletContext, localSone) + verify(sessionManager.useSession(toadletContext)).setAttribute("Sone.CurrentSone", "local") + } + + @Test + fun `provider sets sone ID in session it created`() { + val localSone: Sone = IdOnlySone("local") + whenever(sessionManager.useSession(toadletContext)).thenReturn(null) + provider.setCurrentSone(toadletContext, localSone) + verify(sessionManager.createSession(anyString(), eq(toadletContext))).setAttribute("Sone.CurrentSone", "local") + } + + @Test + fun `provider removes sone ID in existing session`() { + provider.setCurrentSone(toadletContext, null) + verify(sessionManager.useSession(toadletContext)).removeAttribute("Sone.CurrentSone") + } + + @Test + fun `provider does not create session if sone is to be removed and session does not exist`() { + whenever(sessionManager.useSession(toadletContext)).thenReturn(null) + provider.setCurrentSone(toadletContext, null) + verify(sessionManager.createSession(anyString(), eq(toadletContext)), never()).removeAttribute(anyString()) + } + + @Test + fun `provider can be created by guice`() { + val injector = Guice.createInjector( + SessionManager::class.isProvidedByMock(), + SoneProvider::class.isProvidedByMock() + ) + assertThat(injector.getInstance(), notNullValue()) + } + +} + +private open class DelegatingSoneProvider(private val soneProvider: SoneProvider) : SoneProvider by soneProvider -- 2.7.4