Use new redirect verification
[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         @Suppress("UNCHECKED_CAST")
53         fun `get request stores sone and identities without sones in template context`() {
54                 request("", GET)
55                 page.handleRequest(freenetRequest, templateContext)
56                 assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
57                 assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
58         }
59
60         @Test
61         @Suppress("UNCHECKED_CAST")
62         fun `post request with invalid sone sets sones and identities without sone in template context`() {
63                 request("", POST)
64                 page.handleRequest(freenetRequest, templateContext)
65                 assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
66                 assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
67         }
68
69         @Test
70         fun `post request with valid sone and redirects to index page`() {
71                 request("", POST)
72                 addHttpRequestParameter("sone-id", "sone2")
73                 verifyRedirect("index.html") {
74                         verify(webInterface).setCurrentSone(toadletContext, sones[1])
75                 }
76         }
77
78         @Test
79         fun `post request with valid sone and target redirects to target page`() {
80                 request("", POST)
81                 addHttpRequestParameter("sone-id", "sone2")
82                 addHttpRequestParameter("target", "foo.html")
83                 verifyRedirect("foo.html") {
84                         verify(webInterface).setCurrentSone(toadletContext, sones[1])
85                 }
86         }
87
88         @Test
89         fun `redirect to index html if a sone is logged in`() {
90                 assertThat(page.getRedirectTarget(freenetRequest), equalTo("index.html"))
91         }
92
93         @Test
94         fun `do not redirect if no sone is logged in`() {
95                 unsetCurrentSone()
96                 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
97         }
98
99         @Test
100         fun `page is not enabled if full access required and request is not full access`() {
101                 core.preferences.isRequireFullAccess = true
102                 assertThat(page.isEnabled(toadletContext), equalTo(false))
103         }
104
105         @Test
106         fun `page is enabled if no full access is required and there is no current sone`() {
107                 unsetCurrentSone()
108                 assertThat(page.isEnabled(toadletContext), equalTo(true))
109         }
110
111         @Test
112         fun `page is not enabled if no full access is required but there is a current sone`() {
113                 assertThat(page.isEnabled(toadletContext), equalTo(false))
114         }
115
116         @Test
117         fun `page is enabled if full access required and request is full access and there is no current sone`() {
118                 core.preferences.isRequireFullAccess = true
119                 unsetCurrentSone()
120                 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
121                 assertThat(page.isEnabled(toadletContext), equalTo(true))
122         }
123
124         @Test
125         fun `page is not enabled if full access required and request is full access but there is a current sone`() {
126                 core.preferences.isRequireFullAccess = true
127                 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
128                 assertThat(page.isEnabled(toadletContext), equalTo(false))
129         }
130
131 }