Fix tests by parsing the created JSON reply
[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.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
9 import org.junit.Test
10 import org.mockito.Mockito.never
11 import org.mockito.Mockito.verify
12
13 /**
14  * Unit test for [EditProfileFieldPage].
15  */
16 class EditProfileFieldPageTest: WebPageTest(::EditProfileFieldPage) {
17
18         private val profile = Profile(currentSone)
19         private val field = profile.addField("Name")
20
21         @Before
22         fun setupProfile() {
23                 whenever(currentSone.profile).thenReturn(profile)
24         }
25
26         @Test
27         fun `page returns correct path`() {
28             assertThat(page.path, equalTo("editProfileField.html"))
29         }
30
31         @Test
32         fun `page requires login`() {
33             assertThat(page.requiresLogin(), equalTo(true))
34         }
35
36         @Test
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"))
40         }
41
42         @Test
43         fun `get request with invalid field redirects to invalid page`() {
44                 verifyRedirect("invalid.html")
45         }
46
47         @Test
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))
52         }
53
54         @Test
55         fun `post request with cancel set redirects to profile edit page`() {
56                 setMethod(POST)
57                 addHttpRequestPart("field", field.id)
58                 addHttpRequestPart("cancel", "true")
59                 verifyRedirect("editProfile.html#profile-fields")
60         }
61
62         @Test
63         fun `post request with new name renames field and redirects to profile edit page`() {
64                 setMethod(POST)
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
70                 }
71         }
72
73         @Test
74         fun `post request with same name does not modify field and redirects to profile edit page`() {
75                 setMethod(POST)
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
81                 }
82         }
83
84         @Test
85         fun `post request with same name as different field sets error condition in template`() {
86                 setMethod(POST)
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))
94         }
95
96 }