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