Add unit test for delete profile field page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Nov 2016 19:41:56 +0000 (20:41 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Nov 2016 19:41:56 +0000 (20:41 +0100)
src/test/kotlin/net/pterodactylus/sone/web/DeleteProfileFieldPageTest.kt [new file with mode: 0644]

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/DeleteProfileFieldPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/DeleteProfileFieldPageTest.kt
new file mode 100644 (file)
index 0000000..a52ac48
--- /dev/null
@@ -0,0 +1,79 @@
+package net.pterodactylus.sone.web
+
+import net.pterodactylus.sone.data.Profile
+import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.web.WebTestUtils.redirectsTo
+import net.pterodactylus.util.web.Method.GET
+import net.pterodactylus.util.web.Method.POST
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.nullValue
+import org.junit.Before
+import org.junit.Test
+import org.mockito.Mockito.verify
+import kotlin.test.fail
+
+/**
+ * Unit test for [DeleteProfileFieldPage].
+ */
+class DeleteProfileFieldPageTest : WebPageTest() {
+
+       private val page = DeleteProfileFieldPage(template, webInterface)
+
+       private val profile = Profile(currentSone)
+       private val field = profile.addField("name")
+
+       @Before
+       fun setupProfile() {
+               whenever(currentSone.profile).thenReturn(profile)
+               field.value = "value"
+       }
+
+       @Test
+       fun `get request with invalid field name redirects to invalid page`() {
+               request("", GET)
+               expectedException.expect(redirectsTo("invalid.html"))
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `post request with invalid field name redirects to invalid page`() {
+               request("", POST)
+               addHttpRequestParameter("field", "wrong-id")
+               expectedException.expect(redirectsTo("invalid.html"))
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `get request with valid field name sets field in template context`() {
+               request("", GET)
+               addHttpRequestParameter("field", field.id)
+               page.handleRequest(freenetRequest, templateContext)
+               assertThat(templateContext["field"], equalTo<Any>(field))
+       }
+
+       @Test
+       fun `post request without confirm redirects to edit profile page`() {
+               request("", POST)
+               addHttpRequestParameter("field", field.id)
+               expectedException.expect(redirectsTo("editProfile.html#profile-fields"))
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `post request with confirm removes field and redirects to edit profile page`() {
+               request("", POST)
+               addHttpRequestParameter("field", field.id)
+               addHttpRequestParameter("confirm", "true")
+               expectedException.expect(redirectsTo("editProfile.html#profile-fields"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+                       fail()
+               } catch (e: Exception) {
+                       assertThat(profile.getFieldById(field.id), nullValue())
+                       verify(currentSone).profile = profile
+                       throw e
+               }
+       }
+
+}