Don’t send request to node if there are no posts or replies to get the times for
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / UntrustPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.test.mock
5 import net.pterodactylus.util.web.Method.POST
6 import org.hamcrest.MatcherAssert.assertThat
7 import org.hamcrest.Matchers.equalTo
8 import org.junit.Test
9 import org.mockito.ArgumentMatchers.any
10 import org.mockito.ArgumentMatchers.eq
11 import org.mockito.Mockito.never
12 import org.mockito.Mockito.verify
13
14 /**
15  * Unit test for [UntrustPage].
16  */
17 class UntrustPageTest: WebPageTest(::UntrustPage) {
18
19         @Test
20         fun `page returns correct path`() {
21             assertThat(page.path, equalTo("untrust.html"))
22         }
23
24         @Test
25         fun `page requires login`() {
26             assertThat(page.requiresLogin(), equalTo(true))
27         }
28
29         @Test
30         fun `page returns correct title`() {
31                 addTranslation("Page.Untrust.Title", "untrust page title")
32             assertThat(page.getPageTitle(freenetRequest), equalTo("untrust page title"))
33         }
34
35         @Test
36         fun `get request does not redirect`() {
37                 verifyNoRedirect {
38                         verify(core, never()).untrustSone(eq(currentSone), any())
39                 }
40         }
41
42         @Test
43         fun `post request without sone parameter does not untrust but redirects`() {
44                 setMethod(POST)
45                 addHttpRequestPart("returnPage", "return.html")
46                 verifyRedirect("return.html") {
47                         verify(core, never()).untrustSone(eq(currentSone), any())
48                 }
49         }
50
51         @Test
52         fun `post request with invalid sone parameter does not untrust but redirects`() {
53                 setMethod(POST)
54                 addHttpRequestPart("returnPage", "return.html")
55                 addHttpRequestPart("sone", "no-sone")
56                 verifyRedirect("return.html") {
57                         verify(core, never()).untrustSone(eq(currentSone), any())
58                 }
59         }
60
61         @Test
62         fun `post request with valid sone parameter untrusts and redirects`() {
63                 setMethod(POST)
64                 addHttpRequestPart("returnPage", "return.html")
65                 addHttpRequestPart("sone", "sone-id")
66                 val sone = mock<Sone>()
67                 addSone("sone-id", sone)
68                 verifyRedirect("return.html") {
69                         verify(core).untrustSone(currentSone, sone)
70                 }
71         }
72
73 }