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