1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Profile
4 import net.pterodactylus.sone.test.getInstance
5 import net.pterodactylus.sone.test.whenever
6 import net.pterodactylus.sone.web.baseInjector
7 import net.pterodactylus.util.web.Method.POST
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.equalTo
10 import org.hamcrest.Matchers.notNullValue
11 import org.junit.Before
13 import org.mockito.Mockito.never
14 import org.mockito.Mockito.verify
17 * Unit test for [EditProfileFieldPage].
19 class EditProfileFieldPageTest: WebPageTest(::EditProfileFieldPage) {
21 private val profile = Profile(currentSone)
22 private val field = profile.addField("Name")
26 whenever(currentSone.profile).thenReturn(profile)
30 fun `page returns correct path`() {
31 assertThat(page.path, equalTo("editProfileField.html"))
35 fun `page requires login`() {
36 assertThat(page.requiresLogin(), equalTo(true))
40 fun `page returns correct title`() {
41 whenever(l10n.getString("Page.EditProfileField.Title")).thenReturn("edit profile field title")
42 assertThat(page.getPageTitle(freenetRequest), equalTo("edit profile field title"))
46 fun `get request with invalid field redirects to invalid page`() {
47 verifyRedirect("invalid.html")
51 fun `get request with valid field stores field in template context`() {
52 addHttpRequestParameter("field", field.id)
53 page.processTemplate(freenetRequest, templateContext)
54 assertThat(templateContext["field"], equalTo<Any>(field))
58 fun `post request with cancel set redirects to profile edit page`() {
60 addHttpRequestPart("field", field.id)
61 addHttpRequestPart("cancel", "true")
62 verifyRedirect("editProfile.html#profile-fields")
66 fun `post request with new name renames field and redirects to profile edit page`() {
68 addHttpRequestPart("field", field.id)
69 addHttpRequestPart("name", "New Name")
70 verifyRedirect("editProfile.html#profile-fields") {
71 assertThat(field.name, equalTo("New Name"))
72 verify(currentSone).profile = profile
77 fun `post request with same name does not modify field and redirects to profile edit page`() {
79 addHttpRequestPart("field", field.id)
80 addHttpRequestPart("name", "Name")
81 verifyRedirect("editProfile.html#profile-fields") {
82 assertThat(field.name, equalTo("Name"))
83 verify(currentSone, never()).profile = profile
88 fun `post request with same name as different field sets error condition in template`() {
90 profile.addField("New Name")
91 addHttpRequestPart("field", field.id)
92 addHttpRequestPart("name", "New Name")
93 page.processTemplate(freenetRequest, templateContext)
94 assertThat(field.name, equalTo("Name"))
95 verify(currentSone, never()).profile = profile
96 assertThat(templateContext["duplicateFieldName"], equalTo<Any>(true))
100 fun `page can be created by dependency injection`() {
101 assertThat(baseInjector.getInstance<EditProfileFieldPage>(), notNullValue())