Remove obsolete loading animation
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / LoginPageTest.kt
1 package net.pterodactylus.sone.web
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.freenet.wot.Identity
5 import net.pterodactylus.sone.freenet.wot.OwnIdentity
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.thenReturnMock
8 import net.pterodactylus.sone.test.whenever
9 import net.pterodactylus.util.web.Method.GET
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.containsInAnyOrder
14 import org.hamcrest.Matchers.equalTo
15 import org.hamcrest.Matchers.nullValue
16 import org.junit.Before
17 import org.junit.Test
18 import org.mockito.Mockito.verify
19
20 /**
21  * Unit test for [LoginPage].
22  */
23 class LoginPageTest : WebPageTest() {
24
25         private val page = LoginPage(template, webInterface)
26
27         private val sones = listOf(createSone("Sone", "Test"), createSone("Test"), createSone("Sone"))
28
29         override fun getPage() = page
30
31         private fun createSone(vararg contexts: String) = mock<Sone>().apply {
32                 whenever(id).thenReturn(hashCode().toString())
33                 val identity = mock<OwnIdentity>().apply {
34                         whenever(this.contexts).thenReturn(contexts.toSet())
35                         contexts.forEach { whenever(hasContext(it)).thenReturn(true) }
36                 }
37                 whenever(this.identity).thenReturn(identity)
38                 whenever(profile).thenReturnMock()
39         }
40
41         @Before
42         fun setupSones() {
43                 addLocalSone("sone1", sones[0])
44                 addLocalSone("sone2", sones[1])
45                 addLocalSone("sone3", sones[2])
46                 addOwnIdentity(sones[0].identity as OwnIdentity)
47                 addOwnIdentity(sones[1].identity as OwnIdentity)
48                 addOwnIdentity(sones[2].identity as OwnIdentity)
49         }
50
51         @Test
52         fun `page returns correct path`() {
53             assertThat(page.path, equalTo("login.html"))
54         }
55
56         @Test
57         fun `page does not require login`() {
58             assertThat(page.requiresLogin(), equalTo(false))
59         }
60
61         @Test
62         @Suppress("UNCHECKED_CAST")
63         fun `get request stores sones in template context`() {
64                 request("", GET)
65                 page.processTemplate(freenetRequest, templateContext)
66                 assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
67         }
68
69         @Test
70         @Suppress("UNCHECKED_CAST")
71         fun `get request stores identities without sones in template context`() {
72                 request("", GET)
73                 page.processTemplate(freenetRequest, templateContext)
74                 assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
75         }
76
77         @Test
78         @Suppress("UNCHECKED_CAST")
79         fun `post request with invalid sone sets sones and identities without sone in template context`() {
80                 request("", POST)
81                 page.processTemplate(freenetRequest, templateContext)
82                 assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
83                 assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
84         }
85
86         @Test
87         fun `post request with valid sone logs in the sone and redirects to index page`() {
88                 request("", POST)
89                 addHttpRequestParameter("sone-id", "sone2")
90                 verifyRedirect("index.html") {
91                         verify(webInterface).setCurrentSone(toadletContext, sones[1])
92                 }
93         }
94
95         @Test
96         fun `post request with valid sone and target redirects to target page`() {
97                 request("", POST)
98                 addHttpRequestParameter("sone-id", "sone2")
99                 addHttpRequestParameter("target", "foo.html")
100                 verifyRedirect("foo.html") {
101                         verify(webInterface).setCurrentSone(toadletContext, sones[1])
102                 }
103         }
104
105         @Test
106         fun `redirect to index html if a sone is logged in`() {
107                 assertThat(page.getRedirectTarget(freenetRequest), equalTo("index.html"))
108         }
109
110         @Test
111         fun `do not redirect if no sone is logged in`() {
112                 unsetCurrentSone()
113                 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
114         }
115
116         @Test
117         fun `page is not enabled if full access required and request is not full access`() {
118                 core.preferences.isRequireFullAccess = true
119                 assertThat(page.isEnabled(toadletContext), equalTo(false))
120         }
121
122         @Test
123         fun `page is enabled if no full access is required and there is no current sone`() {
124                 unsetCurrentSone()
125                 assertThat(page.isEnabled(toadletContext), equalTo(true))
126         }
127
128         @Test
129         fun `page is not enabled if no full access is required but there is a current sone`() {
130                 assertThat(page.isEnabled(toadletContext), equalTo(false))
131         }
132
133         @Test
134         fun `page is enabled if full access required and request is full access and there is no current sone`() {
135                 core.preferences.isRequireFullAccess = true
136                 unsetCurrentSone()
137                 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
138                 assertThat(page.isEnabled(toadletContext), equalTo(true))
139         }
140
141         @Test
142         fun `page is not enabled if full access required and request is full access but there is a current sone`() {
143                 core.preferences.isRequireFullAccess = true
144                 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
145                 assertThat(page.isEnabled(toadletContext), equalTo(false))
146         }
147
148 }