✨ Return filtered elements for new-posts page
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / NewPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.test.*
5 import net.pterodactylus.sone.test.Matchers.isPostWithId
6 import net.pterodactylus.sone.utils.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.sone.web.page.*
9 import org.hamcrest.MatcherAssert.*
10 import org.hamcrest.Matchers.*
11 import org.junit.*
12 import java.util.Arrays.*
13
14 /**
15  * Unit test for [NewPage].
16  */
17 class NewPageTest : WebPageTest() {
18
19         private val newElements = mock<NewElements>()
20         override val page: SoneTemplatePage
21                 get() = NewPage(webInterface, loaders, templateRenderer, newElements)
22
23         @Before
24         fun setupNumberOfPostsPerPage() {
25                 webInterface.core.preferences.newPostsPerPage = 5
26         }
27
28         @Test
29         fun `page returns correct path`() {
30                 assertThat(page.path, equalTo("new.html"))
31         }
32
33         @Test
34         fun `page does not require login`() {
35                 assertThat(page.requiresLogin(), equalTo(false))
36         }
37
38         @Test
39         fun `page returns correct title`() {
40                 addTranslation("Page.New.Title", "new page title")
41                 assertThat(page.getPageTitle(soneRequest), equalTo("new page title"))
42         }
43
44         @Test
45         fun `posts are not duplicated when they come from both new posts and new replies notifications`() {
46                 unsetCurrentSone()
47                 val extraPost = mock<Post>().withTime(2000)
48                 val posts = asList(mock<Post>().withTime(1000), mock<Post>().withTime(3000))
49                 val postReplies = asList(mock<PostReply>(), mock())
50                 whenever(postReplies[0].post).thenReturn(posts[0].asOptional())
51                 whenever(postReplies[1].post).thenReturn(extraPost.asOptional())
52                 whenever(newElements.newPosts()).thenReturn(posts)
53                 whenever(newElements.newReplies()).thenReturn(postReplies)
54
55                 verifyNoRedirect {
56                         val renderedPosts = templateContext.get<List<Post>>("posts", List::class.java)
57                         assertThat(renderedPosts, containsInAnyOrder(posts[1], extraPost, posts[0]))
58                 }
59         }
60
61         private fun Post.withTime(time: Long) = apply { whenever(this.time).thenReturn(time) }
62
63         @Test
64         @Suppress("UNCHECKED_CAST")
65         fun `posts are paginated properly`() {
66                 unsetCurrentSone()
67                 webInterface.core.preferences.newPostsPerPage = 2
68                 val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
69                 whenever(newElements.newPosts()).thenReturn(posts)
70                 verifyNoRedirect {
71                         assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[1], posts[0]))
72                 }
73         }
74
75         @Test
76         @Suppress("UNCHECKED_CAST")
77         fun `posts are paginated properly on second page`() {
78                 unsetCurrentSone()
79                 webInterface.core.preferences.newPostsPerPage = 2
80                 addHttpRequestParameter("page", "1")
81                 val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
82                 whenever(newElements.newPosts()).thenReturn(posts)
83                 verifyNoRedirect {
84                         assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[2]))
85                 }
86         }
87
88         @Test
89         fun `posts are unfiltered if no current Sone is set`() {
90                 unsetCurrentSone()
91                 val posts = listOf(createPost(id = "post1"), createPost(id = "post2"))
92                 whenever(newElements.newPosts()).thenReturn(posts)
93                 verifyNoRedirect {
94                         assertThat(templateContext["posts"] as Collection<Post>, containsInAnyOrder(isPostWithId("post1"), isPostWithId("post2")))
95                 }
96         }
97
98         @Test
99         fun `posts are filtered if a current Sone is set`() {
100                 val posts = listOf(createPost(id = "post1"), createPost(id = "post2"))
101                 whenever(newElements.newPosts(currentSone)).thenReturn(posts.last().asList())
102                 verifyNoRedirect {
103                         assertThat(templateContext["posts"] as Collection<Post>, contains(isPostWithId("post2")))
104                 }
105         }
106
107         @Test
108         fun `replies are unfiltered if no current Sone is set`() {
109                 unsetCurrentSone()
110                 val replies = listOf(createPostReply(id = "reply1", post = createPost(id = "post1")), createPostReply(id = "reply2", post = createPost(id = "post2")))
111                 whenever(newElements.newReplies()).thenReturn(replies)
112                 verifyNoRedirect {
113                         assertThat(templateContext["posts"] as Collection<Post>, containsInAnyOrder(isPostWithId("post1"), isPostWithId("post2")))
114                 }
115         }
116
117         @Test
118         fun `replies are filtered if a current Sone is set`() {
119                 val replies = listOf(createPostReply(id = "reply1", post = createPost(id = "post1")), createPostReply(id = "reply2", post = createPost(id = "post2")))
120                 whenever(newElements.newReplies(currentSone)).thenReturn(replies.last().asList())
121                 verifyNoRedirect {
122                         assertThat(templateContext["posts"] as Collection<Post>, contains(isPostWithId("post2")))
123                 }
124         }
125
126         @Test
127         fun `page can be created by dependency injection`() {
128                 assertThat(baseInjector.getInstance<NewPage>(), notNullValue())
129         }
130
131         @Test
132         fun `page is annotated with the correct menuname`() {
133                 assertThat(page.menuName, equalTo("New"))
134         }
135
136         @Test
137         fun `page is annotated with correct template path`() {
138                 assertThat(page.templatePath, equalTo("/templates/new.html"))
139         }
140
141 }