1 package net.pterodactylus.sone.web
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.GET
10 import net.pterodactylus.util.web.Method.POST
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.contains
13 import org.hamcrest.Matchers.containsInAnyOrder
14 import org.hamcrest.Matchers.equalTo
15 import org.hamcrest.Matchers.nullValue
16 import org.junit.Before
18 import org.mockito.Mockito.verify
21 * Unit test for [LoginPage].
23 class LoginPageTest : WebPageTest() {
25 private val page = LoginPage(template, webInterface)
27 private val sones = listOf(createSone("Sone", "Test"), createSone("Test"), createSone("Sone"))
29 override fun getPage() = page
31 private fun createSone(vararg contexts: String) = mock<Sone>().apply {
32 whenever(id).thenReturn(hashCode().toString())
33 val identity = mock<OwnIdentity>().apply {
34 whenever(this.contexts).thenReturn(contexts.toSet())
35 contexts.forEach { whenever(hasContext(it)).thenReturn(true) }
37 whenever(this.identity).thenReturn(identity)
38 whenever(profile).thenReturnMock()
43 addLocalSone("sone1", sones[0])
44 addLocalSone("sone2", sones[1])
45 addLocalSone("sone3", sones[2])
46 addOwnIdentity(sones[0].identity as OwnIdentity)
47 addOwnIdentity(sones[1].identity as OwnIdentity)
48 addOwnIdentity(sones[2].identity as OwnIdentity)
52 @Suppress("UNCHECKED_CAST")
53 fun `get request stores sone and identities without sones in template context`() {
55 page.handleRequest(freenetRequest, templateContext)
56 assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
57 assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
61 @Suppress("UNCHECKED_CAST")
62 fun `post request with invalid sone sets sones and identities without sone in template context`() {
64 page.handleRequest(freenetRequest, templateContext)
65 assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
66 assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
70 fun `post request with valid sone and redirects to index page`() {
72 addHttpRequestParameter("sone-id", "sone2")
73 verifyRedirect("index.html") {
74 verify(webInterface).setCurrentSone(toadletContext, sones[1])
79 fun `post request with valid sone and target redirects to target page`() {
81 addHttpRequestParameter("sone-id", "sone2")
82 addHttpRequestParameter("target", "foo.html")
83 verifyRedirect("foo.html") {
84 verify(webInterface).setCurrentSone(toadletContext, sones[1])
89 fun `redirect to index html if a sone is logged in`() {
90 assertThat(page.getRedirectTarget(freenetRequest), equalTo("index.html"))
94 fun `do not redirect if no sone is logged in`() {
96 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
100 fun `page is not enabled if full access required and request is not full access`() {
101 core.preferences.isRequireFullAccess = true
102 assertThat(page.isEnabled(toadletContext), equalTo(false))
106 fun `page is enabled if no full access is required and there is no current sone`() {
108 assertThat(page.isEnabled(toadletContext), equalTo(true))
112 fun `page is not enabled if no full access is required but there is a current sone`() {
113 assertThat(page.isEnabled(toadletContext), equalTo(false))
117 fun `page is enabled if full access required and request is full access and there is no current sone`() {
118 core.preferences.isRequireFullAccess = true
120 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
121 assertThat(page.isEnabled(toadletContext), equalTo(true))
125 fun `page is not enabled if full access required and request is full access but there is a current sone`() {
126 core.preferences.isRequireFullAccess = true
127 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
128 assertThat(page.isEnabled(toadletContext), equalTo(false))