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
16 * Unit test for [ViewPostPage].
18 class ViewPostPageTest: WebPageTest(::ViewPostPage) {
20 private val post = mock<Post>()
23 fun `page returns correct path`() {
24 assertThat(page.path, equalTo("viewPost.html"))
28 fun `page does not require login`() {
29 assertThat(page.requiresLogin(), equalTo(false))
33 fun `the view post page is link-excepted`() {
34 assertThat(page.isLinkExcepted(null), equalTo(true))
38 fun `get request without parameters stores null in template context`() {
40 assertThat(templateContext["post"], nullValue())
41 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
46 fun `get request with invalid post id stores null in template context`() {
47 addHttpRequestParameter("post", "invalid-post-id")
49 assertThat(templateContext["post"], nullValue())
50 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
55 fun `get request with valid post id stores post in template context`() {
56 addPost("post-id", post)
57 addHttpRequestParameter("post", "post-id")
59 assertThat(templateContext["post"], equalTo<Any>(post))
60 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
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")
70 assertThat(templateContext["post"], equalTo<Any>(post))
71 assertThat(templateContext["raw"] as? Boolean, equalTo(true))
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"))
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"))
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 {
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"))
104 fun `page can be created by dependency injection`() {
105 assertThat(baseInjector.getInstance<ViewPostPage>(), notNullValue())