1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.test.*
5 import net.pterodactylus.sone.web.*
6 import net.pterodactylus.sone.web.page.*
7 import org.hamcrest.MatcherAssert.*
8 import org.hamcrest.Matchers.*
13 * Unit test for [ViewPostPage].
15 class ViewPostPageTest : WebPageTest(::ViewPostPage) {
17 private val post = mock<Post>()
20 fun `page returns correct path`() {
21 assertThat(page.path, equalTo("viewPost.html"))
25 fun `page does not require login`() {
26 assertThat(page.requiresLogin(), equalTo(false))
30 fun `the view post page is link-excepted`() {
31 assertThat(page.isLinkExcepted(URI("")), equalTo(true))
35 fun `get request without parameters stores null in template context`() {
37 assertThat(templateContext["post"], nullValue())
38 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
43 fun `get request with invalid post id stores null in template context`() {
44 addHttpRequestParameter("post", "invalid-post-id")
46 assertThat(templateContext["post"], nullValue())
47 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
52 fun `get request with valid post id stores post in template context`() {
53 addPost("post-id", post)
54 addHttpRequestParameter("post", "post-id")
56 assertThat(templateContext["post"], equalTo<Any>(post))
57 assertThat(templateContext["raw"] as? Boolean, equalTo(false))
62 fun `get request with valid post id and raw=true stores post in template context`() {
63 addPost("post-id", post)
64 addHttpRequestParameter("post", "post-id")
65 addHttpRequestParameter("raw", "true")
67 assertThat(templateContext["post"], equalTo<Any>(post))
68 assertThat(templateContext["raw"] as? Boolean, equalTo(true))
73 fun `page title for request without parameters is default title`() {
74 addTranslation("Page.ViewPost.Title", "view post title")
75 assertThat(page.getPageTitle(soneRequest), equalTo("view post title"))
79 fun `page title for request with invalid post is default title`() {
80 addHttpRequestParameter("post", "invalid-post-id")
81 addTranslation("Page.ViewPost.Title", "view post title")
82 assertThat(page.getPageTitle(soneRequest), equalTo("view post title"))
86 fun `page title for request with valid post is first twenty chars of post plus sone name plus default title`() {
87 whenever(currentSone.profile).thenReturn(Profile(currentSone).apply {
92 whenever(post.sone).thenReturn(currentSone)
93 whenever(post.text).thenReturn("This is a text that is longer than twenty characters.")
94 addPost("post-id", post)
95 addHttpRequestParameter("post", "post-id")
96 addTranslation("Page.ViewPost.Title", "view post title")
97 assertThat(page.getPageTitle(soneRequest), equalTo("This is a text that … - First M. Last - view post title"))
101 fun `page can be created by dependency injection`() {
102 assertThat(baseInjector.getInstance<ViewPostPage>(), notNullValue())
106 fun `page is annotated with correct template path`() {
107 assertThat(page.templatePath, equalTo("/templates/viewPost.html"))