1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Image
5 import net.pterodactylus.sone.data.Post
6 import net.pterodactylus.sone.data.PostReply
7 import net.pterodactylus.sone.data.Profile
8 import net.pterodactylus.sone.data.Sone
9 import net.pterodactylus.sone.freenet.wot.Identity
10 import net.pterodactylus.sone.freenet.wot.OwnIdentity
11 import net.pterodactylus.sone.test.mock
12 import net.pterodactylus.sone.test.whenever
13 import net.pterodactylus.sone.utils.Pagination
14 import org.hamcrest.MatcherAssert.assertThat
15 import org.hamcrest.Matchers.contains
16 import org.hamcrest.Matchers.equalTo
17 import org.junit.Before
21 * Unit test for [KnownSonesPage].
23 class KnownSonesPageTest: WebPageTest(::KnownSonesPage) {
25 private val sones = listOf(
26 createSone(1000, 4, 7, 2, "sone2", true, true),
27 createSone(2000, 3, 2, 3, "Sone1", false, true),
28 createSone(3000, 3, 8, 1, "Sone3", true, false),
29 createSone(4000, 1, 6, 0, "sone0", false, false)
34 addSone("sone1", sones[0])
35 addSone("sone2", sones[1])
36 addSone("sone3", sones[2])
37 addSone("sone4", sones[3])
40 private fun createSone(time: Long, posts: Int, replies: Int, images: Int, name: String, local: Boolean, new: Boolean) = mock<Sone>().apply {
41 whenever(identity).thenReturn(if (local) mock<OwnIdentity>() else mock<Identity>())
42 whenever(this.isLocal).thenReturn(local)
43 whenever(isKnown).thenReturn(!new)
44 whenever(this.time).thenReturn(time)
45 whenever(this.posts).thenReturn((0..(posts - 1)).map { mock<Post>() })
46 whenever(this.replies).thenReturn((0..(replies - 1)).map { mock<PostReply>() }.toSet())
47 val album = mock<Album>()
48 whenever(album.images).thenReturn(((0..(images - 1)).map { mock<Image>() }))
49 val rootAlbum = mock<Album>().apply {
50 whenever(albums).thenReturn(listOf(album))
52 whenever(this.rootAlbum).thenReturn(rootAlbum)
53 whenever(this.profile).thenReturn(mock<Profile>())
54 whenever(id).thenReturn(name.toLowerCase())
55 whenever(this.name).thenReturn(name)
58 private fun verifySonesAreInOrder(vararg indices: Int) {
59 @Suppress("UNCHECKED_CAST")
60 assertThat(templateContext["knownSones"] as Iterable<Sone>, contains(
61 *indices.map { sones[it] }.toTypedArray()
65 private fun verifyStoredFields(sort: String, order: String, filter: String) {
66 assertThat(templateContext["sort"], equalTo<Any>(sort))
67 assertThat(templateContext["order"], equalTo<Any>(order))
68 assertThat(templateContext["filter"], equalTo<Any>(filter))
72 fun `page returns correct path`() {
73 assertThat(page.path, equalTo("knownSones.html"))
77 fun `page does not require login`() {
78 assertThat(page.requiresLogin(), equalTo(false))
82 fun `page returns correct title`() {
83 whenever(l10n.getString("Page.KnownSones.Title")).thenReturn("known sones page title")
84 assertThat(page.getPageTitle(freenetRequest), equalTo("known sones page title"))
88 fun `default known sones are sorted newest first`() {
90 verifySonesAreInOrder(3, 2, 1, 0)
91 verifyStoredFields("activity", "desc", "")
96 fun `known sones can be sorted by oldest first`() {
97 addHttpRequestParameter("order", "asc")
99 verifySonesAreInOrder(0, 1, 2, 3)
100 verifyStoredFields("activity", "asc", "")
105 fun `known sones can be sorted by posts, most posts first`() {
106 addHttpRequestParameter("sort", "posts")
108 verifySonesAreInOrder(0, 2, 1, 3)
109 verifyStoredFields("posts", "desc", "")
114 fun `known sones can be sorted by posts, least posts first`() {
115 addHttpRequestParameter("sort", "posts")
116 addHttpRequestParameter("order", "asc")
118 verifySonesAreInOrder(3, 1, 2, 0)
119 verifyStoredFields("posts", "asc", "")
124 fun `known sones can be sorted by images, most images first`() {
125 addHttpRequestParameter("sort", "images")
127 verifySonesAreInOrder(1, 0, 2, 3)
128 verifyStoredFields("images", "desc", "")
133 fun `known sones can be sorted by images, least images first`() {
134 addHttpRequestParameter("sort", "images")
135 addHttpRequestParameter("order", "asc")
137 verifySonesAreInOrder(3, 2, 0, 1)
138 verifyStoredFields("images", "asc", "")
143 fun `known sones can be sorted by nice name, ascending`() {
144 addHttpRequestParameter("sort", "name")
145 addHttpRequestParameter("order", "asc")
147 verifySonesAreInOrder(3, 1, 0, 2)
148 verifyStoredFields("name", "asc", "")
153 fun `known sones can be sorted by nice name, descending`() {
154 addHttpRequestParameter("sort", "name")
156 verifySonesAreInOrder(2, 0, 1, 3)
157 verifyStoredFields("name", "desc", "")
162 fun `known sones can be filtered by local sones`() {
163 addHttpRequestParameter("filter", "own")
165 verifySonesAreInOrder(2, 0)
166 verifyStoredFields("activity", "desc", "own")
171 fun `known sones can be filtered by non-local sones`() {
172 addHttpRequestParameter("filter", "not-own")
174 verifySonesAreInOrder(3, 1)
175 verifyStoredFields("activity", "desc", "not-own")
180 fun `known sones can be filtered by new sones`() {
181 addHttpRequestParameter("filter", "new")
183 verifySonesAreInOrder(1, 0)
184 verifyStoredFields("activity", "desc", "new")
189 fun `known sones can be filtered by known sones`() {
190 addHttpRequestParameter("filter", "not-new")
192 verifySonesAreInOrder(3, 2)
193 verifyStoredFields("activity", "desc", "not-new")
198 fun `known sones can be filtered by followed sones`() {
199 addHttpRequestParameter("filter", "followed")
200 listOf("sone1", "sone3").forEach { whenever(currentSone.hasFriend(it)).thenReturn(true) }
202 verifySonesAreInOrder(2, 1)
203 verifyStoredFields("activity", "desc", "followed")
208 fun `known sones can be filtered by not-followed sones`() {
209 addHttpRequestParameter("filter", "not-followed")
210 listOf("sone1", "sone3").forEach { whenever(currentSone.hasFriend(it)).thenReturn(true) }
212 verifySonesAreInOrder(3, 0)
213 verifyStoredFields("activity", "desc", "not-followed")
218 fun `known sones can not be filtered by followed sones if there is no current sone`() {
219 addHttpRequestParameter("filter", "followed")
222 verifySonesAreInOrder(3, 2, 1, 0)
223 verifyStoredFields("activity", "desc", "followed")
228 fun `known sones can not be filtered by not-followed sones if there is no current sone`() {
229 addHttpRequestParameter("filter", "not-followed")
232 verifySonesAreInOrder(3, 2, 1, 0)
233 verifyStoredFields("activity", "desc", "not-followed")
238 fun `pagination is set in template context`() {
240 @Suppress("UNCHECKED_CAST")
241 assertThat((templateContext["pagination"] as Pagination<Sone>).items, contains(*listOf(3, 2, 1, 0).map { sones[it] }.toTypedArray()))