♻️ Move generic error pages to custom classes
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / EditProfileFieldPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
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
13 import org.junit.Test
14 import org.mockito.Mockito.never
15 import org.mockito.Mockito.verify
16
17 /**
18  * Unit test for [EditProfileFieldPage].
19  */
20 class EditProfileFieldPageTest: WebPageTest(::EditProfileFieldPage) {
21
22         private val profile = Profile(currentSone)
23         private val field = profile.addField("Name")
24
25         @Before
26         fun setupProfile() {
27                 whenever(currentSone.profile).thenReturn(profile)
28         }
29
30         @Test
31         fun `page returns correct path`() {
32             assertThat(page.path, equalTo("editProfileField.html"))
33         }
34
35         @Test
36         fun `page requires login`() {
37             assertThat(page.requiresLogin(), equalTo(true))
38         }
39
40         @Test
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"))
44         }
45
46         @Test
47         fun `get request with invalid field redirects to invalid page`() {
48                 verifyRedirect("invalid.html")
49         }
50
51         @Test
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))
56         }
57
58         @Test
59         fun `post request with cancel set redirects to profile edit page`() {
60                 setMethod(POST)
61                 addHttpRequestPart("field", field.id)
62                 addHttpRequestPart("cancel", "true")
63                 verifyRedirect("editProfile.html#profile-fields")
64         }
65
66         @Test
67         fun `post request with new name renames field and redirects to profile edit page`() {
68                 setMethod(POST)
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
74                 }
75         }
76
77         @Test
78         fun `post request with same name does not modify field and redirects to profile edit page`() {
79                 setMethod(POST)
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
85                 }
86         }
87
88         @Test
89         fun `post request with same name as different field sets error condition in template`() {
90                 setMethod(POST)
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))
98         }
99
100         @Test
101         fun `page can be created by dependency injection`() {
102                 assertThat(baseInjector.getInstance<EditProfileFieldPage>(), notNullValue())
103         }
104
105         @Test
106         fun `page is annotated with correct template path`() {
107             assertThat(page.templatePath, equalTo("/templates/editProfileField.html"))
108         }
109
110 }