Merge branch 'release-0.9.7'
[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.util.web.Method.POST
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.contains
10 import org.hamcrest.Matchers.equalTo
11 import org.hamcrest.Matchers.notNullValue
12 import org.junit.Before
13 import org.junit.Test
14 import org.mockito.Mockito.never
15 import org.mockito.Mockito.verify
16
17 /**
18  * Unit test for [EditProfilePage].
19  */
20 class EditProfilePageTest: WebPageTest(::EditProfilePage) {
21
22         private val profile = Profile(currentSone)
23         private val firstField = profile.addField("First Field")
24         private val secondField = profile.addField("Second Field")
25
26         @Before
27         fun setupProfile() {
28                 val avatar = mock<Image>()
29                 whenever(avatar.id).thenReturn("image-id")
30                 whenever(avatar.sone).thenReturn(currentSone)
31                 profile.firstName = "First"
32                 profile.middleName = "Middle"
33                 profile.lastName = "Last"
34                 profile.birthDay = 31
35                 profile.birthMonth = 12
36                 profile.birthYear = 1999
37                 profile.setAvatar(avatar)
38                 whenever(currentSone.profile).thenReturn(profile)
39         }
40
41         @Test
42         fun `page returns correct path`() {
43             assertThat(page.path, equalTo("editProfile.html"))
44         }
45
46         @Test
47         fun `page requires login`() {
48             assertThat(page.requiresLogin(), equalTo(true))
49         }
50
51         @Test
52         fun `page returns correct title`() {
53             whenever(l10n.getString("Page.EditProfile.Title")).thenReturn("edit profile page title")
54                 assertThat(page.getPageTitle(freenetRequest), equalTo("edit profile page title"))
55         }
56
57         @Test
58         fun `get request stores fields of current sone’s profile in template context`() {
59                 page.processTemplate(freenetRequest, templateContext)
60                 assertThat(templateContext["firstName"], equalTo<Any>("First"))
61                 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
62                 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
63                 assertThat(templateContext["birthDay"], equalTo<Any>(31))
64                 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
65                 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
66                 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
67                 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
68         }
69
70         @Test
71         fun `post request without any command stores fields of current sone’s profile in template context`() {
72                 setMethod(POST)
73                 page.processTemplate(freenetRequest, templateContext)
74                 assertThat(templateContext["firstName"], equalTo<Any>("First"))
75                 assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
76                 assertThat(templateContext["lastName"], equalTo<Any>("Last"))
77                 assertThat(templateContext["birthDay"], equalTo<Any>(31))
78                 assertThat(templateContext["birthMonth"], equalTo<Any>(12))
79                 assertThat(templateContext["birthYear"], equalTo<Any>(1999))
80                 assertThat(templateContext["avatarId"], equalTo<Any>("image-id"))
81                 assertThat(templateContext["fields"], equalTo<Any>(listOf(firstField, secondField)))
82         }
83
84         private fun <T> verifySingleFieldCanBeChanged(fieldName: String, newValue: T, expectedValue: T = newValue, fieldAccessor: () -> T) {
85                 setMethod(POST)
86                 addHttpRequestPart("save-profile", "true")
87                 addHttpRequestPart(fieldName, newValue.toString())
88                 verifyRedirect("editProfile.html") {
89                         verify(core).touchConfiguration()
90                         assertThat(fieldAccessor(), equalTo(expectedValue))
91                 }
92         }
93
94         @Test
95         fun `post request with new first name and save profile saves the profile and redirects back to profile edit page`() {
96                 verifySingleFieldCanBeChanged("first-name", "New First") { profile.firstName }
97         }
98
99         @Test
100         fun `post request with new middle name and save profile saves the profile and redirects back to profile edit page`() {
101                 verifySingleFieldCanBeChanged("middle-name", "New Middle") { profile.middleName }
102         }
103
104         @Test
105         fun `post request with new last name and save profile saves the profile and redirects back to profile edit page`() {
106                 verifySingleFieldCanBeChanged("last-name", "New Last") { profile.lastName }
107         }
108
109         @Test
110         fun `post request with new birth day and save profile saves the profile and redirects back to profile edit page`() {
111                 verifySingleFieldCanBeChanged("birth-day", 1) { profile.birthDay }
112         }
113
114         @Test
115         fun `post request with new birth month and save profile saves the profile and redirects back to profile edit page`() {
116                 verifySingleFieldCanBeChanged("birth-month", 1) { profile.birthMonth }
117         }
118
119         @Test
120         fun `post request with new birth year and save profile saves the profile and redirects back to profile edit page`() {
121                 verifySingleFieldCanBeChanged("birth-year", 1) { profile.birthYear }
122         }
123
124         @Test
125         fun `post request with new avatar ID and save profile saves the profile and redirects back to profile edit page`() {
126                 val newAvatar = mock<Image>()
127                 whenever(newAvatar.sone).thenReturn(currentSone)
128                 whenever(newAvatar.id).thenReturn("avatar-id")
129                 addImage("avatar-id", newAvatar)
130                 verifySingleFieldCanBeChanged("avatarId", "avatar-id") { profile.avatar }
131         }
132
133         @Test
134         fun `post request with field value saves profile and redirects back to profile edit page`() {
135                 val field = profile.addField("name")
136                 field.value = "old"
137                 verifySingleFieldCanBeChanged("field-${field.id}", "new") { profile.getFieldByName("name")!!.value }
138         }
139
140         @Test
141         fun `post request with field value saves filtered value to profile and redirects back to profile edit page`() {
142                 val field = profile.addField("name")
143                 field.value = "old"
144                 addHttpRequestHeader("Host", "www.te.st")
145                 verifySingleFieldCanBeChanged("field-${field.id}", "http://www.te.st/KSK@GPL.txt", "KSK@GPL.txt") { profile.getFieldByName("name")!!.value }
146         }
147
148         @Test
149         fun `adding a field with a duplicate name sets error in template context`() {
150                 setMethod(POST)
151                 profile.addField("new-field")
152                 addHttpRequestPart("add-field", "true")
153                 addHttpRequestPart("field-name", "new-field")
154                 page.processTemplate(freenetRequest, templateContext)
155                 assertThat(templateContext["fieldName"], equalTo<Any>("new-field"))
156                 assertThat(templateContext["duplicateFieldName"], equalTo<Any>(true))
157                 verify(core, never()).touchConfiguration()
158         }
159
160         @Test
161         fun `adding a field with a new name sets adds field to profile and redirects to profile edit page`() {
162                 setMethod(POST)
163                 addHttpRequestPart("add-field", "true")
164                 addHttpRequestPart("field-name", "new-field")
165                 verifyRedirect("editProfile.html#profile-fields") {
166                         assertThat(profile.getFieldByName("new-field"), notNullValue())
167                         verify(currentSone).profile = profile
168                         verify(core).touchConfiguration()
169                 }
170         }
171
172         @Test
173         fun `deleting a field redirects to delete field page`() {
174                 setMethod(POST)
175                 addHttpRequestPart("delete-field-${firstField.id}", "true")
176                 verifyRedirect("deleteProfileField.html?field=${firstField.id}")
177         }
178
179         @Test
180         fun `moving a field up moves the field up and redirects to the edit profile page`() {
181                 setMethod(POST)
182                 addHttpRequestPart("move-up-field-${secondField.id}", "true")
183                 verifyRedirect("editProfile.html#profile-fields") {
184                         assertThat(profile.fields, contains(secondField, firstField))
185                         verify(currentSone).profile = profile
186                 }
187         }
188
189         @Test
190         fun `moving an invalid field up does not redirect`() {
191                 setMethod(POST)
192                 addHttpRequestPart("move-up-field-foo", "true")
193                 page.processTemplate(freenetRequest, templateContext)
194         }
195
196         @Test
197         fun `moving a field down moves the field down and redirects to the edit profile page`() {
198                 setMethod(POST)
199                 addHttpRequestPart("move-down-field-${firstField.id}", "true")
200                 verifyRedirect("editProfile.html#profile-fields") {
201                         assertThat(profile.fields, contains(secondField, firstField))
202                         verify(currentSone).profile = profile
203                 }
204         }
205
206         @Test
207         fun `moving an invalid field down does not redirect`() {
208                 setMethod(POST)
209                 addHttpRequestPart("move-down-field-foo", "true")
210                 page.processTemplate(freenetRequest, templateContext)
211         }
212
213         @Test
214         fun `editing a field redirects to the edit profile page`() {
215                 setMethod(POST)
216                 addHttpRequestPart("edit-field-${firstField.id}", "true")
217                 verifyRedirect("editProfileField.html?field=${firstField.id}")
218         }
219
220 }