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.ArgumentMatchers.anyString
13 import org.mockito.Mockito.verify
16 * Unit test for [CreateSonePage].
18 class CreateSonePageTest : WebPageTest(::CreateSonePage) {
20 private val localSones_ = listOf(
21 createSone("local-sone1"),
22 createSone("local-sone2"),
23 createSone("local-sone3")
26 private fun createSone(id: String) = mock<Sone>().apply {
27 whenever(this.id).thenReturn(id)
28 whenever(profile).thenReturn(Profile(this))
31 private val ownIdentities_ = listOf(
32 createOwnIdentity("own-id-1", "Sone"),
33 createOwnIdentity("own-id-2", "Test", "Foo"),
34 createOwnIdentity("own-id-3"),
35 createOwnIdentity("own-id-4", "Sone")
38 private fun createOwnIdentity(id: String, vararg contexts: String) = mock<OwnIdentity>().apply {
39 whenever(this.id).thenReturn(id)
40 whenever(this.nickname).thenReturn(id)
41 whenever(this.contexts).thenReturn(contexts.toSet())
42 whenever(this.hasContext(anyString())).thenAnswer { invocation -> invocation.getArgument<String>(0) in contexts }
46 fun `page returns correct path`() {
47 assertThat(page.path, equalTo("createSone.html"))
51 fun `page does not require login`() {
52 assertThat(page.requiresLogin(), equalTo(false))
55 private fun addExistingSones() {
56 listOf(2, 0, 1).map { localSones_[it] }.forEach { addLocalSone(it.id, it) }
60 @Suppress("UNCHECKED_CAST")
61 fun `get request stores sorted list of local sones in template context`() {
63 page.processTemplate(freenetRequest, templateContext)
64 assertThat(templateContext["sones"] as Collection<Sone>, contains(localSones_[0], localSones_[1], localSones_[2]))
67 private fun addExistingOwnIdentities() {
68 listOf(2, 0, 3, 1).map { ownIdentities_[it] }.forEach { addOwnIdentity(it) }
72 @Suppress("UNCHECKED_CAST")
73 fun `get request stores sorted sones without sone context in the template context`() {
74 addExistingOwnIdentities()
75 page.processTemplate(freenetRequest, templateContext)
76 assertThat(templateContext["identitiesWithoutSone"] as Collection<OwnIdentity>, contains(ownIdentities_[1], ownIdentities_[2]))
80 fun `sone is created and logged in`() {
81 addExistingOwnIdentities()
83 addHttpRequestPart("identity", "own-id-3")
84 val newSone = mock<Sone>()
85 whenever(core.createSone(ownIdentities_[2])).thenReturn(newSone)
86 verifyRedirect("index.html") {
87 verify(webInterface).setCurrentSone(toadletContext, newSone)
92 fun `on invalid identity id a flag is set in the template context`() {
94 addHttpRequestParameter("identity", "own-id-3")
95 page.processTemplate(freenetRequest, templateContext)
96 assertThat(templateContext["errorNoIdentity"], equalTo<Any>(true))
100 fun `if sone is not created user is still redirected to index`() {
101 addExistingOwnIdentities()
103 addHttpRequestPart("identity", "own-id-3")
104 whenever(core.createSone(ownIdentities_[2])).thenReturn(null)
105 verifyRedirect("index.html") {
106 verify(core).createSone(ownIdentities_[2])
107 verify(webInterface).setCurrentSone(toadletContext, null)
112 fun `create sone is not shown in menu if full access is required but client doesn’t have full access`() {
113 core.preferences.newRequireFullAccess = true
114 assertThat(page.isEnabled(toadletContext), equalTo(false))
118 fun `create sone is shown in menu if no sone is logged in`() {
120 assertThat(page.isEnabled(toadletContext), equalTo(true))
124 fun `create sone is shown in menu if a single sone exists`() {
125 addLocalSone("local-sone", localSones_[0])
126 assertThat(page.isEnabled(toadletContext), equalTo(true))
130 fun `create sone is not shown in menu if more than one sone exists`() {
131 addLocalSone("local-sone1", localSones_[0])
132 addLocalSone("local-sone2", localSones_[1])
133 assertThat(page.isEnabled(toadletContext), equalTo(false))
137 fun `create sone is shown in menu if no sone is logged in and client has full access`() {
138 core.preferences.newRequireFullAccess = true
139 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
141 assertThat(page.isEnabled(toadletContext), equalTo(true))
145 fun `page can be created by dependency injection`() {
146 assertThat(baseInjector.getInstance<CreateSonePage>(), notNullValue())
150 fun `page is annotated with the correct menuname`() {
151 assertThat(page.menuName, equalTo("CreateSone"))
155 fun `page is annotated with the correct template path`() {
156 assertThat(page.templatePath, equalTo("/templates/createSone.html"))