🔀 Merge changes from other next branch
[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.*
4 import com.google.common.base.Optional.*
5 import net.pterodactylus.sone.data.*
6 import net.pterodactylus.sone.notify.*
7 import net.pterodactylus.sone.test.*
8 import net.pterodactylus.sone.utils.*
9 import net.pterodactylus.sone.web.*
10 import net.pterodactylus.sone.web.page.*
11 import org.hamcrest.MatcherAssert.*
12 import org.hamcrest.Matchers.*
13 import org.junit.*
14 import org.mockito.*
15
16 /**
17  * Unit test for [IndexPage].
18  */
19 class IndexPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> IndexPage(webInterface, loaders, templateRenderer, postVisibilityFilter) }) {
20
21         companion object {
22                 private val postVisibilityFilter = mock<PostVisibilityFilter>()
23         }
24
25         @Test
26         fun `page returns correct path`() {
27                 assertThat(page.path, equalTo("index.html"))
28         }
29
30         @Test
31         fun `page requires login`() {
32                 assertThat(page.requiresLogin(), equalTo(true))
33         }
34
35         @Test
36         fun `page returns correct title`() {
37                 whenever(l10n.getString("Page.Index.Title")).thenReturn("index page title")
38                 assertThat(page.getPageTitle(soneRequest), equalTo("index page title"))
39         }
40
41         @Before
42         fun setupPostVisibilityFilter() {
43                 whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn(Predicate<Post> { true })
44         }
45
46         @Before
47         fun setupCurrentSone() {
48                 whenever(currentSone.id).thenReturn("current")
49         }
50
51         @Before
52         fun setupDirectedPosts() {
53                 whenever(core.getDirectedPosts("current")).thenReturn(emptyList())
54         }
55
56         private fun createPost(time: Long, directed: Boolean = false) = mock<Post>().apply {
57                 whenever(this.time).thenReturn(time)
58                 whenever(recipient).thenReturn(fromNullable(if (directed) currentSone else null))
59         }
60
61         @Test
62         fun `index page shows all posts of current sone`() {
63                 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
64                 whenever(currentSone.posts).thenReturn(posts)
65                 page.processTemplate(freenetRequest, templateContext)
66                 @Suppress("UNCHECKED_CAST")
67                 assertThat(templateContext["posts"] as Iterable<Post>, contains(*posts.toTypedArray()))
68         }
69
70         @Test
71         fun `index page shows posts directed at current sone from non-followed sones`() {
72                 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
73                 whenever(currentSone.posts).thenReturn(posts)
74                 val notFollowedSone = mock<Sone>()
75                 val notFollowedPosts = listOf(createPost(2500, true), createPost(1500))
76                 whenever(notFollowedSone.posts).thenReturn(notFollowedPosts)
77                 addSone("notfollowed1", notFollowedSone)
78                 whenever(core.getDirectedPosts("current")).thenReturn(listOf(notFollowedPosts[0]))
79                 page.processTemplate(freenetRequest, templateContext)
80                 @Suppress("UNCHECKED_CAST")
81                 assertThat(templateContext["posts"] as Iterable<Post>, contains(
82                                 posts[0], notFollowedPosts[0], posts[1], posts[2]
83                 ))
84         }
85
86         @Test
87         fun `index page does not show duplicate posts`() {
88                 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
89                 whenever(currentSone.posts).thenReturn(posts)
90                 val followedSone = mock<Sone>()
91                 val followedPosts = listOf(createPost(2500, true), createPost(1500))
92                 whenever(followedSone.posts).thenReturn(followedPosts)
93                 whenever(currentSone.friends).thenReturn(listOf("followed1", "followed2"))
94                 addSone("followed1", followedSone)
95                 page.processTemplate(freenetRequest, templateContext)
96                 @Suppress("UNCHECKED_CAST")
97                 assertThat(templateContext["posts"] as Iterable<Post>, contains(
98                                 posts[0], followedPosts[0], posts[1], followedPosts[1], posts[2]
99                 ))
100         }
101
102         @Test
103         fun `index page uses post visibility filter`() {
104                 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
105                 whenever(currentSone.posts).thenReturn(posts)
106                 val followedSone = mock<Sone>()
107                 val followedPosts = listOf(createPost(2500, true), createPost(1500))
108                 whenever(followedSone.posts).thenReturn(followedPosts)
109                 whenever(currentSone.friends).thenReturn(listOf("followed1", "followed2"))
110                 whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn(Predicate<Post> { (it?.time ?: 10000) < 2500 })
111                 addSone("followed1", followedSone)
112                 page.processTemplate(freenetRequest, templateContext)
113                 @Suppress("UNCHECKED_CAST")
114                 assertThat(templateContext["posts"] as Iterable<Post>, contains(
115                                 posts[1], followedPosts[1], posts[2]
116                 ))
117         }
118
119         @Test
120         fun `index page sets pagination correctly`() {
121                 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
122                 whenever(currentSone.posts).thenReturn(posts)
123                 page.processTemplate(freenetRequest, templateContext)
124                 @Suppress("UNCHECKED_CAST")
125                 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(
126                                 posts[0], posts[1], posts[2]
127                 ))
128         }
129
130         @Test
131         fun `index page sets page correctly`() {
132                 val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
133                 whenever(currentSone.posts).thenReturn(posts)
134                 core.preferences.newPostsPerPage = 1
135                 addHttpRequestParameter("page", "2")
136                 page.processTemplate(freenetRequest, templateContext)
137                 @Suppress("UNCHECKED_CAST")
138                 assertThat((templateContext["pagination"] as Pagination<Post>).page, equalTo(2))
139         }
140
141         @Test
142         fun `index page without posts sets correct pagination`() {
143                 core.preferences.newPostsPerPage = 1
144                 page.processTemplate(freenetRequest, templateContext)
145                 @Suppress("UNCHECKED_CAST")
146                 (templateContext["pagination"] as Pagination<Post>).let { pagination ->
147                         assertThat(pagination.items, emptyIterable())
148                 }
149         }
150
151         @Test
152         fun `page can be created by dependency injection`() {
153                 assertThat(baseInjector.getInstance<IndexPage>(), notNullValue())
154         }
155
156         @Test
157         fun `page is annotated with correct menuname`() {
158                 assertThat(page.menuName, equalTo("Index"))
159         }
160
161         @Test
162         fun `page is annotated with correct template path`() {
163                 assertThat(page.templatePath, equalTo("/templates/index.html"))
164         }
165
166 }