From 53bc43f92329d259cc2d6f4c3a3131270578e85d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 24 Nov 2016 20:41:56 +0100 Subject: [PATCH] Add unit test for delete profile field page --- .../sone/web/DeleteProfileFieldPageTest.kt | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/DeleteProfileFieldPageTest.kt 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 index 0000000..a52ac48 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/DeleteProfileFieldPageTest.kt @@ -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(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 + } + } + +} -- 2.7.4