Add test for get times ajax page (test is timezone-dependent for now)
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / GetTimesAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import com.fasterxml.jackson.databind.JsonNode
4 import net.pterodactylus.sone.data.Post
5 import net.pterodactylus.sone.data.PostReply
6 import net.pterodactylus.sone.freenet.L10nFilter
7 import net.pterodactylus.sone.freenet.L10nText
8 import net.pterodactylus.sone.test.get
9 import net.pterodactylus.sone.test.mock
10 import net.pterodactylus.sone.test.whenever
11 import net.pterodactylus.sone.text.TimeText
12 import net.pterodactylus.sone.text.TimeTextConverter
13 import net.pterodactylus.sone.utils.jsonObject
14 import org.hamcrest.MatcherAssert.assertThat
15 import org.hamcrest.Matchers.containsInAnyOrder
16 import org.hamcrest.Matchers.emptyIterable
17 import org.hamcrest.Matchers.equalTo
18 import org.junit.Before
19 import org.junit.Test
20 import org.mockito.ArgumentMatchers.any
21 import org.mockito.ArgumentMatchers.anyLong
22
23 /**
24  * Unit test for [GetTimesAjaxPage].
25  */
26 class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = false, requiresLogin = false) {
27
28         private val timeTextConverter = mock<TimeTextConverter>()
29         private val l10nFilter = mock<L10nFilter>()
30         override val page: JsonPage by lazy { GetTimesAjaxPage(webInterface, timeTextConverter, l10nFilter) }
31         private val posts = listOf(createPost(1), createPost(2))
32         private val replies = listOf(createReply(1), createReply(2))
33
34         private fun createPost(index: Int): Post {
35                 return mock<Post>().apply {
36                         whenever(id).thenReturn("post$index")
37                         whenever(time).thenReturn(index.toLong() * 1000)
38                 }
39         }
40
41         private fun createReply(index: Int): PostReply {
42                 return mock<PostReply>().apply {
43                         whenever(id).thenReturn("reply$index")
44                         whenever(time).thenReturn(index.toLong() * 1000)
45                 }
46         }
47
48         @Before
49         fun setupMocks() {
50                 whenever(timeTextConverter.getTimeText(anyLong())).then { TimeText(L10nText(it.get<Long>(0).toString()), it.get<Long>(0) * 2) }
51                 whenever(l10nFilter.format(any(), any(), any())).then { it.get<L10nText>(1).text }
52         }
53
54         @Test
55         fun `request without any parameters responds with empty post and reply times`() {
56                 assertThat(json.isSuccess, equalTo(true))
57                 assertThat(json["postTimes"].toList(), emptyIterable())
58                 assertThat(json["replyTimes"].toList(), emptyIterable())
59         }
60
61         @Test
62         fun `request with single post parameter responds with post times and empty reply times`() {
63                 addPost(posts[0])
64                 addRequestParameter("posts", "post1")
65                 assertThat(json.isSuccess, equalTo(true))
66                 assertThat(json["postTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
67                                 "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01")
68                 ))
69                 assertThat(json["replyTimes"].toList(), emptyIterable())
70         }
71
72         @Test
73         fun `request with single reply parameter responds with reply times and empty post times`() {
74                 addReply(replies[0])
75                 addRequestParameter("replies", "reply1")
76                 assertThat(json.isSuccess, equalTo(true))
77                 assertThat(json["postTimes"].toList(), emptyIterable())
78                 assertThat(json["replyTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
79                                 "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01")
80                 ))
81         }
82
83         @Test
84         fun `request with multiple post parameter responds with post times and empty reply times`() {
85                 addPost(posts[0])
86                 addPost(posts[1])
87                 addRequestParameter("posts", "post1,post2,post3")
88                 assertThat(json.isSuccess, equalTo(true))
89                 assertThat(json["postTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
90                                 "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01"),
91                                 "post2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 01:00:02")
92                 ))
93                 assertThat(json["replyTimes"].toList(), emptyIterable())
94         }
95
96         @Test
97         fun `request with multiple reply parameters responds with reply times and empty post times`() {
98                 addReply(replies[0])
99                 addReply(replies[1])
100                 addRequestParameter("replies", "reply1,reply2,reply3")
101                 assertThat(json.isSuccess, equalTo(true))
102                 assertThat(json["postTimes"].toList(), emptyIterable())
103                 assertThat(json["replyTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
104                                 "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01"),
105                                 "reply2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 01:00:02")
106                 ))
107         }
108
109 }