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