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