Merge branch 'release-0.9.7'
[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 import java.util.TimeZone.getTimeZone
23
24 /**
25  * Unit test for [GetTimesAjaxPage].
26  */
27 class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = false, requiresLogin = false) {
28
29         private val timeTextConverter = mock<TimeTextConverter>()
30         private val l10nFilter = mock<L10nFilter>()
31         override val page: JsonPage by lazy { GetTimesAjaxPage(webInterface, timeTextConverter, l10nFilter, getTimeZone("UTC")) }
32         private val testPosts = listOf(createPost(1), createPost(2))
33         private val testReplies = listOf(createReply(1), createReply(2))
34
35         private fun createPost(index: Int): Post {
36                 return mock<Post>().apply {
37                         whenever(id).thenReturn("post$index")
38                         whenever(time).thenReturn(index.toLong() * 1000)
39                 }
40         }
41
42         private fun createReply(index: Int): PostReply {
43                 return mock<PostReply>().apply {
44                         whenever(id).thenReturn("reply$index")
45                         whenever(time).thenReturn(index.toLong() * 1000)
46                 }
47         }
48
49         @Before
50         fun setupMocks() {
51                 whenever(timeTextConverter.getTimeText(anyLong())).then { TimeText(L10nText(it.get<Long>(0).toString()), it.get<Long>(0) * 2) }
52                 whenever(l10nFilter.format(any(), any(), any())).then { it.get<L10nText>(1).text }
53         }
54
55         @Test
56         fun `request without any parameters responds with empty post and reply times`() {
57                 assertThatJsonIsSuccessful()
58                 assertThat(json["postTimes"]?.toList(), emptyIterable())
59                 assertThat(json["replyTimes"]?.toList(), emptyIterable())
60         }
61
62         @Test
63         fun `request with single post parameter responds with post times and empty reply times`() {
64                 addPost(testPosts[0])
65                 addRequestParameter("posts", "post1")
66                 assertThatJsonIsSuccessful()
67                 assertThat(json["postTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
68                                 "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01")
69                 ))
70                 assertThat(json["replyTimes"]?.toList(), emptyIterable())
71         }
72
73         @Test
74         fun `request with single reply parameter responds with reply times and empty post times`() {
75                 addReply(testReplies[0])
76                 addRequestParameter("replies", "reply1")
77                 assertThatJsonIsSuccessful()
78                 assertThat(json["postTimes"]?.toList(), emptyIterable())
79                 assertThat(json["replyTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
80                                 "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01")
81                 ))
82         }
83
84         @Test
85         fun `request with multiple post parameter responds with post times and empty reply times`() {
86                 addPost(testPosts[0])
87                 addPost(testPosts[1])
88                 addRequestParameter("posts", "post1,post2,post3")
89                 assertThatJsonIsSuccessful()
90                 assertThat(json["postTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
91                                 "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"),
92                                 "post2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02")
93                 ))
94                 assertThat(json["replyTimes"]?.toList(), emptyIterable())
95         }
96
97         @Test
98         fun `request with multiple reply parameters responds with reply times and empty post times`() {
99                 addReply(testReplies[0])
100                 addReply(testReplies[1])
101                 addRequestParameter("replies", "reply1,reply2,reply3")
102                 assertThatJsonIsSuccessful()
103                 assertThat(json["postTimes"]?.toList(), emptyIterable())
104                 assertThat(json["replyTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
105                                 "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"),
106                                 "reply2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02")
107                 ))
108         }
109
110 }