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.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
14 import org.mockito.Mockito.any
15 import org.mockito.Mockito.never
16 import org.mockito.Mockito.verify
19 * Unit test for [DeleteProfileFieldPage].
21 class DeleteProfileFieldPageTest: WebPageTest(::DeleteProfileFieldPage) {
23 private val profile = Profile(currentSone)
24 private val field = profile.addField("name")
28 whenever(currentSone.profile).thenReturn(profile)
33 fun `page returns correct path`() {
34 assertThat(page.path, equalTo("deleteProfileField.html"))
38 fun `page requires login`() {
39 assertThat(page.requiresLogin(), equalTo(true))
43 fun `get request with invalid field name redirects to invalid page`() {
44 verifyRedirect("invalid.html")
48 fun `post request with invalid field name redirects to invalid page`() {
50 addHttpRequestPart("field", "wrong-id")
51 verifyRedirect("invalid.html")
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))
62 fun `post request without confirm redirects to edit profile page`() {
64 addHttpRequestPart("field", field.id)
65 verifyRedirect("editProfile.html#profile-fields") {
66 verify(currentSone, never()).profile = any()
71 fun `post request with confirm removes field and redirects to edit profile page`() {
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
82 fun `page can be created by dependency injection`() {
83 assertThat(baseInjector.getInstance<DeleteProfileFieldPage>(), notNullValue())