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.GET
10 import net.pterodactylus.util.web.Method.POST
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.contains
13 import org.hamcrest.Matchers.equalTo
14 import org.hamcrest.Matchers.notNullValue
15 import org.junit.Before
17 import org.mockito.Mockito.never
18 import org.mockito.Mockito.verify
21 * Unit test for [EditProfilePage].
23 class EditProfilePageTest : WebPageTest() {
25 private val page = EditProfilePage(template, webInterface)
27 private val profile = Profile(currentSone)
28 private val firstField = profile.addField("First Field")
29 private val secondField = profile.addField("Second Field")
31 override fun getPage() = page
35 val avatar = mock<Image>()
36 whenever(avatar.id).thenReturn("image-id")
37 whenever(avatar.sone).thenReturn(currentSone)
38 profile.firstName = "First"
39 profile.middleName = "Middle"
40 profile.lastName = "Last"
42 profile.birthMonth = 12
43 profile.birthYear = 1999
44 profile.setAvatar(avatar)
45 whenever(currentSone.profile).thenReturn(profile)
49 fun `get request stores fields of current sone’s profile in template context`() {
51 page.handleRequest(freenetRequest, templateContext)
52 assertThat(templateContext["firstName"], equalTo<Any>("First"))
53 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
54 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
55 assertThat(templateContext["birthDay"], equalTo<Any>(31))
56 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
57 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
58 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
59 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
63 fun `post request without any command stores fields of current sone’s profile in template context`() {
65 page.handleRequest(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)))
76 private fun <T> verifySingleFieldCanBeChanged(fieldName: String, newValue: T, expectedValue: T = newValue, fieldAccessor: () -> T) {
78 addHttpRequestParameter("save-profile", "true")
79 addHttpRequestParameter(fieldName, newValue.toString())
80 verifyRedirect("editProfile.html") {
81 verify(core).touchConfiguration()
82 assertThat(fieldAccessor(), equalTo(expectedValue))
87 fun `post request with new first name and save profile saves the profile and redirects back to profile edit page`() {
88 verifySingleFieldCanBeChanged("first-name", "New First") { profile.firstName }
92 fun `post request with new middle name and save profile saves the profile and redirects back to profile edit page`() {
93 verifySingleFieldCanBeChanged("middle-name", "New Middle") { profile.middleName }
97 fun `post request with new last name and save profile saves the profile and redirects back to profile edit page`() {
98 verifySingleFieldCanBeChanged("last-name", "New Last") { profile.lastName }
102 fun `post request with new birth day and save profile saves the profile and redirects back to profile edit page`() {
103 verifySingleFieldCanBeChanged("birth-day", 1) { profile.birthDay }
107 fun `post request with new birth month and save profile saves the profile and redirects back to profile edit page`() {
108 verifySingleFieldCanBeChanged("birth-month", 1) { profile.birthMonth }
112 fun `post request with new birth year and save profile saves the profile and redirects back to profile edit page`() {
113 verifySingleFieldCanBeChanged("birth-year", 1) { profile.birthYear }
117 fun `post request with new avatar ID and save profile saves the profile and redirects back to profile edit page`() {
118 val newAvatar = mock<Image>()
119 whenever(newAvatar.sone).thenReturn(currentSone)
120 whenever(newAvatar.id).thenReturn("avatar-id")
121 addImage("avatar-id", newAvatar)
122 verifySingleFieldCanBeChanged("avatarId", "avatar-id") { profile.avatar }
126 fun `post request with field value saves profile and redirects back to profile edit page`() {
127 val field = profile.addField("name")
129 verifySingleFieldCanBeChanged("field-${field.id}", "new") { profile.getFieldByName("name")!!.value }
133 fun `post request with field value saves filtered value to profile and redirects back to profile edit page`() {
134 val field = profile.addField("name")
136 addHttpRequestHeader("Host", "www.te.st")
137 verifySingleFieldCanBeChanged("field-${field.id}", "http://www.te.st/KSK@GPL.txt", "KSK@GPL.txt") { profile.getFieldByName("name")!!.value }
141 fun `adding a field with a duplicate name sets error in template context`() {
143 profile.addField("new-field")
144 addHttpRequestParameter("add-field", "true")
145 addHttpRequestParameter("field-name", "new-field")
146 page.handleRequest(freenetRequest, templateContext)
147 assertThat(templateContext["fieldName"], equalTo<Any>("new-field"))
148 assertThat(templateContext["duplicateFieldName"], equalTo<Any>(true))
149 verify(core, never()).touchConfiguration()
153 fun `adding a field with a new name sets adds field to profile and redirects to profile edit page`() {
155 addHttpRequestParameter("add-field", "true")
156 addHttpRequestParameter("field-name", "new-field")
157 verifyRedirect("editProfile.html#profile-fields") {
158 assertThat(profile.getFieldByName("new-field"), notNullValue())
159 verify(currentSone).profile = profile
160 verify(core).touchConfiguration()
165 fun `deleting a field redirects to delete field page`() {
167 addHttpRequestParameter("delete-field-${firstField.id}", "true")
168 verifyRedirect("deleteProfileField.html?field=${firstField.id}")
172 fun `moving a field up moves the field up and redirects to the edit profile page`() {
174 addHttpRequestParameter("move-up-field-${secondField.id}", "true")
175 verifyRedirect("editProfile.html#profile-fields") {
176 assertThat(profile.fields, contains(secondField, firstField))
177 verify(currentSone).profile = profile
182 fun `moving an invalid field up redirects to the invalid page`() {
184 addHttpRequestParameter("move-up-field-foo", "true")
185 verifyRedirect("invalid.html")
189 fun `moving a field down moves the field down and redirects to the edit profile page`() {
191 addHttpRequestParameter("move-down-field-${firstField.id}", "true")
192 verifyRedirect("editProfile.html#profile-fields") {
193 assertThat(profile.fields, contains(secondField, firstField))
194 verify(currentSone).profile = profile
199 fun `moving an invalid field down redirects to the invalid page`() {
201 addHttpRequestParameter("move-down-field-foo", "true")
202 verifyRedirect("invalid.html")
206 fun `editing a field redirects to the edit profile page`() {
208 addHttpRequestParameter("edit-field-${firstField.id}", "true")
209 verifyRedirect("editProfileField.html?field=${firstField.id}")