From e12cdcb23b4549e1daf9fe498b01029cc289f75d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 22 Jan 2023 11:54:39 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Return=20filtered=20elements=20for?= =?utf8?q?=20new-posts=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/pages/NewPage.kt | 2 +- .../pterodactylus/sone/web/pages/NewPageTest.kt | 42 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt index 070fc4c..a3a8461 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt @@ -26,7 +26,7 @@ class NewPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) = getCurrentSone(soneRequest.toadletContext).let { currentSone -> - (newElements.newPosts() + newElements.newReplies().mapPresent { it.post }) + (newElements.newPosts(currentSone) + newElements.newReplies(currentSone).mapPresent { it.post }) .distinct() .sortedByDescending { it.time } .let { posts -> diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt index fa84e5d..3b6f316 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt @@ -2,6 +2,7 @@ package net.pterodactylus.sone.web.pages import net.pterodactylus.sone.data.* import net.pterodactylus.sone.test.* +import net.pterodactylus.sone.test.Matchers.isPostWithId import net.pterodactylus.sone.utils.* import net.pterodactylus.sone.web.* import net.pterodactylus.sone.web.page.* @@ -42,6 +43,7 @@ class NewPageTest : WebPageTest() { @Test fun `posts are not duplicated when they come from both new posts and new replies notifications`() { + unsetCurrentSone() val extraPost = mock().withTime(2000) val posts = asList(mock().withTime(1000), mock().withTime(3000)) val postReplies = asList(mock(), mock()) @@ -61,6 +63,7 @@ class NewPageTest : WebPageTest() { @Test @Suppress("UNCHECKED_CAST") fun `posts are paginated properly`() { + unsetCurrentSone() webInterface.core.preferences.newPostsPerPage = 2 val posts = listOf(mock().withTime(2000), mock().withTime(3000), mock().withTime(1000)) whenever(newElements.newPosts()).thenReturn(posts) @@ -72,6 +75,7 @@ class NewPageTest : WebPageTest() { @Test @Suppress("UNCHECKED_CAST") fun `posts are paginated properly on second page`() { + unsetCurrentSone() webInterface.core.preferences.newPostsPerPage = 2 addHttpRequestParameter("page", "1") val posts = listOf(mock().withTime(2000), mock().withTime(3000), mock().withTime(1000)) @@ -82,6 +86,44 @@ class NewPageTest : WebPageTest() { } @Test + fun `posts are unfiltered if no current Sone is set`() { + unsetCurrentSone() + val posts = listOf(createPost(id = "post1"), createPost(id = "post2")) + whenever(newElements.newPosts()).thenReturn(posts) + verifyNoRedirect { + assertThat(templateContext["posts"] as Collection, containsInAnyOrder(isPostWithId("post1"), isPostWithId("post2"))) + } + } + + @Test + fun `posts are filtered if a current Sone is set`() { + val posts = listOf(createPost(id = "post1"), createPost(id = "post2")) + whenever(newElements.newPosts(currentSone)).thenReturn(posts.last().asList()) + verifyNoRedirect { + assertThat(templateContext["posts"] as Collection, contains(isPostWithId("post2"))) + } + } + + @Test + fun `replies are unfiltered if no current Sone is set`() { + unsetCurrentSone() + val replies = listOf(createPostReply(id = "reply1", post = createPost(id = "post1")), createPostReply(id = "reply2", post = createPost(id = "post2"))) + whenever(newElements.newReplies()).thenReturn(replies) + verifyNoRedirect { + assertThat(templateContext["posts"] as Collection, containsInAnyOrder(isPostWithId("post1"), isPostWithId("post2"))) + } + } + + @Test + fun `replies are filtered if a current Sone is set`() { + val replies = listOf(createPostReply(id = "reply1", post = createPost(id = "post1")), createPostReply(id = "reply2", post = createPost(id = "post2"))) + whenever(newElements.newReplies(currentSone)).thenReturn(replies.last().asList()) + verifyNoRedirect { + assertThat(templateContext["posts"] as Collection, contains(isPostWithId("post2"))) + } + } + + @Test fun `page can be created by dependency injection`() { assertThat(baseInjector.getInstance(), notNullValue()) } -- 2.7.4