2 * Sone - FreenetSessionProviderTest.kt - Copyright © 2020 David ‘Bombe’ Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web
20 import com.google.inject.Guice
21 import freenet.clients.http.SessionManager
22 import freenet.clients.http.ToadletContext
23 import net.pterodactylus.sone.data.Sone
24 import net.pterodactylus.sone.data.impl.IdOnlySone
25 import net.pterodactylus.sone.database.SoneProvider
26 import net.pterodactylus.sone.test.deepMock
27 import net.pterodactylus.sone.test.eq
28 import net.pterodactylus.sone.test.getInstance
29 import net.pterodactylus.sone.test.isProvidedByMock
30 import net.pterodactylus.sone.test.mock
31 import net.pterodactylus.sone.test.whenever
32 import org.hamcrest.MatcherAssert.assertThat
33 import org.hamcrest.Matchers.equalTo
34 import org.hamcrest.Matchers.notNullValue
35 import org.hamcrest.Matchers.nullValue
36 import org.hamcrest.Matchers.sameInstance
38 import org.mockito.ArgumentMatchers.anyString
39 import org.mockito.Mockito.never
40 import org.mockito.Mockito.verify
43 * Unit test for FreenetSessionProviderTest.
45 class FreenetSessionProviderTest {
47 private var soneProvider: SoneProvider = DelegatingSoneProvider(mock())
48 private val sessionManager: SessionManager = deepMock()
49 private val provider by lazy { FreenetSessionProvider(soneProvider, sessionManager) }
50 private val toadletContext = mock<ToadletContext>()
53 fun `provider returns null for current sone if no sone exists`() {
54 assertThat(provider.getCurrentSone(toadletContext), nullValue())
58 fun `provider returns singular sone if one sone exists`() {
59 val localSone: Sone = IdOnlySone("local")
60 soneProvider = object : DelegatingSoneProvider(mock()) {
61 override val localSones: Collection<Sone> = listOf(localSone)
63 assertThat(provider.getCurrentSone(toadletContext), sameInstance(localSone))
67 fun `provider returns null if more than one sones exist but none is stored in the session`() {
68 soneProvider = object : DelegatingSoneProvider(mock()) {
69 override val localSones: Collection<Sone> = listOf(IdOnlySone("1"), IdOnlySone("2"))
71 assertThat(provider.getCurrentSone(toadletContext), nullValue())
75 fun `provider returns sone if more than one sones exist and one is stored in the session`() {
76 val localSone = object : IdOnlySone("1") {
77 override fun isLocal() = true
79 soneProvider = object : DelegatingSoneProvider(mock()) {
80 override val localSones: Collection<Sone> = listOf(localSone, IdOnlySone("2"))
81 override val soneLoader: (String) -> Sone? get() = { id -> localSone.takeIf { id == "1" } }
83 whenever(sessionManager.useSession(toadletContext).getAttribute("Sone.CurrentSone")).thenReturn("1")
84 assertThat(provider.getCurrentSone(toadletContext), equalTo<Sone>(localSone))
88 fun `provider sets sone ID in existing session`() {
89 val localSone: Sone = IdOnlySone("local")
90 provider.setCurrentSone(toadletContext, localSone)
91 verify(sessionManager.useSession(toadletContext)).setAttribute("Sone.CurrentSone", "local")
95 fun `provider sets sone ID in session it created`() {
96 val localSone: Sone = IdOnlySone("local")
97 whenever(sessionManager.useSession(toadletContext)).thenReturn(null)
98 provider.setCurrentSone(toadletContext, localSone)
99 verify(sessionManager.createSession(anyString(), eq(toadletContext))).setAttribute("Sone.CurrentSone", "local")
103 fun `provider removes sone ID in existing session`() {
104 provider.setCurrentSone(toadletContext, null)
105 verify(sessionManager.useSession(toadletContext)).removeAttribute("Sone.CurrentSone")
109 fun `provider does not create session if sone is to be removed and session does not exist`() {
110 whenever(sessionManager.useSession(toadletContext)).thenReturn(null)
111 provider.setCurrentSone(toadletContext, null)
112 verify(sessionManager.createSession(anyString(), eq(toadletContext)), never()).removeAttribute(anyString())
116 fun `provider can be created by guice`() {
117 val injector = Guice.createInjector(
118 SessionManager::class.isProvidedByMock(),
119 SoneProvider::class.isProvidedByMock()
121 assertThat(injector.getInstance<FreenetSessionProvider>(), notNullValue())
126 private open class DelegatingSoneProvider(private val soneProvider: SoneProvider) : SoneProvider by soneProvider