1 package net.pterodactylus.sone.web
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.Profile
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import org.hamcrest.MatcherAssert.assertThat
8 import org.hamcrest.Matchers.equalTo
9 import org.hamcrest.Matchers.nullValue
13 * Unit test for [ViewPostPage].
15 class ViewPostPageTest : WebPageTest() {
17 private val page = ViewPostPage(template, webInterface)
18 private val post = mock<Post>()
20 override fun getPage() = page
23 fun `the view post page is link-excepted`() {
24 assertThat(page.isLinkExcepted(null), equalTo(true))
28 fun `get request without parameters stores null in template context`() {
29 page.handleRequest(freenetRequest, templateContext)
30 assertThat(templateContext["post"], nullValue())
31 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
35 fun `get request with invalid post id stores null in template context`() {
36 addHttpRequestParameter("post", "invalid-post-id")
37 page.handleRequest(freenetRequest, templateContext)
38 assertThat(templateContext["post"], nullValue())
39 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
43 fun `get request with valid post id stores post in template context`() {
44 addPost("post-id", post)
45 addHttpRequestParameter("post", "post-id")
46 page.handleRequest(freenetRequest, templateContext)
47 assertThat(templateContext["post"], equalTo<Any>(post))
48 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
52 fun `get request with valid post id and raw=true stores post in template context`() {
53 addPost("post-id", post)
54 addHttpRequestParameter("post", "post-id")
55 addHttpRequestParameter("raw", "true")
56 page.handleRequest(freenetRequest, templateContext)
57 assertThat(templateContext["post"], equalTo<Any>(post))
58 assertThat(templateContext["raw"] as? Boolean, equalTo(true))
62 fun `page title for request without parameters is default title`() {
63 assertThat(page.getPageTitle(freenetRequest), equalTo("Page.ViewPost.Title"))
67 fun `page title for request with invalid post is default title`() {
68 addHttpRequestParameter("post", "invalid-post-id")
69 assertThat(page.getPageTitle(freenetRequest), equalTo("Page.ViewPost.Title"))
73 fun `page title for request with valid post is first twenty chars of post plus sone name plus default title`() {
74 whenever(currentSone.profile).thenReturn(Profile(currentSone).apply {
79 whenever(post.sone).thenReturn(currentSone)
80 whenever(post.text).thenReturn("This is a text that is longer than twenty characters.")
81 addPost("post-id", post)
82 addHttpRequestParameter("post", "post-id")
83 assertThat(page.getPageTitle(freenetRequest), equalTo("This is a text that … - First M. Last - Page.ViewPost.Title"))