Add test for delete profile field ajax page
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / DeleteProfileFieldPageTest.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.hamcrest.Matchers.nullValue
9 import org.junit.Before
10 import org.junit.Test
11 import org.mockito.Mockito.any
12 import org.mockito.Mockito.never
13 import org.mockito.Mockito.verify
14
15 /**
16  * Unit test for [DeleteProfileFieldPage].
17  */
18 class DeleteProfileFieldPageTest: WebPageTest(::DeleteProfileFieldPage) {
19
20         private val profile = Profile(currentSone)
21         private val field = profile.addField("name")
22
23         @Before
24         fun setupProfile() {
25                 whenever(currentSone.profile).thenReturn(profile)
26                 field.value = "value"
27         }
28
29         @Test
30         fun `page returns correct path`() {
31                 assertThat(page.path, equalTo("deleteProfileField.html"))
32         }
33
34         @Test
35         fun `page requires login`() {
36                 assertThat(page.requiresLogin(), equalTo(true))
37         }
38
39         @Test
40         fun `get request with invalid field name redirects to invalid page`() {
41                 verifyRedirect("invalid.html")
42         }
43
44         @Test
45         fun `post request with invalid field name redirects to invalid page`() {
46                 setMethod(POST)
47                 addHttpRequestPart("field", "wrong-id")
48                 verifyRedirect("invalid.html")
49         }
50
51         @Test
52         fun `get request with valid field name sets 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 without confirm redirects to edit profile page`() {
60                 setMethod(POST)
61                 addHttpRequestPart("field", field.id)
62                 verifyRedirect("editProfile.html#profile-fields") {
63                         verify(currentSone, never()).profile = any()
64                 }
65         }
66
67         @Test
68         fun `post request with confirm removes field and redirects to edit profile page`() {
69                 setMethod(POST)
70                 addHttpRequestPart("field", field.id)
71                 addHttpRequestPart("confirm", "true")
72                 verifyRedirect("editProfile.html#profile-fields") {
73                         assertThat(profile.getFieldById(field.id), nullValue())
74                         verify(currentSone).profile = profile
75                 }
76         }
77
78 }