1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.freenet.wot.*
5 import net.pterodactylus.sone.test.*
6 import net.pterodactylus.sone.web.*
7 import net.pterodactylus.sone.web.page.*
8 import net.pterodactylus.util.web.Method.*
9 import org.hamcrest.MatcherAssert.*
10 import org.hamcrest.Matchers.*
12 import org.mockito.Mockito.*
15 * Unit test for [LoginPage].
17 class LoginPageTest : WebPageTest(::LoginPage) {
19 private val sones = listOf(createSone("Sone", "Test"), createSone("Test"), createSone("Sone"))
21 private fun createSone(vararg contexts: String) = mock<Sone>().apply {
22 whenever(id).thenReturn(hashCode().toString())
23 val identity = mock<OwnIdentity>().apply {
24 whenever(this.contexts).thenReturn(contexts.toSet())
25 contexts.forEach { whenever(hasContext(it)).thenReturn(true) }
27 whenever(this.identity).thenReturn(identity)
28 whenever(profile).thenReturnMock()
33 addLocalSone("sone1", sones[0])
34 addLocalSone("sone2", sones[1])
35 addLocalSone("sone3", sones[2])
36 addOwnIdentity(sones[0].identity as OwnIdentity)
37 addOwnIdentity(sones[1].identity as OwnIdentity)
38 addOwnIdentity(sones[2].identity as OwnIdentity)
42 fun `page returns correct path`() {
43 assertThat(page.path, equalTo("login.html"))
47 fun `page does not require login`() {
48 assertThat(page.requiresLogin(), equalTo(false))
52 @Suppress("UNCHECKED_CAST")
53 fun `get request stores sones in template context`() {
54 page.processTemplate(freenetRequest, templateContext)
55 assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
59 @Suppress("UNCHECKED_CAST")
60 fun `get request stores identities without sones in template context`() {
61 page.processTemplate(freenetRequest, templateContext)
62 assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
66 @Suppress("UNCHECKED_CAST")
67 fun `post request with invalid sone sets sones and identities without sone in template context`() {
69 page.processTemplate(freenetRequest, templateContext)
70 assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
71 assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
75 fun `post request with valid sone logs in the sone and redirects to index page`() {
77 addHttpRequestPart("sone-id", "sone2")
78 verifyRedirect("index.html") {
79 verify(webInterface).setCurrentSone(toadletContext, sones[1])
84 fun `post request with valid sone and target redirects to target page`() {
86 addHttpRequestPart("sone-id", "sone2")
87 addHttpRequestParameter("target", "foo.html")
88 verifyRedirect("foo.html") {
89 verify(webInterface).setCurrentSone(toadletContext, sones[1])
94 fun `redirect to index html if a sone is logged in`() {
95 assertThat(page.getRedirectTarget(freenetRequest), equalTo("index.html"))
99 fun `do not redirect if no sone is logged in`() {
101 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
105 fun `page is not enabled if full access required and request is not full access`() {
106 core.preferences.newRequireFullAccess = true
107 assertThat(page.isEnabled(toadletContext), equalTo(false))
111 fun `page is enabled if no full access is required and there is no current sone`() {
113 assertThat(page.isEnabled(toadletContext), equalTo(true))
117 fun `page is not enabled if no full access is required but there is a current sone`() {
118 assertThat(page.isEnabled(toadletContext), equalTo(false))
122 fun `page is enabled if full access required and request is full access and there is no current sone`() {
123 core.preferences.newRequireFullAccess = true
125 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
126 assertThat(page.isEnabled(toadletContext), equalTo(true))
130 fun `page is not enabled if full access required and request is full access but there is a current sone`() {
131 core.preferences.newRequireFullAccess = true
132 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
133 assertThat(page.isEnabled(toadletContext), equalTo(false))
137 fun `page can be created by dependency injection`() {
138 assertThat(baseInjector.getInstance<LoginPage>(), notNullValue())
142 fun `page is annotated with correct menuname`() {
143 assertThat(page.menuName, equalTo("Login"))
147 fun `page is annotated with correct template path`() {
148 assertThat(page.templatePath, equalTo("/templates/login.html"))