Let status page give out information about loaded elements
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / GetStatusAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import com.fasterxml.jackson.databind.JsonNode
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.data.Sone.SoneStatus.downloading
6 import net.pterodactylus.sone.data.Sone.SoneStatus.inserting
7 import net.pterodactylus.sone.freenet.L10nFilter
8 import net.pterodactylus.sone.freenet.L10nText
9 import net.pterodactylus.sone.test.deepMock
10 import net.pterodactylus.sone.test.mock
11 import net.pterodactylus.sone.test.whenever
12 import net.pterodactylus.sone.text.TimeText
13 import net.pterodactylus.sone.text.TimeTextConverter
14 import net.pterodactylus.sone.utils.jsonArray
15 import net.pterodactylus.util.notify.Notification
16 import org.hamcrest.MatcherAssert.assertThat
17 import org.hamcrest.Matchers.allOf
18 import org.hamcrest.Matchers.containsInAnyOrder
19 import org.hamcrest.Matchers.emptyIterable
20 import org.hamcrest.Matchers.equalTo
21 import org.hamcrest.Matchers.hasEntry
22 import org.junit.Before
23 import org.junit.Test
24 import org.mockito.ArgumentMatchers.any
25 import org.mockito.ArgumentMatchers.anyLong
26
27 /**
28  * Unit test for [GetStatusAjaxPage].
29  */
30 class GetStatusAjaxPageTest: JsonPageTest() {
31
32         private val timeTextConverter = mock<TimeTextConverter>()
33         private val l10nFilter = mock<L10nFilter>()
34         override var page: JsonPage = GetStatusAjaxPage(webInterface, elementLoader, timeTextConverter, l10nFilter)
35
36         @Before
37         fun setupTimeTextConverter() {
38                 whenever(timeTextConverter.getTimeText(anyLong())).thenAnswer { TimeText(L10nText(it.getArgument<Long>(0).toString()), it.getArgument(0)) }
39                 whenever(l10nFilter.format(any(), any(), any())).thenAnswer { it.getArgument<L10nText>(1).text }
40         }
41
42         @Test
43         fun `page returns correct path`() {
44                 assertThat(page.path, equalTo("getStatus.ajax"))
45         }
46
47         @Test
48         fun `page does not require form password`() {
49                 assertThat(page.needsFormPassword(), equalTo(false))
50         }
51
52         @Test
53         fun `page does not require login`() {
54                 assertThat(page.requiresLogin(), equalTo(false))
55         }
56
57         @Test
58         fun `page returns correct attribute “loggedIn” if sone is logged in`() {
59                 assertThat(json.get("loggedIn").asText(), equalTo("true"))
60         }
61
62         @Test
63         fun `page returns correct attribute “loggedIn” if sone is not logged in`() {
64                 unsetCurrentSone()
65                 assertThat(json.get("loggedIn").asText(), equalTo("false"))
66         }
67
68         @Test
69         fun `page returns options for sone if sone is logged in`() {
70                 assertThat(json.get("options").toMap(), allOf(
71                                 hasEntry("ShowNotification/NewSones", "false"),
72                                 hasEntry("ShowNotification/NewPosts", "false"),
73                                 hasEntry("ShowNotification/NewReplies", "false")
74                 ))
75         }
76
77         @Test
78         fun `page returns empty options if sone is not logged in`() {
79                 unsetCurrentSone()
80                 assertThat(json.get("options"), emptyIterable())
81         }
82
83         @Test
84         fun `page returns empty sones object if no sone is logged in and no sones parameter is given`() {
85                 unsetCurrentSone()
86                 assertThat(json.get("sones"), emptyIterable())
87         }
88
89         @Test
90         fun `page returns a sones object with the current sone if not other sones parameter is given`() {
91                 assertThat(json.get("sones").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
92                                 mapOf<String, String?>("id" to "soneId", "name" to "Sone_Id", "local" to "true", "status" to "idle", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 01:00:01", "lastUpdatedText" to "1000")
93                 ))
94         }
95
96         @Test
97         fun `page returns some sones objects with the current sone and some sones given as sones parameter`() {
98                 addSone(deepMock<Sone>().mock("sone1", "Sone 1", false, 2000, downloading))
99                 addSone(deepMock<Sone>().mock("sone3", "Sone 3", true, 3000, inserting))
100                 addRequestParameter("soneIds", "sone1,sone2,sone3")
101                 assertThat(json.get("sones").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
102                                 mapOf<String, String?>("id" to "soneId", "name" to "Sone_Id", "local" to "true", "status" to "idle", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 01:00:01", "lastUpdatedText" to "1000"),
103                                 mapOf("id" to "sone1", "name" to "Sone 1", "local" to "false", "status" to "downloading", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 01:00:02", "lastUpdatedText" to "2000"),
104                                 mapOf("id" to "sone3", "name" to "Sone 3", "local" to "true", "status" to "inserting", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 01:00:03", "lastUpdatedText" to "3000")
105                 ))
106         }
107
108         @Test
109         fun `page returns correct notifications hash`() {
110                 val notifications = listOf(
111                                 mock<Notification>().apply { whenever(this.createdTime).thenReturn(2000) },
112                                 mock<Notification>().apply { whenever(this.createdTime).thenReturn(1000) }
113                 )
114                 addNotification(*notifications.toTypedArray())
115                 assertThat(json.get("notificationHash").asInt(), equalTo(notifications.sortedBy { it.createdTime }.hashCode()))
116         }
117
118         @Test
119         fun `page returns new posts`() {
120                 addNewPost("post1", "sone1", 1000)
121                 addNewPost("post2", "sone2", 2000, "sone1")
122                 assertThat(json.get("newPosts").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
123                                 mapOf("id" to "post1", "sone" to "sone1", "time" to "1000", "recipient" to null),
124                                 mapOf("id" to "post2", "sone" to "sone2", "time" to "2000", "recipient" to "sone1")
125                 ))
126         }
127
128         @Test
129         fun `page returns new replies`() {
130                 addNewReply("reply1", "sone1", "post1", "sone11")
131                 addNewReply("reply2", "sone2", "post2", "sone22")
132                 assertThat(json.get("newReplies").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
133                                 mapOf<String, String?>("id" to "reply1", "sone" to "sone1", "post" to "post1", "postSone" to "sone11"),
134                                 mapOf("id" to "reply2", "sone" to "sone2", "post" to "post2", "postSone" to "sone22")
135                 ))
136         }
137
138         @Test
139         fun `page returns information about loaded elements`() {
140             addLoadedElement("KSK@test.png", loading = false, failed = false)
141                 addLoadedElement("KSK@test.html", loading = true, failed = false)
142                 addLoadedElement("KSK@test.jpeg", loading = false, failed = true)
143                 addRequestParameter("elements", jsonArray("KSK@test.png", "KSK@test.html", "KSK@test.jpeg").toString())
144                 assertThat(json.get("loadedElements").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
145                                 mapOf<String, String?>("link" to "KSK@test.png", "loading" to "false", "failed" to "false"),
146                                 mapOf("link" to "KSK@test.html", "loading" to "true", "failed" to "false"),
147                                 mapOf("link" to "KSK@test.jpeg", "loading" to "false", "failed" to "true")
148                 ))
149         }
150
151         private fun JsonNode.toMap() = fields().asSequence().map { it.key!! to if (it.value.isNull) null else it.value.asText()!! }.toMap()
152
153 }