From 2468dfbe115467831b57c7ad2c5b550793998bed Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 22 Jan 2017 12:16:08 +0100 Subject: [PATCH 1/1] Add unit test for view post page --- .../net/pterodactylus/sone/web/ViewPostPageTest.kt | 86 ++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/ViewPostPageTest.kt 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")) + } + +} -- 2.7.4