🔀 Merge branch 'release/v82'
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / FreenetSessionProviderTest.kt
1 /**
2  * Sone - FreenetSessionProviderTest.kt - Copyright Â© 2020 David â€˜Bombe’ Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web
19
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
37 import org.junit.Test
38 import org.mockito.ArgumentMatchers.anyString
39 import org.mockito.Mockito.never
40 import org.mockito.Mockito.verify
41
42 /**
43  * Unit test for FreenetSessionProviderTest.
44  */
45 class FreenetSessionProviderTest {
46
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>()
51
52         @Test
53         fun `provider returns null for current sone if no sone exists`() {
54                 assertThat(provider.getCurrentSone(toadletContext), nullValue())
55         }
56
57         @Test
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)
62                 }
63                 assertThat(provider.getCurrentSone(toadletContext), sameInstance(localSone))
64         }
65
66         @Test
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"))
70                 }
71                 assertThat(provider.getCurrentSone(toadletContext), nullValue())
72         }
73
74         @Test
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
78                 }
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" } }
82                 }
83                 whenever(sessionManager.useSession(toadletContext).getAttribute("Sone.CurrentSone")).thenReturn("1")
84                 assertThat(provider.getCurrentSone(toadletContext), equalTo<Sone>(localSone))
85         }
86
87         @Test
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")
92         }
93
94         @Test
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")
100         }
101
102         @Test
103         fun `provider removes sone ID in existing session`() {
104                 provider.setCurrentSone(toadletContext, null)
105                 verify(sessionManager.useSession(toadletContext)).removeAttribute("Sone.CurrentSone")
106         }
107
108         @Test
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())
113         }
114
115         @Test
116         fun `provider can be created by guice`() {
117                 val injector = Guice.createInjector(
118                                 SessionManager::class.isProvidedByMock(),
119                                 SoneProvider::class.isProvidedByMock()
120                 )
121                 assertThat(injector.getInstance<FreenetSessionProvider>(), notNullValue())
122         }
123
124 }
125
126 private open class DelegatingSoneProvider(private val soneProvider: SoneProvider) : SoneProvider by soneProvider