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.sone.web.page.*
8 import net.pterodactylus.util.web.Method.POST
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.equalTo
11 import org.hamcrest.Matchers.notNullValue
12 import org.junit.Before
14 import org.mockito.Mockito.never
15 import org.mockito.Mockito.verify
18 * Unit test for [EditProfileFieldPage].
20 class EditProfileFieldPageTest: WebPageTest(::EditProfileFieldPage) {
22 private val profile = Profile(currentSone)
23 private val field = profile.addField("Name")
27 whenever(currentSone.profile).thenReturn(profile)
31 fun `page returns correct path`() {
32 assertThat(page.path, equalTo("editProfileField.html"))
36 fun `page requires login`() {
37 assertThat(page.requiresLogin(), equalTo(true))
41 fun `page returns correct title`() {
42 whenever(l10n.getString("Page.EditProfileField.Title")).thenReturn("edit profile field title")
43 assertThat(page.getPageTitle(soneRequest), equalTo("edit profile field title"))
47 fun `get request with invalid field redirects to invalid page`() {
48 verifyRedirect("invalid.html")
52 fun `get request with valid field stores field in template context`() {
53 addHttpRequestParameter("field", field.id)
54 page.processTemplate(freenetRequest, templateContext)
55 assertThat(templateContext["field"], equalTo<Any>(field))
59 fun `post request with cancel set redirects to profile edit page`() {
61 addHttpRequestPart("field", field.id)
62 addHttpRequestPart("cancel", "true")
63 verifyRedirect("editProfile.html#profile-fields")
67 fun `post request with new name renames field and redirects to profile edit page`() {
69 addHttpRequestPart("field", field.id)
70 addHttpRequestPart("name", "New Name")
71 verifyRedirect("editProfile.html#profile-fields") {
72 assertThat(field.name, equalTo("New Name"))
73 verify(currentSone).profile = profile
78 fun `post request with same name does not modify field and redirects to profile edit page`() {
80 addHttpRequestPart("field", field.id)
81 addHttpRequestPart("name", "Name")
82 verifyRedirect("editProfile.html#profile-fields") {
83 assertThat(field.name, equalTo("Name"))
84 verify(currentSone, never()).profile = profile
89 fun `post request with same name as different field sets error condition in template`() {
91 profile.addField("New Name")
92 addHttpRequestPart("field", field.id)
93 addHttpRequestPart("name", "New Name")
94 page.processTemplate(freenetRequest, templateContext)
95 assertThat(field.name, equalTo("Name"))
96 verify(currentSone, never()).profile = profile
97 assertThat(templateContext["duplicateFieldName"], equalTo<Any>(true))
101 fun `page can be created by dependency injection`() {
102 assertThat(baseInjector.getInstance<EditProfileFieldPage>(), notNullValue())
106 fun `page is annotated with correct template path`() {
107 assertThat(page.templatePath, equalTo("/templates/editProfileField.html"))