✅ Add tests for WOTC.setProperty
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / freenet / wot / WebOfTrustConnectorTest.kt
1 package net.pterodactylus.sone.freenet.wot
2
3 import freenet.support.*
4 import freenet.support.api.*
5 import net.pterodactylus.sone.freenet.*
6 import net.pterodactylus.sone.freenet.plugin.*
7 import net.pterodactylus.sone.test.*
8 import org.hamcrest.*
9 import org.hamcrest.MatcherAssert.*
10 import org.hamcrest.Matchers.*
11 import org.hamcrest.core.*
12 import kotlin.test.*
13
14 /**
15  * Unit test for [WebOfTrustConnector].
16  */
17 class WebOfTrustConnectorTest {
18
19         private val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
20
21         @Test
22         fun `wot plugin can be pinged`() {
23                 createPluginConnector("Ping")
24                                 .connect { ping() }
25         }
26
27         @Test
28         fun `own identities are returned correctly`() {
29                 val ownIdentities = createPluginConnector("GetOwnIdentities") {
30                         put("Identity0", "id-0")
31                         put("RequestURI0", "request-uri-0")
32                         put("InsertURI0", "insert-uri-0")
33                         put("Nickname0", "nickname-0")
34                         put("Contexts0.Context0", "id-0-context-0")
35                         put("Properties0.Property0.Name", "id-0-property-0-name")
36                         put("Properties0.Property0.Value", "id-0-property-0-value")
37                         put("Identity1", "id-1")
38                         put("RequestURI1", "request-uri-1")
39                         put("InsertURI1", "insert-uri-1")
40                         put("Nickname1", "nickname-1")
41                         put("Contexts1.Context0", "id-1-context-0")
42                         put("Properties1.Property0.Name", "id-1-property-0-name")
43                         put("Properties1.Property0.Value", "id-1-property-0-value")
44                 }.connect { loadAllOwnIdentities() }
45                 assertThat(ownIdentities, containsInAnyOrder(
46                                 isOwnIdentity("id-0", "nickname-0", "request-uri-0", "insert-uri-0", contains("id-0-context-0"), hasEntry("id-0-property-0-name", "id-0-property-0-value")),
47                                 isOwnIdentity("id-1", "nickname-1", "request-uri-1", "insert-uri-1", contains("id-1-context-0"), hasEntry("id-1-property-0-name", "id-1-property-0-value"))
48                 ))
49         }
50
51         @Test
52         fun `trusted identities are requested with correct own identity`() {
53                 val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Truster", equalTo("id")))
54                                 .connect { loadTrustedIdentities(ownIdentity) }
55         }
56
57         @Test
58         fun `trusted identities are requested with correct selection parameter`() {
59                 val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Selection", equalTo("+")))
60                                 .connect { loadTrustedIdentities(ownIdentity) }
61         }
62
63         @Test
64         fun `trusted identities are requested with empty context if null context requested`() {
65                 val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("")))
66                                 .connect { loadTrustedIdentities(ownIdentity) }
67         }
68
69         @Test
70         fun `trusted identities are requested with context if context requested`() {
71                 val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("TestContext")))
72                                 .connect { loadTrustedIdentities(ownIdentity, "TestContext") }
73         }
74
75         @Test
76         fun `trusted identities are requested with trust values`() {
77                 createPluginConnector("GetIdentitiesByScore", hasField("WantTrustValues", equalTo("true")))
78                                 .connect { loadTrustedIdentities(ownIdentity) }
79         }
80
81         @Test
82         fun `empty list of trusted identities is returned correctly`() {
83                 val trustedIdentities = createPluginConnector("GetIdentitiesByScore")
84                                 .connect { loadTrustedIdentities(ownIdentity) }
85                 assertThat(trustedIdentities, empty())
86         }
87
88         @Test
89         fun `trusted identities without context, properties, or trust value are returned correctly`() {
90                 val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
91                         put("Identity0", "id0")
92                         put("Nickname0", "nickname0")
93                         put("RequestURI0", "request-uri0")
94                         put("Identity1", "id1")
95                         put("Nickname1", "nickname1")
96                         put("RequestURI1", "request-uri1")
97                 }.connect { loadTrustedIdentities(ownIdentity) }
98                 assertThat(trustedIdentities, contains(
99                                 allOf(
100                                                 isIdentity("id0", "nickname0", "request-uri0", empty<String>(), isEmptyMap()),
101                                                 isTrusted(ownIdentity, isTrust(null, 0, 0))
102                                 ),
103                                 allOf(
104                                                 isIdentity("id1", "nickname1", "request-uri1", empty<String>(), isEmptyMap()),
105                                                 isTrusted(ownIdentity, isTrust(null, 0, 0))
106                                 )
107                 ))
108         }
109
110         @Test
111         fun `trusted identity with contexts is returned correctly`() {
112                 val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
113                         put("Identity0", "id0")
114                         put("Nickname0", "nickname0")
115                         put("RequestURI0", "request-uri0")
116                         put("Contexts0.Context0", "Context0")
117                         put("Contexts0.Context1", "Context1")
118                 }.connect { loadTrustedIdentities(ownIdentity) }
119                 assertThat(trustedIdentities, contains(
120                                 isIdentity("id0", "nickname0", "request-uri0", containsInAnyOrder("Context0", "Context1"), isEmptyMap())
121                 ))
122         }
123
124         @Test
125         fun `trusted identity with properties is returned correctly`() {
126                 val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
127                         put("Identity0", "id0")
128                         put("Nickname0", "nickname0")
129                         put("RequestURI0", "request-uri0")
130                         put("Properties0.Property0.Name", "foo")
131                         put("Properties0.Property0.Value", "bar")
132                         put("Properties0.Property1.Name", "baz")
133                         put("Properties0.Property1.Value", "quo")
134                 }.connect { loadTrustedIdentities(ownIdentity) }
135                 assertThat(trustedIdentities, contains(
136                                 isIdentity("id0", "nickname0", "request-uri0", empty(), allOf(hasEntry("foo", "bar"), hasEntry("baz", "quo")))
137                 ))
138         }
139
140         @Test
141         fun `trusted identity with trust value is returned correctly`() {
142                 val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
143                         put("Identity0", "id0")
144                         put("Nickname0", "nickname0")
145                         put("RequestURI0", "request-uri0")
146                         put("Trust0", "12")
147                         put("Score0", "34")
148                         put("Rank0", "56")
149                 }.connect { loadTrustedIdentities(ownIdentity) }
150                 assertThat(trustedIdentities, contains(
151                                 allOf(
152                                                 isIdentity("id0", "nickname0", "request-uri0", empty(), isEmptyMap()),
153                                                 isTrusted(ownIdentity, isTrust(12, 34, 56))
154                                 )
155                 ))
156         }
157
158         @Test
159         fun `adding a context sends the correct own identity id`() {
160                 createPluginConnector("AddContext", hasField("Identity", equalTo(ownIdentity.id)))
161                                 .connect { addContext(ownIdentity, "TestContext") }
162         }
163
164         @Test
165         fun `adding a context sends the correct context`() {
166                 createPluginConnector("AddContext", hasField("Context", equalTo("TestContext")))
167                                 .connect { addContext(ownIdentity, "TestContext") }
168         }
169
170         @Test
171         fun `removing a context sends the correct own identity id`() {
172                 createPluginConnector("RemoveContext", hasField("Identity", equalTo(ownIdentity.id)))
173                                 .connect { removeContext(ownIdentity, "TestContext") }
174         }
175
176         @Test
177         fun `removing a context sends the correct context`() {
178                 createPluginConnector("RemoveContext", hasField("Context", equalTo("TestContext")))
179                                 .connect { removeContext(ownIdentity, "TestContext") }
180         }
181
182         @Test
183         fun `setting a property sends the correct identity id`() {
184                 createPluginConnector("SetProperty", hasField("Identity", equalTo(ownIdentity.id)))
185                                 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
186         }
187
188         @Test
189         fun `setting a property sends the correct property name`() {
190                 createPluginConnector("SetProperty", hasField("Property", equalTo("TestProperty")))
191                                 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
192         }
193
194         @Test
195         fun `setting a property sends the correct property value`() {
196                 createPluginConnector("SetProperty", hasField("Value", equalTo("TestValue")))
197                                 .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
198         }
199
200 }
201
202 private fun <R> PluginConnector.connect(block: WebOfTrustConnector.() -> R) =
203                 WebOfTrustConnector(this).let(block)
204
205 fun createPluginConnector(message: String, fieldsMatcher: Matcher<SimpleFieldSet> = IsAnything<SimpleFieldSet>(), build: SimpleFieldSetBuilder.() -> Unit = {}) =
206                 object : PluginConnector {
207                         override fun sendRequest(pluginName: String, identifier: String, fields: SimpleFieldSet, data: Bucket?) =
208                                         if ((pluginName != wotPluginName) || (fields.get("Message") != message)) {
209                                                 throw PluginException()
210                                         } else {
211                                                 assertThat(fields, fieldsMatcher)
212                                                 PluginReply(SimpleFieldSetBuilder().apply(build).get(), null)
213                                         }
214                 }
215
216 private const val wotPluginName = "plugins.WebOfTrust.WebOfTrust"