🚧 Add session provider implementation
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 7 Apr 2020 18:47:03 +0000 (20:47 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 7 Apr 2020 18:47:03 +0000 (20:47 +0200)
src/main/kotlin/net/pterodactylus/sone/web/FreenetSessionProvider.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/FreenetSessionProviderTest.kt [new file with mode: 0644]

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 (file)
index 0000000..80bacdf
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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 (file)
index 0000000..835b14d
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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<ToadletContext>()
+
+       @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<Sone> = 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<Sone> = 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<Sone> = 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<Sone>(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<FreenetSessionProvider>(), notNullValue())
+       }
+
+}
+
+private open class DelegatingSoneProvider(private val soneProvider: SoneProvider) : SoneProvider by soneProvider