1 package net.pterodactylus.sone.web
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.util.web.Method.GET
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.contains
13 import org.junit.Before
15 import org.mockito.ArgumentMatchers
18 * Unit test for [IndexPage].
20 class IndexPageTest : WebPageTest() {
22 private val postVisibilityFilter = mock<PostVisibilityFilter>()
23 private val page = IndexPage(template, webInterface, postVisibilityFilter)
26 fun setupPostVisibilityFilter() {
27 whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn(object : Predicate<Post> {
28 override fun apply(input: Post?) = true
32 private fun createPost(time: Long, directed: Boolean = false) = mock<Post>().apply {
33 whenever(this.time).thenReturn(time)
34 whenever(recipient).thenReturn(fromNullable(if (directed) currentSone else null))
38 fun `index page shows all posts of current sone`() {
39 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
40 whenever(currentSone.posts).thenReturn(posts)
42 page.handleRequest(freenetRequest, templateContext)
43 @Suppress("UNCHECKED_CAST")
44 assertThat(templateContext["posts"] as Iterable<Post>, contains(*posts.toTypedArray()))
48 fun `index page shows posts directed at current sone from non-followed sones`() {
49 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
50 whenever(currentSone.posts).thenReturn(posts)
51 val notFollowedSone = mock<Sone>()
52 val notFollowedPosts = listOf(createPost(2500, true), createPost(1500))
53 whenever(notFollowedSone.posts).thenReturn(notFollowedPosts)
54 addSone("notfollowed1", notFollowedSone)
56 page.handleRequest(freenetRequest, templateContext)
57 @Suppress("UNCHECKED_CAST")
58 assertThat(templateContext["posts"] as Iterable<Post>, contains(
59 posts[0], notFollowedPosts[0], posts[1], posts[2]
64 fun `index page does not show duplicate posts`() {
65 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
66 whenever(currentSone.posts).thenReturn(posts)
67 val followedSone = mock<Sone>()
68 val followedPosts = listOf(createPost(2500, true), createPost(1500))
69 whenever(followedSone.posts).thenReturn(followedPosts)
70 whenever(currentSone.friends).thenReturn(listOf("followed1", "followed2"))
71 addSone("followed1", followedSone)
73 page.handleRequest(freenetRequest, templateContext)
74 @Suppress("UNCHECKED_CAST")
75 assertThat(templateContext["posts"] as Iterable<Post>, contains(
76 posts[0], followedPosts[0], posts[1], followedPosts[1], posts[2]