🎨 Clean up imports
[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.utils.*
6 import net.pterodactylus.sone.web.*
7 import net.pterodactylus.sone.web.page.*
8 import org.hamcrest.MatcherAssert.*
9 import org.hamcrest.Matchers.*
10 import org.junit.*
11 import java.util.Arrays.*
12
13 /**
14  * Unit test for [NewPage].
15  */
16 class NewPageTest: WebPageTest(::NewPage) {
17
18         @Before
19         fun setupNumberOfPostsPerPage() {
20                 webInterface.core.preferences.newPostsPerPage = 5
21         }
22
23         @Test
24         fun `page returns correct path`() {
25                 assertThat(page.path, equalTo("new.html"))
26         }
27
28         @Test
29         fun `page does not require login`() {
30                 assertThat(page.requiresLogin(), equalTo(false))
31         }
32
33         @Test
34         fun `page returns correct title`() {
35                 addTranslation("Page.New.Title", "new page title")
36                 assertThat(page.getPageTitle(soneRequest), equalTo("new page title"))
37         }
38
39         @Test
40         fun `posts are not duplicated when they come from both new posts and new replies notifications`() {
41                 val extraPost = mock<Post>().withTime(2000)
42                 val posts = asList(mock<Post>().withTime(1000), mock<Post>().withTime(3000))
43                 val postReplies = asList(mock<PostReply>(), mock())
44                 whenever(postReplies[0].post).thenReturn(posts[0].asOptional())
45                 whenever(postReplies[1].post).thenReturn(extraPost.asOptional())
46                 whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
47                 whenever(webInterface.getNewReplies(currentSone)).thenReturn(postReplies)
48
49                 verifyNoRedirect {
50                         val renderedPosts = templateContext.get<List<Post>>("posts", List::class.java)
51                         assertThat(renderedPosts, containsInAnyOrder(posts[1], extraPost, posts[0]))
52                 }
53         }
54
55         private fun Post.withTime(time: Long) = apply { whenever(this.time).thenReturn(time) }
56
57         @Test
58         @Suppress("UNCHECKED_CAST")
59         fun `posts are paginated properly`() {
60                 webInterface.core.preferences.newPostsPerPage = 2
61                 val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
62                 whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
63                 verifyNoRedirect {
64                         assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[1], posts[0]))
65                 }
66         }
67
68         @Test
69         @Suppress("UNCHECKED_CAST")
70         fun `posts are paginated properly on second page`() {
71                 webInterface.core.preferences.newPostsPerPage = 2
72                 addHttpRequestParameter("page", "1")
73                 val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
74                 whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
75                 verifyNoRedirect {
76                         assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[2]))
77                 }
78         }
79
80         @Test
81         fun `page can be created by dependency injection`() {
82             assertThat(baseInjector.getInstance<NewPage>(), notNullValue())
83         }
84
85         @Test
86         fun `page is annotated with the correct menuname`() {
87             assertThat(page.menuName, equalTo("New"))
88         }
89
90         @Test
91         fun `page is annotated with correct template path`() {
92             assertThat(page.templatePath, equalTo("/templates/new.html"))
93         }
94
95 }