1 package net.pterodactylus.sone.web.pages
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
18 * Unit test for [ViewPostPage].
20 class ViewPostPageTest: WebPageTest(::ViewPostPage) {
22 private val post = mock<Post>()
25 fun `page returns correct path`() {
26 assertThat(page.path, equalTo("viewPost.html"))
30 fun `page does not require login`() {
31 assertThat(page.requiresLogin(), equalTo(false))
35 fun `the view post page is link-excepted`() {
36 assertThat(page.isLinkExcepted(URI("")), equalTo(true))
40 fun `get request without parameters stores null in template context`() {
42 assertThat(templateContext["post"], nullValue())
43 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
48 fun `get request with invalid post id stores null in template context`() {
49 addHttpRequestParameter("post", "invalid-post-id")
51 assertThat(templateContext["post"], nullValue())
52 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
57 fun `get request with valid post id stores post in template context`() {
58 addPost("post-id", post)
59 addHttpRequestParameter("post", "post-id")
61 assertThat(templateContext["post"], equalTo<Any>(post))
62 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
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")
72 assertThat(templateContext["post"], equalTo<Any>(post))
73 assertThat(templateContext["raw"] as? Boolean, equalTo(true))
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"))
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"))
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 {
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"))
106 fun `page can be created by dependency injection`() {
107 assertThat(baseInjector.getInstance<ViewPostPage>(), notNullValue())
111 fun `page is annotated with correct template path`() {
112 assertThat(page.templatePath, equalTo("/templates/viewPost.html"))