From: David ‘Bombe’ Roden Date: Sun, 22 Jan 2017 11:16:08 +0000 (+0100) Subject: Add unit test for view post page X-Git-Tag: 0.9.7^2~319 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2468dfbe115467831b57c7ad2c5b550793998bed;p=Sone.git Add unit test for view post page --- diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ViewPostPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ViewPostPageTest.kt new file mode 100644 index 0000000..36c70d2 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/ViewPostPageTest.kt @@ -0,0 +1,86 @@ +package net.pterodactylus.sone.web + +import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.data.Profile +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.nullValue +import org.junit.Test + +/** + * Unit test for [ViewPostPage]. + */ +class ViewPostPageTest : WebPageTest() { + + private val page = ViewPostPage(template, webInterface) + private val post = mock() + + override fun getPage() = page + + @Test + fun `the view post page is link-excepted`() { + assertThat(page.isLinkExcepted(null), equalTo(true)) + } + + @Test + fun `get request without parameters stores null in template context`() { + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["post"], nullValue()) + assertThat(templateContext["raw"] as? Boolean, equalTo(false)) + } + + @Test + fun `get request with invalid post id stores null in template context`() { + addHttpRequestParameter("post", "invalid-post-id") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["post"], nullValue()) + assertThat(templateContext["raw"] as? Boolean, equalTo(false)) + } + + @Test + fun `get request with valid post id stores post in template context`() { + addPost("post-id", post) + addHttpRequestParameter("post", "post-id") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["post"], equalTo(post)) + assertThat(templateContext["raw"] as? Boolean, equalTo(false)) + } + + @Test + fun `get request with valid post id and raw=true stores post in template context`() { + addPost("post-id", post) + addHttpRequestParameter("post", "post-id") + addHttpRequestParameter("raw", "true") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["post"], equalTo(post)) + assertThat(templateContext["raw"] as? Boolean, equalTo(true)) + } + + @Test + fun `page title for request without parameters is default title`() { + assertThat(page.getPageTitle(freenetRequest), equalTo("Page.ViewPost.Title")) + } + + @Test + fun `page title for request with invalid post is default title`() { + addHttpRequestParameter("post", "invalid-post-id") + assertThat(page.getPageTitle(freenetRequest), equalTo("Page.ViewPost.Title")) + } + + @Test + fun `page title for request with valid post is first twenty chars of post plus sone name plus default title`() { + whenever(currentSone.profile).thenReturn(Profile(currentSone).apply { + firstName = "First" + middleName = "M." + lastName = "Last" + }) + whenever(post.sone).thenReturn(currentSone) + whenever(post.text).thenReturn("This is a text that is longer than twenty characters.") + addPost("post-id", post) + addHttpRequestParameter("post", "post-id") + assertThat(page.getPageTitle(freenetRequest), equalTo("This is a text that … - First M. Last - Page.ViewPost.Title")) + } + +}