From 29fb2251e46b943f93c1910e7592b446180840af Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 26 Nov 2016 16:41:08 +0100 Subject: [PATCH] Add unit test for edit profile field page and fix logic in page --- .../sone/web/EditProfileFieldPage.java | 11 ++- .../sone/web/EditProfileFieldPageTest.kt | 94 ++++++++++++++++++++++ 2 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/EditProfileFieldPageTest.kt diff --git a/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java b/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java index e5bee78..bbc6d6b 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java @@ -75,12 +75,15 @@ public class EditProfileFieldPage extends SoneTemplatePage { } String name = request.getHttpRequest().getPartAsStringFailsafe("name", 256); Field existingField = profile.getFieldByName(name); - if ((existingField == null) || (existingField.equals(field))) { - field.setName(name); - currentSone.setProfile(profile); + if ((existingField != null) && (!field.equals(existingField))) { + templateContext.set("duplicateFieldName", true); + } else { + if (!name.equals(field.getName())) { + field.setName(name); + currentSone.setProfile(profile); + } throw new RedirectException("editProfile.html#profile-fields"); } - templateContext.set("duplicateFieldName", true); } /* store current values in template. */ diff --git a/src/test/kotlin/net/pterodactylus/sone/web/EditProfileFieldPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/EditProfileFieldPageTest.kt new file mode 100644 index 0000000..1b896da --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/EditProfileFieldPageTest.kt @@ -0,0 +1,94 @@ +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.junit.Before +import org.junit.Test +import org.mockito.Mockito.never +import org.mockito.Mockito.verify + +/** + * Unit test for [EditProfileFieldPage]. + */ +class EditProfileFieldPageTest : WebPageTest() { + + private val page = EditProfileFieldPage(template, webInterface) + + private val profile = Profile(currentSone) + private val field = profile.addField("Name") + + @Before + fun setupProfile() { + whenever(currentSone.profile).thenReturn(profile) + } + + @Test + fun `get request with invalid field redirects to invalid page`() { + request("", GET) + expectedException.expect(redirectsTo("invalid.html")) + page.handleRequest(freenetRequest, templateContext) + } + + @Test + fun `get request with valid field stores field in template context`() { + request("", GET) + addHttpRequestParameter("field", field.id) + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["field"], equalTo(field)) + } + + @Test + fun `post request with cancel set redirects to profile edit page`() { + request("", POST) + addHttpRequestParameter("field", field.id) + addHttpRequestParameter("cancel", "true") + expectedException.expect(redirectsTo("editProfile.html#profile-fields")) + page.handleRequest(freenetRequest, templateContext) + } + + @Test + fun `post request with new name renames field and redirects to profile edit page`() { + request("", POST) + addHttpRequestParameter("field", field.id) + addHttpRequestParameter("name", "New Name") + expectedException.expect(redirectsTo("editProfile.html#profile-fields")) + try { + page.handleRequest(freenetRequest, templateContext) + } finally { + assertThat(field.name, equalTo("New Name")) + verify(currentSone).profile = profile + } + } + + @Test + fun `post request with same name does not modify field and redirects to profile edit page`() { + request("", POST) + addHttpRequestParameter("field", field.id) + addHttpRequestParameter("name", "Name") + expectedException.expect(redirectsTo("editProfile.html#profile-fields")) + try { + page.handleRequest(freenetRequest, templateContext) + } finally { + assertThat(field.name, equalTo("Name")) + verify(currentSone, never()).profile = profile + } + } + + @Test + fun `post request with same name as different field sets error condition in template`() { + request("", POST) + profile.addField("New Name") + addHttpRequestParameter("field", field.id) + addHttpRequestParameter("name", "New Name") + page.handleRequest(freenetRequest, templateContext) + assertThat(field.name, equalTo("Name")) + verify(currentSone, never()).profile = profile + assertThat(templateContext["duplicateFieldName"], equalTo(true)) + } + +} -- 2.7.4