Add more tests for follow Sone page
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / CreateSonePageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Profile
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.freenet.wot.OwnIdentity
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.sone.web.pages.WebPageTest
9 import net.pterodactylus.sone.web.pages.CreateSonePage
10 import net.pterodactylus.util.web.Method.POST
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.contains
13 import org.hamcrest.Matchers.equalTo
14 import org.junit.Test
15 import org.mockito.ArgumentMatchers.anyString
16 import org.mockito.Mockito.verify
17
18 /**
19  * Unit test for [CreateSonePage].
20  */
21 class CreateSonePageTest: WebPageTest() {
22
23         private val page = CreateSonePage(template, webInterface)
24         override fun getPage() = page
25
26         private val localSones_ = listOf(
27                         createSone("local-sone1"),
28                         createSone("local-sone2"),
29                         createSone("local-sone3")
30         )
31
32         private fun createSone(id: String) = mock<Sone>().apply {
33                 whenever(this.id).thenReturn(id)
34                 whenever(profile).thenReturn(Profile(this))
35         }
36
37         private val ownIdentities_ = listOf(
38                         createOwnIdentity("own-id-1", "Sone"),
39                         createOwnIdentity("own-id-2", "Test", "Foo"),
40                         createOwnIdentity("own-id-3"),
41                         createOwnIdentity("own-id-4", "Sone")
42         )
43
44         private fun createOwnIdentity(id: String, vararg contexts: String) = mock<OwnIdentity>().apply {
45                 whenever(this.id).thenReturn(id)
46                 whenever(this.nickname).thenReturn(id)
47                 whenever(this.contexts).thenReturn(contexts.toSet())
48                 whenever(this.hasContext(anyString())).thenAnswer { invocation -> invocation.getArgument<String>(0) in contexts }
49         }
50
51         @Test
52         fun `page returns correct path`() {
53                 assertThat(page.path, equalTo("createSone.html"))
54         }
55
56         @Test
57         fun `page does not require login`() {
58                 assertThat(page.requiresLogin(), equalTo(false))
59         }
60
61         private fun addExistingSones() {
62                 listOf(2, 0, 1).map { localSones_[it] }.forEach { addLocalSone(it.id, it) }
63         }
64
65         @Test
66         @Suppress("UNCHECKED_CAST")
67         fun `get request stores sorted list of local sones in template context`() {
68                 addExistingSones()
69                 page.processTemplate(freenetRequest, templateContext)
70                 assertThat(templateContext["sones"] as Collection<Sone>, contains(localSones_[0], localSones_[1], localSones_[2]))
71         }
72
73         private fun addExistingOwnIdentities() {
74                 listOf(2, 0, 3, 1).map { ownIdentities_[it] }.forEach { addOwnIdentity(it) }
75         }
76
77         @Test
78         @Suppress("UNCHECKED_CAST")
79         fun `get request stores sorted sones without sone context in the template context`() {
80                 addExistingOwnIdentities()
81                 page.processTemplate(freenetRequest, templateContext)
82                 assertThat(templateContext["identitiesWithoutSone"] as Collection<OwnIdentity>, contains(ownIdentities_[1], ownIdentities_[2]))
83         }
84
85         @Test
86         fun `sone is created and logged in`() {
87                 addExistingOwnIdentities()
88                 request("", POST)
89                 addHttpRequestParameter("identity", "own-id-3")
90                 val newSone = mock<Sone>()
91                 whenever(core.createSone(ownIdentities_[2])).thenReturn(newSone)
92                 verifyRedirect("index.html") {
93                         verify(webInterface).setCurrentSone(toadletContext, newSone)
94                 }
95         }
96
97         @Test
98         fun `on invalid identity id a flag is set in the template context`() {
99                 request("", POST)
100                 addHttpRequestParameter("identity", "own-id-3")
101                 page.processTemplate(freenetRequest, templateContext)
102                 assertThat(templateContext["errorNoIdentity"], equalTo<Any>(true))
103         }
104
105         @Test
106         fun `if sone is not created user is still redirected to index`() {
107                 addExistingOwnIdentities()
108                 request("", POST)
109                 addHttpRequestParameter("identity", "own-id-3")
110                 whenever(core.createSone(ownIdentities_[2])).thenReturn(null)
111                 verifyRedirect("index.html") {
112                         verify(core).createSone(ownIdentities_[2])
113                         verify(webInterface).setCurrentSone(toadletContext, null)
114                 }
115         }
116
117         @Test
118         fun `create sone is not shown in menu if full access is required but client doesn’t have full access`() {
119                 core.preferences.isRequireFullAccess = true
120                 assertThat(page.isEnabled(toadletContext), equalTo(false))
121         }
122
123         @Test
124         fun `create sone is shown in menu if no sone is logged in`() {
125                 unsetCurrentSone()
126                 assertThat(page.isEnabled(toadletContext), equalTo(true))
127         }
128
129         @Test
130         fun `create sone is shown in menu if a single sone exists`() {
131                 addLocalSone("local-sone", localSones_[0])
132                 assertThat(page.isEnabled(toadletContext), equalTo(true))
133         }
134
135         @Test
136         fun `create sone is not shown in menu if more than one sone exists`() {
137                 addLocalSone("local-sone1", localSones_[0])
138                 addLocalSone("local-sone2", localSones_[1])
139                 assertThat(page.isEnabled(toadletContext), equalTo(false))
140         }
141
142         @Test
143         fun `create sone is shown in menu if no sone is logged in and client has full access`() {
144                 core.preferences.isRequireFullAccess = true
145                 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
146                 unsetCurrentSone()
147                 assertThat(page.isEnabled(toadletContext), equalTo(true))
148         }
149
150 }