Add unit test for logout page
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / LogoutPageTest.kt
1 package net.pterodactylus.sone.web
2
3 import net.pterodactylus.sone.test.whenever
4 import net.pterodactylus.sone.web.WebTestUtils.redirectsTo
5 import org.hamcrest.MatcherAssert.assertThat
6 import org.hamcrest.Matchers.equalTo
7 import org.junit.Test
8 import org.mockito.Mockito.verify
9
10 /**
11  * Unit test for [LogoutPage].
12  */
13 class LogoutPageTest : WebPageTest() {
14
15         private val page = LogoutPage(template, webInterface)
16
17         @Test
18         fun `page unsets current sone and redirects to index`() {
19                 expectedException.expect(redirectsTo("index.html"))
20                 try {
21                         page.handleRequest(freenetRequest, templateContext)
22                 } finally {
23                         verify(webInterface).setCurrentSone(toadletContext, null)
24                 }
25         }
26
27         @Test
28         fun `page is not enabled if sone requires full access and request does not have full access`() {
29                 core.preferences.isRequireFullAccess = true
30                 assertThat(page.isEnabled(toadletContext), equalTo(false))
31         }
32
33         @Test
34         fun `page is disabled if no sone is logged in`() {
35                 unsetCurrentSone()
36                 assertThat(page.isEnabled(toadletContext), equalTo(false))
37         }
38
39         @Test
40         fun `page is disabled if sone is logged in but there is only one sone`() {
41                 whenever(core.localSones).thenReturn(listOf(currentSone))
42                 assertThat(page.isEnabled(toadletContext), equalTo(false))
43         }
44
45         @Test
46         fun `page is enabled if sone is logged in and there is more than one sone`() {
47                 whenever(core.localSones).thenReturn(listOf(currentSone, currentSone))
48                 assertThat(page.isEnabled(toadletContext), equalTo(true))
49         }
50
51         @Test
52         fun `page is enabled if full access is required and present and sone is logged in and there is more than one sone`() {
53                 core.preferences.isRequireFullAccess = true
54                 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
55                 whenever(core.localSones).thenReturn(listOf(currentSone, currentSone))
56                 assertThat(page.isEnabled(toadletContext), equalTo(true))
57         }
58
59 }