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.mock
7 import net.pterodactylus.sone.test.thenReturnMock
8 import net.pterodactylus.sone.test.whenever
9 import net.pterodactylus.sone.web.pages.WebPageTest
10 import net.pterodactylus.sone.web.pages.LoginPage
11 import net.pterodactylus.util.web.Method.GET
12 import net.pterodactylus.util.web.Method.POST
13 import org.hamcrest.MatcherAssert.assertThat
14 import org.hamcrest.Matchers.contains
15 import org.hamcrest.Matchers.containsInAnyOrder
16 import org.hamcrest.Matchers.equalTo
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() {
27 private val page = LoginPage(template, webInterface)
29 private val sones = listOf(createSone("Sone", "Test"), createSone("Test"), createSone("Sone"))
31 override fun getPage() = page
33 private fun createSone(vararg contexts: String) = mock<Sone>().apply {
34 whenever(id).thenReturn(hashCode().toString())
35 val identity = mock<OwnIdentity>().apply {
36 whenever(this.contexts).thenReturn(contexts.toSet())
37 contexts.forEach { whenever(hasContext(it)).thenReturn(true) }
39 whenever(this.identity).thenReturn(identity)
40 whenever(profile).thenReturnMock()
45 addLocalSone("sone1", sones[0])
46 addLocalSone("sone2", sones[1])
47 addLocalSone("sone3", sones[2])
48 addOwnIdentity(sones[0].identity as OwnIdentity)
49 addOwnIdentity(sones[1].identity as OwnIdentity)
50 addOwnIdentity(sones[2].identity as OwnIdentity)
54 fun `page returns correct path`() {
55 assertThat(page.path, equalTo("login.html"))
59 fun `page does not require login`() {
60 assertThat(page.requiresLogin(), equalTo(false))
64 @Suppress("UNCHECKED_CAST")
65 fun `get request stores sones in template context`() {
67 page.processTemplate(freenetRequest, templateContext)
68 assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
72 @Suppress("UNCHECKED_CAST")
73 fun `get request stores identities without sones in template context`() {
75 page.processTemplate(freenetRequest, templateContext)
76 assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
80 @Suppress("UNCHECKED_CAST")
81 fun `post request with invalid sone sets sones and identities without sone in template context`() {
83 page.processTemplate(freenetRequest, templateContext)
84 assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
85 assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
89 fun `post request with valid sone logs in the sone and redirects to index page`() {
91 addHttpRequestPart("sone-id", "sone2")
92 verifyRedirect("index.html") {
93 verify(webInterface).setCurrentSone(toadletContext, sones[1])
98 fun `post request with valid sone and target redirects to target page`() {
100 addHttpRequestPart("sone-id", "sone2")
101 addHttpRequestParameter("target", "foo.html")
102 verifyRedirect("foo.html") {
103 verify(webInterface).setCurrentSone(toadletContext, sones[1])
108 fun `redirect to index html if a sone is logged in`() {
109 assertThat(page.getRedirectTarget(freenetRequest), equalTo("index.html"))
113 fun `do not redirect if no sone is logged in`() {
115 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
119 fun `page is not enabled if full access required and request is not full access`() {
120 core.preferences.isRequireFullAccess = true
121 assertThat(page.isEnabled(toadletContext), equalTo(false))
125 fun `page is enabled if no full access is required and there is no current sone`() {
127 assertThat(page.isEnabled(toadletContext), equalTo(true))
131 fun `page is not enabled if no full access is required but there is a current sone`() {
132 assertThat(page.isEnabled(toadletContext), equalTo(false))
136 fun `page is enabled if full access required and request is full access and there is no current sone`() {
137 core.preferences.isRequireFullAccess = true
139 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
140 assertThat(page.isEnabled(toadletContext), equalTo(true))
144 fun `page is not enabled if full access required and request is full access but there is a current sone`() {
145 core.preferences.isRequireFullAccess = true
146 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
147 assertThat(page.isEnabled(toadletContext), equalTo(false))