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.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
17 import org.mockito.Mockito.verify
20 * Unit test for [LoginPage].
22 class LoginPageTest : WebPageTest() {
24 private val page = LoginPage(template, webInterface)
26 private val sones = listOf(createSone("Sone", "Test"), createSone("Test"), createSone("Sone"))
28 override fun getPage() = page
30 private fun createSone(vararg contexts: String) = mock<Sone>().apply {
31 whenever(id).thenReturn(hashCode().toString())
32 val identity = mock<OwnIdentity>().apply {
33 whenever(this.contexts).thenReturn(contexts.toSet())
34 contexts.forEach { whenever(hasContext(it)).thenReturn(true) }
36 whenever(this.identity).thenReturn(identity)
37 whenever(profile).thenReturnMock()
42 addLocalSone("sone1", sones[0])
43 addLocalSone("sone2", sones[1])
44 addLocalSone("sone3", sones[2])
45 addOwnIdentity(sones[0].identity as OwnIdentity)
46 addOwnIdentity(sones[1].identity as OwnIdentity)
47 addOwnIdentity(sones[2].identity as OwnIdentity)
51 fun `page returns correct path`() {
52 assertThat(page.path, equalTo("login.html"))
56 fun `page does not require login`() {
57 assertThat(page.requiresLogin(), equalTo(false))
61 @Suppress("UNCHECKED_CAST")
62 fun `get request stores sones in template context`() {
63 page.processTemplate(freenetRequest, templateContext)
64 assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
68 @Suppress("UNCHECKED_CAST")
69 fun `get request stores identities without sones in template context`() {
70 page.processTemplate(freenetRequest, templateContext)
71 assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
75 @Suppress("UNCHECKED_CAST")
76 fun `post request with invalid sone sets sones and identities without sone in template context`() {
78 page.processTemplate(freenetRequest, templateContext)
79 assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
80 assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
84 fun `post request with valid sone logs in the sone and redirects to index page`() {
86 addHttpRequestPart("sone-id", "sone2")
87 verifyRedirect("index.html") {
88 verify(webInterface).setCurrentSone(toadletContext, sones[1])
93 fun `post request with valid sone and target redirects to target page`() {
95 addHttpRequestPart("sone-id", "sone2")
96 addHttpRequestParameter("target", "foo.html")
97 verifyRedirect("foo.html") {
98 verify(webInterface).setCurrentSone(toadletContext, sones[1])
103 fun `redirect to index html if a sone is logged in`() {
104 assertThat(page.getRedirectTarget(freenetRequest), equalTo("index.html"))
108 fun `do not redirect if no sone is logged in`() {
110 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
114 fun `page is not enabled if full access required and request is not full access`() {
115 core.preferences.isRequireFullAccess = true
116 assertThat(page.isEnabled(toadletContext), equalTo(false))
120 fun `page is enabled if no full access is required and there is no current sone`() {
122 assertThat(page.isEnabled(toadletContext), equalTo(true))
126 fun `page is not enabled if no full access is required but there is a current sone`() {
127 assertThat(page.isEnabled(toadletContext), equalTo(false))
131 fun `page is enabled if full access required and request is full access and there is no current sone`() {
132 core.preferences.isRequireFullAccess = true
134 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
135 assertThat(page.isEnabled(toadletContext), equalTo(true))
139 fun `page is not enabled if full access required and request is full access but there is a current sone`() {
140 core.preferences.isRequireFullAccess = true
141 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
142 assertThat(page.isEnabled(toadletContext), equalTo(false))