Add test for delete profile field ajax page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 1 Sep 2017 20:43:54 +0000 (22:43 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 1 Sep 2017 20:45:25 +0000 (22:45 +0200)
src/test/kotlin/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPageTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPageTest.kt
new file mode 100644 (file)
index 0000000..6ea28f3
--- /dev/null
@@ -0,0 +1,31 @@
+package net.pterodactylus.sone.web.ajax
+
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.nullValue
+import org.junit.Test
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [DeleteProfileFieldAjaxPage].
+ */
+class DeleteProfileFieldAjaxPageTest : JsonPageTest("deleteProfileField.ajax", pageSupplier = ::DeleteProfileFieldAjaxPage) {
+
+       @Test
+       fun `request without field id results in invalid field id error`() {
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("invalid-field-id"))
+       }
+
+       @Test
+       fun `request with valid field id results in field deletion`() {
+               profile.addField("foo")
+               val fieldId = profile.getFieldByName("foo")!!.id
+               addRequestParameter("field", fieldId)
+               assertThat(json.isSuccess, equalTo(true))
+               assertThat(profile.getFieldByName("foo"), nullValue())
+               verify(currentSone).profile = profile
+               verify(core).touchConfiguration()
+       }
+
+}
index 5d43a1e..afa9b17 100644 (file)
@@ -8,6 +8,7 @@ import net.pterodactylus.sone.core.ElementLoader
 import net.pterodactylus.sone.core.LinkedElement
 import net.pterodactylus.sone.data.Post
 import net.pterodactylus.sone.data.PostReply
+import net.pterodactylus.sone.data.Profile
 import net.pterodactylus.sone.data.Sone
 import net.pterodactylus.sone.data.Sone.SoneStatus
 import net.pterodactylus.sone.data.Sone.SoneStatus.idle
@@ -51,6 +52,7 @@ abstract class JsonPageTest(
        protected val freenetRequest = mock<FreenetRequest>()
        protected val httpRequest = mock<HTTPRequest>()
        protected val currentSone = deepMock<Sone>()
+       protected val profile = Profile(currentSone)
 
        private val requestHeaders = mutableMapOf<String, String>()
        private val requestParameters = mutableMapOf<String, String>()
@@ -116,6 +118,11 @@ abstract class JsonPageTest(
                whenever(httpRequest.isPartSet(anyString())).thenAnswer { it.getArgument(0) in requestParts }
        }
 
+       @Before
+       fun setupProfile() {
+               whenever(currentSone.profile).thenReturn(profile)
+       }
+
        protected val JsonReturnObject.error get() = if (this is JsonErrorReturnObject) this.error else null
 
        protected fun Sone.mock(id: String, name: String, local: Boolean = false, time: Long, status: SoneStatus = idle) = apply {