1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.PostReply
5 import net.pterodactylus.sone.test.asOptional
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.sone.utils.Pagination
9 import net.pterodactylus.util.web.Method.GET
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.contains
12 import org.hamcrest.Matchers.containsInAnyOrder
13 import org.hamcrest.Matchers.equalTo
14 import org.junit.Before
16 import java.util.Arrays.asList
19 * Unit test for [NewPage].
21 class NewPageTest: WebPageTest() {
23 private val page = NewPage(template, webInterface)
25 override fun getPage() = page
28 fun setupNumberOfPostsPerPage() {
29 webInterface.core.preferences.postsPerPage = 5
33 fun `page returns correct path`() {
34 assertThat(page.path, equalTo("new.html"))
38 fun `page does not require login`() {
39 assertThat(page.requiresLogin(), equalTo(false))
43 fun `page returns correct title`() {
44 addTranslation("Page.New.Title", "new page title")
45 assertThat(page.getPageTitle(freenetRequest), equalTo("new page title"))
49 fun `posts are not duplicated when they come from both new posts and new replies notifications`() {
51 val extraPost = mock<Post>().withTime(2000)
52 val posts = asList(mock<Post>().withTime(1000), mock<Post>().withTime(3000))
53 val postReplies = asList(mock<PostReply>(), mock<PostReply>())
54 whenever(postReplies[0].post).thenReturn(posts[0].asOptional())
55 whenever(postReplies[1].post).thenReturn(extraPost.asOptional())
56 whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
57 whenever(webInterface.getNewReplies(currentSone)).thenReturn(postReplies)
60 val renderedPosts = templateContext.get<List<Post>>("posts", List::class.java)
61 assertThat(renderedPosts, containsInAnyOrder(posts[1], extraPost, posts[0]))
65 private fun Post.withTime(time: Long) = apply { whenever(this.time).thenReturn(time) }
68 @Suppress("UNCHECKED_CAST")
69 fun `posts are paginated properly`() {
71 webInterface.core.preferences.postsPerPage = 2
72 val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
73 whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
75 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[1], posts[0]))
80 @Suppress("UNCHECKED_CAST")
81 fun `posts are paginated properly on second page`() {
83 webInterface.core.preferences.postsPerPage = 2
84 addHttpRequestParameter("page", "1")
85 val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
86 whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
88 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[2]))