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.getInstance
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.sone.utils.asOptional
10 import net.pterodactylus.sone.web.baseInjector
11 import net.pterodactylus.sone.web.page.*
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.contains
14 import org.hamcrest.Matchers.containsInAnyOrder
15 import org.hamcrest.Matchers.equalTo
16 import org.hamcrest.Matchers.notNullValue
17 import org.junit.Before
19 import java.util.Arrays.asList
22 * Unit test for [NewPage].
24 class NewPageTest: WebPageTest(::NewPage) {
27 fun setupNumberOfPostsPerPage() {
28 webInterface.core.preferences.newPostsPerPage = 5
32 fun `page returns correct path`() {
33 assertThat(page.path, equalTo("new.html"))
37 fun `page does not require login`() {
38 assertThat(page.requiresLogin(), equalTo(false))
42 fun `page returns correct title`() {
43 addTranslation("Page.New.Title", "new page title")
44 assertThat(page.getPageTitle(soneRequest), equalTo("new page title"))
48 fun `posts are not duplicated when they come from both new posts and new replies notifications`() {
49 val extraPost = mock<Post>().withTime(2000)
50 val posts = asList(mock<Post>().withTime(1000), mock<Post>().withTime(3000))
51 val postReplies = asList(mock<PostReply>(), mock())
52 whenever(postReplies[0].post).thenReturn(posts[0].asOptional())
53 whenever(postReplies[1].post).thenReturn(extraPost.asOptional())
54 whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
55 whenever(webInterface.getNewReplies(currentSone)).thenReturn(postReplies)
58 val renderedPosts = templateContext.get<List<Post>>("posts", List::class.java)
59 assertThat(renderedPosts, containsInAnyOrder(posts[1], extraPost, posts[0]))
63 private fun Post.withTime(time: Long) = apply { whenever(this.time).thenReturn(time) }
66 @Suppress("UNCHECKED_CAST")
67 fun `posts are paginated properly`() {
68 webInterface.core.preferences.newPostsPerPage = 2
69 val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
70 whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
72 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[1], posts[0]))
77 @Suppress("UNCHECKED_CAST")
78 fun `posts are paginated properly on second page`() {
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(webInterface.getNewPosts(currentSone)).thenReturn(posts)
84 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[2]))
89 fun `page can be created by dependency injection`() {
90 assertThat(baseInjector.getInstance<NewPage>(), notNullValue())
94 fun `page is annotated with the correct menuname`() {
95 assertThat(page.menuName, equalTo("New"))