1 package net.pterodactylus.sone.web.pages
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.*
11 import org.mockito.Mockito.*
14 * Unit test for [EditProfilePage].
16 class EditProfilePageTest : WebPageTest(::EditProfilePage) {
18 private val profile = Profile(currentSone)
19 private val firstField = profile.addField("First Field")
20 private val secondField = profile.addField("Second Field")
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"
31 profile.birthMonth = 12
32 profile.birthYear = 1999
33 profile.setAvatar(avatar)
34 whenever(currentSone.profile).thenReturn(profile)
38 fun `page returns correct path`() {
39 assertThat(page.path, equalTo("editProfile.html"))
43 fun `page requires login`() {
44 assertThat(page.requiresLogin(), equalTo(true))
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"))
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)))
67 fun `post request without any command stores fields of current sone’s profile in template context`() {
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)))
80 private fun <T> verifySingleFieldCanBeChanged(fieldName: String, newValue: T, expectedValue: T = newValue, fieldAccessor: () -> T) {
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))
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 }
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 }
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 }
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 }
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 }
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 }
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 }
131 fun `post request with field value saves profile and redirects back to profile edit page`() {
132 val field = profile.addField("name")
134 verifySingleFieldCanBeChanged("field-${field.id}", "new") { profile.getFieldByName("name")!!.value }
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")
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 }
146 fun `adding a field with a duplicate name sets error in template context`() {
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()
158 fun `adding a field with a new name sets adds field to profile and redirects to profile edit page`() {
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()
170 fun `deleting a field redirects to delete field page`() {
172 addHttpRequestPart("delete-field-${firstField.id}", "true")
173 verifyRedirect("deleteProfileField.html?field=${firstField.id}")
177 fun `moving a field up moves the field up and redirects to the edit profile page`() {
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
187 fun `moving an invalid field up does not redirect`() {
189 addHttpRequestPart("move-up-field-foo", "true")
190 page.processTemplate(freenetRequest, templateContext)
194 fun `moving a field down moves the field down and redirects to the edit profile page`() {
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
204 fun `moving an invalid field down does not redirect`() {
206 addHttpRequestPart("move-down-field-foo", "true")
207 page.processTemplate(freenetRequest, templateContext)
211 fun `editing a field redirects to the edit profile page`() {
213 addHttpRequestPart("edit-field-${firstField.id}", "true")
214 verifyRedirect("editProfileField.html?field=${firstField.id}")
218 fun `page can be created by dependency injection`() {
219 assertThat(baseInjector.getInstance<EditProfilePage>(), notNullValue())
223 fun `page is annotated with correct menuname`() {
224 assertThat(page.menuName, equalTo("EditProfile"))
228 fun `page is annotated with correct template path`() {
229 assertThat(page.templatePath, equalTo("/templates/editProfile.html"))