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