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