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