1 package net.pterodactylus.sone.web.ajax
3 import net.pterodactylus.sone.main.SonePlugin
4 import net.pterodactylus.sone.test.argumentCaptor
5 import net.pterodactylus.sone.test.get
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.util.notify.Notification
9 import net.pterodactylus.util.notify.TemplateNotification
10 import net.pterodactylus.util.template.TemplateContext
11 import net.pterodactylus.util.template.TemplateContextFactory
12 import net.pterodactylus.util.version.Version
13 import org.hamcrest.MatcherAssert.assertThat
14 import org.hamcrest.Matchers.containsInAnyOrder
15 import org.hamcrest.Matchers.empty
16 import org.hamcrest.Matchers.equalTo
18 import org.mockito.ArgumentMatchers.any
19 import org.mockito.Mockito.verify
23 * Unit test for [GetNotificationsAjaxPage].
25 class GetNotificationsAjaxPageTest : JsonPageTest("getNotifications.ajax", requiresLogin = false, needsFormPassword = false, pageSupplier = ::GetNotificationsAjaxPage) {
27 private val testNotifications = listOf(
28 createNotification("n1", 2000, "t1", 5000, true),
29 createNotification("n2", 1000, "t2", 6000, false),
30 createNotification("n3", 3000, "t3", 7000, true)
33 private fun createNotification(id: String, createdTime: Long, text: String, lastUpdatedTime: Long, dismissable: Boolean): Notification {
34 return mock<Notification>().apply {
35 whenever(this.id).thenReturn(id)
36 whenever(this.createdTime).thenReturn(createdTime)
37 whenever(this.lastUpdatedTime).thenReturn(lastUpdatedTime)
38 whenever(this.isDismissable).thenReturn(dismissable)
39 whenever(render(any())).then { it.get<Writer>(0).write(text) }
44 fun `notification hash is calculated correctly`() {
45 testNotifications.forEach { addNotification(it) }
46 assertThatJsonIsSuccessful()
47 assertThat(json["notificationHash"]?.asInt(), equalTo(listOf(1, 0, 2).map(testNotifications::get).hashCode()))
51 fun `options are included correctly`() {
52 assertThatJsonIsSuccessful()
53 assertThat(json["options"]!!["ShowNotification/NewSones"].asBoolean(), equalTo(true))
54 assertThat(json["options"]!!["ShowNotification/NewPosts"].asBoolean(), equalTo(true))
55 assertThat(json["options"]!!["ShowNotification/NewReplies"].asBoolean(), equalTo(true))
59 fun `options are included correctly when all false`() {
60 currentSone.options.isShowNewSoneNotifications = false
61 currentSone.options.isShowNewPostNotifications = false
62 currentSone.options.isShowNewReplyNotifications = false
63 assertThatJsonIsSuccessful()
64 assertThat(json["options"]!!["ShowNotification/NewSones"].asBoolean(), equalTo(false))
65 assertThat(json["options"]!!["ShowNotification/NewPosts"].asBoolean(), equalTo(false))
66 assertThat(json["options"]!!["ShowNotification/NewReplies"].asBoolean(), equalTo(false))
70 fun `options are not included if user is not logged in`() {
72 assertThatJsonIsSuccessful()
73 assertThat(json["options"]?.toList(), empty())
77 fun `notifications are rendered correctly`() {
78 testNotifications.forEach { addNotification(it) }
79 assertThatJsonIsSuccessful()
80 assertThat(json["notifications"]!!.toList().map { node -> listOf("id", "text", "createdTime", "lastUpdatedTime", "dismissable").map { it to node.get(it).asText() }.toMap() }, containsInAnyOrder(
81 mapOf("id" to "n1", "createdTime" to "2000", "lastUpdatedTime" to "5000", "dismissable" to "true", "text" to "t1"),
82 mapOf("id" to "n2", "createdTime" to "1000", "lastUpdatedTime" to "6000", "dismissable" to "false", "text" to "t2"),
83 mapOf("id" to "n3", "createdTime" to "3000", "lastUpdatedTime" to "7000", "dismissable" to "true", "text" to "t3")
88 fun `template notifications are rendered correctly`() {
89 whenever(webInterface.templateContextFactory).thenReturn(TemplateContextFactory())
90 whenever(updateChecker.hasLatestVersion()).thenReturn(true)
91 whenever(updateChecker.latestEdition).thenReturn(999)
92 whenever(updateChecker.latestVersion).thenReturn(Version(0, 1, 2))
93 whenever(updateChecker.latestVersionDate).thenReturn(998)
94 val templateNotification = mock<TemplateNotification>().apply {
95 whenever(id).thenReturn("n4")
96 whenever(createdTime).thenReturn(4000)
97 whenever(templateContext).thenReturn(TemplateContext())
98 whenever(render(any(), any())).then { it.get<Writer>(1).write("t4") }
100 testNotifications.forEach { addNotification(it) }
101 addNotification(templateNotification)
102 assertThatJsonIsSuccessful()
103 assertThat(json["notifications"]!!.last()["text"].asText(), equalTo("t4"))
104 val templateContext = argumentCaptor<TemplateContext>()
105 verify(templateNotification).render(templateContext.capture(), any())
106 assertThat(templateContext.value["core"], equalTo<Any>(core))
107 assertThat(templateContext.value["currentSone"], equalTo<Any>(currentSone))
108 assertThat(templateContext.value["localSones"], equalTo<Any>(core.localSones))
109 assertThat(templateContext.value["request"], equalTo<Any>(freenetRequest))
110 assertThat(templateContext.value["currentVersion"], equalTo<Any>(SonePlugin.getPluginVersion()))
111 assertThat(templateContext.value["hasLatestVersion"], equalTo<Any>(true))
112 assertThat(templateContext.value["latestEdition"], equalTo<Any>(999L))
113 assertThat(templateContext.value["latestVersion"], equalTo<Any>(Version(0, 1, 2)))
114 assertThat(templateContext.value["latestVersionTime"], equalTo<Any>(998L))
115 assertThat(templateContext.value["notification"], equalTo<Any>(templateNotification))