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