import net.pterodactylus.sone.main.SonePlugin
import net.pterodactylus.sone.test.argumentCaptor
import net.pterodactylus.sone.test.get
+import net.pterodactylus.sone.test.getInstance
import net.pterodactylus.sone.test.mock
import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.web.baseInjector
import net.pterodactylus.util.notify.Notification
import net.pterodactylus.util.notify.TemplateNotification
import net.pterodactylus.util.template.TemplateContext
import org.hamcrest.Matchers.containsInAnyOrder
import org.hamcrest.Matchers.empty
import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.notNullValue
import org.junit.Test
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito.verify
/**
* Unit test for [GetNotificationsAjaxPage].
*/
-class GetNotificationsAjaxPageTest : JsonPageTest("getNotifications.ajax", requiresLogin = false, needsFormPassword = false, pageSupplier = ::GetNotificationsAjaxPage) {
+class GetNotificationsAjaxPageTest : JsonPageTest("getNotifications.ajax", requiresLogin = false, needsFormPassword = false) {
+
+ override val page: JsonPage by lazy { GetNotificationsAjaxPage(webInterface, TemplateContextFactory()) }
private val testNotifications = listOf(
createNotification("n1", 2000, "t1", 5000, true),
@Test
fun `notification hash is calculated correctly`() {
testNotifications.forEach { addNotification(it) }
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["notificationHash"]?.asInt(), equalTo(listOf(1, 0, 2).map(testNotifications::get).hashCode()))
}
@Test
fun `options are included correctly`() {
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["options"]!!["ShowNotification/NewSones"].asBoolean(), equalTo(true))
assertThat(json["options"]!!["ShowNotification/NewPosts"].asBoolean(), equalTo(true))
assertThat(json["options"]!!["ShowNotification/NewReplies"].asBoolean(), equalTo(true))
currentSone.options.isShowNewSoneNotifications = false
currentSone.options.isShowNewPostNotifications = false
currentSone.options.isShowNewReplyNotifications = false
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["options"]!!["ShowNotification/NewSones"].asBoolean(), equalTo(false))
assertThat(json["options"]!!["ShowNotification/NewPosts"].asBoolean(), equalTo(false))
assertThat(json["options"]!!["ShowNotification/NewReplies"].asBoolean(), equalTo(false))
@Test
fun `options are not included if user is not logged in`() {
unsetCurrentSone()
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["options"]?.toList(), empty())
}
@Test
fun `notifications are rendered correctly`() {
testNotifications.forEach { addNotification(it) }
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["notifications"]!!.toList().map { node -> listOf("id", "text", "createdTime", "lastUpdatedTime", "dismissable").map { it to node.get(it).asText() }.toMap() }, containsInAnyOrder(
mapOf("id" to "n1", "createdTime" to "2000", "lastUpdatedTime" to "5000", "dismissable" to "true", "text" to "t1"),
mapOf("id" to "n2", "createdTime" to "1000", "lastUpdatedTime" to "6000", "dismissable" to "false", "text" to "t2"),
@Test
fun `template notifications are rendered correctly`() {
- whenever(webInterface.templateContextFactory).thenReturn(TemplateContextFactory())
whenever(updateChecker.hasLatestVersion()).thenReturn(true)
whenever(updateChecker.latestEdition).thenReturn(999)
whenever(updateChecker.latestVersion).thenReturn(Version(0, 1, 2))
}
testNotifications.forEach { addNotification(it) }
addNotification(templateNotification)
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["notifications"]!!.last()["text"].asText(), equalTo("t4"))
val templateContext = argumentCaptor<TemplateContext>()
verify(templateNotification).render(templateContext.capture(), any())
assertThat(templateContext.value["notification"], equalTo<Any>(templateNotification))
}
+ @Test
+ fun `page can be created by dependency injection`() {
+ assertThat(baseInjector.getInstance<GetNotificationsAjaxPage>(), notNullValue())
+ }
+
}