🚧 Use strictly-filtered identities
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / fcp / GetPostCommandTest.kt
1 package net.pterodactylus.sone.fcp
2
3 import freenet.support.SimpleFieldSet
4 import net.pterodactylus.sone.core.Core
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.sone.utils.asOptional
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.containsInAnyOrder
11 import org.hamcrest.Matchers.equalTo
12 import org.hamcrest.Matchers.nullValue
13 import org.junit.Before
14 import org.junit.Test
15
16 /**
17  * Unit test for [GetPostCommand].
18  */
19 class GetPostCommandTest : SoneCommandTest() {
20
21         private val sone = mock<Sone>().apply {
22                 whenever(id).thenReturn("SoneId")
23         }
24         private val post = createPost("ValidPostId", sone, null, 1000, "Post Text\r\nSecond \\Line")
25         private val sone1 = mock<Sone>().apply { whenever(id).thenReturn("Sone1") }
26         private val sone2 = mock<Sone>().apply { whenever(id).thenReturn("Sone2") }
27         private val postReply1 = createReply("ReplyId1", sone1, post, 1000, "Reply 1")
28         private val postReply2 = createReply("ReplyId2", sone2, post, 2000, "Reply 2")
29
30         override fun createCommand(core: Core) = GetPostCommand(core)
31
32         @Before
33         fun setupPostWithLikesAndReplies() {
34                 whenever(core.getPost("ValidPostId")).thenReturn(post)
35                 whenever(core.getLikes(post)).thenReturn(setOf(sone1, sone2))
36                 val replies = listOf(postReply1, postReply2)
37                 whenever(core.getReplies("ValidPostId")).thenReturn(replies)
38         }
39
40         @Test
41         fun `command does not require write access`() {
42                 assertThat(command.requiresWriteAccess, equalTo(false))
43         }
44
45         @Test
46         fun `request without any parameter results in fcp exception`() {
47                 requestWithoutAnyParameterResultsInFcpException()
48         }
49
50         @Test
51         fun `request with invalid post parameter results in fcp exception`() {
52                 parameters += "Post" to "InvalidPostId"
53                 executeCommandAndExpectFcpException()
54         }
55
56         private fun verifyPostWithLikes(replyParameters: SimpleFieldSet) {
57                 assertThat(replyParameters["Message"], equalTo("Post"))
58                 assertThat(replyParameters.parsePost("Post."), matchesPost(post))
59                 assertThat(replyParameters["Post.Likes.Count"], equalTo("2"))
60                 assertThat((0..1).map { replyParameters["Post.Likes.$it.ID"] }, containsInAnyOrder("Sone1", "Sone2"))
61         }
62
63         private fun verifyReplies(replyParameters: SimpleFieldSet) {
64                 assertThat(replyParameters["Post.Replies.Count"], equalTo("2"))
65                 assertThat(replyParameters.parsePost("Post.Replies.0."), matchesReply(postReply1))
66                 assertThat(replyParameters.parsePost("Post.Replies.1."), matchesReply(postReply2))
67         }
68
69         @Test
70         fun `request with valid post parameter returns post response`() {
71                 parameters += "Post" to "ValidPostId"
72                 val replyParameters = command.execute(parameters).replyParameters
73                 verifyPostWithLikes(replyParameters)
74                 assertThat(replyParameters["Post.Recipient"], nullValue())
75                 verifyReplies(replyParameters)
76         }
77
78         @Test
79         fun `request with valid post parameter without replies returns post response without replies`() {
80                 parameters += "Post" to "ValidPostId"
81                 parameters += "IncludeReplies" to "false"
82                 val replyParameters = command.execute(parameters).replyParameters
83                 verifyPostWithLikes(replyParameters)
84                 assertThat(replyParameters["Post.Recipient"], nullValue())
85                 assertThat(replyParameters["Post.Replies.Count"], nullValue())
86         }
87
88         @Test
89         fun `request with valid post parameter returns post response with recipient`() {
90                 parameters += "Post" to "ValidPostId"
91                 whenever(post.recipientId).thenReturn("Sone2".asOptional())
92                 val replyParameters = command.execute(parameters).replyParameters
93                 verifyPostWithLikes(replyParameters)
94                 assertThat(replyParameters["Post.Recipient"], equalTo("Sone2"))
95                 verifyReplies(replyParameters)
96         }
97
98         @Test
99         fun `request with valid post parameter without replies returns post response without replies but with recipient`() {
100                 parameters += "Post" to "ValidPostId"
101                 parameters += "IncludeReplies" to "false"
102                 whenever(post.recipientId).thenReturn("Sone2".asOptional())
103                 val replyParameters = command.execute(parameters).replyParameters
104                 verifyPostWithLikes(replyParameters)
105                 assertThat(replyParameters["Post.Recipient"], equalTo("Sone2"))
106                 assertThat(replyParameters["Post.Replies.Count"], nullValue())
107         }
108
109 }