Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50671 views
1
###
2
TESTING of user queries specifically involving changefeeds - part 3 -- collaborators, ...
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 'test changefeed of all collaborators of a user -- ', ->
19
before(setup)
20
after(teardown)
21
22
accounts = projects = string_id = undefined
23
it 'creates 4 accounts', (done) ->
24
create_accounts 4, (err, x) -> accounts=x; done(err)
25
it 'creates 1 project', (done) ->
26
create_projects 1, accounts[0], (err, x) -> projects=x; done(err)
27
it 'creates another project', (done) ->
28
create_projects 1, accounts[3], (err, x) -> projects.push(x[0]); done(err)
29
30
it 'create changefeed of collaborators of account0', (done) ->
31
changefeed_id = misc.uuid()
32
db.user_query
33
account_id : accounts[0]
34
query : {collaborators:[{account_id:null, first_name:null, last_name:null, last_active:null, profile:null}]}
35
changes : changefeed_id
36
cb : changefeed_series([
37
(x, cb) ->
38
expect(x.collaborators.length).toEqual(1)
39
40
# add account1 to project
41
db.add_user_to_project(account_id:accounts[1], project_id:projects[0], cb:cb)
42
(x, cb) ->
43
expect(x).toEqual({action:'insert', new_val:{account_id:accounts[1], first_name:'Firstname1', last_name:'Lastname1'}})
44
45
# remove account1 from project
46
db.remove_collaborator_from_project(account_id:accounts[1], project_id:projects[0], cb:cb)
47
(x, cb) ->
48
expect(x).toEqual({action:'delete', old_val:{account_id:accounts[1]}})
49
50
# add account2 to project
51
db.add_user_to_project(account_id:accounts[2], project_id:projects[0], cb:cb)
52
(x, cb) ->
53
expect(x).toEqual({action:'insert', new_val:{account_id:accounts[2], first_name:'Firstname2', last_name:'Lastname2'}})
54
55
# add account1 to a different project that account0 isn't on -- doesn't fire changefeed
56
db.add_user_to_project account_id:accounts[1], project_id:projects[1], cb:->
57
# now add account0 to that project -- this fires changefeeds
58
db.add_user_to_project(account_id:accounts[0], project_id:projects[1], cb:cb)
59
(x, cb) ->
60
# we get accounts1 and accounts3 added to our collabs -- but don't know order
61
expect(x.action).toEqual('insert')
62
expect(x.new_val.account_id in [accounts[1], accounts[3]]).toEqual(true)
63
cb()
64
(x, cb) ->
65
expect(x.action).toEqual('insert')
66
expect(x.new_val.account_id in [accounts[1], accounts[3]]).toEqual(true)
67
68
# remove account0 from this other project again...
69
db.remove_user_from_project(account_id:accounts[0], project_id:projects[1], cb:cb)
70
(x, cb) ->
71
# ... which triggers a delete.
72
expect(x.action).toEqual('delete')
73
cb()
74
(x, cb) ->
75
# ... and another delete.
76
expect(x.action).toEqual('delete')
77
78
db.user_query_cancel_changefeed(id:changefeed_id, cb:cb)
79
(x, cb) ->
80
expect(x).toEqual({action:'close'})
81
82
cb()
83
], done)
84
85
86
87
describe 'test collaborators fields are updated -- ', ->
88
before(setup)
89
after(teardown)
90
91
accounts = projects = string_id = undefined
92
t0 = new Date()
93
it 'creates 2 accounts', (done) ->
94
create_accounts 2, (err, x) -> accounts=x; done(err)
95
it 'creates 1 project', (done) ->
96
create_projects 1, accounts[0], (err, x) -> projects=x; done(err)
97
it 'adds accounts1 to projects[0]', (done) ->
98
db.add_user_to_project(account_id:accounts[1], project_id:projects[0], cb:done)
99
100
it 'create changefeed of collaborators of account0', (done) ->
101
changefeed_id = misc.uuid()
102
db.user_query
103
account_id : accounts[0]
104
query : {collaborators:[{account_id:null, first_name:null, last_name:null, last_active:null, profile:null}]}
105
changes : changefeed_id
106
cb : changefeed_series([
107
(x, cb) ->
108
expect(x.collaborators.length).toEqual(2)
109
110
# changes first and last names
111
db._query
112
query : "UPDATE accounts"
113
set : {first_name:"X1", last_name:"Y1"}
114
where : {account_id:accounts[1]}
115
cb : cb
116
(x, cb) ->
117
expect(x).toEqual({action:'update', new_val:{account_id:accounts[1], first_name:'X1', last_name:'Y1'}})
118
119
# change last_active and profile
120
db._query
121
query : "UPDATE accounts"
122
set : {last_active:t0, profile:{foo:'bar'}}
123
where : {account_id:accounts[1]}
124
cb : cb
125
126
(x, cb) ->
127
expect(x).toEqual({action:'update', new_val:{account_id:accounts[1], first_name:'X1', last_name:'Y1', last_active:t0, profile:{foo:'bar'}}})
128
129
db.user_query_cancel_changefeed(id:changefeed_id, cb:cb)
130
(x, cb) ->
131
expect(x).toEqual({action:'close'})
132
133
cb()
134
], done)
135
136
137