1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.data.impl.*
5 import net.pterodactylus.sone.test.*
6 import net.pterodactylus.sone.web.*
7 import net.pterodactylus.sone.web.page.*
8 import net.pterodactylus.util.web.Method.*
9 import org.hamcrest.MatcherAssert.*
10 import org.hamcrest.Matchers.*
12 import org.mockito.Mockito.*
15 * Unit test for [EditProfilePage].
17 class EditProfilePageTest : WebPageTest(::EditProfilePage) {
19 private val profile = Profile(currentSone)
20 private val firstField = profile.addField("First Field")
21 private val secondField = profile.addField("Second Field")
25 profile.firstName = "First"
26 profile.middleName = "Middle"
27 profile.lastName = "Last"
29 profile.birthMonth = 12
30 profile.birthYear = 1999
31 profile.setAvatar(ImageImpl("image-id").modify().setSone(currentSone).update())
32 whenever(currentSone.profile).thenReturn(profile)
36 fun `page returns correct path`() {
37 assertThat(page.path, equalTo("editProfile.html"))
41 fun `page requires login`() {
42 assertThat(page.requiresLogin(), equalTo(true))
46 fun `page returns correct title`() {
47 addTranslation("Page.EditProfile.Title", "edit profile page title")
48 assertThat(page.getPageTitle(soneRequest), equalTo("edit profile page title"))
52 fun `get request stores fields of current sone’s profile in template context`() {
53 page.processTemplate(freenetRequest, templateContext)
54 assertThat(templateContext["firstName"], equalTo<Any>("First"))
55 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
56 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
57 assertThat(templateContext["birthDay"], equalTo<Any>(31))
58 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
59 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
60 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
61 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
65 fun `post request without any command stores fields of current sone’s profile in template context`() {
67 page.processTemplate(freenetRequest, templateContext)
68 assertThat(templateContext["firstName"], equalTo<Any>("First"))
69 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
70 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
71 assertThat(templateContext["birthDay"], equalTo<Any>(31))
72 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
73 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
74 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
75 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
78 private fun <T> verifySingleFieldCanBeChanged(fieldName: String, newValue: T, expectedValue: T = newValue, fieldAccessor: () -> T) {
80 addHttpRequestPart("save-profile", "true")
81 addHttpRequestPart(fieldName, newValue.toString())
82 verifyRedirect("editProfile.html") {
83 verify(currentSone).profile = profile
84 verify(core).touchConfiguration()
85 assertThat(fieldAccessor(), equalTo(expectedValue))
90 fun `post request with new first name and save profile saves the profile and redirects back to profile edit page`() {
91 verifySingleFieldCanBeChanged("first-name", "New First") { profile.firstName }
95 fun `post request with new middle name and save profile saves the profile and redirects back to profile edit page`() {
96 verifySingleFieldCanBeChanged("middle-name", "New Middle") { profile.middleName }
100 fun `post request with new last name and save profile saves the profile and redirects back to profile edit page`() {
101 verifySingleFieldCanBeChanged("last-name", "New Last") { profile.lastName }
105 fun `post request with new birth day and save profile saves the profile and redirects back to profile edit page`() {
106 verifySingleFieldCanBeChanged("birth-day", 1) { profile.birthDay }
110 fun `post request with new birth month and save profile saves the profile and redirects back to profile edit page`() {
111 verifySingleFieldCanBeChanged("birth-month", 1) { profile.birthMonth }
115 fun `post request with new birth year and save profile saves the profile and redirects back to profile edit page`() {
116 verifySingleFieldCanBeChanged("birth-year", 1) { profile.birthYear }
120 fun `post request with new avatar ID and save profile saves the profile and redirects back to profile edit page`() {
121 val newAvatar = ImageImpl("avatar-id").modify().setSone(currentSone).update()
122 addImage("avatar-id", newAvatar)
123 verifySingleFieldCanBeChanged("avatarId", "avatar-id") { profile.avatar }
127 fun `post request with field value saves profile and redirects back to profile edit page`() {
128 val field = profile.addField("name")
130 verifySingleFieldCanBeChanged("field-${field.id}", "new") { profile.getFieldByName("name")!!.value }
134 fun `post request with field value saves filtered value to profile and redirects back to profile edit page`() {
135 val field = profile.addField("name")
137 addHttpRequestHeader("Host", "www.te.st")
138 verifySingleFieldCanBeChanged("field-${field.id}", "http://www.te.st/KSK@GPL.txt", "KSK@GPL.txt") { profile.getFieldByName("name")!!.value }
142 fun `adding a field with a duplicate name sets error in template context`() {
144 profile.addField("new-field")
145 addHttpRequestPart("add-field", "true")
146 addHttpRequestPart("field-name", "new-field")
147 page.processTemplate(freenetRequest, templateContext)
148 assertThat(templateContext["fieldName"], equalTo<Any>("new-field"))
149 assertThat(templateContext["duplicateFieldName"], equalTo<Any>(true))
150 verify(core, never()).touchConfiguration()
154 fun `adding a field with a new name sets adds field to profile and redirects to profile edit page`() {
156 addHttpRequestPart("add-field", "true")
157 addHttpRequestPart("field-name", "new-field")
158 verifyRedirect("editProfile.html#profile-fields") {
159 assertThat(profile.getFieldByName("new-field"), notNullValue())
160 verify(currentSone).profile = profile
161 verify(core).touchConfiguration()
166 fun `deleting a field redirects to delete field page`() {
168 addHttpRequestPart("delete-field-${firstField.id}", "true")
169 verifyRedirect("deleteProfileField.html?field=${firstField.id}")
173 fun `moving a field up moves the field up and redirects to the edit profile page`() {
175 addHttpRequestPart("move-up-field-${secondField.id}", "true")
176 verifyRedirect("editProfile.html#profile-fields") {
177 assertThat(profile.fields, contains(secondField, firstField))
178 verify(currentSone).profile = profile
183 fun `moving an invalid field up does not redirect`() {
185 addHttpRequestPart("move-up-field-foo", "true")
186 page.processTemplate(freenetRequest, templateContext)
190 fun `moving a field down moves the field down and redirects to the edit profile page`() {
192 addHttpRequestPart("move-down-field-${firstField.id}", "true")
193 verifyRedirect("editProfile.html#profile-fields") {
194 assertThat(profile.fields, contains(secondField, firstField))
195 verify(currentSone).profile = profile
200 fun `moving an invalid field down does not redirect`() {
202 addHttpRequestPart("move-down-field-foo", "true")
203 page.processTemplate(freenetRequest, templateContext)
207 fun `editing a field redirects to the edit profile page`() {
209 addHttpRequestPart("edit-field-${firstField.id}", "true")
210 verifyRedirect("editProfileField.html?field=${firstField.id}")
214 fun `page can be created by dependency injection`() {
215 assertThat(baseInjector.getInstance<EditProfilePage>(), notNullValue())
219 fun `page is annotated with correct menuname`() {
220 assertThat(page.menuName, equalTo("EditProfile"))
224 fun `page is annotated with correct template path`() {
225 assertThat(page.templatePath, equalTo("/templates/editProfile.html"))