Replace Preferences with Kotlin version
[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.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
12 import org.junit.Test
13 import org.mockito.Mockito.never
14 import org.mockito.Mockito.verify
15
16 /**
17  * Unit test for [EditProfileFieldPage].
18  */
19 class EditProfileFieldPageTest: WebPageTest(::EditProfileFieldPage) {
20
21         private val profile = Profile(currentSone)
22         private val field = profile.addField("Name")
23
24         @Before
25         fun setupProfile() {
26                 whenever(currentSone.profile).thenReturn(profile)
27         }
28
29         @Test
30         fun `page returns correct path`() {
31             assertThat(page.path, equalTo("editProfileField.html"))
32         }
33
34         @Test
35         fun `page requires login`() {
36             assertThat(page.requiresLogin(), equalTo(true))
37         }
38
39         @Test
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"))
43         }
44
45         @Test
46         fun `get request with invalid field redirects to invalid page`() {
47                 verifyRedirect("invalid.html")
48         }
49
50         @Test
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))
55         }
56
57         @Test
58         fun `post request with cancel set redirects to profile edit page`() {
59                 setMethod(POST)
60                 addHttpRequestPart("field", field.id)
61                 addHttpRequestPart("cancel", "true")
62                 verifyRedirect("editProfile.html#profile-fields")
63         }
64
65         @Test
66         fun `post request with new name renames field and redirects to profile edit page`() {
67                 setMethod(POST)
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
73                 }
74         }
75
76         @Test
77         fun `post request with same name does not modify field and redirects to profile edit page`() {
78                 setMethod(POST)
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
84                 }
85         }
86
87         @Test
88         fun `post request with same name as different field sets error condition in template`() {
89                 setMethod(POST)
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))
97         }
98
99         @Test
100         fun `page can be created by dependency injection`() {
101                 assertThat(baseInjector.getInstance<EditProfileFieldPage>(), notNullValue())
102         }
103
104 }