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(::LoginPage) {
24 private val sones = listOf(createSone("Sone", "Test"), createSone("Test"), createSone("Sone"))
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) }
32 whenever(this.identity).thenReturn(identity)
33 whenever(profile).thenReturnMock()
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)
47 fun `page returns correct path`() {
48 assertThat(page.path, equalTo("login.html"))
52 fun `page does not require login`() {
53 assertThat(page.requiresLogin(), equalTo(false))
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]))
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))
71 @Suppress("UNCHECKED_CAST")
72 fun `post request with invalid sone sets sones and identities without sone in template context`() {
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))
80 fun `post request with valid sone logs in the sone and redirects to index page`() {
82 addHttpRequestPart("sone-id", "sone2")
83 verifyRedirect("index.html") {
84 verify(webInterface).setCurrentSone(toadletContext, sones[1])
89 fun `post request with valid sone and target redirects to target page`() {
91 addHttpRequestPart("sone-id", "sone2")
92 addHttpRequestParameter("target", "foo.html")
93 verifyRedirect("foo.html") {
94 verify(webInterface).setCurrentSone(toadletContext, sones[1])
99 fun `redirect to index html if a sone is logged in`() {
100 assertThat(page.getRedirectTarget(freenetRequest), equalTo("index.html"))
104 fun `do not redirect if no sone is logged in`() {
106 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
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))
116 fun `page is enabled if no full access is required and there is no current sone`() {
118 assertThat(page.isEnabled(toadletContext), equalTo(true))
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))
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
130 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
131 assertThat(page.isEnabled(toadletContext), equalTo(true))
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))