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