1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Profile
4 import net.pterodactylus.sone.test.whenever
5 import net.pterodactylus.util.web.Method.POST
6 import org.hamcrest.MatcherAssert.assertThat
7 import org.hamcrest.Matchers.equalTo
8 import org.junit.Before
10 import org.mockito.Mockito.never
11 import org.mockito.Mockito.verify
14 * Unit test for [EditProfileFieldPage].
16 class EditProfileFieldPageTest: WebPageTest(::EditProfileFieldPage) {
18 private val profile = Profile(currentSone)
19 private val field = profile.addField("Name")
23 whenever(currentSone.profile).thenReturn(profile)
27 fun `page returns correct path`() {
28 assertThat(page.path, equalTo("editProfileField.html"))
32 fun `page requires login`() {
33 assertThat(page.requiresLogin(), equalTo(true))
37 fun `page returns correct title`() {
38 whenever(l10n.getString("Page.EditProfileField.Title")).thenReturn("edit profile field title")
39 assertThat(page.getPageTitle(freenetRequest), equalTo("edit profile field title"))
43 fun `get request with invalid field redirects to invalid page`() {
44 verifyRedirect("invalid.html")
48 fun `get request with valid field stores field in template context`() {
49 addHttpRequestParameter("field", field.id)
50 page.processTemplate(freenetRequest, templateContext)
51 assertThat(templateContext["field"], equalTo<Any>(field))
55 fun `post request with cancel set redirects to profile edit page`() {
57 addHttpRequestPart("field", field.id)
58 addHttpRequestPart("cancel", "true")
59 verifyRedirect("editProfile.html#profile-fields")
63 fun `post request with new name renames field and redirects to profile edit page`() {
65 addHttpRequestPart("field", field.id)
66 addHttpRequestPart("name", "New Name")
67 verifyRedirect("editProfile.html#profile-fields") {
68 assertThat(field.name, equalTo("New Name"))
69 verify(currentSone).profile = profile
74 fun `post request with same name does not modify field and redirects to profile edit page`() {
76 addHttpRequestPart("field", field.id)
77 addHttpRequestPart("name", "Name")
78 verifyRedirect("editProfile.html#profile-fields") {
79 assertThat(field.name, equalTo("Name"))
80 verify(currentSone, never()).profile = profile
85 fun `post request with same name as different field sets error condition in template`() {
87 profile.addField("New Name")
88 addHttpRequestPart("field", field.id)
89 addHttpRequestPart("name", "New Name")
90 page.processTemplate(freenetRequest, templateContext)
91 assertThat(field.name, equalTo("Name"))
92 verify(currentSone, never()).profile = profile
93 assertThat(templateContext["duplicateFieldName"], equalTo<Any>(true))