0baaffebc7440ac892761efe92777f034b54800e
[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.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.hamcrest.Matchers.nullValue
12 import org.junit.Before
13 import org.junit.Test
14 import org.mockito.Mockito.any
15 import org.mockito.Mockito.never
16 import org.mockito.Mockito.verify
17
18 /**
19  * Unit test for [DeleteProfileFieldPage].
20  */
21 class DeleteProfileFieldPageTest: WebPageTest(::DeleteProfileFieldPage) {
22
23         private val profile = Profile(currentSone)
24         private val field = profile.addField("name")
25
26         @Before
27         fun setupProfile() {
28                 whenever(currentSone.profile).thenReturn(profile)
29                 field.value = "value"
30         }
31
32         @Test
33         fun `page returns correct path`() {
34                 assertThat(page.path, equalTo("deleteProfileField.html"))
35         }
36
37         @Test
38         fun `page requires login`() {
39                 assertThat(page.requiresLogin(), equalTo(true))
40         }
41
42         @Test
43         fun `get request with invalid field name redirects to invalid page`() {
44                 verifyRedirect("invalid.html")
45         }
46
47         @Test
48         fun `post request with invalid field name redirects to invalid page`() {
49                 setMethod(POST)
50                 addHttpRequestPart("field", "wrong-id")
51                 verifyRedirect("invalid.html")
52         }
53
54         @Test
55         fun `get request with valid field name sets field in template context`() {
56                 addHttpRequestParameter("field", field.id)
57                 page.processTemplate(freenetRequest, templateContext)
58                 assertThat(templateContext["field"], equalTo<Any>(field))
59         }
60
61         @Test
62         fun `post request without confirm redirects to edit profile page`() {
63                 setMethod(POST)
64                 addHttpRequestPart("field", field.id)
65                 verifyRedirect("editProfile.html#profile-fields") {
66                         verify(currentSone, never()).profile = any()
67                 }
68         }
69
70         @Test
71         fun `post request with confirm removes field and redirects to edit profile page`() {
72                 setMethod(POST)
73                 addHttpRequestPart("field", field.id)
74                 addHttpRequestPart("confirm", "true")
75                 verifyRedirect("editProfile.html#profile-fields") {
76                         assertThat(profile.getFieldById(field.id), nullValue())
77                         verify(currentSone).profile = profile
78                 }
79         }
80
81         @Test
82         fun `page can be created by dependency injection`() {
83                 assertThat(baseInjector.getInstance<DeleteProfileFieldPage>(), notNullValue())
84         }
85
86 }