1 package net.pterodactylus.sone.web.pages
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.*
11 import java.util.Arrays.*
14 * Unit test for [NewPage].
16 class NewPageTest : WebPageTest(::NewPage) {
19 fun setupNumberOfPostsPerPage() {
20 webInterface.core.preferences.newPostsPerPage = 5
24 fun `page returns correct path`() {
25 assertThat(page.path, equalTo("new.html"))
29 fun `page does not require login`() {
30 assertThat(page.requiresLogin(), equalTo(false))
34 fun `page returns correct title`() {
35 addTranslation("Page.New.Title", "new page title")
36 assertThat(page.getPageTitle(soneRequest), equalTo("new page title"))
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)
50 val renderedPosts = templateContext.get<List<Post>>("posts", List::class.java)
51 assertThat(renderedPosts, containsInAnyOrder(posts[1], extraPost, posts[0]))
55 private fun Post.withTime(time: Long) = apply { whenever(this.time).thenReturn(time) }
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)
64 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[1], posts[0]))
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)
76 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[2]))
81 fun `page can be created by dependency injection`() {
82 assertThat(baseInjector.getInstance<NewPage>(), notNullValue())
86 fun `page is annotated with the correct menuname`() {
87 assertThat(page.menuName, equalTo("New"))
91 fun `page is annotated with correct template path`() {
92 assertThat(page.templatePath, equalTo("/templates/new.html"))