b4e657044c7468cfa5bc22e117169ae47beeae55
[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(currentSone).profile = profile
90                         verify(core).touchConfiguration()
91                         assertThat(fieldAccessor(), equalTo(expectedValue))
92                 }
93         }
94
95         @Test
96         fun `post request with new first name and save profile saves the profile and redirects back to profile edit page`() {
97                 verifySingleFieldCanBeChanged("first-name", "New First") { profile.firstName }
98         }
99
100         @Test
101         fun `post request with new middle name and save profile saves the profile and redirects back to profile edit page`() {
102                 verifySingleFieldCanBeChanged("middle-name", "New Middle") { profile.middleName }
103         }
104
105         @Test
106         fun `post request with new last name and save profile saves the profile and redirects back to profile edit page`() {
107                 verifySingleFieldCanBeChanged("last-name", "New Last") { profile.lastName }
108         }
109
110         @Test
111         fun `post request with new birth day and save profile saves the profile and redirects back to profile edit page`() {
112                 verifySingleFieldCanBeChanged("birth-day", 1) { profile.birthDay }
113         }
114
115         @Test
116         fun `post request with new birth month and save profile saves the profile and redirects back to profile edit page`() {
117                 verifySingleFieldCanBeChanged("birth-month", 1) { profile.birthMonth }
118         }
119
120         @Test
121         fun `post request with new birth year and save profile saves the profile and redirects back to profile edit page`() {
122                 verifySingleFieldCanBeChanged("birth-year", 1) { profile.birthYear }
123         }
124
125         @Test
126         fun `post request with new avatar ID and save profile saves the profile and redirects back to profile edit page`() {
127                 val newAvatar = mock<Image>()
128                 whenever(newAvatar.sone).thenReturn(currentSone)
129                 whenever(newAvatar.id).thenReturn("avatar-id")
130                 addImage("avatar-id", newAvatar)
131                 verifySingleFieldCanBeChanged("avatarId", "avatar-id") { profile.avatar }
132         }
133
134         @Test
135         fun `post request with field value saves profile and redirects back to profile edit page`() {
136                 val field = profile.addField("name")
137                 field.value = "old"
138                 verifySingleFieldCanBeChanged("field-${field.id}", "new") { profile.getFieldByName("name")!!.value }
139         }
140
141         @Test
142         fun `post request with field value saves filtered value to profile and redirects back to profile edit page`() {
143                 val field = profile.addField("name")
144                 field.value = "old"
145                 addHttpRequestHeader("Host", "www.te.st")
146                 verifySingleFieldCanBeChanged("field-${field.id}", "http://www.te.st/KSK@GPL.txt", "KSK@GPL.txt") { profile.getFieldByName("name")!!.value }
147         }
148
149         @Test
150         fun `adding a field with a duplicate name sets error in template context`() {
151                 setMethod(POST)
152                 profile.addField("new-field")
153                 addHttpRequestPart("add-field", "true")
154                 addHttpRequestPart("field-name", "new-field")
155                 page.processTemplate(freenetRequest, templateContext)
156                 assertThat(templateContext["fieldName"], equalTo<Any>("new-field"))
157                 assertThat(templateContext["duplicateFieldName"], equalTo<Any>(true))
158                 verify(core, never()).touchConfiguration()
159         }
160
161         @Test
162         fun `adding a field with a new name sets adds field to profile and redirects to profile edit page`() {
163                 setMethod(POST)
164                 addHttpRequestPart("add-field", "true")
165                 addHttpRequestPart("field-name", "new-field")
166                 verifyRedirect("editProfile.html#profile-fields") {
167                         assertThat(profile.getFieldByName("new-field"), notNullValue())
168                         verify(currentSone).profile = profile
169                         verify(core).touchConfiguration()
170                 }
171         }
172
173         @Test
174         fun `deleting a field redirects to delete field page`() {
175                 setMethod(POST)
176                 addHttpRequestPart("delete-field-${firstField.id}", "true")
177                 verifyRedirect("deleteProfileField.html?field=${firstField.id}")
178         }
179
180         @Test
181         fun `moving a field up moves the field up and redirects to the edit profile page`() {
182                 setMethod(POST)
183                 addHttpRequestPart("move-up-field-${secondField.id}", "true")
184                 verifyRedirect("editProfile.html#profile-fields") {
185                         assertThat(profile.fields, contains(secondField, firstField))
186                         verify(currentSone).profile = profile
187                 }
188         }
189
190         @Test
191         fun `moving an invalid field up does not redirect`() {
192                 setMethod(POST)
193                 addHttpRequestPart("move-up-field-foo", "true")
194                 page.processTemplate(freenetRequest, templateContext)
195         }
196
197         @Test
198         fun `moving a field down moves the field down and redirects to the edit profile page`() {
199                 setMethod(POST)
200                 addHttpRequestPart("move-down-field-${firstField.id}", "true")
201                 verifyRedirect("editProfile.html#profile-fields") {
202                         assertThat(profile.fields, contains(secondField, firstField))
203                         verify(currentSone).profile = profile
204                 }
205         }
206
207         @Test
208         fun `moving an invalid field down does not redirect`() {
209                 setMethod(POST)
210                 addHttpRequestPart("move-down-field-foo", "true")
211                 page.processTemplate(freenetRequest, templateContext)
212         }
213
214         @Test
215         fun `editing a field redirects to the edit profile page`() {
216                 setMethod(POST)
217                 addHttpRequestPart("edit-field-${firstField.id}", "true")
218                 verifyRedirect("editProfileField.html?field=${firstField.id}")
219         }
220
221 }