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