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