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