Replace web page test base with Kotlin version
[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.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
16 import org.junit.Test
17 import org.mockito.ArgumentMatchers
18
19 /**
20  * Unit test for [IndexPage].
21  */
22 class IndexPageTest: WebPageTest({ template, webInterface -> IndexPage(template, webInterface, postVisibilityFilter) }) {
23
24         companion object {
25                 private val postVisibilityFilter = mock<PostVisibilityFilter>()
26         }
27
28         @Test
29         fun `page returns correct path`() {
30             assertThat(page.path, equalTo("index.html"))
31         }
32
33         @Test
34         fun `page requires login`() {
35             assertThat(page.requiresLogin(), equalTo(true))
36         }
37
38         @Test
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"))
42         }
43
44         @Before
45         fun setupPostVisibilityFilter() {
46                 whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn(Predicate<Post> { true })
47         }
48
49         @Before
50         fun setupCurrentSone() {
51                 whenever(currentSone.id).thenReturn("current")
52         }
53
54         @Before
55         fun setupDirectedPosts() {
56                 whenever(core.getDirectedPosts("current")).thenReturn(emptyList())
57         }
58
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))
62         }
63
64         @Test
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()))
71         }
72
73         @Test
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]
86                 ))
87         }
88
89         @Test
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]
102                 ))
103         }
104
105         @Test
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]
119                 ))
120         }
121
122         @Test
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]
130                 ))
131         }
132
133         @Test
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))
142         }
143
144         @Test
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())
151                 }
152         }
153
154 }