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