1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Image
4 import net.pterodactylus.sone.data.Profile
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import net.pterodactylus.util.web.Method.POST
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.contains
10 import org.hamcrest.Matchers.equalTo
11 import org.hamcrest.Matchers.notNullValue
12 import org.junit.Before
14 import org.mockito.Mockito.never
15 import org.mockito.Mockito.verify
18 * Unit test for [EditProfilePage].
20 class EditProfilePageTest: WebPageTest(::EditProfilePage) {
22 private val profile = Profile(currentSone)
23 private val firstField = profile.addField("First Field")
24 private val secondField = profile.addField("Second Field")
28 val avatar = mock<Image>()
29 whenever(avatar.id).thenReturn("image-id")
30 whenever(avatar.sone).thenReturn(currentSone)
31 profile.firstName = "First"
32 profile.middleName = "Middle"
33 profile.lastName = "Last"
35 profile.birthMonth = 12
36 profile.birthYear = 1999
37 profile.setAvatar(avatar)
38 whenever(currentSone.profile).thenReturn(profile)
42 fun `page returns correct path`() {
43 assertThat(page.path, equalTo("editProfile.html"))
47 fun `page requires login`() {
48 assertThat(page.requiresLogin(), equalTo(true))
52 fun `page returns correct title`() {
53 whenever(l10n.getString("Page.EditProfile.Title")).thenReturn("edit profile page title")
54 assertThat(page.getPageTitle(freenetRequest), equalTo("edit profile page title"))
58 fun `get request stores fields of current sone’s profile in template context`() {
59 page.processTemplate(freenetRequest, templateContext)
60 assertThat(templateContext["firstName"], equalTo<Any>("First"))
61 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
62 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
63 assertThat(templateContext["birthDay"], equalTo<Any>(31))
64 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
65 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
66 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
67 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
71 fun `post request without any command stores fields of current sone’s profile in template context`() {
73 page.processTemplate(freenetRequest, templateContext)
74 assertThat(templateContext["firstName"], equalTo<Any>("First"))
75 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
76 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
77 assertThat(templateContext["birthDay"], equalTo<Any>(31))
78 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
79 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
80 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
81 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
84 private fun <T> verifySingleFieldCanBeChanged(fieldName: String, newValue: T, expectedValue: T = newValue, fieldAccessor: () -> T) {
86 addHttpRequestPart("save-profile", "true")
87 addHttpRequestPart(fieldName, newValue.toString())
88 verifyRedirect("editProfile.html") {
89 verify(core).touchConfiguration()
90 assertThat(fieldAccessor(), equalTo(expectedValue))
95 fun `post request with new first name and save profile saves the profile and redirects back to profile edit page`() {
96 verifySingleFieldCanBeChanged("first-name", "New First") { profile.firstName }
100 fun `post request with new middle name and save profile saves the profile and redirects back to profile edit page`() {
101 verifySingleFieldCanBeChanged("middle-name", "New Middle") { profile.middleName }
105 fun `post request with new last name and save profile saves the profile and redirects back to profile edit page`() {
106 verifySingleFieldCanBeChanged("last-name", "New Last") { profile.lastName }
110 fun `post request with new birth day and save profile saves the profile and redirects back to profile edit page`() {
111 verifySingleFieldCanBeChanged("birth-day", 1) { profile.birthDay }
115 fun `post request with new birth month and save profile saves the profile and redirects back to profile edit page`() {
116 verifySingleFieldCanBeChanged("birth-month", 1) { profile.birthMonth }
120 fun `post request with new birth year and save profile saves the profile and redirects back to profile edit page`() {
121 verifySingleFieldCanBeChanged("birth-year", 1) { profile.birthYear }
125 fun `post request with new avatar ID and save profile saves the profile and redirects back to profile edit page`() {
126 val newAvatar = mock<Image>()
127 whenever(newAvatar.sone).thenReturn(currentSone)
128 whenever(newAvatar.id).thenReturn("avatar-id")
129 addImage("avatar-id", newAvatar)
130 verifySingleFieldCanBeChanged("avatarId", "avatar-id") { profile.avatar }
134 fun `post request with field value saves profile and redirects back to profile edit page`() {
135 val field = profile.addField("name")
137 verifySingleFieldCanBeChanged("field-${field.id}", "new") { profile.getFieldByName("name")!!.value }
141 fun `post request with field value saves filtered value to profile and redirects back to profile edit page`() {
142 val field = profile.addField("name")
144 addHttpRequestHeader("Host", "www.te.st")
145 verifySingleFieldCanBeChanged("field-${field.id}", "http://www.te.st/KSK@GPL.txt", "KSK@GPL.txt") { profile.getFieldByName("name")!!.value }
149 fun `adding a field with a duplicate name sets error in template context`() {
151 profile.addField("new-field")
152 addHttpRequestPart("add-field", "true")
153 addHttpRequestPart("field-name", "new-field")
154 page.processTemplate(freenetRequest, templateContext)
155 assertThat(templateContext["fieldName"], equalTo<Any>("new-field"))
156 assertThat(templateContext["duplicateFieldName"], equalTo<Any>(true))
157 verify(core, never()).touchConfiguration()
161 fun `adding a field with a new name sets adds field to profile and redirects to profile edit page`() {
163 addHttpRequestPart("add-field", "true")
164 addHttpRequestPart("field-name", "new-field")
165 verifyRedirect("editProfile.html#profile-fields") {
166 assertThat(profile.getFieldByName("new-field"), notNullValue())
167 verify(currentSone).profile = profile
168 verify(core).touchConfiguration()
173 fun `deleting a field redirects to delete field page`() {
175 addHttpRequestPart("delete-field-${firstField.id}", "true")
176 verifyRedirect("deleteProfileField.html?field=${firstField.id}")
180 fun `moving a field up moves the field up and redirects to the edit profile page`() {
182 addHttpRequestPart("move-up-field-${secondField.id}", "true")
183 verifyRedirect("editProfile.html#profile-fields") {
184 assertThat(profile.fields, contains(secondField, firstField))
185 verify(currentSone).profile = profile
190 fun `moving an invalid field up does not redirect`() {
192 addHttpRequestPart("move-up-field-foo", "true")
193 page.processTemplate(freenetRequest, templateContext)
197 fun `moving a field down moves the field down and redirects to the edit profile page`() {
199 addHttpRequestPart("move-down-field-${firstField.id}", "true")
200 verifyRedirect("editProfile.html#profile-fields") {
201 assertThat(profile.fields, contains(secondField, firstField))
202 verify(currentSone).profile = profile
207 fun `moving an invalid field down does not redirect`() {
209 addHttpRequestPart("move-down-field-foo", "true")
210 page.processTemplate(freenetRequest, templateContext)
214 fun `editing a field redirects to the edit profile page`() {
216 addHttpRequestPart("edit-field-${firstField.id}", "true")
217 verifyRedirect("editProfileField.html?field=${firstField.id}")