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