🎨 Use Java’s Predicate in reply visibility filter
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / notify / PostVisibilityFilterTest.kt
1 package net.pterodactylus.sone.notify
2
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
12 import org.junit.Test
13
14 /**
15  * Unit test for [PostVisibilityFilterTest].
16  */
17 class PostVisibilityFilterTest {
18
19         private val postVisibilityFilter = PostVisibilityFilter()
20         private val localSone = createLocalSone()
21         private val remoteSone = createRemoteSone()
22
23         @Test
24         fun `post visibility filter is only created once`() {
25                 val injector = Guice.createInjector()
26                 injector.verifySingletonInstance<PostVisibilityFilter>()
27         }
28
29         @Test
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))
33         }
34
35         @Test
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))
39         }
40
41         @Test
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))
46         }
47
48         @Test
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))
53         }
54
55         @Test
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))
60         }
61
62         @Test
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))
68         }
69
70         @Test
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))
76         }
77
78         @Test
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))
84         }
85
86         @Test
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))
90         }
91
92         @Test
93         fun `loaded post from local sone is visible`() {
94                 val post = createPost(sone = localSone)
95                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
96         }
97
98         @Test
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))
103         }
104
105         @Test
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))
109         }
110
111         @Test
112         fun `predicate will correctly recognize visible post`() {
113                 val post = createPost(sone = localSone)
114                 assertThat(postVisibilityFilter.isVisible(null).test(post), equalTo(true))
115         }
116
117         @Test
118         fun `predicate will correctly recognize not visible post`() {
119                 val post = createPost(loaded = false)
120                 assertThat(postVisibilityFilter.isVisible(null).test(post), equalTo(false))
121         }
122
123 }