Add test for delete profile field ajax page
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / KnownSonesPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
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
18 import org.junit.Test
19
20 /**
21  * Unit test for [KnownSonesPage].
22  */
23 class KnownSonesPageTest: WebPageTest(::KnownSonesPage) {
24
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)
30         )
31
32         @Before
33         fun setupSones() {
34                 addSone("sone1", sones[0])
35                 addSone("sone2", sones[1])
36                 addSone("sone3", sones[2])
37                 addSone("sone4", sones[3])
38         }
39
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))
51                 }
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)
56         }
57
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()
62                 ))
63         }
64
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))
69         }
70
71         @Test
72         fun `page returns correct path`() {
73                 assertThat(page.path, equalTo("knownSones.html"))
74         }
75
76         @Test
77         fun `page does not require login`() {
78                 assertThat(page.requiresLogin(), equalTo(false))
79         }
80
81         @Test
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"))
85         }
86
87         @Test
88         fun `default known sones are sorted newest first`() {
89                 verifyNoRedirect {
90                         verifySonesAreInOrder(3, 2, 1, 0)
91                         verifyStoredFields("activity", "desc", "")
92                 }
93         }
94
95         @Test
96         fun `known sones can be sorted by oldest first`() {
97                 addHttpRequestParameter("order", "asc")
98                 verifyNoRedirect {
99                         verifySonesAreInOrder(0, 1, 2, 3)
100                         verifyStoredFields("activity", "asc", "")
101                 }
102         }
103
104         @Test
105         fun `known sones can be sorted by posts, most posts first`() {
106                 addHttpRequestParameter("sort", "posts")
107                 verifyNoRedirect {
108                         verifySonesAreInOrder(0, 2, 1, 3)
109                         verifyStoredFields("posts", "desc", "")
110                 }
111         }
112
113         @Test
114         fun `known sones can be sorted by posts, least posts first`() {
115                 addHttpRequestParameter("sort", "posts")
116                 addHttpRequestParameter("order", "asc")
117                 verifyNoRedirect {
118                         verifySonesAreInOrder(3, 1, 2, 0)
119                         verifyStoredFields("posts", "asc", "")
120                 }
121         }
122
123         @Test
124         fun `known sones can be sorted by images, most images first`() {
125                 addHttpRequestParameter("sort", "images")
126                 verifyNoRedirect {
127                         verifySonesAreInOrder(1, 0, 2, 3)
128                         verifyStoredFields("images", "desc", "")
129                 }
130         }
131
132         @Test
133         fun `known sones can be sorted by images, least images first`() {
134                 addHttpRequestParameter("sort", "images")
135                 addHttpRequestParameter("order", "asc")
136                 verifyNoRedirect {
137                         verifySonesAreInOrder(3, 2, 0, 1)
138                         verifyStoredFields("images", "asc", "")
139                 }
140         }
141
142         @Test
143         fun `known sones can be sorted by nice name, ascending`() {
144                 addHttpRequestParameter("sort", "name")
145                 addHttpRequestParameter("order", "asc")
146                 verifyNoRedirect {
147                         verifySonesAreInOrder(3, 1, 0, 2)
148                         verifyStoredFields("name", "asc", "")
149                 }
150         }
151
152         @Test
153         fun `known sones can be sorted by nice name, descending`() {
154                 addHttpRequestParameter("sort", "name")
155                 verifyNoRedirect {
156                         verifySonesAreInOrder(2, 0, 1, 3)
157                         verifyStoredFields("name", "desc", "")
158                 }
159         }
160
161         @Test
162         fun `known sones can be filtered by local sones`() {
163                 addHttpRequestParameter("filter", "own")
164                 verifyNoRedirect {
165                         verifySonesAreInOrder(2, 0)
166                         verifyStoredFields("activity", "desc", "own")
167                 }
168         }
169
170         @Test
171         fun `known sones can be filtered by non-local sones`() {
172                 addHttpRequestParameter("filter", "not-own")
173                 verifyNoRedirect {
174                         verifySonesAreInOrder(3, 1)
175                         verifyStoredFields("activity", "desc", "not-own")
176                 }
177         }
178
179         @Test
180         fun `known sones can be filtered by new sones`() {
181                 addHttpRequestParameter("filter", "new")
182                 verifyNoRedirect {
183                         verifySonesAreInOrder(1, 0)
184                         verifyStoredFields("activity", "desc", "new")
185                 }
186         }
187
188         @Test
189         fun `known sones can be filtered by known sones`() {
190                 addHttpRequestParameter("filter", "not-new")
191                 verifyNoRedirect {
192                         verifySonesAreInOrder(3, 2)
193                         verifyStoredFields("activity", "desc", "not-new")
194                 }
195         }
196
197         @Test
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) }
201                 verifyNoRedirect {
202                         verifySonesAreInOrder(2, 1)
203                         verifyStoredFields("activity", "desc", "followed")
204                 }
205         }
206
207         @Test
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) }
211                 verifyNoRedirect {
212                         verifySonesAreInOrder(3, 0)
213                         verifyStoredFields("activity", "desc", "not-followed")
214                 }
215         }
216
217         @Test
218         fun `known sones can not be filtered by followed sones if there is no current sone`() {
219                 addHttpRequestParameter("filter", "followed")
220                 unsetCurrentSone()
221                 verifyNoRedirect {
222                         verifySonesAreInOrder(3, 2, 1, 0)
223                         verifyStoredFields("activity", "desc", "followed")
224                 }
225         }
226
227         @Test
228         fun `known sones can not be filtered by not-followed sones if there is no current sone`() {
229                 addHttpRequestParameter("filter", "not-followed")
230                 unsetCurrentSone()
231                 verifyNoRedirect {
232                         verifySonesAreInOrder(3, 2, 1, 0)
233                         verifyStoredFields("activity", "desc", "not-followed")
234                 }
235         }
236
237         @Test
238         fun `pagination is set in template context`() {
239                 verifyNoRedirect {
240                         @Suppress("UNCHECKED_CAST")
241                         assertThat((templateContext["pagination"] as Pagination<Sone>).items, contains(*listOf(3, 2, 1, 0).map { sones[it] }.toTypedArray()))
242                 }
243         }
244
245 }