🚧 Use strictly-filtered identities
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / fcp / CreatePostCommandTest.kt
1 package net.pterodactylus.sone.fcp
2
3 import net.pterodactylus.sone.core.Core
4 import net.pterodactylus.sone.data.Post
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import org.hamcrest.MatcherAssert.assertThat
8 import org.hamcrest.Matchers.equalTo
9 import org.hamcrest.Matchers.notNullValue
10 import org.junit.Test
11
12 /**
13  * Unit test for [CreatePostCommand].
14  */
15 class CreatePostCommandTest : SoneCommandTest() {
16
17         override fun createCommand(core: Core) = CreatePostCommand(core)
18
19         @Test
20         fun `command requires write access`() {
21                 assertThat(command.requiresWriteAccess, equalTo(true))
22         }
23
24         @Test
25         fun `request without any parameters results in fcp exception`() {
26                 requestWithoutAnyParameterResultsInFcpException()
27         }
28
29         @Test
30         fun `request with empty Sone parameter results in fcp exception`() {
31             requestWithEmptySoneParameterResultsInFcpException()
32         }
33
34         @Test
35         fun `request with invalid Sone parameter results in fcp exception`() {
36                 requestWithInvalidSoneParameterResultsInFcpException()
37         }
38
39         @Test
40         fun `request with valid remote Sone parameter results in fcp exception`() {
41                 requestWithValidRemoteSoneParameterResultsInFcpException()
42         }
43
44         @Test
45         fun `request without text results in fcp exception`() {
46                 parameters += "Sone" to "LocalSoneId"
47                 whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
48                 executeCommandAndExpectFcpException()
49         }
50
51         @Test
52         fun `request with text creates post`() {
53                 parameters += "Sone" to "LocalSoneId"
54                 parameters += "Text" to "Test"
55                 whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
56                 val post = mock<Post>().apply { whenever(id).thenReturn("PostId") }
57                 whenever(core.createPost(localSone, null, "Test")).thenReturn(post)
58                 val response = command.execute(parameters)
59                 assertThat(response.replyParameters.get("Message"), equalTo("PostCreated"))
60                 assertThat(response.replyParameters.get("Post"), equalTo("PostId"))
61         }
62
63         @Test
64         fun `request with invalid recipient results in fcp exception`() {
65                 parameters += "Sone" to "LocalSoneId"
66                 parameters += "Text" to "Test"
67                 parameters += "Recipient" to "InvalidSoneId"
68                 whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
69                 executeCommandAndExpectFcpException()
70         }
71
72         @Test
73         fun `request with recipient the same as the sender returns an error response`() {
74                 parameters += "Sone" to "LocalSoneId"
75                 parameters += "Text" to "Test"
76                 parameters += "Recipient" to "LocalSoneId"
77                 whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
78                 val response = command.execute(parameters)
79                 assertThat(response.replyParameters["Message"], equalTo("Error"))
80                 assertThat(response.replyParameters["ErrorMessage"], notNullValue())
81         }
82
83         @Test
84         fun `request with text and recipient creates post`() {
85                 parameters += "Sone" to "LocalSoneId"
86                 parameters += "Text" to "Test"
87                 parameters += "Recipient" to "RemoteSoneId"
88                 whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
89                 whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone)
90                 val post = mock<Post>().apply { whenever(id).thenReturn("PostId") }
91                 whenever(core.createPost(localSone, remoteSone, "Test")).thenReturn(post)
92                 val response = command.execute(parameters)
93                 assertThat(response.replyParameters.get("Message"), equalTo("PostCreated"))
94                 assertThat(response.replyParameters.get("Post"), equalTo("PostId"))
95         }
96
97 }