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