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