Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 42a22f7

Browse files
committed
test: access api tests
1 parent 28c4507 commit 42a22f7

File tree

1 file changed

+269
-68
lines changed

1 file changed

+269
-68
lines changed

test/access.js

Lines changed: 269 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,91 +6,292 @@ var client = common.freshClient()
66

77
function nop () {}
88

9-
var URI = 'http://localhost:1337/-/package/underscore/access'
10-
var TOKEN = 'foo'
11-
var AUTH = {
12-
token: TOKEN
13-
}
14-
var LEVEL = 'public'
9+
var URI = 'http://localhost:1337'
1510
var PARAMS = {
16-
level: LEVEL,
17-
auth: AUTH
11+
auth: { token: 'foo' },
12+
scope: 'myorg',
13+
team: 'myteam',
14+
package: '@foo/bar',
15+
permissions: 'read-write'
1816
}
1917

20-
test('access call contract', function (t) {
21-
t.throws(function () {
22-
client.access(undefined, AUTH, nop)
23-
}, 'requires a URI')
24-
25-
t.throws(function () {
26-
client.access([], PARAMS, nop)
27-
}, 'requires URI to be a string')
28-
29-
t.throws(function () {
30-
client.access(URI, undefined, nop)
31-
}, 'requires params object')
32-
33-
t.throws(function () {
34-
client.access(URI, '', nop)
35-
}, 'params must be object')
36-
37-
t.throws(function () {
38-
client.access(URI, PARAMS, undefined)
39-
}, 'requires callback')
40-
41-
t.throws(function () {
42-
client.access(URI, PARAMS, 'callback')
43-
}, 'callback must be function')
44-
45-
t.throws(
46-
function () {
47-
var params = {
48-
auth: AUTH
49-
}
50-
client.access(URI, params, nop)
51-
},
52-
{ name: 'AssertionError', message: 'must pass level to access' },
53-
'access must include level'
54-
)
55-
56-
t.throws(
57-
function () {
58-
var params = {
59-
level: LEVEL
60-
}
61-
client.access(URI, params, nop)
62-
},
63-
{ name: 'AssertionError', message: 'must pass auth to access' },
64-
'access must include auth'
65-
)
18+
var commands = [
19+
'public', 'restricted', 'grant', 'revoke', 'ls-packages', 'ls-collaborators'
20+
]
6621

67-
t.end()
22+
test('access public', function (t) {
23+
server.expect('POST', '/-/package/%40foo%2Fbar/access', function (req, res) {
24+
t.equal(req.method, 'POST')
25+
onJsonReq(req, function (json) {
26+
t.deepEqual(json, { access: 'public' })
27+
res.statusCode = 200
28+
res.json({ accessChanged: true })
29+
})
30+
})
31+
var params = Object.create(PARAMS)
32+
params.package = '@foo/bar'
33+
client.access('public', URI, params, function (error, data) {
34+
t.ifError(error, 'no errors')
35+
t.ok(data.accessChanged, 'access level set')
36+
t.end()
37+
})
6838
})
6939

70-
test('set access level on a package', function (t) {
71-
server.expect('POST', '/-/package/underscore/access', function (req, res) {
40+
test('access restricted', function (t) {
41+
server.expect('POST', '/-/package/%40foo%2Fbar/access', function (req, res) {
7242
t.equal(req.method, 'POST')
73-
74-
var b = ''
75-
req.setEncoding('utf8')
76-
req.on('data', function (d) {
77-
b += d
43+
onJsonReq(req, function (json) {
44+
t.deepEqual(json, { access: 'restricted' })
45+
res.statusCode = 200
46+
res.json({ accessChanged: true })
7847
})
48+
})
49+
client.access('restricted', URI, PARAMS, function (error, data) {
50+
t.ifError(error, 'no errors')
51+
t.ok(data.accessChanged, 'access level set')
52+
t.end()
53+
})
54+
})
7955

80-
req.on('end', function () {
81-
var updated = JSON.parse(b)
82-
83-
t.deepEqual(updated, { access: 'public' })
84-
56+
test('access grant basic', function (t) {
57+
server.expect('PUT', '/-/team/myorg/myteam/package', function (req, res) {
58+
t.equal(req.method, 'PUT')
59+
onJsonReq(req, function (json) {
60+
t.deepEqual(json, {
61+
permissions: PARAMS.permissions,
62+
package: PARAMS.package
63+
})
8564
res.statusCode = 201
8665
res.json({ accessChanged: true })
8766
})
8867
})
68+
client.access('grant', URI, PARAMS, function (error, data) {
69+
t.ifError(error, 'no errors')
70+
t.ok(data.accessChanged, 'access level set')
71+
t.end()
72+
})
73+
})
8974

90-
client.access(URI, PARAMS, function (error, data) {
75+
test('access revoke basic', function (t) {
76+
server.expect('DELETE', '/-/team/myorg/myteam/package', function (req, res) {
77+
t.equal(req.method, 'DELETE')
78+
onJsonReq(req, function (json) {
79+
t.deepEqual(json, {
80+
package: PARAMS.package
81+
})
82+
res.statusCode = 200
83+
res.json({ accessChanged: true })
84+
})
85+
})
86+
client.access('revoke', URI, PARAMS, function (error, data) {
9187
t.ifError(error, 'no errors')
9288
t.ok(data.accessChanged, 'access level set')
89+
t.end()
90+
})
91+
})
92+
93+
test('ls-packages on team', function (t) {
94+
var serverPackages = {
95+
'@foo/bar': 'write',
96+
'@foo/util': 'read'
97+
}
98+
var clientPackages = {
99+
'@foo/bar': 'read-write',
100+
'@foo/util': 'read-only'
101+
}
102+
var uri = '/-/team/myorg/myteam/package?format=cli'
103+
server.expect('GET', uri, function (req, res) {
104+
t.equal(req.method, 'GET')
105+
res.statusCode = 200
106+
res.json(serverPackages)
107+
})
108+
client.access('ls-packages', URI, PARAMS, function (error, data) {
109+
t.ifError(error, 'no errors')
110+
t.same(data, clientPackages)
111+
t.end()
112+
})
113+
})
114+
115+
test('ls-packages on org', function (t) {
116+
var serverPackages = {
117+
'@foo/bar': 'write',
118+
'@foo/util': 'read'
119+
}
120+
var clientPackages = {
121+
'@foo/bar': 'read-write',
122+
'@foo/util': 'read-only'
123+
}
124+
var uri = '/-/org/myorg/package?format=cli'
125+
server.expect('GET', uri, function (req, res) {
126+
t.equal(req.method, 'GET')
127+
res.statusCode = 200
128+
res.json(serverPackages)
129+
})
130+
var params = Object.create(PARAMS)
131+
params.team = null
132+
client.access('ls-packages', URI, params, function (error, data) {
133+
t.ifError(error, 'no errors')
134+
t.same(data, clientPackages)
135+
t.end()
136+
})
137+
})
93138

139+
test('ls-packages on user', function (t) {
140+
var serverPackages = {
141+
'@foo/bar': 'write',
142+
'@foo/util': 'read'
143+
}
144+
var clientPackages = {
145+
'@foo/bar': 'read-write',
146+
'@foo/util': 'read-only'
147+
}
148+
var firstUri = '/-/org/myorg/package?format=cli'
149+
server.expect('GET', firstUri, function (req, res) {
150+
t.equal(req.method, 'GET')
151+
res.statusCode = 404
152+
res.json({error: 'not found'})
153+
})
154+
var secondUri = '/-/user/myorg/package?format=cli'
155+
server.expect('GET', secondUri, function (req, res) {
156+
t.equal(req.method, 'GET')
157+
res.statusCode = 200
158+
res.json(serverPackages)
159+
})
160+
var params = Object.create(PARAMS)
161+
params.team = null
162+
client.access('ls-packages', URI, params, function (error, data) {
163+
t.ifError(error, 'no errors')
164+
t.same(data, clientPackages)
94165
t.end()
95166
})
96167
})
168+
169+
test('ls-collaborators', function (t) {
170+
var serverCollaborators = {
171+
'myorg:myteam': 'write',
172+
'myorg:anotherteam': 'read'
173+
}
174+
var clientCollaborators = {
175+
'myorg:myteam': 'read-write',
176+
'myorg:anotherteam': 'read-only'
177+
}
178+
var uri = '/-/package/%40foo%2Fbar/collaborators?format=cli'
179+
server.expect('GET', uri, function (req, res) {
180+
t.equal(req.method, 'GET')
181+
res.statusCode = 200
182+
res.json(serverCollaborators)
183+
})
184+
client.access('ls-collaborators', URI, PARAMS, function (error, data) {
185+
t.ifError(error, 'no errors')
186+
t.same(data, clientCollaborators)
187+
t.end()
188+
})
189+
})
190+
191+
test('ls-collaborators w/ scope', function (t) {
192+
var serverCollaborators = {
193+
'myorg:myteam': 'write',
194+
'myorg:anotherteam': 'read'
195+
}
196+
var clientCollaborators = {
197+
'myorg:myteam': 'read-write',
198+
'myorg:anotherteam': 'read-only'
199+
}
200+
var uri = '/-/package/%40foo%2Fbar/collaborators?format=cli&user=zkat'
201+
server.expect('GET', uri, function (req, res) {
202+
t.equal(req.method, 'GET')
203+
res.statusCode = 200
204+
res.json(serverCollaborators)
205+
})
206+
var params = Object.create(PARAMS)
207+
params.user = 'zkat'
208+
client.access('ls-collaborators', URI, params, function (error, data) {
209+
t.ifError(error, 'no errors')
210+
t.same(data, clientCollaborators)
211+
t.end()
212+
})
213+
})
214+
215+
test('access command base validation', function (t) {
216+
t.throws(function () {
217+
client.access(undefined, URI, PARAMS, nop)
218+
}, 'command is required')
219+
t.throws(function () {
220+
client.access('whoops', URI, PARAMS, nop)
221+
}, 'command must be a valid subcommand')
222+
commands.forEach(function (cmd) {
223+
t.throws(function () {
224+
client.access(cmd, undefined, PARAMS, nop)
225+
}, 'registry URI is required')
226+
t.throws(function () {
227+
client.access(cmd, URI, undefined, nop)
228+
}, 'params is required')
229+
t.throws(function () {
230+
client.access(cmd, URI, '', nop)
231+
}, 'params must be an object')
232+
t.throws(function () {
233+
client.access(cmd, URI, {scope: 'o', team: 't'}, nop)
234+
}, 'auth is required')
235+
t.throws(function () {
236+
client.access(cmd, URI, {auth: 5, scope: 'o', team: 't'}, nop)
237+
}, 'auth must be an object')
238+
t.throws(function () {
239+
client.access(cmd, URI, PARAMS, {})
240+
}, 'callback must be a function')
241+
t.throws(function () {
242+
client.access(cmd, URI, PARAMS, undefined)
243+
}, 'callback is required')
244+
if (contains([
245+
'public', 'restricted', 'grant', 'revoke', 'ls-collaborators'
246+
], cmd)) {
247+
t.throws(function () {
248+
var params = Object.create(PARAMS)
249+
params.package = null
250+
client.access(cmd, URI, params, nop)
251+
}, 'package is required')
252+
t.throws(function () {
253+
var params = Object.create(PARAMS)
254+
params.package = 'underscore'
255+
client.access(cmd, URI, params, nop)
256+
}, 'only scopes packages are allowed')
257+
}
258+
if (contains(['grant', 'revoke', 'ls-packages'], cmd)) {
259+
t.throws(function () {
260+
var params = Object.create(PARAMS)
261+
params.scope = null
262+
client.access(cmd, URI, params, nop)
263+
}, 'scope is required')
264+
}
265+
if (contains(['grant', 'revoke'], cmd)) {
266+
t.throws(function () {
267+
var params = Object.create(PARAMS)
268+
params.team = null
269+
client.access(cmd, URI, params, nop)
270+
}, 'team is required')
271+
}
272+
if (cmd === 'grant') {
273+
t.throws(function () {
274+
var params = Object.create(PARAMS)
275+
params.permissions = null
276+
client.access(cmd, URI, params, nop)
277+
}, 'permissions are required')
278+
t.throws(function () {
279+
var params = Object.create(PARAMS)
280+
params.permissions = 'idkwhat'
281+
client.access(cmd, URI, params, nop)
282+
}, 'permissions must be either read-only or read-write')
283+
}
284+
})
285+
t.end()
286+
})
287+
288+
function onJsonReq (req, cb) {
289+
var buffer = ''
290+
req.setEncoding('utf8')
291+
req.on('data', function (data) { buffer += data })
292+
req.on('end', function () { cb(buffer ? JSON.parse(buffer) : undefined) })
293+
}
294+
295+
function contains (arr, item) {
296+
return arr.indexOf(item) !== -1
297+
}

0 commit comments

Comments
 (0)