1 package net.pterodactylus.sone.notify
3 import com.google.inject.Guice
4 import net.pterodactylus.sone.freenet.wot.OwnIdentity
5 import net.pterodactylus.sone.freenet.wot.Trust
6 import net.pterodactylus.sone.test.createLocalSone
7 import net.pterodactylus.sone.test.createPost
8 import net.pterodactylus.sone.test.createRemoteSone
9 import net.pterodactylus.sone.test.verifySingletonInstance
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.equalTo
15 * Unit test for [DefaultPostVisibilityFilter].
17 class DefaultPostVisibilityFilterTest {
19 private val postVisibilityFilter = DefaultPostVisibilityFilter()
20 private val localSone = createLocalSone()
21 private val remoteSone = createRemoteSone()
24 fun `post visibility filter is only created once`() {
25 val injector = Guice.createInjector()
26 injector.verifySingletonInstance<PostVisibilityFilter>()
30 fun `post is not visible if it is not loaded`() {
31 val post = createPost(loaded = false)
32 assertThat(postVisibilityFilter.isPostVisible(null, post), equalTo(false))
36 fun `loaded post is visible without sone and in the past`() {
37 val post = createPost(sone = null)
38 assertThat(postVisibilityFilter.isPostVisible(null, post), equalTo(true))
42 fun `loaded post from the future is not visible`() {
43 // the offset for the future must be large enough to survive loading freenet.crypt.Util.
44 val post = createPost(time = System.currentTimeMillis() + 100000, sone = null)
45 assertThat(postVisibilityFilter.isPostVisible(null, post), equalTo(false))
49 fun `loaded post from explicitely not trusted sone is not visible`() {
50 remoteSone.identity.setTrust(localSone.identity as OwnIdentity, Trust(-1, null, null))
51 val post = createPost(sone = remoteSone)
52 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(false))
56 fun `loaded post from implicitely untrusted sone is not visible`() {
57 remoteSone.identity.setTrust(localSone.identity as OwnIdentity, Trust(null, -1, null))
58 val post = createPost(sone = remoteSone)
59 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(false))
63 fun `loaded post from implicitely untrusted but followed sone is visible`() {
64 localSone.friends.add(remoteSone.id)
65 remoteSone.identity.setTrust(localSone.identity as OwnIdentity, Trust(1, -1, null))
66 val post = createPost(sone = remoteSone)
67 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
71 fun `loaded post from implicitely trusted and followed sone is visible`() {
72 localSone.friends.add(remoteSone.id)
73 remoteSone.identity.setTrust(localSone.identity as OwnIdentity, Trust(null, 1, null))
74 val post = createPost(sone = remoteSone)
75 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
79 fun `loaded post from followed sone with unknown trust is visible`() {
80 localSone.friends.add(remoteSone.id)
81 remoteSone.identity.setTrust(localSone.identity as OwnIdentity, Trust(null, null, null))
82 val post = createPost(sone = remoteSone)
83 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
87 fun `loaded post from unfollowed remote sone that is not directed at local sone is not visible`() {
88 val post = createPost(sone = remoteSone)
89 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(false))
93 fun `loaded post from local sone is visible`() {
94 val post = createPost(sone = localSone)
95 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
99 fun `loaded post from followed remote sone that is not directed at local sone is visible`() {
100 localSone.friends.add(remoteSone.id)
101 val post = createPost(sone = remoteSone)
102 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
106 fun `loaded post from remote sone that is directed at local sone is visible`() {
107 val post = createPost(sone = remoteSone, recipient = localSone)
108 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
112 fun `predicate will correctly recognize visible post`() {
113 val post = createPost(sone = localSone)
114 assertThat(postVisibilityFilter.isVisible(null).invoke(post), equalTo(true))
118 fun `predicate will correctly recognize not visible post`() {
119 val post = createPost(loaded = false)
120 assertThat(postVisibilityFilter.isVisible(null).invoke(post), equalTo(false))