e6256bbd21e9b83914e03db0bb3c60540490185a
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / IndexPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
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.web.pages.IndexPage
11 import net.pterodactylus.sone.web.pages.WebPageTest
12 import net.pterodactylus.util.web.Method.GET
13 import org.hamcrest.MatcherAssert.assertThat
14 import org.hamcrest.Matchers.contains
15 import org.junit.Before
16 import org.junit.Test
17 import org.mockito.ArgumentMatchers
18
19 /**
20  * Unit test for [IndexPage].
21  */
22 class IndexPageTest : WebPageTest() {
23
24         private val postVisibilityFilter = mock<PostVisibilityFilter>()
25         private val page = IndexPage(template, webInterface, postVisibilityFilter)
26
27         @Before
28         fun setupPostVisibilityFilter() {
29                 whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn(Predicate<Post> { true })
30         }
31
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))
35         }
36
37         @Test
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)
41                 request("", GET)
42                 page.handleRequest(freenetRequest, templateContext)
43                 @Suppress("UNCHECKED_CAST")
44                 assertThat(templateContext["posts"] as Iterable<Post>, contains(*posts.toTypedArray()))
45         }
46
47         @Test
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)
55                 request("", GET)
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]
60                 ))
61         }
62
63         @Test
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)
72                 request("", GET)
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]
77                 ))
78         }
79
80 }