1 package net.pterodactylus.sone.web
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.GET
8 import net.pterodactylus.util.web.Method.POST
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.contains
11 import org.hamcrest.Matchers.equalTo
12 import org.hamcrest.Matchers.notNullValue
13 import org.junit.Before
15 import org.mockito.Mockito.never
16 import org.mockito.Mockito.verify
19 * Unit test for [EditProfilePage].
21 class EditProfilePageTest : WebPageTest() {
23 private val page = EditProfilePage(template, webInterface)
25 private val profile = Profile(currentSone)
26 private val firstField = profile.addField("First Field")
27 private val secondField = profile.addField("Second Field")
29 override fun getPage() = page
33 val avatar = mock<Image>()
34 whenever(avatar.id).thenReturn("image-id")
35 whenever(avatar.sone).thenReturn(currentSone)
36 profile.firstName = "First"
37 profile.middleName = "Middle"
38 profile.lastName = "Last"
40 profile.birthMonth = 12
41 profile.birthYear = 1999
42 profile.setAvatar(avatar)
43 whenever(currentSone.profile).thenReturn(profile)
47 fun `get request stores fields of current sone’s profile in template context`() {
49 page.handleRequest(freenetRequest, templateContext)
50 assertThat(templateContext["firstName"], equalTo<Any>("First"))
51 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
52 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
53 assertThat(templateContext["birthDay"], equalTo<Any>(31))
54 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
55 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
56 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
57 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
61 fun `post request without any command stores fields of current sone’s profile in template context`() {
63 page.handleRequest(freenetRequest, templateContext)
64 assertThat(templateContext["firstName"], equalTo<Any>("First"))
65 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
66 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
67 assertThat(templateContext["birthDay"], equalTo<Any>(31))
68 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
69 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
70 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
71 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
74 private fun <T> verifySingleFieldCanBeChanged(fieldName: String, newValue: T, expectedValue: T = newValue, fieldAccessor: () -> T) {
76 addHttpRequestParameter("save-profile", "true")
77 addHttpRequestParameter(fieldName, newValue.toString())
78 verifyRedirect("editProfile.html") {
79 verify(core).touchConfiguration()
80 assertThat(fieldAccessor(), equalTo(expectedValue))
85 fun `post request with new first name and save profile saves the profile and redirects back to profile edit page`() {
86 verifySingleFieldCanBeChanged("first-name", "New First") { profile.firstName }
90 fun `post request with new middle name and save profile saves the profile and redirects back to profile edit page`() {
91 verifySingleFieldCanBeChanged("middle-name", "New Middle") { profile.middleName }
95 fun `post request with new last name and save profile saves the profile and redirects back to profile edit page`() {
96 verifySingleFieldCanBeChanged("last-name", "New Last") { profile.lastName }
100 fun `post request with new birth day and save profile saves the profile and redirects back to profile edit page`() {
101 verifySingleFieldCanBeChanged("birth-day", 1) { profile.birthDay }
105 fun `post request with new birth month and save profile saves the profile and redirects back to profile edit page`() {
106 verifySingleFieldCanBeChanged("birth-month", 1) { profile.birthMonth }
110 fun `post request with new birth year and save profile saves the profile and redirects back to profile edit page`() {
111 verifySingleFieldCanBeChanged("birth-year", 1) { profile.birthYear }
115 fun `post request with new avatar ID and save profile saves the profile and redirects back to profile edit page`() {
116 val newAvatar = mock<Image>()
117 whenever(newAvatar.sone).thenReturn(currentSone)
118 whenever(newAvatar.id).thenReturn("avatar-id")
119 addImage("avatar-id", newAvatar)
120 verifySingleFieldCanBeChanged("avatarId", "avatar-id") { profile.avatar }
124 fun `post request with field value saves profile and redirects back to profile edit page`() {
125 val field = profile.addField("name")
127 verifySingleFieldCanBeChanged("field-${field.id}", "new") { profile.getFieldByName("name").value }
131 fun `post request with field value saves filtered value to profile and redirects back to profile edit page`() {
132 val field = profile.addField("name")
134 addHttpRequestHeader("Host", "www.te.st")
135 verifySingleFieldCanBeChanged("field-${field.id}", "http://www.te.st/KSK@GPL.txt", "KSK@GPL.txt") { profile.getFieldByName("name").value }
139 fun `adding a field with a duplicate name sets error in template context`() {
141 profile.addField("new-field")
142 addHttpRequestParameter("add-field", "true")
143 addHttpRequestParameter("field-name", "new-field")
144 page.handleRequest(freenetRequest, templateContext)
145 assertThat(templateContext["fieldName"], equalTo<Any>("new-field"))
146 assertThat(templateContext["duplicateFieldName"], equalTo<Any>(true))
147 verify(core, never()).touchConfiguration()
151 fun `adding a field with a new name sets adds field to profile and redirects to profile edit page`() {
153 addHttpRequestParameter("add-field", "true")
154 addHttpRequestParameter("field-name", "new-field")
155 verifyRedirect("editProfile.html#profile-fields") {
156 assertThat(profile.getFieldByName("new-field"), notNullValue())
157 verify(currentSone).profile = profile
158 verify(core).touchConfiguration()
163 fun `deleting a field redirects to delete field page`() {
165 addHttpRequestParameter("delete-field-${firstField.id}", "true")
166 verifyRedirect("deleteProfileField.html?field=${firstField.id}")
170 fun `moving a field up moves the field up and redirects to the edit profile page`() {
172 addHttpRequestParameter("move-up-field-${secondField.id}", "true")
173 verifyRedirect("editProfile.html#profile-fields") {
174 assertThat(profile.fields, contains(secondField, firstField))
175 verify(currentSone).profile = profile
180 fun `moving an invalid field up redirects to the invalid page`() {
182 addHttpRequestParameter("move-up-field-foo", "true")
183 verifyRedirect("invalid.html")
187 fun `moving a field down moves the field down and redirects to the edit profile page`() {
189 addHttpRequestParameter("move-down-field-${firstField.id}", "true")
190 verifyRedirect("editProfile.html#profile-fields") {
191 assertThat(profile.fields, contains(secondField, firstField))
192 verify(currentSone).profile = profile
197 fun `moving an invalid field down redirects to the invalid page`() {
199 addHttpRequestParameter("move-down-field-foo", "true")
200 verifyRedirect("invalid.html")
204 fun `editing a field redirects to the edit profile page`() {
206 addHttpRequestParameter("edit-field-${firstField.id}", "true")
207 verifyRedirect("editProfileField.html?field=${firstField.id}")