1 package net.pterodactylus.sone.web.pages
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
20 import org.mockito.Mockito.verify
23 * Unit test for [LoginPage].
25 class LoginPageTest: WebPageTest(::LoginPage) {
27 private val sones = listOf(createSone("Sone", "Test"), createSone("Test"), createSone("Sone"))
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) }
35 whenever(this.identity).thenReturn(identity)
36 whenever(profile).thenReturnMock()
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)
50 fun `page returns correct path`() {
51 assertThat(page.path, equalTo("login.html"))
55 fun `page does not require login`() {
56 assertThat(page.requiresLogin(), equalTo(false))
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]))
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))
74 @Suppress("UNCHECKED_CAST")
75 fun `post request with invalid sone sets sones and identities without sone in template context`() {
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))
83 fun `post request with valid sone logs in the sone and redirects to index page`() {
85 addHttpRequestPart("sone-id", "sone2")
86 verifyRedirect("index.html") {
87 verify(webInterface).setCurrentSone(toadletContext, sones[1])
92 fun `post request with valid sone and target redirects to target page`() {
94 addHttpRequestPart("sone-id", "sone2")
95 addHttpRequestParameter("target", "foo.html")
96 verifyRedirect("foo.html") {
97 verify(webInterface).setCurrentSone(toadletContext, sones[1])
102 fun `redirect to index html if a sone is logged in`() {
103 assertThat(page.getRedirectTarget(freenetRequest), equalTo("index.html"))
107 fun `do not redirect if no sone is logged in`() {
109 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
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))
119 fun `page is enabled if no full access is required and there is no current sone`() {
121 assertThat(page.isEnabled(toadletContext), equalTo(true))
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))
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
133 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
134 assertThat(page.isEnabled(toadletContext), equalTo(true))
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))
145 fun `page can be created by dependency injection`() {
146 assertThat(baseInjector.getInstance<LoginPage>(), notNullValue())