1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Profile
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.freenet.wot.OwnIdentity
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.util.web.Method.POST
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.contains
11 import org.hamcrest.Matchers.equalTo
13 import org.mockito.ArgumentMatchers.anyString
14 import org.mockito.Mockito.verify
17 * Unit test for [CreateSonePage].
19 class CreateSonePageTest: WebPageTest() {
21 private val page = CreateSonePage(template, webInterface)
22 override fun getPage() = page
24 private val localSones_ = listOf(
25 createSone("local-sone1"),
26 createSone("local-sone2"),
27 createSone("local-sone3")
30 private fun createSone(id: String) = mock<Sone>().apply {
31 whenever(this.id).thenReturn(id)
32 whenever(profile).thenReturn(Profile(this))
35 private val ownIdentities_ = listOf(
36 createOwnIdentity("own-id-1", "Sone"),
37 createOwnIdentity("own-id-2", "Test", "Foo"),
38 createOwnIdentity("own-id-3"),
39 createOwnIdentity("own-id-4", "Sone")
42 private fun createOwnIdentity(id: String, vararg contexts: String) = mock<OwnIdentity>().apply {
43 whenever(this.id).thenReturn(id)
44 whenever(this.nickname).thenReturn(id)
45 whenever(this.contexts).thenReturn(contexts.toSet())
46 whenever(this.hasContext(anyString())).thenAnswer { invocation -> invocation.getArgument<String>(0) in contexts }
50 fun `page returns correct path`() {
51 assertThat(page.path, equalTo("createSone.html"))
55 fun `page does not require login`() {
56 assertThat(page.requiresLogin(), equalTo(false))
59 private fun addExistingSones() {
60 listOf(2, 0, 1).map { localSones_[it] }.forEach { addLocalSone(it.id, it) }
64 @Suppress("UNCHECKED_CAST")
65 fun `get request stores sorted list of local sones in template context`() {
67 page.processTemplate(freenetRequest, templateContext)
68 assertThat(templateContext["sones"] as Collection<Sone>, contains(localSones_[0], localSones_[1], localSones_[2]))
71 private fun addExistingOwnIdentities() {
72 listOf(2, 0, 3, 1).map { ownIdentities_[it] }.forEach { addOwnIdentity(it) }
76 @Suppress("UNCHECKED_CAST")
77 fun `get request stores sorted sones without sone context in the template context`() {
78 addExistingOwnIdentities()
79 page.processTemplate(freenetRequest, templateContext)
80 assertThat(templateContext["identitiesWithoutSone"] as Collection<OwnIdentity>, contains(ownIdentities_[1], ownIdentities_[2]))
84 fun `sone is created and logged in`() {
85 addExistingOwnIdentities()
87 addHttpRequestPart("identity", "own-id-3")
88 val newSone = mock<Sone>()
89 whenever(core.createSone(ownIdentities_[2])).thenReturn(newSone)
90 verifyRedirect("index.html") {
91 verify(webInterface).setCurrentSone(toadletContext, newSone)
96 fun `on invalid identity id a flag is set in the template context`() {
98 addHttpRequestParameter("identity", "own-id-3")
99 page.processTemplate(freenetRequest, templateContext)
100 assertThat(templateContext["errorNoIdentity"], equalTo<Any>(true))
104 fun `if sone is not created user is still redirected to index`() {
105 addExistingOwnIdentities()
107 addHttpRequestPart("identity", "own-id-3")
108 whenever(core.createSone(ownIdentities_[2])).thenReturn(null)
109 verifyRedirect("index.html") {
110 verify(core).createSone(ownIdentities_[2])
111 verify(webInterface).setCurrentSone(toadletContext, null)
116 fun `create sone is not shown in menu if full access is required but client doesn’t have full access`() {
117 core.preferences.isRequireFullAccess = true
118 assertThat(page.isEnabled(toadletContext), equalTo(false))
122 fun `create sone is shown in menu if no sone is logged in`() {
124 assertThat(page.isEnabled(toadletContext), equalTo(true))
128 fun `create sone is shown in menu if a single sone exists`() {
129 addLocalSone("local-sone", localSones_[0])
130 assertThat(page.isEnabled(toadletContext), equalTo(true))
134 fun `create sone is not shown in menu if more than one sone exists`() {
135 addLocalSone("local-sone1", localSones_[0])
136 addLocalSone("local-sone2", localSones_[1])
137 assertThat(page.isEnabled(toadletContext), equalTo(false))
141 fun `create sone is shown in menu if no sone is logged in and client has full access`() {
142 core.preferences.isRequireFullAccess = true
143 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
145 assertThat(page.isEnabled(toadletContext), equalTo(true))