Add test for DI constructability of TrustAjaxPage
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / EditProfileFieldAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.test.getInstance
4 import net.pterodactylus.sone.web.baseInjector
5 import org.hamcrest.MatcherAssert.assertThat
6 import org.hamcrest.Matchers.equalTo
7 import org.hamcrest.Matchers.notNullValue
8 import org.junit.Test
9 import org.mockito.Mockito.verify
10
11 /**
12  * Unit test for [EditProfileFieldAjaxPage].
13  */
14 class EditProfileFieldAjaxPageTest : JsonPageTest("editProfileField.ajax", pageSupplier = ::EditProfileFieldAjaxPage) {
15
16         @Test
17         fun `request without field id results in invalid-field-id`() {
18                 assertThatJsonFailed("invalid-field-id")
19         }
20
21         @Test
22         fun `request with empty new name results in invalid-parameter-name`() {
23                 val field = currentSone.profile.addField("test-field")
24                 addRequestParameter("field", field.id)
25                 addRequestParameter("name", "  \t ")
26                 assertThatJsonFailed("invalid-parameter-name")
27         }
28
29         @Test
30         fun `request with duplicate new name results in duplicate-field-name`() {
31                 currentSone.profile.addField("other-field")
32                 val field = currentSone.profile.addField("test-field")
33                 addRequestParameter("field", field.id)
34                 addRequestParameter("name", "other-field")
35                 assertThatJsonFailed("duplicate-field-name")
36         }
37
38         @Test
39         fun `request with valid field name changes field name`() {
40                 val profile = currentSone.profile
41                 val field = profile.addField("test-field")
42                 addRequestParameter("field", field.id)
43                 addRequestParameter("name", "  new name ")
44                 assertThatJsonIsSuccessful()
45                 assertThat(field.name, equalTo("new name"))
46                 verify(currentSone).profile = profile
47         }
48
49         @Test
50         fun `page can be created by dependency injection`() {
51             assertThat(baseInjector.getInstance<EditProfileFieldAjaxPage>(), notNullValue())
52         }
53
54 }