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