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 org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.equalTo
11 import org.hamcrest.Matchers.notNullValue
12 import org.hamcrest.Matchers.nullValue
17 * Unit test for [ViewPostPage].
19 class ViewPostPageTest: WebPageTest(::ViewPostPage) {
21 private val post = mock<Post>()
24 fun `page returns correct path`() {
25 assertThat(page.path, equalTo("viewPost.html"))
29 fun `page does not require login`() {
30 assertThat(page.requiresLogin(), equalTo(false))
34 fun `the view post page is link-excepted`() {
35 assertThat(page.isLinkExcepted(URI("")), equalTo(true))
39 fun `get request without parameters stores null in template context`() {
41 assertThat(templateContext["post"], nullValue())
42 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
47 fun `get request with invalid post id stores null in template context`() {
48 addHttpRequestParameter("post", "invalid-post-id")
50 assertThat(templateContext["post"], nullValue())
51 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
56 fun `get request with valid post id stores post in template context`() {
57 addPost("post-id", post)
58 addHttpRequestParameter("post", "post-id")
60 assertThat(templateContext["post"], equalTo<Any>(post))
61 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
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")
71 assertThat(templateContext["post"], equalTo<Any>(post))
72 assertThat(templateContext["raw"] as? Boolean, equalTo(true))
77 fun `page title for request without parameters is default title`() {
78 addTranslation("Page.ViewPost.Title", "view post title")
79 assertThat(page.getPageTitle(freenetRequest), equalTo("view post title"))
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(freenetRequest), equalTo("view post title"))
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 {
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(freenetRequest), equalTo("This is a text that … - First M. Last - view post title"))
105 fun `page can be created by dependency injection`() {
106 assertThat(baseInjector.getInstance<ViewPostPage>(), notNullValue())