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