♻️ Move generic error pages to custom classes
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / FollowSonePageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.test.getInstance
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import net.pterodactylus.sone.web.baseInjector
8 import net.pterodactylus.util.web.Method.POST
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.equalTo
11 import org.hamcrest.Matchers.notNullValue
12 import org.junit.Test
13 import org.mockito.ArgumentMatchers
14 import org.mockito.ArgumentMatchers.any
15 import org.mockito.ArgumentMatchers.anyString
16 import org.mockito.Mockito.never
17 import org.mockito.Mockito.verify
18
19 /**
20  * Unit test for [FollowSonePage].
21  */
22 class FollowSonePageTest: WebPageTest(::FollowSonePage) {
23
24         @Test
25         fun `page returns correct path`() {
26             assertThat(page.path, equalTo("followSone.html"))
27         }
28
29         @Test
30         fun `page requires login`() {
31             assertThat(page.requiresLogin(), equalTo(true))
32         }
33
34         @Test
35         fun `page returns correct title`() {
36             whenever(l10n.getString("Page.FollowSone.Title")).thenReturn("follow sone page title")
37                 assertThat(page.getPageTitle(soneRequest), equalTo("follow sone page title"))
38         }
39
40         @Test
41         fun `get request does not redirect`() {
42                 page.processTemplate(freenetRequest, templateContext)
43         }
44
45         @Test
46         fun `a single sone can be followed`() {
47                 setMethod(POST)
48                 val sone = mock<Sone>()
49                 addSone("sone-id", sone)
50                 addHttpRequestPart("sone", "sone-id")
51                 addHttpRequestPart("returnPage", "return.html")
52                 verifyRedirect("return.html") {
53                         verify(core).followSone(currentSone, "sone-id")
54                         verify(core).markSoneKnown(sone)
55                 }
56         }
57
58         @Test
59         fun `multiple sones can be followed`() {
60                 setMethod(POST)
61                 val firstSone = mock<Sone>()
62                 addSone("sone-id1", firstSone)
63                 val secondSone = mock<Sone>()
64                 addSone("sone-id2", secondSone)
65                 addHttpRequestPart("sone", "sone-id1,sone-id2")
66                 addHttpRequestPart("returnPage", "return.html")
67                 verifyRedirect("return.html") {
68                         verify(core).followSone(currentSone, "sone-id1")
69                         verify(core).followSone(currentSone, "sone-id2")
70                         verify(core).markSoneKnown(firstSone)
71                         verify(core).markSoneKnown(secondSone)
72                 }
73         }
74
75         @Test
76         fun `a non-existing sone is not followed`() {
77                 setMethod(POST)
78                 addHttpRequestPart("sone", "sone-id")
79                 addHttpRequestPart("returnPage", "return.html")
80                 verifyRedirect("return.html") {
81                         verify(core, never()).followSone(ArgumentMatchers.eq(currentSone), anyString())
82                         verify(core, never()).markSoneKnown(any<Sone>())
83                 }
84         }
85
86         @Test
87         fun `page can be created by dependency injection`() {
88             assertThat(baseInjector.getInstance<FollowSonePage>(), notNullValue())
89         }
90
91 }