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.sone.web.pages.EditProfilePage
8 import net.pterodactylus.sone.web.pages.WebPageTest
9 import net.pterodactylus.util.web.Method.POST
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.contains
12 import org.hamcrest.Matchers.equalTo
13 import org.hamcrest.Matchers.notNullValue
14 import org.junit.Before
16 import org.mockito.Mockito.never
17 import org.mockito.Mockito.verify
20 * Unit test for [EditProfilePage].
22 class EditProfilePageTest : WebPageTest() {
24 private val page = EditProfilePage(template, webInterface)
26 private val profile = Profile(currentSone)
27 private val firstField = profile.addField("First Field")
28 private val secondField = profile.addField("Second Field")
30 override fun getPage() = page
34 val avatar = mock<Image>()
35 whenever(avatar.id).thenReturn("image-id")
36 whenever(avatar.sone).thenReturn(currentSone)
37 profile.firstName = "First"
38 profile.middleName = "Middle"
39 profile.lastName = "Last"
41 profile.birthMonth = 12
42 profile.birthYear = 1999
43 profile.setAvatar(avatar)
44 whenever(currentSone.profile).thenReturn(profile)
48 fun `page returns correct path`() {
49 assertThat(page.path, equalTo("editProfile.html"))
53 fun `page requires login`() {
54 assertThat(page.requiresLogin(), equalTo(true))
58 fun `page returns correct title`() {
59 whenever(l10n.getString("Page.EditProfile.Title")).thenReturn("edit profile page title")
60 assertThat(page.getPageTitle(freenetRequest), equalTo("edit profile page title"))
64 fun `get request stores fields of current sone’s profile in template context`() {
65 page.processTemplate(freenetRequest, templateContext)
66 assertThat(templateContext["firstName"], equalTo<Any>("First"))
67 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
68 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
69 assertThat(templateContext["birthDay"], equalTo<Any>(31))
70 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
71 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
72 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
73 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
77 fun `post request without any command stores fields of current sone’s profile in template context`() {
79 page.processTemplate(freenetRequest, templateContext)
80 assertThat(templateContext["firstName"], equalTo<Any>("First"))
81 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
82 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
83 assertThat(templateContext["birthDay"], equalTo<Any>(31))
84 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
85 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
86 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
87 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
90 private fun <T> verifySingleFieldCanBeChanged(fieldName: String, newValue: T, expectedValue: T = newValue, fieldAccessor: () -> T) {
92 addHttpRequestPart("save-profile", "true")
93 addHttpRequestPart(fieldName, newValue.toString())
94 verifyRedirect("editProfile.html") {
95 verify(core).touchConfiguration()
96 assertThat(fieldAccessor(), equalTo(expectedValue))
101 fun `post request with new first name and save profile saves the profile and redirects back to profile edit page`() {
102 verifySingleFieldCanBeChanged("first-name", "New First") { profile.firstName }
106 fun `post request with new middle name and save profile saves the profile and redirects back to profile edit page`() {
107 verifySingleFieldCanBeChanged("middle-name", "New Middle") { profile.middleName }
111 fun `post request with new last name and save profile saves the profile and redirects back to profile edit page`() {
112 verifySingleFieldCanBeChanged("last-name", "New Last") { profile.lastName }
116 fun `post request with new birth day and save profile saves the profile and redirects back to profile edit page`() {
117 verifySingleFieldCanBeChanged("birth-day", 1) { profile.birthDay }
121 fun `post request with new birth month and save profile saves the profile and redirects back to profile edit page`() {
122 verifySingleFieldCanBeChanged("birth-month", 1) { profile.birthMonth }
126 fun `post request with new birth year and save profile saves the profile and redirects back to profile edit page`() {
127 verifySingleFieldCanBeChanged("birth-year", 1) { profile.birthYear }
131 fun `post request with new avatar ID and save profile saves the profile and redirects back to profile edit page`() {
132 val newAvatar = mock<Image>()
133 whenever(newAvatar.sone).thenReturn(currentSone)
134 whenever(newAvatar.id).thenReturn("avatar-id")
135 addImage("avatar-id", newAvatar)
136 verifySingleFieldCanBeChanged("avatarId", "avatar-id") { profile.avatar }
140 fun `post request with field value saves profile and redirects back to profile edit page`() {
141 val field = profile.addField("name")
143 verifySingleFieldCanBeChanged("field-${field.id}", "new") { profile.getFieldByName("name")!!.value }
147 fun `post request with field value saves filtered value to profile and redirects back to profile edit page`() {
148 val field = profile.addField("name")
150 addHttpRequestHeader("Host", "www.te.st")
151 verifySingleFieldCanBeChanged("field-${field.id}", "http://www.te.st/KSK@GPL.txt", "KSK@GPL.txt") { profile.getFieldByName("name")!!.value }
155 fun `adding a field with a duplicate name sets error in template context`() {
157 profile.addField("new-field")
158 addHttpRequestPart("add-field", "true")
159 addHttpRequestPart("field-name", "new-field")
160 page.processTemplate(freenetRequest, templateContext)
161 assertThat(templateContext["fieldName"], equalTo<Any>("new-field"))
162 assertThat(templateContext["duplicateFieldName"], equalTo<Any>(true))
163 verify(core, never()).touchConfiguration()
167 fun `adding a field with a new name sets adds field to profile and redirects to profile edit page`() {
169 addHttpRequestPart("add-field", "true")
170 addHttpRequestPart("field-name", "new-field")
171 verifyRedirect("editProfile.html#profile-fields") {
172 assertThat(profile.getFieldByName("new-field"), notNullValue())
173 verify(currentSone).profile = profile
174 verify(core).touchConfiguration()
179 fun `deleting a field redirects to delete field page`() {
181 addHttpRequestPart("delete-field-${firstField.id}", "true")
182 verifyRedirect("deleteProfileField.html?field=${firstField.id}")
186 fun `moving a field up moves the field up and redirects to the edit profile page`() {
188 addHttpRequestPart("move-up-field-${secondField.id}", "true")
189 verifyRedirect("editProfile.html#profile-fields") {
190 assertThat(profile.fields, contains(secondField, firstField))
191 verify(currentSone).profile = profile
196 fun `moving an invalid field up does not redirect`() {
198 addHttpRequestPart("move-up-field-foo", "true")
199 page.processTemplate(freenetRequest, templateContext)
203 fun `moving a field down moves the field down and redirects to the edit profile page`() {
205 addHttpRequestPart("move-down-field-${firstField.id}", "true")
206 verifyRedirect("editProfile.html#profile-fields") {
207 assertThat(profile.fields, contains(secondField, firstField))
208 verify(currentSone).profile = profile
213 fun `moving an invalid field down does not redirect`() {
215 addHttpRequestPart("move-down-field-foo", "true")
216 page.processTemplate(freenetRequest, templateContext)
220 fun `editing a field redirects to the edit profile page`() {
222 addHttpRequestPart("edit-field-${firstField.id}", "true")
223 verifyRedirect("editProfileField.html?field=${firstField.id}")