Move web pages to their own package
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / EditProfilePageTest.kt
1 package net.pterodactylus.sone.web.pages
2
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
16 import org.junit.Test
17 import org.mockito.Mockito.never
18 import org.mockito.Mockito.verify
19
20 /**
21  * Unit test for [EditProfilePage].
22  */
23 class EditProfilePageTest : WebPageTest() {
24
25         private val page = EditProfilePage(template, webInterface)
26
27         private val profile = Profile(currentSone)
28         private val firstField = profile.addField("First Field")
29         private val secondField = profile.addField("Second Field")
30
31         override fun getPage() = page
32
33         @Before
34         fun setupProfile() {
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"
41                 profile.birthDay = 31
42                 profile.birthMonth = 12
43                 profile.birthYear = 1999
44                 profile.setAvatar(avatar)
45                 whenever(currentSone.profile).thenReturn(profile)
46         }
47
48         @Test
49         fun `get request stores fields of current sone’s profile in template context`() {
50                 request("", GET)
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)))
60         }
61
62         @Test
63         fun `post request without any command stores fields of current sone’s profile in template context`() {
64                 request("", POST)
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)))
74         }
75
76         private fun <T> verifySingleFieldCanBeChanged(fieldName: String, newValue: T, expectedValue: T = newValue, fieldAccessor: () -> T) {
77                 request("", POST)
78                 addHttpRequestParameter("save-profile", "true")
79                 addHttpRequestParameter(fieldName, newValue.toString())
80                 verifyRedirect("editProfile.html") {
81                         verify(core).touchConfiguration()
82                         assertThat(fieldAccessor(), equalTo(expectedValue))
83                 }
84         }
85
86         @Test
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 }
89         }
90
91         @Test
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 }
94         }
95
96         @Test
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 }
99         }
100
101         @Test
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 }
104         }
105
106         @Test
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 }
109         }
110
111         @Test
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 }
114         }
115
116         @Test
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 }
123         }
124
125         @Test
126         fun `post request with field value saves profile and redirects back to profile edit page`() {
127                 val field = profile.addField("name")
128                 field.value = "old"
129                 verifySingleFieldCanBeChanged("field-${field.id}", "new") { profile.getFieldByName("name")!!.value }
130         }
131
132         @Test
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")
135                 field.value = "old"
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 }
138         }
139
140         @Test
141         fun `adding a field with a duplicate name sets error in template context`() {
142                 request("", POST)
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()
150         }
151
152         @Test
153         fun `adding a field with a new name sets adds field to profile and redirects to profile edit page`() {
154                 request("", POST)
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()
161                 }
162         }
163
164         @Test
165         fun `deleting a field redirects to delete field page`() {
166                 request("", POST)
167                 addHttpRequestParameter("delete-field-${firstField.id}", "true")
168                 verifyRedirect("deleteProfileField.html?field=${firstField.id}")
169         }
170
171         @Test
172         fun `moving a field up moves the field up and redirects to the edit profile page`() {
173                 request("", POST)
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
178                 }
179         }
180
181         @Test
182         fun `moving an invalid field up redirects to the invalid page`() {
183                 request("", POST)
184                 addHttpRequestParameter("move-up-field-foo", "true")
185                 verifyRedirect("invalid.html")
186         }
187
188         @Test
189         fun `moving a field down moves the field down and redirects to the edit profile page`() {
190                 request("", POST)
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
195                 }
196         }
197
198         @Test
199         fun `moving an invalid field down redirects to the invalid page`() {
200                 request("", POST)
201                 addHttpRequestParameter("move-down-field-foo", "true")
202                 verifyRedirect("invalid.html")
203         }
204
205         @Test
206         fun `editing a field redirects to the edit profile page`() {
207                 request("", POST)
208                 addHttpRequestParameter("edit-field-${firstField.id}", "true")
209                 verifyRedirect("editProfileField.html?field=${firstField.id}")
210         }
211
212 }