forked from servalproject/serval-dna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeshmbrestful
executable file
·312 lines (295 loc) · 11.8 KB
/
meshmbrestful
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/bash
source "${0%/*}/../testframework.sh"
source "${0%/*}/../testdefs.sh"
source "${0%/*}/../testdefs_json.sh"
setup_instance() {
set_instance $1
executeOk_servald config \
set api.restful.users.harry.password potter \
set api.restful.users.ron.password weasley \
set api.restful.users.hermione.password grainger \
set debug.http_server on \
set debug.httpd on \
set debug.meshmb on \
set debug.meshms on \
set debug.verbose on \
set log.console.level debug
set_extra_config
if [ -z "$IDENTITY_COUNT" ]; then
create_single_identity
else
create_identities $IDENTITY_COUNT
fi
}
set_extra_config() {
:
}
setup() {
setup_curl 7
setup_json
setup_servald
export SERVALD_RHIZOME_DB_RETRY_LIMIT_MS=60000
setup_instance +A
start_servald_instances +A
wait_until servald_restful_http_server_started +A
get_servald_restful_http_server_port PORTA +A
}
finally() {
stop_all_servald_servers
}
teardown() {
kill_all_servald_processes
assert_no_servald_processes
report_all_servald_servers
}
doc_MeshMBRestSend="Restful send of a broadcast message"
test_MeshMBRestSend() {
executeOk curl \
--silent --fail --show-error \
--output sendmessage.json \
--basic --user harry:potter \
--form "message=Hello World;type=text/plain;charset=utf-8" \
"http://$addr_localhost:$PORTA/restful/meshmb/$IDA1/sendmessage"
executeOk_servald rhizome export bundle $IDA1 broadcast.manifest broadcast
tfw_cat -h broadcast.manifest
tfw_cat -h broadcast
}
doc_MeshMBRestList="Restful list of meshmb messages"
setup_MeshMBRestList() {
setup
executeOk_servald meshmb send $IDA1 "Message 1"
executeOk_servald meshmb send $IDA1 "Message 2"
}
test_MeshMBRestList() {
executeOk curl \
--silent --fail --show-error \
--output listmessages.json \
--dump-header http.headers \
--basic --user harry:potter \
"http://$addr_localhost:$PORTA/restful/meshmb/$IDA1/messagelist.json"
tfw_cat http.headers listmessages.json
tfw_preserve listmessages.json
assert [ "$(jq '.rows | length' listmessages.json)" = 2 ]
transform_list_json listmessages.json list.json
tfw_preserve list.json
assertJq list.json \
"contains([
{ offset: 12,
text: \"Message 1\"
}
])"
assertJq list.json \
"contains([
{ offset: 30,
text: \"Message 2\"
}
])"
}
doc_MeshMBRestFollow="Restful follow another feed"
setup_MeshMBRestFollow() {
IDENTITY_COUNT=3
setup
executeOk_servald meshmb send $IDA1 "Message 1"
executeOk_servald meshmb send $IDA2 "Message 2"
executeOk_servald meshmb send $IDA3 "Message 3"
}
test_MeshMBRestFollow() {
executeOk curl \
--silent --fail --show-error \
--output follow.json \
--basic --user harry:potter \
--request POST \
"http://$addr_localhost:$PORTA/restful/meshmb/$IDA1/follow/$IDA2"
executeOk_servald meshmb list following $IDA1
assertStdoutGrep --matches=1 ":$IDA2:$SIDA2:false::[0-9]\+:Message 2\$"
assertStdoutGrep --matches=0 ":$IDA3:$SIDA3:false::[0-9]\+:Message 3\$"
executeOk curl \
--silent --fail --show-error \
--output follow.json \
--basic --user harry:potter \
--request POST \
"http://$addr_localhost:$PORTA/restful/meshmb/$IDA1/follow/$IDA3"
executeOk_servald meshmb list following $IDA1
assertStdoutGrep --matches=1 ":$IDA2:$SIDA2:false::[0-9]\+:Message 2\$"
assertStdoutGrep --matches=1 ":$IDA3:$SIDA3:false::[0-9]\+:Message 3\$"
executeOk curl \
--silent --fail --show-error \
--output follow.json \
--basic --user harry:potter \
--request POST \
"http://$addr_localhost:$PORTA/restful/meshmb/$IDA1/ignore/$IDA2"
executeOk_servald meshmb list following $IDA1
assertStdoutGrep --matches=0 ":$IDA2:$SIDA2:false::[0-9]\+:Message 2\$"
assertStdoutGrep --matches=1 ":$IDA3:$SIDA3:false::[0-9]\+:Message 3\$"
}
doc_MeshMBRestFeeds="Restful list subscribed feeds"
setup_MeshMBRestFeeds() {
IDENTITY_COUNT=3
setup
executeOk_servald meshmb send $IDA1 "Message 1"
executeOk_servald meshmb send $IDA2 "Message 2"
executeOk_servald meshmb send $IDA3 "Message 3"
executeOk_servald meshmb follow $IDA1 $IDA2
executeOk_servald meshmb follow $IDA1 $IDA3
}
test_MeshMBRestFeeds() {
executeOk curl \
--silent --fail --show-error \
--output feedlist.json \
--dump-header http.headers \
--basic --user harry:potter \
"http://$addr_localhost:$PORTA/restful/meshmb/$IDA1/feedlist.json"
tfw_cat http.headers feedlist.json
tfw_preserve feedlist.json
assert [ "$(jq '.rows | length' feedlist.json)" = 2 ]
transform_list_json feedlist.json list.json
tfw_preserve list.json
assertJq list.json \
"contains([
{ id: \"$IDA2\",
last_message: \"Message 2\"
}
])"
assertJq list.json \
"contains([
{ id: \"$IDA3\",
last_message: \"Message 3\"
}
])"
}
doc_MeshMBRestEmptyActivity="Restful activity with no content"
setup_MeshMBRestEmptyActivity() {
IDENTITY_COUNT=1
setup
}
test_MeshMBRestEmptyActivity() {
executeOk --timeout=10 curl \
--silent --fail --show-error \
--output activity.json \
--dump-header http.headers \
--basic --user harry:potter \
"http://$addr_localhost:$PORTA/restful/meshmb/$IDA1/activity.json"
tfw_cat http.headers activity.json
tfw_preserve activity.json
assert [ "$(jq '.rows | length' activity.json)" = 0 ]
transform_list_json activity.json list.json
tfw_preserve list.json
}
doc_MeshMBRestActivity="Restful thread incoming activity"
setup_MeshMBRestActivity() {
IDENTITY_COUNT=5
setup
executeOk_servald keyring set did $SIDA1 "" "Feed A"
executeOk_servald keyring set did $SIDA2 "" "Feed B"
executeOk_servald keyring set did $SIDA3 "" "Feed C"
executeOk_servald keyring set did $SIDA4 "" "Feed D"
executeOk_servald keyring set did $SIDA5 "" "Feed E"
executeOk_servald meshmb send $IDA2 "Message 2"
executeOk_servald meshmb send $IDA3 "Message 3"
executeOk_servald meshmb follow $IDA1 $IDA2
executeOk_servald meshmb follow $IDA1 $IDA3
executeOk_servald meshmb send $IDA4 "Message 4"
executeOk_servald meshmb follow $IDA1 $IDA4
executeOk_servald meshmb send $IDA4 "Message 5"
executeOk_servald meshmb activity $IDA1
executeOk_servald meshmb send $IDA5 "Message 6"
executeOk_servald meshmb follow $IDA1 $IDA5
executeOk_servald meshmb send $IDA2 "Message 7"
executeOk_servald meshmb activity $IDA1
executeOk_servald meshmb send $IDA3 "Message 8"
executeOk_servald meshmb activity $IDA1
executeOk_servald meshmb send $IDA4 "Message 9"
executeOk_servald meshmb activity $IDA1
executeOk_servald meshmb send $IDA5 "Message 10"
executeOk_servald meshmb activity $IDA1
executeOk_servald meshmb send $IDA1 "Message 11"
executeOk_servald meshmb activity $IDA1
}
test_MeshMBRestActivity() {
executeOk curl \
--silent --fail --show-error \
--output activity.json \
--dump-header http.headers \
--basic --user harry:potter \
"http://$addr_localhost:$PORTA/restful/meshmb/$IDA1/activity.json"
tfw_cat http.headers activity.json
tfw_preserve activity.json
assert [ "$(jq '.rows | length' activity.json)" = 10 ]
transform_list_json activity.json list.json
tfw_preserve list.json
assertJq list.json "contains([{ __index: 0, id: \"$IDA1\", author: \"$SIDA1\", name: \"Feed A\", message: \"Message 11\"}])"
assertJq list.json "contains([{ __index: 1, id: \"$IDA5\", author: \"$SIDA5\", name: \"Feed E\", message: \"Message 10\"}])"
assertJq list.json "contains([{ __index: 2, id: \"$IDA4\", author: \"$SIDA4\", name: \"Feed D\", message: \"Message 9\"}])"
assertJq list.json "contains([{ __index: 3, id: \"$IDA3\", author: \"$SIDA3\", name: \"Feed C\", message: \"Message 8\"}])"
assertJq list.json "contains([{ __index: 4, id: \"$IDA2\", author: \"$SIDA2\", name: \"Feed B\", message: \"Message 7\"}])"
assertJq list.json "contains([{ __index: 5, id: \"$IDA5\", author: \"$SIDA5\", name: \"Feed E\", message: \"Message 6\"}])"
assertJq list.json "contains([{ __index: 6, id: \"$IDA4\", author: \"$SIDA4\", name: \"Feed D\", message: \"Message 5\"}])"
assertJq list.json "contains([{ __index: 7, id: \"$IDA4\", author: \"$SIDA4\", name: \"Feed D\", message: \"Message 4\"}])"
assertJq list.json "contains([{ __index: 8, id: \"$IDA3\", author: \"$SIDA3\", name: \"Feed C\", message: \"Message 3\"}])"
assertJq list.json "contains([{ __index: 9, id: \"$IDA2\", author: \"$SIDA2\", name: \"Feed B\", message: \"Message 2\"}])"
}
doc_MeshMBRestNewActivity="Restful newsince incoming activity"
setup_MeshMBRestNewActivity() {
IDENTITY_COUNT=3
setup
executeOk_servald keyring set did $SIDA1 "" "Feed A"
executeOk_servald keyring set did $SIDA2 "" "Feed B"
executeOk_servald keyring set did $SIDA3 "" "Feed C"
executeOk_servald meshmb send $IDA2 "Message 1"
executeOk_servald meshmb send $IDA3 "Message 2"
executeOk_servald meshmb follow $IDA1 $IDA2
executeOk_servald meshmb follow $IDA1 $IDA3
executeOk curl \
--silent --fail --show-error \
--output activity.json \
--dump-header http.headers \
--basic --user harry:potter \
"http://$addr_localhost:$PORTA/restful/meshmb/$IDA1/activity.json"
assert [ "$(jq '.rows | length' activity.json)" = 2 ]
transform_list_json activity.json array_of_objects.json
tfw_preserve array_of_objects.json
token=$(jq --raw-output '.[0][".token"]' array_of_objects.json)
assert [ -n "$token" ]
}
test_MeshMBRestNewActivity() {
for i in 1 2 3; do
fork %curl$i curl \
--silent --fail --show-error \
--no-buffer \
--output newsince$i.json \
--basic --user harry:potter \
"http://$addr_localhost:$PORTA/restful/meshmb/$IDA1/activity/$token/activity.json"
done
wait_until [ -e newsince1.json -a -e newsince2.json -a -e newsince3.json ]
executeOk_servald meshmb send $IDA2 "Message 3"
executeOk_servald meshmb send $IDA3 "Message 4"
executeOk_servald meshmb send $IDA2 "Message 5"
executeOk_servald meshmb send $IDA3 "Message 6"
executeOk_servald meshmb send $IDA1 "Message 7"
for i in 1 2 3; do
wait_until grep "Message 3" newsince$i.json
wait_until grep "Message 4" newsince$i.json
wait_until grep "Message 5" newsince$i.json
wait_until grep "Message 6" newsince$i.json
wait_until grep "Message 7" newsince$i.json
done
fork_terminate_all
fork_wait_all
for i in 1 2 3; do
if [ $(jq . newsince$i | wc -c) -eq 0 ]; then
echo ']}' >>newsince$i.json
assert [ $(jq . newsince$i.json | wc -c) -ne 0 ]
fi
transform_list_json newsince$i.json objects$i.json
tfw_preserve newsince$i.json objects$i.json
done
for i in 1 2 3; do
assert [ "$(jq '.rows | length' newsince$i.json)" = 5 ]
assertJq objects$i.json "contains([{ id: \"$IDA2\", author: \"$SIDA2\", name: \"Feed B\", message: \"Message 3\"}])"
assertJq objects$i.json "contains([{ id: \"$IDA3\", author: \"$SIDA3\", name: \"Feed C\", message: \"Message 4\"}])"
assertJq objects$i.json "contains([{ id: \"$IDA2\", author: \"$SIDA2\", name: \"Feed B\", message: \"Message 5\"}])"
assertJq objects$i.json "contains([{ id: \"$IDA3\", author: \"$SIDA3\", name: \"Feed C\", message: \"Message 6\"}])"
assertJq objects$i.json "contains([{ id: \"$IDA1\", author: \"$SIDA1\", name: \"Feed A\", message: \"Message 7\"}])"
done
}
runTests "$@"