X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FViewSonePageTest.kt;h=ea1f52823a8c86bd382b5dba20facebed683f7e0;hp=335d839e12381e2fa5368ae877fbd5fd40cb6d5b;hb=03c29a3838e23ed0b9731ca4d84cf58038c30dfe;hpb=5ba707d40f9d2a20094aaabc21647aeec1feed46 diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt index 335d839..ea1f528 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt @@ -4,12 +4,17 @@ import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply import net.pterodactylus.sone.data.Profile import net.pterodactylus.sone.data.Sone -import net.pterodactylus.sone.test.asOptional +import net.pterodactylus.sone.test.getInstance +import net.pterodactylus.sone.test.isOnPage import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.Pagination +import net.pterodactylus.sone.utils.asOptional +import net.pterodactylus.sone.web.baseInjector import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.contains import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.notNullValue import org.hamcrest.Matchers.nullValue import org.junit.Before import org.junit.Test @@ -17,21 +22,19 @@ import org.junit.Test /** * Unit test for [ViewSonePage]. */ -class ViewSonePageTest : WebPageTest() { +class ViewSonePageTest: WebPageTest(::ViewSonePage) { init { whenever(currentSone.id).thenReturn("sone-id") } - private val page = ViewSonePage(template, webInterface) private val post1 = createPost("post1", "First Post.", 1000, currentSone) private val post2 = createPost("post2", "Second Post.", 2000, currentSone) - private val foreignPost1 = createPost("foreign-post1", "First Foreign Post.", 1000, mock()) - private val foreignPost2 = createPost("foreign-post2", "Second Foreign Post.", 2000, mock()) - private val directed1 = createPost("post3", "First directed.", 1500, mock(), recipient = currentSone) - private val directed2 = createPost("post4", "Second directed.", 2500, mock(), recipient = currentSone) - - override fun getPage() = page + private val foreignPost1 = createPost("foreign-post1", "First Foreign Post.", 1000, mock()) + private val foreignPost2 = createPost("foreign-post2", "Second Foreign Post.", 2000, mock()) + private val foreignPost3 = createPost("foreign-post3", "Third Foreign Post.", 3000, mock()) + private val directed1 = createPost("post3", "First directed.", 1500, mock(), recipient = currentSone) + private val directed2 = createPost("post4", "Second directed.", 2500, mock(), recipient = currentSone) @Before fun setup() { @@ -41,18 +44,30 @@ class ViewSonePageTest : WebPageTest() { } @Test + fun `page returns correct path`() { + assertThat(page.path, equalTo("viewSone.html")) + } + + @Test + fun `page does not require login`() { + assertThat(page.requiresLogin(), equalTo(false)) + } + + @Test fun `get request without sone parameter stores null in template context`() { - page.handleRequest(freenetRequest, templateContext) - assertThat(templateContext["sone"], nullValue()) - assertThat(templateContext["soneId"], equalTo("")) + verifyNoRedirect { + assertThat(templateContext["sone"], nullValue()) + assertThat(templateContext["soneId"], equalTo("")) + } } @Test fun `get request with invalid sone parameter stores null in template context`() { addHttpRequestParameter("sone", "invalid-sone-id") - page.handleRequest(freenetRequest, templateContext) - assertThat(templateContext["sone"], nullValue()) - assertThat(templateContext["soneId"], equalTo("invalid-sone-id")) + verifyNoRedirect { + assertThat(templateContext["sone"], nullValue()) + assertThat(templateContext["soneId"], equalTo("invalid-sone-id")) + } } @Test @@ -61,9 +76,10 @@ class ViewSonePageTest : WebPageTest() { whenever(core.getDirectedPosts("sone-id")).thenReturn(emptyList()) addHttpRequestParameter("sone", "sone-id") addSone("sone-id", currentSone) - page.handleRequest(freenetRequest, templateContext) - assertThat(templateContext["sone"], equalTo(currentSone)) - assertThat(templateContext["soneId"], equalTo("sone-id")) + verifyNoRedirect { + assertThat(templateContext["sone"], equalTo(currentSone)) + assertThat(templateContext["soneId"], equalTo("sone-id")) + } } private fun createPost(id: String, text: String, time: Long, sender: Sone? = null, recipient: Sone? = null) = mock().apply { @@ -81,8 +97,10 @@ class ViewSonePageTest : WebPageTest() { fun `request with valid sone stores posts and directed posts in template context`() { addSone("sone-id", currentSone) addHttpRequestParameter("sone", "sone-id") - page.handleRequest(freenetRequest, templateContext) - assertThat(templateContext["posts"] as Iterable, contains(directed2, post2)) + verifyNoRedirect { + assertThat(templateContext["posts"] as Iterable, contains(directed2, post2)) + assertThat(templateContext["postPagination"] as Pagination, isOnPage(0).hasPages(2)) + } } @Test @@ -91,8 +109,10 @@ class ViewSonePageTest : WebPageTest() { addSone("sone-id", currentSone) addHttpRequestParameter("sone", "sone-id") addHttpRequestParameter("postPage", "1") - page.handleRequest(freenetRequest, templateContext) - assertThat(templateContext["posts"] as Iterable, contains(directed1, post1)) + verifyNoRedirect { + assertThat(templateContext["posts"] as Iterable, contains(directed1, post1)) + assertThat(templateContext["postPagination"] as Pagination, isOnPage(1).hasPages(2)) + } } private fun createReply(text: String, time: Long, post: Post?) = mock().apply { @@ -104,35 +124,69 @@ class ViewSonePageTest : WebPageTest() { @Test @Suppress("UNCHECKED_CAST") fun `replies are shown correctly`() { - val reply1 = createReply("First Reply", 1500, foreignPost1) + val reply1 = createReply("First Reply", 1500, foreignPost1) val reply2 = createReply("Second Reply", 2500, foreignPost2) val reply3 = createReply("Third Reply", 1750, post1) val reply4 = createReply("Fourth Reply", 2250, post2) - val reply5 = createReply("Fifth Reply", 1600, post1) - val reply6 = createReply("Sixth Reply", 2100, directed1) - val reply7 = createReply("Seventh Reply", 2200, null) - val reply8 = createReply("Eigth Reply", 2300, foreignPost1) - whenever(currentSone.replies).thenReturn(setOf(reply1, reply2, reply3, reply4, reply5, reply6, reply7, reply8)) + val reply5 = createReply("Fifth Reply", 1600, post1) + val reply6 = createReply("Sixth Reply", 2100, directed1) + val reply7 = createReply("Seventh Reply", 2200, null) + val reply8 = createReply("Eigth Reply", 2300, foreignPost1) + val reply9 = createReply("Ninth Reply", 2050, foreignPost3) + whenever(currentSone.replies).thenReturn(setOf(reply1, reply2, reply3, reply4, reply5, reply6, reply7, reply8, reply9)) whenever(core.getReplies("post1")).thenReturn(listOf(reply3, reply5)) whenever(core.getReplies("post2")).thenReturn(listOf(reply4)) - whenever(core.getReplies("foreign-post1")).thenReturn(listOf(reply1)) + whenever(core.getReplies("foreign-post1")).thenReturn(listOf(reply8, reply1)) whenever(core.getReplies("foreign-post2")).thenReturn(listOf(reply2)) whenever(core.getReplies("post3")).thenReturn(listOf(reply6)) + whenever(core.getReplies("foreign-post3")).thenReturn(listOf(reply9)) addSone("sone-id", currentSone) addHttpRequestParameter("sone", "sone-id") - page.handleRequest(freenetRequest, templateContext) - assertThat(templateContext["repliedPosts"] as Iterable, contains(foreignPost2, foreignPost1)) + verifyNoRedirect { + assertThat(templateContext["repliedPosts"] as Iterable, contains(foreignPost2, foreignPost1)) + assertThat(templateContext["repliedPostPagination"] as Pagination, isOnPage(0).hasPages(2)) + } + } + + @Test + @Suppress("UNCHECKED_CAST") + fun `second page of replies is shown correctly`() { + val reply1 = createReply("First Reply", 1500, foreignPost1) + val reply2 = createReply("Second Reply", 2500, foreignPost2) + val reply3 = createReply("Third Reply", 1750, post1) + val reply4 = createReply("Fourth Reply", 2250, post2) + val reply5 = createReply("Fifth Reply", 1600, post1) + val reply6 = createReply("Sixth Reply", 2100, directed1) + val reply7 = createReply("Seventh Reply", 2200, null) + val reply8 = createReply("Eigth Reply", 2300, foreignPost1) + val reply9 = createReply("Ninth Reply", 2050, foreignPost3) + whenever(currentSone.replies).thenReturn(setOf(reply1, reply2, reply3, reply4, reply5, reply6, reply7, reply8, reply9)) + whenever(core.getReplies("post1")).thenReturn(listOf(reply3, reply5)) + whenever(core.getReplies("post2")).thenReturn(listOf(reply4)) + whenever(core.getReplies("foreign-post1")).thenReturn(listOf(reply8, reply1)) + whenever(core.getReplies("foreign-post2")).thenReturn(listOf(reply2)) + whenever(core.getReplies("post3")).thenReturn(listOf(reply6)) + whenever(core.getReplies("foreign-post3")).thenReturn(listOf(reply9)) + addSone("sone-id", currentSone) + addHttpRequestParameter("sone", "sone-id") + addHttpRequestParameter("repliedPostPage", "1") + verifyNoRedirect { + assertThat(templateContext["repliedPosts"] as Iterable, contains(foreignPost3)) + assertThat(templateContext["repliedPostPagination"] as Pagination, isOnPage(1).hasPages(2)) + } } @Test fun `page title is default for request without parameters`() { - assertThat(page.getPageTitle(freenetRequest), equalTo("Page.ViewSone.Page.TitleWithoutSone")) + addTranslation("Page.ViewSone.Page.TitleWithoutSone", "view sone page without sone") + assertThat(page.getPageTitle(freenetRequest), equalTo("view sone page without sone")) } @Test fun `page title is default for request with invalid sone parameters`() { addHttpRequestParameter("sone", "invalid-sone-id") - assertThat(page.getPageTitle(freenetRequest), equalTo("Page.ViewSone.Page.TitleWithoutSone")) + addTranslation("Page.ViewSone.Page.TitleWithoutSone", "view sone page without sone") + assertThat(page.getPageTitle(freenetRequest), equalTo("view sone page without sone")) } @Test @@ -144,12 +198,18 @@ class ViewSonePageTest : WebPageTest() { middleName = "M." lastName = "Last" }) - assertThat(page.getPageTitle(freenetRequest), equalTo("First M. Last - Page.ViewSone.Title")) + addTranslation("Page.ViewSone.Title", "view sone page") + assertThat(page.getPageTitle(freenetRequest), equalTo("First M. Last - view sone page")) } @Test fun `page is link-excepted`() { - assertThat(page.isLinkExcepted(null), equalTo(true)) + assertThat(page.isLinkExcepted(null), equalTo(true)) + } + + @Test + fun `page can be created by dependency injection`() { + assertThat(baseInjector.getInstance(), notNullValue()) } }