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.getInstance
7 import net.pterodactylus.sone.test.mock
8 import net.pterodactylus.sone.test.whenever
9 import net.pterodactylus.sone.web.baseInjector
10 import net.pterodactylus.util.notify.Notification
11 import net.pterodactylus.util.notify.TemplateNotification
12 import net.pterodactylus.util.template.TemplateContext
13 import net.pterodactylus.util.template.TemplateContextFactory
14 import net.pterodactylus.util.version.Version
15 import org.hamcrest.MatcherAssert.assertThat
16 import org.hamcrest.Matchers.containsInAnyOrder
17 import org.hamcrest.Matchers.empty
18 import org.hamcrest.Matchers.equalTo
19 import org.hamcrest.Matchers.notNullValue
21 import org.mockito.ArgumentMatchers.any
22 import org.mockito.Mockito.verify
26 * Unit test for [GetNotificationsAjaxPage].
28 class GetNotificationsAjaxPageTest : JsonPageTest("getNotifications.ajax", requiresLogin = false, needsFormPassword = false, pageSupplier = ::GetNotificationsAjaxPage) {
30 private val testNotifications = listOf(
31 createNotification("n1", 2000, "t1", 5000, true),
32 createNotification("n2", 1000, "t2", 6000, false),
33 createNotification("n3", 3000, "t3", 7000, true)
36 private fun createNotification(id: String, createdTime: Long, text: String, lastUpdatedTime: Long, dismissable: Boolean): Notification {
37 return mock<Notification>().apply {
38 whenever(this.id).thenReturn(id)
39 whenever(this.createdTime).thenReturn(createdTime)
40 whenever(this.lastUpdatedTime).thenReturn(lastUpdatedTime)
41 whenever(this.isDismissable).thenReturn(dismissable)
42 whenever(render(any())).then { it.get<Writer>(0).write(text) }
47 fun `notification hash is calculated correctly`() {
48 testNotifications.forEach { addNotification(it) }
49 assertThatJsonIsSuccessful()
50 assertThat(json["notificationHash"]?.asInt(), equalTo(listOf(1, 0, 2).map(testNotifications::get).hashCode()))
54 fun `options are included correctly`() {
55 assertThatJsonIsSuccessful()
56 assertThat(json["options"]!!["ShowNotification/NewSones"].asBoolean(), equalTo(true))
57 assertThat(json["options"]!!["ShowNotification/NewPosts"].asBoolean(), equalTo(true))
58 assertThat(json["options"]!!["ShowNotification/NewReplies"].asBoolean(), equalTo(true))
62 fun `options are included correctly when all false`() {
63 currentSone.options.isShowNewSoneNotifications = false
64 currentSone.options.isShowNewPostNotifications = false
65 currentSone.options.isShowNewReplyNotifications = false
66 assertThatJsonIsSuccessful()
67 assertThat(json["options"]!!["ShowNotification/NewSones"].asBoolean(), equalTo(false))
68 assertThat(json["options"]!!["ShowNotification/NewPosts"].asBoolean(), equalTo(false))
69 assertThat(json["options"]!!["ShowNotification/NewReplies"].asBoolean(), equalTo(false))
73 fun `options are not included if user is not logged in`() {
75 assertThatJsonIsSuccessful()
76 assertThat(json["options"]?.toList(), empty())
80 fun `notifications are rendered correctly`() {
81 testNotifications.forEach { addNotification(it) }
82 assertThatJsonIsSuccessful()
83 assertThat(json["notifications"]!!.toList().map { node -> listOf("id", "text", "createdTime", "lastUpdatedTime", "dismissable").map { it to node.get(it).asText() }.toMap() }, containsInAnyOrder(
84 mapOf("id" to "n1", "createdTime" to "2000", "lastUpdatedTime" to "5000", "dismissable" to "true", "text" to "t1"),
85 mapOf("id" to "n2", "createdTime" to "1000", "lastUpdatedTime" to "6000", "dismissable" to "false", "text" to "t2"),
86 mapOf("id" to "n3", "createdTime" to "3000", "lastUpdatedTime" to "7000", "dismissable" to "true", "text" to "t3")
91 fun `template notifications are rendered correctly`() {
92 whenever(webInterface.templateContextFactory).thenReturn(TemplateContextFactory())
93 whenever(updateChecker.hasLatestVersion()).thenReturn(true)
94 whenever(updateChecker.latestEdition).thenReturn(999)
95 whenever(updateChecker.latestVersion).thenReturn(Version(0, 1, 2))
96 whenever(updateChecker.latestVersionDate).thenReturn(998)
97 val templateNotification = mock<TemplateNotification>().apply {
98 whenever(id).thenReturn("n4")
99 whenever(createdTime).thenReturn(4000)
100 whenever(templateContext).thenReturn(TemplateContext())
101 whenever(render(any(), any())).then { it.get<Writer>(1).write("t4") }
103 testNotifications.forEach { addNotification(it) }
104 addNotification(templateNotification)
105 assertThatJsonIsSuccessful()
106 assertThat(json["notifications"]!!.last()["text"].asText(), equalTo("t4"))
107 val templateContext = argumentCaptor<TemplateContext>()
108 verify(templateNotification).render(templateContext.capture(), any())
109 assertThat(templateContext.value["core"], equalTo<Any>(core))
110 assertThat(templateContext.value["currentSone"], equalTo<Any>(currentSone))
111 assertThat(templateContext.value["localSones"], equalTo<Any>(core.localSones))
112 assertThat(templateContext.value["request"], equalTo<Any>(freenetRequest))
113 assertThat(templateContext.value["currentVersion"], equalTo<Any>(SonePlugin.getPluginVersion()))
114 assertThat(templateContext.value["hasLatestVersion"], equalTo<Any>(true))
115 assertThat(templateContext.value["latestEdition"], equalTo<Any>(999L))
116 assertThat(templateContext.value["latestVersion"], equalTo<Any>(Version(0, 1, 2)))
117 assertThat(templateContext.value["latestVersionTime"], equalTo<Any>(998L))
118 assertThat(templateContext.value["notification"], equalTo<Any>(templateNotification))
122 fun `page can be created by dependency injection`() {
123 assertThat(baseInjector.getInstance<GetNotificationsAjaxPage>(), notNullValue())