1 package net.pterodactylus.sone.web.pages
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.*
17 * Unit test for [IndexPage].
19 class IndexPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> IndexPage(webInterface, loaders, templateRenderer, postVisibilityFilter) }) {
22 private val postVisibilityFilter = mock<PostVisibilityFilter>()
26 fun `page returns correct path`() {
27 assertThat(page.path, equalTo("index.html"))
31 fun `page requires login`() {
32 assertThat(page.requiresLogin(), equalTo(true))
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"))
42 fun setupPostVisibilityFilter() {
43 whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn(Predicate<Post> { true })
47 fun setupCurrentSone() {
48 whenever(currentSone.id).thenReturn("current")
52 fun setupDirectedPosts() {
53 whenever(core.getDirectedPosts("current")).thenReturn(emptyList())
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))
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()))
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]
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]
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]
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]
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))
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())
152 fun `page can be created by dependency injection`() {
153 assertThat(baseInjector.getInstance<IndexPage>(), notNullValue())
157 fun `page is annotated with correct menuname`() {
158 assertThat(page.menuName, equalTo("Index"))
162 fun `page is annotated with correct template path`() {
163 assertThat(page.templatePath, equalTo("/templates/index.html"))