-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun.sh
executable file
·385 lines (313 loc) · 9.03 KB
/
run.sh
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
# A script to set up Okapi and run mod-notes
# Requirements
# - Run this script in mod-notes main directory
# - okapi in ../okapi, compiled ok
# - mod-notes itself compiled ok
# - mod-permissions in ../mod-permissions, compiled ok
# - mod-users in ../mod-users, compiled ok
# - mod-login in ../mod-login, compiled ok
# - mod-authtoken in ../mod-authtoken, compiled ok
# - mod-notify in ../mod-notify, compiled ok
# - curl installed
# Parameters
OKAPIURL="http://localhost:9130"
OKAPILOG="-Dloglevel=DEBUG" # comment out if you don't want debug logs
CURL="curl -w\n -D - "
# Most often used headers
PERM="-HX-Okapi-Permissions:notes.domain.users,notes.domain.items,notes.all,users.all"
TEN="-HX-Okapi-Tenant:testlib22"
JSON="-HContent-type:application/json"
USER="-HX-Okapi-User-Id:99999999-9999-4999-9999-999999999999"
# Check we have the fat jar
if [ ! -f target/mod-notes-fat.jar ]
then
echo No fat jar found, no point in trying to run
exit 1
fi
# Helper function to load, deploy, and enable a module
# Now that all modules have a good deploymentDescriptor, this is only used
# with one argument, the name of the module.
function mod {
MODNAME=$1 # name of the module, when enabling it, "mod-users"
MD=${2:-../$MODNAME/target/ModuleDescriptor.json}
DD=${3:-../$MODNAME/target/DeploymentDescriptor.json}
EM=${4:-$MODNAME}
if [ ! -f ../$MODNAME/target/*-fat.jar ]
then
echo "Module ../$MODNAME/target/*-fat.jar not found. No point in going on."
exit 1
fi
echo "###"
echo "### Loading $MODNAME"
echo "###"
if [ ! -f $MD ]
then
echo No ModuleDescriptor found for $MODNAME: $MD
exit 1
fi
if [ ! -f $DD ]
then
echo "No DeploymentDescriptor for $MODNAME: $DD"
exit 1
fi
$CURL -X POST -d@$MD $OKAPIURL/_/proxy/modules
echo
echo "Deploying $MODNAME"
$CURL -X POST \
-d@$DD \
$OKAPIURL/_/discovery/modules
echo
echo "Enabling $MODNAME"
$CURL -X POST \
-d"{\"id\":\"$EM\"}" \
$OKAPIURL/_/proxy/tenants/testlib22/modules
echo
}
# Start Okapi (in dev mode, no database)
OKAPIPATH="../okapi/okapi-core/target/okapi-core-fat.jar"
java $OKAPILOG -jar $OKAPIPATH dev > okapi.log 2>&1 &
PID=$!
echo Started okapi PID=$PID
sleep 2 # give it time to start
echo
# Test tenant
echo "Creating test tenant"
cat > /tmp/okapi.tenant.json <<END
{
"id": "testlib22",
"name": "Test Library",
"description": "Our Own Test Library"
}
END
$CURL -d@/tmp/okapi.tenant.json $OKAPIURL/_/proxy/tenants
echo
######################
# Start the modules
# Start mod-perms first, so we can get out TenantPermissions to work
# (Not strictly necessary any more, enabling mod-permissions will catch up
# with modules enabled earlier.)
mod mod-permissions
# Users
mod mod-users
#################
# mod-login is quite like mod-permissions
mod mod-login
#############
# The notify module is quite standard
mod mod-notify
###################
# mod-notes itself, at last
mod mod-notes
#################
# Set up our test user
echo Post our test user
cat > /tmp/user.json <<END
{ "id":"99999999-9999-4999-9999-999999999999",
"username":"testuser",
"active": true,
"personal": {
"lastName": "User",
"firstName": "Test"
}
}
END
$CURL $TEN $JSON \
-X POST \
-d@/tmp/user.json\
$OKAPIURL/users
echo
echo Post login user
cat >/tmp/loginuser.json << END
{ "userId":"99999999-9999-4999-9999-999999999999",
"password":"secretpassword" }
END
$CURL $TEN $JSON \
-X POST \
-d@/tmp/loginuser.json\
$OKAPIURL/authn/credentials
echo List all permsissions
$CURL $TEN \
$OKAPIURL/perms/permissions?query=permissionName=notes
# We use 'notes.domain.users' and 'notes.domain.items' in our tests.
# These should have been defined in mod-users (missing at the moment),
# and in mod-items (which we don't even have in this test).
# So we just inject them into the permission system, so we can grant
# them to our test user(s)
echo Define our notes.domain permissions
cat > /tmp/domainusers.json << END
{ "permissionName":"notes.domain.users",
"displayName":"notes for the users domain"}
END
$CURL $TEN $JSON \
-X POST \
-d@/tmp/domainusers.json\
$OKAPIURL/perms/permissions
cat > /tmp/domainitems.json << END
{ "permissionName":"notes.domain.items",
"displayName":"notes for the items domain"}
END
$CURL $TEN $JSON \
-X POST \
-d@/tmp/domainitems.json\
$OKAPIURL/perms/permissions
echo Post perm user
cat >/tmp/permuser.json << END
{ "userId":"99999999-9999-4999-9999-999999999999",
"permissions":[ "notes.allops", "notes.domain.users", "notes.domain.items",
"perms.all",
"users.all", "users.item.get",
"notify.all", "notify.collection.get" ] }
END
$CURL $TEN $JSON \
-X POST \
-d@/tmp/permuser.json\
$OKAPIURL/perms/users
###################
# mod-authtoken
# After this, the system is locked down
mod mod-authtoken
###################
# Actual login
# We can reuse the record from when we set the login user
echo
echo "Logging in"
$CURL $TEN $JSON \
-X POST \
-d@/tmp/loginuser.json\
$OKAPIURL/authn/login > /tmp/loginresp.json
cat /tmp/loginresp.json
TOK=-H`grep -i x-okapi-token /tmp/loginresp.json | sed 's/ //' `
echo Received a token $TOK
if [ "$TOK" == "-H" ]
then
echo "Could not log in, no point in continuing"
echo "Remember to check/kill running processes"
exit 1
fi
# Create the domain-specific permissions
# If the permissions are not defined, they don't get expanded, even if we have
# posted them in the perms user.
cat >/tmp/domain.user.perm.json <<END
{
"permissionName" : "notes.domain.users",
"displayName" : "Notes - allow access the 'users' domain",
"description" : "users domain"
}
END
$CURL $TOK $JSON -X POST \
-d@/tmp/domain.user.perm.json \
$OKAPIURL/perms/permissions
cat >/tmp/domain.user.perm.json <<END
{
"permissionName" : "notes.domain.items",
"displayName" : "Notes - allow access the 'items' domain",
"description" : "items domain"
}
END
$CURL $TOK $JSON -X POST \
-d@/tmp/domain.user.perm.json \
$OKAPIURL/perms/permissions
sleep 1
#############
# Various tests
echo Test 0: no permission
$CURL $TEN $OKAPIURL/notes
echo
echo Test 1: get empty list
$CURL $TOK $OKAPIURL/notes
echo
echo Test 2: Post one
$CURL $TOK $JSON \
-X POST -d '{"id":"44444444-4444-4444-a444-444444444444",
"link":"users/56789","text":"hello there","domain":"users"}' \
$OKAPIURL/notes
echo
echo Test 3: get a list with the new one
$CURL $TOK $OKAPIURL/notes
echo
echo Test 4: Post another one
$CURL $TOK $JSON $USER\
-X POST -d '{"link":"items/23456", "domain":"items",
"text":"hello thing @testuser"}' \
$OKAPIURL/notes
echo
echo Test 5: get a list with both
$CURL $TOK $OKAPIURL/notes
echo
echo Test 6: query the user note
$CURL $TOK $OKAPIURL/notes?query=link=users
echo
echo Test 7: query both
$CURL $TOK $OKAPIURL/notes?query=text=hello
echo
echo Test 8: query both
$CURL $TOK $OKAPIURL/notes?query='link=*56*'
echo
echo Test 9: Bad queries. Should fail with 422
$CURL $TOK $OKAPIURL/notes?query='BADQUERY'
echo
$CURL $TOK $OKAPIURL/notes?query='BADFIELD=foo'
echo
$CURL $TOK $OKAPIURL/notes?query='metadata.BADFIELD=foo'
echo
echo Test 10: limit
$CURL $TOK $OKAPIURL/notes?limit=1
echo
echo Test 11: sort
$CURL $TOK $OKAPIURL/notes?query=text=hello+sortby+link%2Fsort.ascending
echo
$CURL $TOK $OKAPIURL/notes?query=text=hello+sortby+link%2Fsort.descending
echo
echo Test 12: permissions - Expect a 403
# Adding a permission to the request doesn't help.
$CURL $TEN \
-H"X-Okapi-Permissions:notes.domain.users" \
$OKAPIURL/notes?query='link=*56*'
echo
echo Test 13: Post without permission - Expect a 401
$CURL $TEN \
$OKAPIURL/notes/44444444-4444-4444-4444-444444444444
echo
cat >/dev/null << SKIPTHIS
echo Test 14: Post without permission - Expect a 401
# We have domain permissions for 'things' and 'users', not for 'forbidden'
$CURL $TOK $JSON $USER\
-X POST -d '{"link":"forbidden/23456", "domain":"forbidden",
"text":"Forbidden @testuser"}' \
$OKAPIURL/notes
echo
echo Test 15: Show notifications - Should have exactly one
$CURL $TOK \
$OKAPIURL/notify
echo
# Dummy to disable some part of this script
# Copy the cat line anywhere above this. Leave one copy here!
cat >/dev/null << SKIPTHIS
SKIPTHIS
# Let it run
echo
echo "Hit enter to close"
read
# Clean up
echo "Cleaning up: Killing Okapi $PID"
kill $PID
sleep 1
# Wait for all the modules to stop working
ps | grep java && ( echo ... ; sleep 2 )
ps | grep java && ( echo ... ; sleep 2 )
ps | grep java && ( echo ... ; sleep 3 )
ps | grep java && ( echo ... ; sleep 3 )
ps | grep java && ( echo ... ; sleep 4 )
ps | grep java && ( echo ... ; sleep 4 )
ps | grep postgres && ( echo ... ; sleep 2 )
ps | grep postgres && ( echo ... ; sleep 3 )
ps | grep postgres && ( echo ... ; sleep 4 )
ps | grep postgres && ( echo ... ; sleep 5 )
ps | grep postgres && ( echo ... ; sleep 6 )
ps | grep postgres && ( echo ... ; sleep 7 )
ps | grep postgres && ( echo ... ; sleep 8 )
rm -rf /tmp/postgresql-embed*
ps | grep java | grep -v "grep java" && echo "OOPS - Still some java processes running"
ps | grep post | grep -v "grep post" && echo "OOPS - Still some postgres processes running"
echo bye