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")
31 val avatar = mock<Image>()
32 whenever(avatar.id).thenReturn("image-id")
33 whenever(avatar.sone).thenReturn(currentSone)
34 profile.firstName = "First"
35 profile.middleName = "Middle"
36 profile.lastName = "Last"
38 profile.birthMonth = 12
39 profile.birthYear = 1999
40 profile.setAvatar(avatar)
41 whenever(currentSone.profile).thenReturn(profile)
45 fun `get request stores fields of current sone’s profile in template context`() {
47 page.handleRequest(freenetRequest, templateContext)
48 assertThat(templateContext["firstName"], equalTo<Any>("First"))
49 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
50 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
51 assertThat(templateContext["birthDay"], equalTo<Any>(31))
52 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
53 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
54 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
55 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
59 fun `post request without any command stores fields of current sone’s profile in template context`() {
61 page.handleRequest(freenetRequest, templateContext)
62 assertThat(templateContext["firstName"], equalTo<Any>("First"))
63 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
64 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
65 assertThat(templateContext["birthDay"], equalTo<Any>(31))
66 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
67 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
68 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
69 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
72 private fun <T> verifySingleFieldCanBeChanged(fieldName: String, newValue: T, expectedValue: T = newValue, fieldAccessor: () -> T) {
74 addHttpRequestParameter("save-profile", "true")
75 addHttpRequestParameter(fieldName, newValue.toString())
76 expectedException.expect(WebTestUtils.redirectsTo("editProfile.html"))
78 page.handleRequest(freenetRequest, templateContext)
80 verify(core).touchConfiguration()
81 assertThat(fieldAccessor(), equalTo(expectedValue))
86 fun `post request with new first name and save profile saves the profile and redirects back to profile edit page`() {
87 verifySingleFieldCanBeChanged("first-name", "New First") { profile.firstName }
91 fun `post request with new middle name and save profile saves the profile and redirects back to profile edit page`() {
92 verifySingleFieldCanBeChanged("middle-name", "New Middle") { profile.middleName }
96 fun `post request with new last name and save profile saves the profile and redirects back to profile edit page`() {
97 verifySingleFieldCanBeChanged("last-name", "New Last") { profile.lastName }
101 fun `post request with new birth day and save profile saves the profile and redirects back to profile edit page`() {
102 verifySingleFieldCanBeChanged("birth-day", 1) { profile.birthDay }
106 fun `post request with new birth month and save profile saves the profile and redirects back to profile edit page`() {
107 verifySingleFieldCanBeChanged("birth-month", 1) { profile.birthMonth }
111 fun `post request with new birth year and save profile saves the profile and redirects back to profile edit page`() {
112 verifySingleFieldCanBeChanged("birth-year", 1) { profile.birthYear }
116 fun `post request with new avatar ID and save profile saves the profile and redirects back to profile edit page`() {
117 val newAvatar = mock<Image>()
118 whenever(newAvatar.sone).thenReturn(currentSone)
119 whenever(newAvatar.id).thenReturn("avatar-id")
120 addImage("avatar-id", newAvatar)
121 verifySingleFieldCanBeChanged("avatarId", "avatar-id") { profile.avatar }
125 fun `post request with field value saves profile and redirects back to profile edit page`() {
126 val field = profile.addField("name")
128 verifySingleFieldCanBeChanged("field-${field.id}", "new") { profile.getFieldByName("name").value }
132 fun `post request with field value saves filtered value to profile and redirects back to profile edit page`() {
133 val field = profile.addField("name")
135 addHttpRequestHeader("Host", "www.te.st")
136 verifySingleFieldCanBeChanged("field-${field.id}", "http://www.te.st/KSK@GPL.txt", "KSK@GPL.txt") { profile.getFieldByName("name").value }
140 fun `adding a field with a duplicate name sets error in template context`() {
142 profile.addField("new-field")
143 addHttpRequestParameter("add-field", "true")
144 addHttpRequestParameter("field-name", "new-field")
145 page.handleRequest(freenetRequest, templateContext)
146 assertThat(templateContext["fieldName"], equalTo<Any>("new-field"))
147 assertThat(templateContext["duplicateFieldName"], equalTo<Any>(true))
148 verify(core, never()).touchConfiguration()
152 fun `adding a field with a new name sets adds field to profile and redirects to profile edit page`() {
154 addHttpRequestParameter("add-field", "true")
155 addHttpRequestParameter("field-name", "new-field")
156 expectedException.expect(WebTestUtils.redirectsTo("editProfile.html#profile-fields"))
158 page.handleRequest(freenetRequest, templateContext)
160 assertThat(profile.getFieldByName("new-field"), notNullValue())
161 verify(currentSone).profile = profile
162 verify(core).touchConfiguration()
167 fun `deleting a field redirects to delete field page`() {
169 addHttpRequestParameter("delete-field-${firstField.id}", "true")
170 expectedException.expect(WebTestUtils.redirectsTo("deleteProfileField.html?field=${firstField.id}"))
171 page.handleRequest(freenetRequest, templateContext)
175 fun `moving a field up moves the field up and redirects to the edit profile page`() {
177 addHttpRequestParameter("move-up-field-${secondField.id}", "true")
178 expectedException.expect(WebTestUtils.redirectsTo("editProfile.html#profile-fields"))
180 page.handleRequest(freenetRequest, templateContext)
182 assertThat(profile.fields, contains(secondField, firstField))
183 verify(currentSone).profile = profile
188 fun `moving an invalid field up redirects to the invalid page`() {
190 addHttpRequestParameter("move-up-field-foo", "true")
191 expectedException.expect(WebTestUtils.redirectsTo("invalid.html"))
192 page.handleRequest(freenetRequest, templateContext)
196 fun `moving a field down moves the field down and redirects to the edit profile page`() {
198 addHttpRequestParameter("move-down-field-${firstField.id}", "true")
199 expectedException.expect(WebTestUtils.redirectsTo("editProfile.html#profile-fields"))
201 page.handleRequest(freenetRequest, templateContext)
203 assertThat(profile.fields, contains(secondField, firstField))
204 verify(currentSone).profile = profile
209 fun `moving an invalid field down redirects to the invalid page`() {
211 addHttpRequestParameter("move-down-field-foo", "true")
212 expectedException.expect(WebTestUtils.redirectsTo("invalid.html"))
213 page.handleRequest(freenetRequest, templateContext)
217 fun `editing a field redirects to the edit profile page`() {
219 addHttpRequestParameter("edit-field-${firstField.id}", "true")
220 expectedException.expect(WebTestUtils.redirectsTo("editProfileField.html?field=${firstField.id}"))
221 page.handleRequest(freenetRequest, templateContext)