Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50664 views
1
###
2
TESTING of user queries specifically involving changefeeds - part 2 -- projects, ....?
3
4
COPYRIGHT : (c) 2017 SageMath, Inc.
5
LICENSE : AGPLv3
6
###
7
8
async = require('async')
9
expect = require('expect')
10
11
pgtest = require('./pgtest')
12
db = undefined
13
setup = (cb) -> (pgtest.setup (err) -> db=pgtest.db; cb(err))
14
teardown = pgtest.teardown
15
{create_accounts, create_projects, changefeed_series} = pgtest
16
misc = require('smc-util/misc')
17
18
describe 'very basic test of projects table', ->
19
before(setup)
20
after(teardown)
21
22
it 'creates account, project feed, a project, and see it appear', (done) ->
23
changefeed_id = misc.uuid()
24
accounts = undefined
25
projects = []
26
async.series([
27
(cb) ->
28
create_accounts 1, (err, x) -> accounts=x; cb(err)
29
(cb) ->
30
db.user_query
31
account_id : accounts[0]
32
query : {projects:[{project_id:null, title:null, users:null}]}
33
changes : changefeed_id
34
cb : changefeed_series([
35
(x, cb) ->
36
expect(x.projects.length).toEqual(0)
37
create_projects 1, accounts[0], (err, v) ->
38
projects.push(v[0])
39
cb(err)
40
(x, cb) ->
41
expect(x).toEqual({ action: 'insert', new_val: { project_id: projects[0], title: 'Project 0', users:{"#{accounts[0]}":{group:"owner"}} } })
42
43
# Test removing user from the project
44
db.remove_user_from_project(account_id:accounts[0], project_id:projects[0], cb:cb)
45
(x, cb) ->
46
expect(x).toEqual({ action: 'update', new_val: { project_id: projects[0], title: 'Project 0', users: {} } })
47
cb()
48
(x, cb) ->
49
expect(x).toEqual({ action: 'delete', old_val: { project_id: projects[0] } })
50
51
# Test adding user back to the project
52
db.add_user_to_project(account_id:accounts[0], project_id:projects[0], cb:cb)
53
(x, cb) ->
54
expect(x).toEqual({ action: 'insert', new_val: { project_id: projects[0], title: 'Project 0', users:{"#{accounts[0]}":{group:"collaborator"}} } })
55
56
# create another project
57
create_projects 1, accounts[0], (err, v) ->
58
projects.push(v[0])
59
cb(err)
60
(x, cb) ->
61
expect(x).toEqual({ action: 'insert', new_val: { project_id: projects[1], title: 'Project 0', users:{"#{accounts[0]}":{group:"owner"}} } })
62
cb()
63
# Test actually deleting project completely from database
64
db._query
65
query : "DELETE FROM projects"
66
where : {"project_id = $::UUID":projects[1]}
67
cb : cb
68
(x, cb) ->
69
expect(x).toEqual({ action: 'delete', old_val: { project_id: projects[1] } })
70
db.user_query_cancel_changefeed(id:changefeed_id, cb:cb)
71
(x, cb) ->
72
expect(x).toEqual({action:'close'})
73
cb()
74
], cb)
75
], done)
76
77
describe 'create multiple projects with multiple collaborators', ->
78
before(setup)
79
after(teardown)
80
81
#log = console.log
82
log = ->
83
it 'create 3 accounts and several projects, and see them appear in one projects feed properly', (done) ->
84
accounts = undefined
85
projects = []
86
changefeed_id = misc.uuid()
87
async.series([
88
(cb) ->
89
create_accounts 3, (err, x) ->
90
accounts=x; cb(err)
91
(cb) ->
92
db.user_query
93
account_id : accounts[0]
94
query : {projects:[{project_id:null, users:null}]}
95
changes : changefeed_id
96
cb : changefeed_series([
97
(x, cb) ->
98
expect(x.projects.length).toEqual(0)
99
100
log 'create first project'
101
create_projects 1, accounts[0], (err, v) ->
102
projects.push(v[0])
103
cb(err)
104
(x, cb) ->
105
expect(x).toEqual({ action: 'insert', new_val: { project_id: projects[0], users:{"#{accounts[0]}":{group:"owner"}} } })
106
log 'create another project'
107
create_projects 1, accounts[0], (err, v) ->
108
projects.push(v[0])
109
cb(err)
110
(x, cb) ->
111
expect(x).toEqual({ action: 'insert', new_val: { project_id: projects[1], users:{"#{accounts[0]}":{group:"owner"}} } })
112
113
log 'create a project that will get ignored by the feed...'
114
create_projects 1, accounts[1], (err, v) ->
115
if err
116
cb(err); return
117
projects.push(v[0])
118
log '... until we add the first user to it, in which case....'
119
db.add_user_to_project(project_id:v[0], account_id:accounts[0], cb:cb)
120
(x, cb) ->
121
log '... it appears!'
122
expect(x).toEqual({ action: 'insert', new_val: { project_id: projects[2], users:{"#{accounts[0]}":{group:"collaborator"}, "#{accounts[1]}":{group:"owner"}} } })
123
124
log 'Now add another collaborator'
125
db.add_user_to_project(project_id:projects[2], account_id:accounts[2], cb:cb)
126
(x, cb) ->
127
expect(x).toEqual({ action: 'update', new_val: { project_id: projects[2], users:{"#{accounts[0]}":{group:"collaborator"}, "#{accounts[1]}":{group:"owner"}, "#{accounts[2]}":{group:"collaborator"}} } })
128
129
log 'Now take first user back off'
130
db.remove_user_from_project(project_id:projects[2], account_id:accounts[0], cb:cb)
131
(x, cb) ->
132
expect(x).toEqual({ action: 'update', new_val: { project_id: projects[2], users:{"#{accounts[1]}":{group:"owner"}, "#{accounts[2]}":{group:"collaborator"}} } })
133
cb()
134
(x, cb) ->
135
expect(x).toEqual({ action: 'delete', old_val: { project_id: projects[2] }})
136
137
log 'cancel feed'
138
db.user_query_cancel_changefeed(id:changefeed_id, cb:cb)
139
(x, cb) ->
140
expect(x).toEqual({action:'close'})
141
cb()
142
], cb)
143
], done)
144
145
describe 'changefeed on a single project', ->
146
before(setup)
147
after(teardown)
148
149
it 'make 2 projects, feed on single, remove and add user', (done) ->
150
changefeed_id = misc.uuid()
151
accounts = projects = undefined
152
async.series([
153
(cb) ->
154
create_accounts 1, (err, x) -> accounts=x; cb(err)
155
(cb) ->
156
# make 2 projects; one will be comletely ignored
157
create_projects 2, accounts[0], (err, v) ->
158
projects = v
159
cb(err)
160
(cb) ->
161
db.user_query
162
account_id : accounts[0]
163
query : {projects:[{project_id:projects[0], description:null}]}
164
changes : changefeed_id
165
cb : changefeed_series([
166
(x, cb) ->
167
expect(x.projects.length).toEqual(1)
168
169
db.remove_user_from_project(project_id:projects[0], account_id:accounts[0], cb:cb)
170
(x, cb) ->
171
expect(x).toEqual({ action: 'delete', old_val: { project_id: projects[0]} })
172
173
db.add_user_to_project(project_id:projects[0], account_id:accounts[0], cb:cb)
174
(x, cb) ->
175
expect(x).toEqual({ action: 'insert', new_val: { project_id: projects[0], description: "Description 0"} })
176
db.user_query_cancel_changefeed(id:changefeed_id, cb:cb)
177
(x, cb) ->
178
expect(x).toEqual({action:'close'})
179
cb()
180
], cb)
181
], done)
182
183
describe 'changefeed testing all projects fields', ->
184
before(setup)
185
after(teardown)
186
187
it 'make 2 projects, feed, and edit all fields', (done) ->
188
changefeed_id = misc.uuid()
189
accounts = projects = undefined
190
obj0 = undefined
191
last_edited = undefined
192
user_query = (opts) ->
193
opts.account_id = accounts[0]
194
db.user_query(opts)
195
async.series([
196
(cb) ->
197
create_accounts 1, (err, x) -> accounts=x; cb(err)
198
(cb) ->
199
# make 2 projects
200
create_projects 2, accounts[0], (err, v) ->
201
projects = v
202
cb(err)
203
(cb) ->
204
user_query
205
query : {projects:[{ project_id: null, title: null, description: null, users: null, invite: null, invite_requests:null, deleted: null, host: null, settings: null, status: null, state: null, last_edited: null, last_active: null, action_request: null, course: null}]}
206
changes: changefeed_id
207
cb : changefeed_series([
208
(x, cb) ->
209
expect(x.projects.length).toEqual(2)
210
for p in x.projects
211
if p.project_id == projects[0]
212
obj0 = p
213
214
user_query
215
query : {projects:{project_id:projects[0], title:"Foo", description:"bar"}}
216
cb : cb
217
(x, cb) ->
218
obj0.title = 'Foo'
219
obj0.description = 'bar'
220
expect(x).toEqual( { action: 'update', new_val: obj0 })
221
222
user_query
223
query : {projects:{project_id:projects[0], deleted:true}}
224
cb : cb
225
(x, cb) ->
226
obj0.deleted = true
227
expect(x).toEqual( { action: 'update', new_val: obj0 })
228
229
obj0.action_request = {action:'test', started:new Date()}
230
user_query
231
query : {projects:{project_id:projects[0], action_request:obj0.action_request}}
232
cb : cb
233
(x, cb) ->
234
expect(x).toEqual( { action: 'update', new_val:obj0 })
235
236
obj0.last_edited = new Date()
237
db._query
238
query : "UPDATE projects"
239
set : {last_edited : obj0.last_edited}
240
where : {project_id : projects[0]}
241
cb : cb
242
(x, cb) ->
243
expect(x).toEqual( { action: 'update', new_val:obj0 })
244
245
set =
246
invite : {a:'map'}
247
invite_requests : {b:'map2'}
248
host : {host:'compute0-us'}
249
status : {c:'map3'}
250
state : {d:'map4'}
251
last_active : {"#{accounts[0]}":new Date()}
252
course : {project_id:obj0.project_id}
253
misc.merge(obj0, set)
254
db._query
255
query : "UPDATE projects"
256
set : set
257
where : {project_id : projects[0]}
258
cb : cb
259
(x, cb) ->
260
expect(x).toEqual( { action: 'update', new_val:obj0 })
261
262
db.user_query_cancel_changefeed(id:changefeed_id, cb:cb)
263
(x, cb) ->
264
expect(x).toEqual({action:'close'})
265
cb()
266
], cb)
267
], done)
268
269
describe 'testing a changefeed from a project (instead of account)', ->
270
before(setup)
271
after(teardown)
272
273
it 'makes a projects, has project get a feed and see changes', (done) ->
274
changefeed_id = misc.uuid()
275
accounts = projects = obj = undefined
276
async.series([
277
(cb) ->
278
create_accounts 1, (err, x) -> accounts=x; cb(err)
279
(cb) ->
280
create_projects 1, accounts[0], ((err, v) -> projects = v; cb(err))
281
(cb) ->
282
db.user_query
283
project_id : projects[0]
284
query : {projects:[{project_id:projects[0], title:null, description:null}]}
285
changes : changefeed_id
286
cb : changefeed_series([
287
(x, cb) ->
288
obj = { description: 'Description 0', project_id: projects[0], title: 'Project 0' }
289
expect(x.projects).toEqual([obj])
290
291
obj.title = 'Title'; obj.description = 'Description'
292
db.user_query
293
project_id : projects[0]
294
query : {projects:obj}
295
cb : cb
296
(x, cb) ->
297
expect(x).toEqual({action:'update', new_val:obj})
298
299
db.user_query_cancel_changefeed(id:changefeed_id, cb:cb)
300
(x, cb) ->
301
expect(x).toEqual({action:'close'})
302
303
cb()
304
], cb)
305
], done)
306
307
describe 'test changefeed admin-only access to project', ->
308
before(setup)
309
after(teardown)
310
311
accounts = project_id = undefined
312
313
it 'set things up', (done) ->
314
async.series([
315
(cb) ->
316
create_accounts 3, (err, x) -> accounts=x; cb(err)
317
(cb) ->
318
db.make_user_admin(account_id: accounts[0], cb:cb)
319
(cb) ->
320
create_projects 1, accounts[2], ((err, v) -> project_id = v[0]; cb(err))
321
], done)
322
323
it 'tests writing to project as admin user', (done) ->
324
db.user_query
325
account_id : accounts[0]
326
query : {projects:{project_id:project_id, title:"Better Title"}}
327
cb : done
328
329
it 'tests writing to project_admin as admin user', (done) ->
330
db.user_query
331
account_id : accounts[0]
332
query : {projects_admin:{project_id:project_id, title:"Better Title"}}
333
cb : (err) ->
334
expect(err).toEqual("user set queries not allowed for table 'projects_admin'")
335
done()
336
337
it 'tests project title changed properly (so reading as admin)', (done) ->
338
db.user_query
339
account_id : accounts[0]
340
query : {projects:{project_id:project_id, title:null}}
341
cb : (err, x) ->
342
expect(x).toEqual(projects:{project_id:project_id, title:"Better Title"})
343
done(err)
344
345
it 'tests writing to project as non-collab', (done) ->
346
db.user_query
347
account_id : accounts[1]
348
query : {projects:{project_id:project_id, title:"Even Better Title"}}
349
cb : (err) ->
350
expect(err).toEqual('user must be an admin')
351
done()
352
353
it 'tests reading from project as non-collab', (done) ->
354
db.user_query
355
account_id : accounts[1]
356
query : {projects:{project_id:project_id, title:null}}
357
cb : (err) ->
358
expect(err).toEqual('you do not have read access to this project')
359
done()
360
361
it 'tests writing to project as anonymous', (done) ->
362
db.user_query
363
query : {projects:{project_id:project_id, title:null}}
364
cb : (err) ->
365
expect(err).toEqual("anonymous get queries not allowed for table 'projects'")
366
done()
367
368
it 'tests admin changefeed on projects_admin table', (done) ->
369
changefeed_id = misc.uuid()
370
db.user_query
371
account_id : accounts[0] # our admin
372
query : {projects_admin:[{project_id:project_id, title:null}]}
373
changes : changefeed_id
374
cb : changefeed_series([
375
(x, cb) ->
376
expect(x.projects_admin).toEqual([{ project_id: project_id, title: 'Better Title' }])
377
378
db.user_query
379
account_id : accounts[0]
380
query : {projects:{project_id:project_id, title:"WAY Better Title"}}
381
cb : cb
382
(x, cb) ->
383
expect(x).toEqual({action:'update', new_val:{project_id:project_id, title:"WAY Better Title"}})
384
385
db.user_query_cancel_changefeed(id:changefeed_id, cb:cb)
386
(x, cb) ->
387
expect(x).toEqual({action:'close'})
388
389
cb()
390
], done)
391
392
it 'tests that user must be an admin to read from (or get changefeed on) projects_admin table', (done) ->
393
changefeed_id = misc.uuid()
394
db.user_query
395
account_id : accounts[1] # NOT admin
396
query : {projects_admin:[{project_id:project_id, title:null}]}
397
changes : changefeed_id
398
cb : (err) ->
399
expect(err).toEqual('user must be an admin')
400
done()
401
402
403
describe 'test public_projects table -- ', ->
404
before(setup)
405
after(teardown)
406
407
accounts = project_id = undefined
408
409
it 'set things up', (done) ->
410
async.series([
411
(cb) ->
412
create_accounts 2, (err, x) -> accounts=x; cb(err)
413
(cb) ->
414
create_projects 1, accounts[0], ((err, v) -> project_id = v[0]; cb(err))
415
], done)
416
417
it 'get error if project is not public, i.e., has no public paths', (done) ->
418
db.user_query
419
account_id : accounts[1]
420
query : {public_projects:{project_id:project_id, title:null, description:null}}
421
cb : (err, x) ->
422
expect(err).toEqual("project does not have any public paths")
423
done()
424
425
it 'adds a public paths', (done) ->
426
db.user_query
427
account_id : accounts[0]
428
query : {public_paths:{project_id:project_id, path:"foo.txt"}}
429
cb : done
430
431
it 'tests owner can now get title and description of project', (done) ->
432
db.user_query
433
account_id : accounts[0]
434
query : {public_projects:{project_id:project_id, title:null, description:null}}
435
cb : (err, x) ->
436
expect(x).toEqual(public_projects:{project_id:project_id, title:'Project 0', description:'Description 0'})
437
done(err)
438
439
it 'tests other user can get title and description of project', (done) ->
440
db.user_query
441
account_id : accounts[1]
442
query : {public_projects:{project_id:project_id, title:null, description:null}}
443
cb : (err, x) ->
444
expect(x).toEqual(public_projects:{project_id:project_id, title:'Project 0', description:'Description 0'})
445
done(err)
446
447
it 'tests anonymous user can get title and description of project', (done) ->
448
db.user_query
449
account_id : accounts[1]
450
query : {public_projects:{project_id:project_id, title:null, description:null}}
451
cb : (err, x) ->
452
expect(x).toEqual(public_projects:{project_id:project_id, title:'Project 0', description:'Description 0'})
453
done(err)
454
455
456
it 'tests that project_id must be specified', (done) ->
457
db.user_query
458
account_id : accounts[0]
459
query : {public_projects:{project_id:null, title:null, description:null}}
460
cb : (err, x) ->
461
expect(err).toEqual('must specify project_id')
462
done()
463
464
tests = (account_id, done) ->
465
id = misc.uuid()
466
db.user_query
467
account_id : account_id
468
query : {public_projects:[{project_id:project_id, title:null, description:null}]}
469
changes : id
470
cb : changefeed_series([
471
(x, cb) ->
472
expect(x).toEqual(public_projects:[{project_id:project_id, title:'Project 0', description:'Description 0'}])
473
db.user_query
474
account_id : accounts[0]
475
query : {projects:{project_id:project_id, title:'TITLE', description:'DESC'}}
476
cb : cb
477
(x, cb) ->
478
expect(x).toEqual({ action: 'update', new_val: { project_id: project_id, description: 'DESC', title: 'TITLE' } })
479
db.user_query
480
account_id : accounts[0]
481
query : {projects:{project_id:project_id, title:'Project 0', description:'Description 0'}}
482
cb : cb
483
(x, cb) ->
484
db.user_query_cancel_changefeed(id:id, cb:cb)
485
(x, cb) ->
486
expect(x).toEqual({action:'close'})
487
cb()
488
], done)
489
490
it 'tests non-anonymous user on project can get a changefeed on public project', (done) ->
491
tests(accounts[0], done)
492
493
it 'tests non-anonymous NON-user on project can get a changefeed on public project', (done) ->
494
tests(accounts[1], done)
495
496
it 'tests anonymous can get a changefeed on public project', (done) ->
497
tests(undefined, done)
498
499
500
describe 'test public_paths table -- ', ->
501
before(setup)
502
after(teardown)
503
504
accounts = projects = undefined
505
506
it 'set things up', (done) ->
507
async.series([
508
(cb) ->
509
create_accounts 3, (err, x) -> accounts=x; cb(err)
510
(cb) ->
511
db.make_user_admin(account_id: accounts[2], cb:cb)
512
(cb) ->
513
create_projects 2, accounts[0], ((err, v) -> projects = v; cb(err))
514
], done)
515
516
it 'adds a public path to a project', (done) ->
517
db.user_query
518
account_id : accounts[0]
519
query : {public_paths:{project_id:projects[0], path:"foo.txt", description:"foo"}}
520
cb : done
521
522
it 'adds a public path to a project not on as admin', (done) ->
523
db.user_query
524
account_id : accounts[2]
525
query : {public_paths:{project_id:projects[0], path:"bar.txt", description:'bar', disabled:true}}
526
cb : done
527
528
it 'fail to add a public path to a project user is not on', (done) ->
529
db.user_query
530
account_id : accounts[1]
531
query : {public_paths:{project_id:projects[0], path:"bar2.txt"}}
532
cb : (err) ->
533
expect(err).toEqual('user must be an admin')
534
done()
535
536
it 'fail to add a public path when not logged in', (done) ->
537
db.user_query
538
query : {public_paths:{project_id:projects[0], path:'foo2.txt'}}
539
cb : (err) ->
540
expect(err).toEqual('no anonymous set queries')
541
done()
542
543
read_public_paths = (done) ->
544
f = (account_id, cb) ->
545
db.user_query
546
account_id : account_id
547
query : {public_paths:[{project_id:projects[0], path:null, description:null, disabled:null}]}
548
options : [{order_by:'path'}]
549
cb : (err, x) ->
550
expect(x).toEqual({ public_paths: [ { description: 'bar', disabled: true, \
551
path: 'bar.txt', project_id: projects[0] }, { description: 'foo', \
552
path: 'foo.txt', project_id: projects[0] } ] })
553
cb(err)
554
async.map([accounts[0], accounts[1], undefined], f, done)
555
556
it 'reads public paths as owner, non-collab, and anon', (done) ->
557
read_public_paths(done)
558
559
it 'verifies that changefeed required id field (the primary key)', (done) ->
560
db.user_query
561
query : {public_paths:[{project_id:projects[0], path:null}]}
562
changes : misc.uuid()
563
cb : (err) =>
564
expect(err).toEqual("changefeed MUST include primary key (='id') in query")
565
done()
566
567
changefeed_pub_paths = (done) ->
568
f = (account_id, cb) ->
569
changefeed_id = misc.uuid()
570
v = undefined
571
db.user_query
572
account_id : account_id
573
query : {public_paths:[{id:null, project_id:projects[0], path:null, description:null, disabled:null}]}
574
options : [order_by:'path']
575
changes : changefeed_id
576
cb : changefeed_series([
577
(x, cb) ->
578
v = x.public_paths
579
expect(v.length).toEqual(2)
580
db.user_query
581
account_id : accounts[0]
582
query : {public_paths:{project_id:projects[0], path:"foo.txt",\
583
description:"foo2", disabled:true}}
584
cb : cb
585
(x, cb) ->
586
expect(x).toEqual({ action: 'update', new_val: {id:v[1].id, project_id:projects[0], \
587
path:"foo.txt", description:"foo2", disabled:true} })
588
589
db.user_query
590
account_id : accounts[0]
591
query : {public_paths:{project_id:projects[0], path:"foo.txt", \
592
description:"foo", disabled:false}}
593
cb : cb
594
(x, cb) ->
595
expect(x).toEqual({ action: 'update', new_val: {id:v[1].id, project_id:projects[0], path:"foo.txt", \
596
description:"foo", disabled:false} })
597
598
db.user_query_cancel_changefeed(id:changefeed_id, cb:cb)
599
(x, cb) ->
600
expect(x).toEqual({action:'close'})
601
cb()
602
], cb)
603
async.mapSeries([accounts[0], accounts[1], undefined], f, done)
604
605
it 'makes a changefeed and verifies modifying existing entry works', (done) ->
606
changefeed_pub_paths(done)
607
608
describe 'test site_settings table -- ', ->
609
before(setup)
610
after(teardown)
611
612
accounts = undefined
613
614
it 'make an admin and non-admin account', (done) ->
615
async.series([
616
(cb) ->
617
create_accounts 2, (err, x) -> accounts=x; cb(err)
618
(cb) ->
619
db.make_user_admin(account_id: accounts[0], cb:cb)
620
], done)
621
622
it "check writing to wrong field gives an error", (done) ->
623
db.user_query
624
account_id : accounts[0]
625
query : {site_settings:{site_name:'Hacker Site!'}}
626
cb : (err) ->
627
expect(err).toEqual("error setting 'name' -- Error: setting name='undefined' not allowed")
628
done()
629
630
it "check writing to not allowed row", (done) ->
631
db.user_query
632
account_id : accounts[0]
633
query : {site_settings:{name:'foobar', value:'stuff'}}
634
cb : (err) ->
635
expect(err).toEqual("error setting 'name' -- Error: setting name='foobar' not allowed")
636
done()
637
638
it "check anon can't write", (done) ->
639
db.user_query
640
query : {site_settings:{name:'site_name', value:'Hacker Site!'}}
641
cb : (err) ->
642
expect(err).toEqual("no anonymous set queries")
643
done()
644
645
it "check anon can't read", (done) ->
646
db.user_query
647
query : {site_settings:{name:'site_name', value:null}}
648
cb : (err) ->
649
expect(err).toEqual("anonymous get queries not allowed for table 'site_settings'")
650
done()
651
652
it "check non-admin can't write", (done) ->
653
db.user_query
654
account_id : accounts[1]
655
query : {site_settings:{name:'site_name', value:'Hacker Site!'}}
656
cb : (err) ->
657
expect(err).toEqual("user must be an admin")
658
done()
659
660
it "check non-admin can't read", (done) ->
661
db.user_query
662
account_id : accounts[1]
663
query : {site_settings:{name:'site_name', value:null}}
664
cb : (err) ->
665
expect(err).toEqual("user must be an admin")
666
done()
667
668
it "check admin can write", (done) ->
669
db.user_query
670
account_id : accounts[0]
671
query : {site_settings:{name:'site_name', value:'Hacker Site!'}}
672
cb : done
673
674
it "check admin can read", (done) ->
675
db.user_query
676
account_id : accounts[0]
677
query : {site_settings:{name:'site_name', value:null}}
678
cb : (err, x) ->
679
expect(x).toEqual({ site_settings: { name: 'site_name', value: 'Hacker Site!' } } )
680
done()
681
682
it 'create admin changefeed and write some things to it', (done) ->
683
id = misc.uuid()
684
user_query = (query, cb) ->
685
db.user_query(account_id:accounts[0], query:{site_settings:query}, cb:cb)
686
db.user_query
687
account_id : accounts[0]
688
query : {site_settings:[{name:null, value:null}]}
689
changes : id
690
cb : changefeed_series([
691
(x, cb) ->
692
expect(x).toEqual(site_settings:[{name:'site_name', value:'Hacker Site!'}])
693
694
user_query({name:'site_name', value:'CoCalc'}, cb)
695
(x, cb) ->
696
expect(x).toEqual({ action: 'update', new_val: {name:'site_name', value:'CoCalc'} })
697
698
user_query({name:'site_description', value:'The collaborative site'}, cb)
699
(x, cb) ->
700
expect(x).toEqual({ action: 'insert', new_val: {name:'site_description', value:'The collaborative site'} })
701
702
user_query({name:'terms_of_service', value:'Do nice things'}, cb)
703
(x, cb) ->
704
expect(x).toEqual({ action: 'insert', new_val: {name:'terms_of_service', value:'Do nice things'} })
705
706
user_query({name:'account_creation_email_instructions', value:'Create account'}, cb)
707
(x, cb) ->
708
expect(x).toEqual({ action: 'insert', new_val: {name:'account_creation_email_instructions', value:'Create account'} })
709
user_query({name:'help_email', value:'[email protected]'}, cb)
710
(x, cb) ->
711
expect(x).toEqual({ action: 'insert', new_val: {name:'help_email', value:'[email protected]'} })
712
713
user_query({name:'commercial', value:'yes'}, cb)
714
(x, cb) ->
715
expect(x).toEqual({ action: 'insert', new_val: {name:'commercial', value:'yes'} })
716
717
db.user_query_cancel_changefeed(id:id, cb:cb)
718
(x, cb) ->
719
expect(x).toEqual({action:'close'})
720
cb()
721
], done)
722
723
724
describe 'test stats changefeed: ', ->
725
before(setup)
726
after(teardown)
727
728
obj = {id: null, time: null, accounts: null, accounts_created: null, \
729
projects: null, projects_created: null, projects_edited: null, hub_servers:null}
730
731
account_id = undefined
732
it 'make an account', (done) ->
733
async.series([
734
(cb) ->
735
create_accounts 1, (err, x) -> account_id=x[0]; cb(err)
736
], done)
737
738
it 'query the stats table anonymously (get nothing, no error)', (done) ->
739
db.user_query
740
query : {stats:[obj]}
741
cb : (err, x) ->
742
expect(x).toEqual({ stats: [] })
743
done(err)
744
745
it 'query the stats table as user (get nothing, no error)', (done) ->
746
db.user_query
747
account_id : account_id
748
query : {stats:[obj]}
749
cb : (err, x) ->
750
expect(x).toEqual({ stats: [] })
751
done(err)
752
753
it 'insert some entries in the stats table', (done) ->
754
db.get_stats(cb:done)
755
756
it 'query the stats table as user and gets the one entry', (done) ->
757
db.user_query
758
account_id : account_id
759
query : {stats:[obj]}
760
cb : (err, x) ->
761
expect(x).toEqual({ stats: [ { accounts: 1, accounts_created: { '1d': 1, '1h': 1, '30d': 1, '7d': 1 }, hub_servers: [], id:x.stats[0].id, projects: 0, projects_created: { '1d': 0, '1h': 0, '30d': 0, '7d': 0 }, projects_edited: { '1d': 0, '1h': 0, '30d': 0, '5min': 0, '7d': 0 }, time:x.stats[0].time } ] })
762
done(err)
763
764
it 'query the stats table as anon and gets the one entry', (done) ->
765
db.user_query
766
query : {stats:[obj]}
767
cb : (err, x) ->
768
expect(x).toEqual({ stats: [ { accounts: 1, accounts_created: { '1d': 1, '1h': 1, '30d': 1, '7d': 1 }, hub_servers: [], id:x.stats[0].id, projects: 0, projects_created: { '1d': 0, '1h': 0, '30d': 0, '7d': 0 }, projects_edited: { '1d': 0, '1h': 0, '30d': 0, '5min': 0, '7d': 0 }, time:x.stats[0].time } ] })
769
done(err)
770
771
it 'creates some more accounts and projects and add another stats entry', (done) ->
772
async.series([
773
(cb) ->
774
create_accounts 100, 1, (err, x) -> account_id=x[0]; cb(err)
775
(cb) ->
776
create_projects 100, account_id, cb
777
(cb) ->
778
db.get_stats(cb:cb, ttl:-1)
779
(cb) ->
780
db.user_query
781
query : {stats:[obj]}
782
cb : (err, x) ->
783
expect(x.stats[0]).toEqual({ accounts: 101, accounts_created: { '1d': 101, '1h': 101, '30d': 101, '7d': 101 }, hub_servers: [], id:x.stats[0].id, projects: 100, projects_created: { '1d': 100, '1h': 100, '30d': 100, '7d': 100 }, projects_edited: { '1d': 100, '1h': 100, '30d': 100, '5min': 100, '7d': 100 }, time:x.stats[0].time })
784
cb(err)
785
], done)
786
787
it 'create anonymous changefeed on stats and see new entry appear', (done) ->
788
changefeed_id = misc.uuid()
789
remove_id = undefined
790
db.user_query
791
query : {stats:[obj]}
792
changes : changefeed_id
793
cb : changefeed_series([
794
(x, cb) ->
795
expect(x.stats.length).toEqual(2)
796
async.series([
797
(cb) ->
798
create_projects(10, account_id, cb)
799
(cb) ->
800
db.get_stats(ttl:0, ttl_db:0, ttl_dt:0, cb:cb)
801
], cb)
802
(x, cb) ->
803
expect(x).toEqual({ action: 'insert', new_val: { accounts: 101, accounts_created: { '1d': 101, '1h': 101, '30d': 101, '7d': 101 }, hub_servers: [], id:x.new_val.id, projects: 110, projects_created: { '1d': 110, '1h': 110, '30d': 110, '7d': 110 }, projects_edited: { '1d': 110, '1h': 110, '30d': 110, '5min': 110, '7d': 110 }, time: x.new_val.time } })
804
805
db._query
806
query : "UPDATE stats"
807
set : {projects:150}
808
where : {id:x.new_val.id}
809
cb : cb
810
811
(x, cb) ->
812
expect(x).toEqual({ action: 'update', new_val: { accounts: 101, accounts_created: { '1d': 101, '1h': 101, '30d': 101, '7d': 101 }, hub_servers: [], id:x.new_val.id, projects: 150, projects_created: { '1d': 110, '1h': 110, '30d': 110, '7d': 110 }, projects_edited: { '1d': 110, '1h': 110, '30d': 110, '5min': 110, '7d': 110 }, time: x.new_val.time } })
813
814
# remove something from the changefeed by editing its timestamp to be old
815
remove_id = x.new_val.id
816
db._query
817
query : "UPDATE stats"
818
set : {time:misc.hours_ago(2)}
819
where : {id:remove_id}
820
cb : cb
821
(x, cb) ->
822
expect(x.action).toEqual('delete')
823
expect(x.old_val.id).toEqual(remove_id)
824
expect(x.new_val).toEqual(undefined)
825
826
db.user_query_cancel_changefeed(id:changefeed_id, cb:cb)
827
(x, cb) ->
828
expect(x).toEqual({action:'close'})
829
830
cb()
831
], done)
832
833
834
describe 'test system_notifications ', ->
835
before(setup)
836
after(teardown)
837
838
obj = {id:null, time:null, text:null, priority:null, done:null}
839
840
accounts= undefined
841
it 'make two accounts', (done) ->
842
async.series([
843
(cb) ->
844
create_accounts 2, (err, x) -> accounts=x; cb(err)
845
(cb) ->
846
db.make_user_admin(account_id: accounts[0], cb:cb)
847
], done)
848
849
it 'reads empty table as admin, non-admin, and anon', (done) ->
850
f = (account_id, cb) ->
851
db.user_query
852
account_id : account_id
853
query : {system_notifications: [obj]}
854
cb : (err, x) ->
855
expect(x).toEqual(system_notifications: [])
856
cb(err)
857
async.map([accounts[0], accounts[1], undefined], f, done)
858
859
obj0 = {id:misc.uuid(), time:new Date(), text:"watch out!", done:true}
860
it 'tries to write as admin, non-admin and anon to system_notifications table', (done) ->
861
f = (x, cb) ->
862
db.user_query
863
account_id : x.account_id
864
query : {system_notifications: obj0}
865
cb : (err, result) ->
866
expect(err).toEqual(x.err)
867
cb()
868
async.map([{account_id:accounts[0]}, {account_id:accounts[1], err:'user must be an admin'}, {err:'no anonymous set queries'}], f, done)
869
870
it 'reads non-empty table as admin, non-admin, and anon', (done) ->
871
# fill in the defaults from the schema
872
obj0.priority = 'low'
873
f = (account_id, cb) ->
874
db.user_query
875
account_id : account_id
876
query : {system_notifications: [obj]}
877
cb : (err, x) ->
878
expect(x).toEqual(system_notifications: [obj0])
879
cb(err)
880
async.map([accounts[0], accounts[1], undefined], f, done)
881
882
883
it 'create changefeed, insert entry, and see it appear (as admin, non-admin, and anon)', (done) ->
884
f = (account_id, cb) ->
885
obj1 = {id:misc.uuid(), time:new Date(), text:'crazy alert!', priority:'medium', done:false}
886
changefeed_id = misc.uuid()
887
db.user_query
888
account_id : account_id
889
query : {system_notifications: [obj]}
890
changes : changefeed_id
891
cb : changefeed_series([
892
(x, cb) ->
893
expect(x.system_notifications.length).toEqual(1)
894
895
db.user_query
896
account_id : accounts[0] # as admin
897
query : {system_notifications: [obj1]}
898
cb : cb
899
(x, cb) ->
900
expect(x).toEqual( { action: 'insert', new_val: obj1 })
901
902
obj1.done = true
903
db.user_query
904
account_id : accounts[0] # as admin
905
query : {system_notifications: [obj1]}
906
cb : cb
907
(x, cb) ->
908
expect(x).toEqual( { action: 'update', new_val: obj1 })
909
910
db.user_query_cancel_changefeed(id:changefeed_id, cb:cb)
911
(x, cb) ->
912
expect(x).toEqual({action:'close'})
913
cb()
914
], done)
915
916
async.mapSeries([accounts[0], accounts[1], undefined], f, done)
917
918
919
920