X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FEditProfilePageTest.kt;h=ac909ea1e3c1b33f62e5e5aa6f56e4de9ce2b9d1;hp=282dbc2466b1fc4d7324defe96e4eadc97794aef;hb=HEAD;hpb=cc0e665862e8cd6646ce439d5d1f1d44efc17d84 diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt index 282dbc2..ac909ea 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt @@ -1,69 +1,55 @@ package net.pterodactylus.sone.web.pages -import net.pterodactylus.sone.data.Image -import net.pterodactylus.sone.data.Profile -import net.pterodactylus.sone.test.mock -import net.pterodactylus.sone.test.whenever -import net.pterodactylus.sone.web.pages.EditProfilePage -import net.pterodactylus.sone.web.pages.WebPageTest -import net.pterodactylus.util.web.Method.GET -import net.pterodactylus.util.web.Method.POST -import org.hamcrest.MatcherAssert.assertThat -import org.hamcrest.Matchers.contains -import org.hamcrest.Matchers.equalTo -import org.hamcrest.Matchers.notNullValue -import org.junit.Before -import org.junit.Test -import org.mockito.Mockito.never -import org.mockito.Mockito.verify +import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.data.impl.* +import net.pterodactylus.sone.test.* +import net.pterodactylus.sone.web.* +import net.pterodactylus.sone.web.page.* +import net.pterodactylus.util.web.Method.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* +import org.mockito.Mockito.* /** * Unit test for [EditProfilePage]. */ -class EditProfilePageTest : WebPageTest() { - - private val page = EditProfilePage(template, webInterface) +class EditProfilePageTest : WebPageTest(::EditProfilePage) { private val profile = Profile(currentSone) private val firstField = profile.addField("First Field") private val secondField = profile.addField("Second Field") - override fun getPage() = page - @Before fun setupProfile() { - val avatar = mock() - whenever(avatar.id).thenReturn("image-id") - whenever(avatar.sone).thenReturn(currentSone) profile.firstName = "First" profile.middleName = "Middle" profile.lastName = "Last" profile.birthDay = 31 profile.birthMonth = 12 profile.birthYear = 1999 - profile.setAvatar(avatar) + profile.setAvatar(ImageImpl("image-id").modify().setSone(currentSone).update()) whenever(currentSone.profile).thenReturn(profile) } @Test fun `page returns correct path`() { - assertThat(page.path, equalTo("editProfile.html")) + assertThat(page.path, equalTo("editProfile.html")) } @Test fun `page requires login`() { - assertThat(page.requiresLogin(), equalTo(true)) + assertThat(page.requiresLogin(), equalTo(true)) } @Test fun `page returns correct title`() { - whenever(l10n.getString("Page.EditProfile.Title")).thenReturn("edit profile page title") - assertThat(page.getPageTitle(freenetRequest), equalTo("edit profile page title")) + addTranslation("Page.EditProfile.Title", "edit profile page title") + assertThat(page.getPageTitle(soneRequest), equalTo("edit profile page title")) } @Test fun `get request stores fields of current sone’s profile in template context`() { - request("", GET) page.processTemplate(freenetRequest, templateContext) assertThat(templateContext["firstName"], equalTo("First")) assertThat(templateContext["middleName"], equalTo("Middle")) @@ -77,7 +63,7 @@ class EditProfilePageTest : WebPageTest() { @Test fun `post request without any command stores fields of current sone’s profile in template context`() { - request("", POST) + setMethod(POST) page.processTemplate(freenetRequest, templateContext) assertThat(templateContext["firstName"], equalTo("First")) assertThat(templateContext["middleName"], equalTo("Middle")) @@ -90,10 +76,11 @@ class EditProfilePageTest : WebPageTest() { } private fun verifySingleFieldCanBeChanged(fieldName: String, newValue: T, expectedValue: T = newValue, fieldAccessor: () -> T) { - request("", POST) - addHttpRequestParameter("save-profile", "true") - addHttpRequestParameter(fieldName, newValue.toString()) + setMethod(POST) + addHttpRequestPart("save-profile", "true") + addHttpRequestPart(fieldName, newValue.toString()) verifyRedirect("editProfile.html") { + verify(currentSone).profile = profile verify(core).touchConfiguration() assertThat(fieldAccessor(), equalTo(expectedValue)) } @@ -131,9 +118,7 @@ class EditProfilePageTest : WebPageTest() { @Test fun `post request with new avatar ID and save profile saves the profile and redirects back to profile edit page`() { - val newAvatar = mock() - whenever(newAvatar.sone).thenReturn(currentSone) - whenever(newAvatar.id).thenReturn("avatar-id") + val newAvatar = ImageImpl("avatar-id").modify().setSone(currentSone).update() addImage("avatar-id", newAvatar) verifySingleFieldCanBeChanged("avatarId", "avatar-id") { profile.avatar } } @@ -155,10 +140,10 @@ class EditProfilePageTest : WebPageTest() { @Test fun `adding a field with a duplicate name sets error in template context`() { - request("", POST) + setMethod(POST) profile.addField("new-field") - addHttpRequestParameter("add-field", "true") - addHttpRequestParameter("field-name", "new-field") + addHttpRequestPart("add-field", "true") + addHttpRequestPart("field-name", "new-field") page.processTemplate(freenetRequest, templateContext) assertThat(templateContext["fieldName"], equalTo("new-field")) assertThat(templateContext["duplicateFieldName"], equalTo(true)) @@ -167,9 +152,9 @@ class EditProfilePageTest : WebPageTest() { @Test fun `adding a field with a new name sets adds field to profile and redirects to profile edit page`() { - request("", POST) - addHttpRequestParameter("add-field", "true") - addHttpRequestParameter("field-name", "new-field") + setMethod(POST) + addHttpRequestPart("add-field", "true") + addHttpRequestPart("field-name", "new-field") verifyRedirect("editProfile.html#profile-fields") { assertThat(profile.getFieldByName("new-field"), notNullValue()) verify(currentSone).profile = profile @@ -179,15 +164,15 @@ class EditProfilePageTest : WebPageTest() { @Test fun `deleting a field redirects to delete field page`() { - request("", POST) - addHttpRequestParameter("delete-field-${firstField.id}", "true") + setMethod(POST) + addHttpRequestPart("delete-field-${firstField.id}", "true") verifyRedirect("deleteProfileField.html?field=${firstField.id}") } @Test fun `moving a field up moves the field up and redirects to the edit profile page`() { - request("", POST) - addHttpRequestParameter("move-up-field-${secondField.id}", "true") + setMethod(POST) + addHttpRequestPart("move-up-field-${secondField.id}", "true") verifyRedirect("editProfile.html#profile-fields") { assertThat(profile.fields, contains(secondField, firstField)) verify(currentSone).profile = profile @@ -196,15 +181,15 @@ class EditProfilePageTest : WebPageTest() { @Test fun `moving an invalid field up does not redirect`() { - request("", POST) - addHttpRequestParameter("move-up-field-foo", "true") + setMethod(POST) + addHttpRequestPart("move-up-field-foo", "true") page.processTemplate(freenetRequest, templateContext) } @Test fun `moving a field down moves the field down and redirects to the edit profile page`() { - request("", POST) - addHttpRequestParameter("move-down-field-${firstField.id}", "true") + setMethod(POST) + addHttpRequestPart("move-down-field-${firstField.id}", "true") verifyRedirect("editProfile.html#profile-fields") { assertThat(profile.fields, contains(secondField, firstField)) verify(currentSone).profile = profile @@ -213,16 +198,31 @@ class EditProfilePageTest : WebPageTest() { @Test fun `moving an invalid field down does not redirect`() { - request("", POST) - addHttpRequestParameter("move-down-field-foo", "true") + setMethod(POST) + addHttpRequestPart("move-down-field-foo", "true") page.processTemplate(freenetRequest, templateContext) } @Test fun `editing a field redirects to the edit profile page`() { - request("", POST) - addHttpRequestParameter("edit-field-${firstField.id}", "true") + setMethod(POST) + addHttpRequestPart("edit-field-${firstField.id}", "true") verifyRedirect("editProfileField.html?field=${firstField.id}") } + @Test + fun `page can be created by dependency injection`() { + assertThat(baseInjector.getInstance(), notNullValue()) + } + + @Test + fun `page is annotated with correct menuname`() { + assertThat(page.menuName, equalTo("EditProfile")) + } + + @Test + fun `page is annotated with correct template path`() { + assertThat(page.templatePath, equalTo("/templates/editProfile.html")) + } + }