1 package net.pterodactylus.sone.web.pages
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
15 import org.mockito.Mockito.any
16 import org.mockito.Mockito.never
17 import org.mockito.Mockito.verify
20 * Unit test for [DeleteProfileFieldPage].
22 class DeleteProfileFieldPageTest: WebPageTest(::DeleteProfileFieldPage) {
24 private val profile = Profile(currentSone)
25 private val field = profile.addField("name")
29 whenever(currentSone.profile).thenReturn(profile)
34 fun `page returns correct path`() {
35 assertThat(page.path, equalTo("deleteProfileField.html"))
39 fun `page requires login`() {
40 assertThat(page.requiresLogin(), equalTo(true))
44 fun `get request with invalid field name redirects to invalid page`() {
45 verifyRedirect("invalid.html")
49 fun `post request with invalid field name redirects to invalid page`() {
51 addHttpRequestPart("field", "wrong-id")
52 verifyRedirect("invalid.html")
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))
63 fun `post request without confirm redirects to edit profile page`() {
65 addHttpRequestPart("field", field.id)
66 verifyRedirect("editProfile.html#profile-fields") {
67 verify(currentSone, never()).profile = any()
72 fun `post request with confirm removes field and redirects to edit profile page`() {
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
83 fun `page can be created by dependency injection`() {
84 assertThat(baseInjector.getInstance<DeleteProfileFieldPage>(), notNullValue())
88 fun `page is annotated with correct template path`() {
89 assertThat(page.templatePath, equalTo("/templates/deleteProfileField.html"))