♻️ Refactor WOT connector
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / UnlockSonePageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.test.*
5 import net.pterodactylus.sone.web.*
6 import net.pterodactylus.util.web.Method.*
7 import org.hamcrest.MatcherAssert.*
8 import org.hamcrest.Matchers.*
9 import org.junit.*
10 import org.mockito.ArgumentMatchers.any
11 import org.mockito.Mockito.never
12 import org.mockito.Mockito.verify
13
14 /**
15  * Unit test for [UnlockSonePage].
16  */
17 class UnlockSonePageTest : WebPageTest(::UnlockSonePage) {
18
19         @Test
20         fun `page returns correct path`() {
21                 assertThat(page.path, equalTo("unlockSone.html"))
22         }
23
24         @Test
25         fun `page does not require login`() {
26                 assertThat(page.requiresLogin(), equalTo(false))
27         }
28
29         @Test
30         fun `page returns correct title`() {
31                 addTranslation("Page.UnlockSone.Title", "unlock page title")
32                 assertThat(page.getPageTitle(soneRequest), equalTo("unlock page title"))
33         }
34
35         @Test
36         fun `post request without sone redirects to return page`() {
37                 setMethod(POST)
38                 addHttpRequestPart("returnPage", "return.html")
39                 verifyRedirect("return.html") {
40                         verify(core, never()).unlockSone(any())
41                 }
42         }
43
44         @Test
45         fun `post request without invalid local sone does not unlock any sone and redirects to return page`() {
46                 setMethod(POST)
47                 addHttpRequestPart("returnPage", "return.html")
48                 addHttpRequestPart("sone", "invalid-sone")
49                 verifyRedirect("return.html") {
50                         verify(core, never()).unlockSone(any())
51                 }
52         }
53
54         @Test
55         fun `post request without remote sone does not unlock any sone and redirects to return page`() {
56                 setMethod(POST)
57                 addHttpRequestPart("returnPage", "return.html")
58                 addHttpRequestPart("sone", "remote-sone")
59                 addSone("remote-sone", mock())
60                 verifyRedirect("return.html") {
61                         verify(core, never()).unlockSone(any())
62                 }
63         }
64
65         @Test
66         fun `post request with local sone unlocks sone and redirects to return page`() {
67                 setMethod(POST)
68                 addHttpRequestPart("returnPage", "return.html")
69                 addHttpRequestPart("sone", "local-sone")
70                 val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
71                 addLocalSone("local-sone", sone)
72                 verifyRedirect("return.html") {
73                         verify(core).unlockSone(sone)
74                 }
75         }
76
77         @Test
78         fun `page can be created by dependency injection`() {
79                 assertThat(baseInjector.getInstance<UnlockSonePage>(), notNullValue())
80         }
81
82 }