Replace web page test base with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / ViewPostPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.Profile
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import org.hamcrest.MatcherAssert.assertThat
8 import org.hamcrest.Matchers.equalTo
9 import org.hamcrest.Matchers.nullValue
10 import org.junit.Test
11
12 /**
13  * Unit test for [ViewPostPage].
14  */
15 class ViewPostPageTest: WebPageTest(::ViewPostPage) {
16
17         private val post = mock<Post>()
18
19         @Test
20         fun `page returns correct path`() {
21                 assertThat(page.path, equalTo("viewPost.html"))
22         }
23
24         @Test
25         fun `page does not require login`() {
26                 assertThat(page.requiresLogin(), equalTo(false))
27         }
28
29         @Test
30         fun `the view post page is link-excepted`() {
31                 assertThat(page.isLinkExcepted(null), equalTo(true))
32         }
33
34         @Test
35         fun `get request without parameters stores null in template context`() {
36                 verifyNoRedirect {
37                         assertThat(templateContext["post"], nullValue())
38                         assertThat(templateContext["raw"] as? Boolean, equalTo(false))
39                 }
40         }
41
42         @Test
43         fun `get request with invalid post id stores null in template context`() {
44                 addHttpRequestParameter("post", "invalid-post-id")
45                 verifyNoRedirect {
46                         assertThat(templateContext["post"], nullValue())
47                         assertThat(templateContext["raw"] as? Boolean, equalTo(false))
48                 }
49         }
50
51         @Test
52         fun `get request with valid post id stores post in template context`() {
53                 addPost("post-id", post)
54                 addHttpRequestParameter("post", "post-id")
55                 verifyNoRedirect {
56                         assertThat(templateContext["post"], equalTo<Any>(post))
57                         assertThat(templateContext["raw"] as? Boolean, equalTo(false))
58                 }
59         }
60
61         @Test
62         fun `get request with valid post id and raw=true stores post in template context`() {
63                 addPost("post-id", post)
64                 addHttpRequestParameter("post", "post-id")
65                 addHttpRequestParameter("raw", "true")
66                 verifyNoRedirect {
67                         assertThat(templateContext["post"], equalTo<Any>(post))
68                         assertThat(templateContext["raw"] as? Boolean, equalTo(true))
69                 }
70         }
71
72         @Test
73         fun `page title for request without parameters is default title`() {
74                 addTranslation("Page.ViewPost.Title", "view post title")
75                 assertThat(page.getPageTitle(freenetRequest), equalTo("view post title"))
76         }
77
78         @Test
79         fun `page title for request with invalid post is default title`() {
80                 addHttpRequestParameter("post", "invalid-post-id")
81                 addTranslation("Page.ViewPost.Title", "view post title")
82                 assertThat(page.getPageTitle(freenetRequest), equalTo("view post title"))
83         }
84
85         @Test
86         fun `page title for request with valid post is first twenty chars of post plus sone name plus default title`() {
87                 whenever(currentSone.profile).thenReturn(Profile(currentSone).apply {
88                         firstName = "First"
89                         middleName = "M."
90                         lastName = "Last"
91                 })
92                 whenever(post.sone).thenReturn(currentSone)
93                 whenever(post.text).thenReturn("This is a text that is longer than twenty characters.")
94                 addPost("post-id", post)
95                 addHttpRequestParameter("post", "post-id")
96                 addTranslation("Page.ViewPost.Title", "view post title")
97                 assertThat(page.getPageTitle(freenetRequest), equalTo("This is a text that … - First M. Last - view post title"))
98         }
99
100 }