Add test for DI constructability of EditAlbumAjaxPage
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / TrustPageTest.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.web.baseInjector
7 import net.pterodactylus.util.web.Method.POST
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.equalTo
10 import org.hamcrest.Matchers.notNullValue
11 import org.junit.Test
12 import org.mockito.ArgumentMatchers.any
13 import org.mockito.ArgumentMatchers.eq
14 import org.mockito.Mockito.never
15 import org.mockito.Mockito.verify
16
17 /**
18  * Unit test for [TrustPage].
19  */
20 class TrustPageTest: WebPageTest(::TrustPage) {
21
22         @Test
23         fun `page returns correct path`() {
24             assertThat(page.path, equalTo("trust.html"))
25         }
26
27         @Test
28         fun `page requires login`() {
29             assertThat(page.requiresLogin(), equalTo(true))
30         }
31
32         @Test
33         fun `page returns correct title`() {
34             addTranslation("Page.Trust.Title", "title trust page")
35                 assertThat(page.getPageTitle(freenetRequest), equalTo("title trust page"))
36         }
37
38         @Test
39         fun `get method does not redirect`() {
40                 verifyNoRedirect { }
41         }
42
43         @Test
44         fun `post request without sone redirects to return page`() {
45                 setMethod(POST)
46                 addHttpRequestPart("returnPage", "return.html")
47                 verifyRedirect("return.html") {
48                         verify(core, never()).trustSone(eq(currentSone), any())
49                 }
50         }
51
52         @Test
53         fun `post request with missing sone redirects to return page`() {
54                 setMethod(POST)
55                 addHttpRequestPart("returnPage", "return.html")
56                 addHttpRequestPart("sone", "sone-id")
57                 verifyRedirect("return.html") {
58                         verify(core, never()).trustSone(eq(currentSone), any())
59                 }
60         }
61
62         @Test
63         fun `post request with existing sone trusts the identity and redirects to return page`() {
64                 setMethod(POST)
65                 addHttpRequestPart("returnPage", "return.html")
66                 addHttpRequestPart("sone", "sone-id")
67                 val sone = mock<Sone>()
68                 addSone("sone-id", sone)
69                 verifyRedirect("return.html") {
70                         verify(core).trustSone(eq(currentSone), eq(sone))
71                 }
72         }
73
74         @Test
75         fun `page can be created by dependency injection`() {
76             assertThat(baseInjector.getInstance<TrustPage>(), notNullValue())
77         }
78
79 }