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