Add some tests and fix names of tests
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ViewPostPageTest.kt
1 package net.pterodactylus.sone.web
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() {
16
17         private val page = ViewPostPage(template, webInterface)
18         private val post = mock<Post>()
19
20         override fun getPage() = page
21
22         @Test
23         fun `the view post page is link-excepted`() {
24                 assertThat(page.isLinkExcepted(null), equalTo(true))
25         }
26
27         @Test
28         fun `get request without parameters stores null in template context`() {
29                 page.handleRequest(freenetRequest, templateContext)
30                 assertThat(templateContext["post"], nullValue())
31                 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
32         }
33
34         @Test
35         fun `get request with invalid post id stores null in template context`() {
36                 addHttpRequestParameter("post", "invalid-post-id")
37                 page.handleRequest(freenetRequest, templateContext)
38                 assertThat(templateContext["post"], nullValue())
39                 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
40         }
41
42         @Test
43         fun `get request with valid post id stores post in template context`() {
44                 addPost("post-id", post)
45                 addHttpRequestParameter("post", "post-id")
46                 page.handleRequest(freenetRequest, templateContext)
47                 assertThat(templateContext["post"], equalTo<Any>(post))
48                 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
49         }
50
51         @Test
52         fun `get request with valid post id and raw=true stores post in template context`() {
53                 addPost("post-id", post)
54                 addHttpRequestParameter("post", "post-id")
55                 addHttpRequestParameter("raw", "true")
56                 page.handleRequest(freenetRequest, templateContext)
57                 assertThat(templateContext["post"], equalTo<Any>(post))
58                 assertThat(templateContext["raw"] as? Boolean, equalTo(true))
59         }
60
61         @Test
62         fun `page title for request without parameters is default title`() {
63                 assertThat(page.getPageTitle(freenetRequest), equalTo("Page.ViewPost.Title"))
64         }
65
66         @Test
67         fun `page title for request with invalid post is default title`() {
68                 addHttpRequestParameter("post", "invalid-post-id")
69                 assertThat(page.getPageTitle(freenetRequest), equalTo("Page.ViewPost.Title"))
70         }
71
72         @Test
73         fun `page title for request with valid post is first twenty chars of post plus sone name plus default title`() {
74                 whenever(currentSone.profile).thenReturn(Profile(currentSone).apply {
75                         firstName = "First"
76                         middleName = "M."
77                         lastName = "Last"
78                 })
79                 whenever(post.sone).thenReturn(currentSone)
80                 whenever(post.text).thenReturn("This is a text that is longer than twenty characters.")
81                 addPost("post-id", post)
82                 addHttpRequestParameter("post", "post-id")
83                 assertThat(page.getPageTitle(freenetRequest), equalTo("This is a text that … - First M. Last - Page.ViewPost.Title"))
84         }
85
86 }