1 package net.pterodactylus.sone.web.pages
3 import com.google.common.base.Optional.fromNullable
4 import com.google.common.base.Predicate
5 import net.pterodactylus.sone.data.Post
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.notify.PostVisibilityFilter
8 import net.pterodactylus.sone.test.mock
9 import net.pterodactylus.sone.test.whenever
10 import net.pterodactylus.sone.utils.Pagination
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.contains
13 import org.hamcrest.Matchers.emptyIterable
14 import org.hamcrest.Matchers.equalTo
15 import org.junit.Before
17 import org.mockito.ArgumentMatchers
20 * Unit test for [IndexPage].
22 class IndexPageTest: WebPageTest({ template, webInterface -> IndexPage(template, webInterface, postVisibilityFilter) }) {
25 private val postVisibilityFilter = mock<PostVisibilityFilter>()
29 fun `page returns correct path`() {
30 assertThat(page.path, equalTo("index.html"))
34 fun `page requires login`() {
35 assertThat(page.requiresLogin(), equalTo(true))
39 fun `page returns correct title`() {
40 whenever(l10n.getString("Page.Index.Title")).thenReturn("index page title")
41 assertThat(page.getPageTitle(freenetRequest), equalTo("index page title"))
45 fun setupPostVisibilityFilter() {
46 whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn(Predicate<Post> { true })
50 fun setupCurrentSone() {
51 whenever(currentSone.id).thenReturn("current")
55 fun setupDirectedPosts() {
56 whenever(core.getDirectedPosts("current")).thenReturn(emptyList())
59 private fun createPost(time: Long, directed: Boolean = false) = mock<Post>().apply {
60 whenever(this.time).thenReturn(time)
61 whenever(recipient).thenReturn(fromNullable(if (directed) currentSone else null))
65 fun `index page shows all posts of current sone`() {
66 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
67 whenever(currentSone.posts).thenReturn(posts)
68 page.processTemplate(freenetRequest, templateContext)
69 @Suppress("UNCHECKED_CAST")
70 assertThat(templateContext["posts"] as Iterable<Post>, contains(*posts.toTypedArray()))
74 fun `index page shows posts directed at current sone from non-followed sones`() {
75 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
76 whenever(currentSone.posts).thenReturn(posts)
77 val notFollowedSone = mock<Sone>()
78 val notFollowedPosts = listOf(createPost(2500, true), createPost(1500))
79 whenever(notFollowedSone.posts).thenReturn(notFollowedPosts)
80 addSone("notfollowed1", notFollowedSone)
81 whenever(core.getDirectedPosts("current")).thenReturn(listOf(notFollowedPosts[0]))
82 page.processTemplate(freenetRequest, templateContext)
83 @Suppress("UNCHECKED_CAST")
84 assertThat(templateContext["posts"] as Iterable<Post>, contains(
85 posts[0], notFollowedPosts[0], posts[1], posts[2]
90 fun `index page does not show duplicate posts`() {
91 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
92 whenever(currentSone.posts).thenReturn(posts)
93 val followedSone = mock<Sone>()
94 val followedPosts = listOf(createPost(2500, true), createPost(1500))
95 whenever(followedSone.posts).thenReturn(followedPosts)
96 whenever(currentSone.friends).thenReturn(listOf("followed1", "followed2"))
97 addSone("followed1", followedSone)
98 page.processTemplate(freenetRequest, templateContext)
99 @Suppress("UNCHECKED_CAST")
100 assertThat(templateContext["posts"] as Iterable<Post>, contains(
101 posts[0], followedPosts[0], posts[1], followedPosts[1], posts[2]
106 fun `index page uses post visibility filter`() {
107 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
108 whenever(currentSone.posts).thenReturn(posts)
109 val followedSone = mock<Sone>()
110 val followedPosts = listOf(createPost(2500, true), createPost(1500))
111 whenever(followedSone.posts).thenReturn(followedPosts)
112 whenever(currentSone.friends).thenReturn(listOf("followed1", "followed2"))
113 whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn(Predicate<Post> { (it?.time ?: 10000) < 2500 })
114 addSone("followed1", followedSone)
115 page.processTemplate(freenetRequest, templateContext)
116 @Suppress("UNCHECKED_CAST")
117 assertThat(templateContext["posts"] as Iterable<Post>, contains(
118 posts[1], followedPosts[1], posts[2]
123 fun `index page sets pagination correctly`() {
124 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
125 whenever(currentSone.posts).thenReturn(posts)
126 page.processTemplate(freenetRequest, templateContext)
127 @Suppress("UNCHECKED_CAST")
128 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(
129 posts[0], posts[1], posts[2]
134 fun `index page sets page correctly`() {
135 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
136 whenever(currentSone.posts).thenReturn(posts)
137 core.preferences.postsPerPage = 1
138 addHttpRequestParameter("page", "2")
139 page.processTemplate(freenetRequest, templateContext)
140 @Suppress("UNCHECKED_CAST")
141 assertThat((templateContext["pagination"] as Pagination<Post>).page, equalTo(2))
145 fun `index page without posts sets correct pagination`() {
146 core.preferences.postsPerPage = 1
147 page.processTemplate(freenetRequest, templateContext)
148 @Suppress("UNCHECKED_CAST")
149 (templateContext["pagination"] as Pagination<Post>).let { pagination ->
150 assertThat(pagination.items, emptyIterable())