This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.coffee
155 lines (107 loc) · 4.08 KB
/
test.coffee
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
should = require 'should'
clearance = require './index'
americano = require 'americano-cozy'
model = null
rule = null
secretkey = null
janekey = null
stevekey = null
describe 'Basics', ->
it "cozy-clearance exports the functions", ->
should.exist clearance.check
should.exist clearance.make
should.exist clearance.add
should.exist clearance.revoke
return
it "it works on americano-cozy models", (done) ->
Model = americano.getModel 'test',
name: String
clearance: (x) -> x
Model.create name: 'testdoc', (err, created) ->
model = created
done(err)
return
describe 'The clearance object', ->
it "cozy-clearance allows to make a clearance object", ->
arbitraryIdentifiers =
'email': '[email protected]'
'contactid': '3615'
rule = clearance.make model, 'r', arbitraryIdentifiers
return
it "and this clearance have identifiers, perm and a secret key", ->
rule.should.have.property 'perm', 'r'
rule.should.have.property 'email', '[email protected]'
rule.should.have.property 'contactid', '3615'
rule.should.have.property 'key'
return
describe 'Adding clearance to a model', ->
it "cozy-clearance allows to directly add clearance to an object", (done) ->
arbitraryIdentifiers =
'email': '[email protected]'
'contactid': '3615'
clearance.add model, 'r', arbitraryIdentifiers, (err, key) ->
should.exist key
secretkey = key
done err
return
it "the rules are stored as an array of rules", ->
model.clearance.should.have.property 'length', 1
rule = model.clearance[0]
rule.should.have.property 'perm', 'r'
rule.should.have.property 'email', '[email protected]'
rule.should.have.property 'contactid', '3615'
rule.should.have.property 'key'
return
it "so we can add multiple clearances", (done) ->
jane = email: '[email protected]'
steve = email: '[email protected]'
clearance.add model, 'rw', jane, (err, key) ->
return done err if err
janekey = key
clearance.add model, 'w', steve, (err, key) ->
stevekey = key
model.clearance.should.have.length 3
done err
return
describe 'Checking clearances', ->
it "cozy-clearance allows to check clearance for a request", ->
req = query: key: secretkey
clearance.check model, 'r', req, (err, rule) ->
rule.should.be.ok
rule.should.have.property 'perm', 'r'
rule.should.have.property 'email', '[email protected]'
rule.should.have.property 'key'
return
it "if the key is wrong, rule will be false", ->
req = query: key: "not a key"
clearance.check model, 'r', req, (err, rule) ->
rule.should.not.be.ok
return
it "if the perm is wrong, rule will be false", ->
req = query: key: secretkey
clearance.check model, 'w', req, (err, rule) ->
rule.should.not.be.ok
return
it "retrieve correct rule for a given key", (done) ->
req = query: key: janekey
clearance.check model, 'r', req, (err, rule) ->
rule.should.have.property 'email', '[email protected]'
done err
return
it "'rw' perm allows both read and write", (done) ->
req = query: key: janekey
clearance.check model, 'w', req, (err, rule) ->
rule.should.have.property 'email', '[email protected]'
done err
return
describe 'Revoking a clearance', ->
it "cozy-clearance allows to revoke a rule", ->
clearance.revoke model, email: '[email protected]', (err) ->
done err
return
it "then I can't use this key anymore", (done) ->
req = query: key: janekey
clearance.check model, 'r', req, (err, rule) ->
rule.should.be.false
done err
return