1 package net.pterodactylus.sone.notify
3 import com.google.common.base.Optional
4 import com.google.inject.Guice
5 import net.pterodactylus.sone.data.Post
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.freenet.wot.Identity
8 import net.pterodactylus.sone.freenet.wot.OwnIdentity
9 import net.pterodactylus.sone.freenet.wot.Trust
10 import net.pterodactylus.sone.test.mock
11 import net.pterodactylus.sone.test.verifySingletonInstance
12 import net.pterodactylus.sone.test.whenever
13 import org.hamcrest.MatcherAssert.assertThat
14 import org.hamcrest.Matchers.equalTo
18 * Unit test for [PostVisibilityFilterTest].
20 class PostVisibilityFilterTest {
22 private val postVisibilityFilter = PostVisibilityFilter()
23 private val localSone = mock<Sone>()
24 private val localIdentity = mock<OwnIdentity>()
25 private val post = mock<Post>()
26 private val remoteSone = mock<Sone>()
27 private val remoteIdentity = mock<Identity>()
30 whenever(localSone.id).thenReturn(LOCAL_ID)
31 whenever(localSone.isLocal).thenReturn(true)
32 whenever(localSone.identity).thenReturn(localIdentity)
33 whenever(localIdentity.id).thenReturn(LOCAL_ID)
34 whenever(remoteSone.id).thenReturn(REMOTE_ID)
35 whenever(remoteSone.identity).thenReturn(remoteIdentity)
36 whenever(remoteIdentity.id).thenReturn(REMOTE_ID)
37 whenever(post.recipientId).thenReturn(Optional.absent())
41 fun `post visibility filter is only created once`() {
42 val injector = Guice.createInjector()
43 injector.verifySingletonInstance<PostVisibilityFilter>()
47 fun `post is not visible if it is not loaded`() {
48 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(false))
52 fun `loaded post is visible without sone`() {
54 assertThat(postVisibilityFilter.isPostVisible(null, post), equalTo(true))
57 private fun makePostComeFromTheFuture() {
58 whenever(post.time).thenReturn(System.currentTimeMillis() + 1000)
62 fun `loaded post from the future is not visible`() {
64 makePostComeFromTheFuture()
65 assertThat(postVisibilityFilter.isPostVisible(null, post), equalTo(false))
68 private fun makePostFromRemoteSone() {
69 whenever(post.sone).thenReturn(remoteSone)
72 private fun giveRemoteIdentityNegativeExplicitTrust() {
73 whenever(remoteIdentity.getTrust(localIdentity)).thenReturn(Trust(-1, null, null))
77 fun `loaded post from explicitely not trusted sone is not visible`() {
79 makePostFromRemoteSone()
80 giveRemoteIdentityNegativeExplicitTrust()
81 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(false))
84 private fun giveRemoteIdentityNegativeImplicitTrust() {
85 whenever(remoteIdentity.getTrust(localIdentity)).thenReturn(Trust(null, -1, null))
89 fun `loaded post from implicitely untrusted sone is not visible`() {
91 makePostFromRemoteSone()
92 giveRemoteIdentityNegativeImplicitTrust()
93 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(false))
96 private fun makeLocalSoneFollowRemoteSone() {
97 whenever(localSone.hasFriend(REMOTE_ID)).thenReturn(true)
100 private fun giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust() {
101 whenever(remoteIdentity.getTrust(localIdentity)).thenReturn(Trust(1, -1, null))
105 fun `loaded post from explicitely trusted but implicitely untrusted sone is visible`() {
107 makePostFromRemoteSone()
108 makeLocalSoneFollowRemoteSone()
109 giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust()
110 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
113 private fun giveTheRemoteIdentityPositiveImplicitTrust() {
114 whenever(remoteIdentity.getTrust(localIdentity)).thenReturn(Trust(null, 1, null))
118 fun `loaded post from implicitely trusted sone is visible`() {
120 makePostFromRemoteSone()
121 makeLocalSoneFollowRemoteSone()
122 giveTheRemoteIdentityPositiveImplicitTrust()
123 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
126 private fun giveTheRemoteIdentityUnknownTrust() {
127 whenever(remoteIdentity.getTrust(localIdentity)).thenReturn(Trust(null, null, null))
131 fun `loaded post from sone with unknown trust is visible`() {
133 makePostFromRemoteSone()
134 makeLocalSoneFollowRemoteSone()
135 giveTheRemoteIdentityUnknownTrust()
136 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
140 fun `loaded post from unfollowed remote sone that is not directed at local sone is not visible`() {
142 makePostFromRemoteSone()
143 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(false))
146 private fun makePostFromLocalSone() {
148 whenever(post.sone).thenReturn(localSone)
152 fun `loaded post from local sone is visible`() {
153 makePostFromLocalSone()
154 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
158 fun `loaded post from followed remote sone that is not directed at local sone is visible`() {
160 makePostFromRemoteSone()
161 makeLocalSoneFollowRemoteSone()
162 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
165 private fun makePostDirectedAtLocalId() {
166 whenever(post.recipientId).thenReturn(Optional.of(LOCAL_ID))
170 fun `loaded post from remote sone that is directed at local sone is visible`() {
172 makePostFromRemoteSone()
173 makePostDirectedAtLocalId()
174 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
178 fun `predicate will correctly recognize visible post`() {
179 makePostFromLocalSone()
180 assertThat(postVisibilityFilter.isVisible(null).apply(post), equalTo(true))
184 fun `predicate will correctly recognize not visible post`() {
185 assertThat(postVisibilityFilter.isVisible(null).apply(post), equalTo(false))
190 private const val LOCAL_ID = "local-id"
191 private const val REMOTE_ID = "remote-id"
193 private fun makePostLoaded(post: Post) {
194 whenever(post.isLoaded).thenReturn(true)