1 var test = require('tap').test
3 var server = require('./lib/server.js')
4 var common = require('./lib/common.js')
5 var client = common.freshClient()
9 var URI = 'http://localhost:1337'
11 auth: { token: 'foo' },
15 permissions: 'read-write'
18 auth: { token: 'foo' },
22 permissions: 'read-write'
26 'public', 'restricted', 'grant', 'revoke', 'ls-packages', 'ls-collaborators'
29 test('access public', function (t) {
30 server.expect('POST', '/-/package/%40foo%2Fbar/access', function (req, res) {
31 t.equal(req.method, 'POST')
32 onJsonReq(req, function (json) {
33 t.deepEqual(json, { access: 'public' })
35 res.json({ accessChanged: true })
38 var params = Object.create(PARAMS)
39 params.package = '@foo/bar'
40 client.access('public', URI, params, function (error, data) {
41 t.ifError(error, 'no errors')
42 t.ok(data.accessChanged, 'access level set')
47 test('access restricted', function (t) {
48 server.expect('POST', '/-/package/%40foo%2Fbar/access', function (req, res) {
49 t.equal(req.method, 'POST')
50 onJsonReq(req, function (json) {
51 t.deepEqual(json, { access: 'restricted' })
53 res.json({ accessChanged: true })
56 client.access('restricted', URI, PARAMS, function (error, data) {
57 t.ifError(error, 'no errors')
58 t.ok(data.accessChanged, 'access level set')
63 test('access grant basic', function (t) {
64 server.expect('PUT', '/-/team/myorg/myteam/package', function (req, res) {
65 t.equal(req.method, 'PUT')
66 onJsonReq(req, function (json) {
68 permissions: PARAMS.permissions,
69 package: PARAMS.package
72 res.json({ accessChanged: true })
75 client.access('grant', URI, PARAMS, function (error, data) {
76 t.ifError(error, 'no errors')
77 t.ok(data.accessChanged, 'access level set')
82 test('access grant basic unscoped', function (t) {
83 server.expect('PUT', '/-/team/myorg/myteam/package', function (req, res) {
84 t.equal(req.method, 'PUT')
85 onJsonReq(req, function (json) {
87 permissions: UNSCOPED.permissions,
88 package: UNSCOPED.package
91 res.json({ accessChanged: true })
94 client.access('grant', URI, UNSCOPED, function (error, data) {
95 t.ifError(error, 'no errors')
96 t.ok(data.accessChanged, 'access level set')
101 test('access revoke basic', function (t) {
102 server.expect('DELETE', '/-/team/myorg/myteam/package', function (req, res) {
103 t.equal(req.method, 'DELETE')
104 onJsonReq(req, function (json) {
106 package: PARAMS.package
109 res.json({ accessChanged: true })
112 client.access('revoke', URI, PARAMS, function (error, data) {
113 t.ifError(error, 'no errors')
114 t.ok(data.accessChanged, 'access level set')
119 test('access revoke basic unscoped', function (t) {
120 server.expect('DELETE', '/-/team/myorg/myteam/package', function (req, res) {
121 t.equal(req.method, 'DELETE')
122 onJsonReq(req, function (json) {
124 package: UNSCOPED.package
127 res.json({ accessChanged: true })
130 client.access('revoke', URI, UNSCOPED, function (error, data) {
131 t.ifError(error, 'no errors')
132 t.ok(data.accessChanged, 'access level set')
137 test('ls-packages on team', function (t) {
138 var serverPackages = {
142 var clientPackages = {
143 '@foo/bar': 'read-write',
144 '@foo/util': 'read-only'
146 var uri = '/-/team/myorg/myteam/package?format=cli'
147 server.expect('GET', uri, function (req, res) {
148 t.equal(req.method, 'GET')
150 res.json(serverPackages)
152 client.access('ls-packages', URI, PARAMS, function (error, data) {
153 t.ifError(error, 'no errors')
154 t.same(data, clientPackages)
159 test('ls-packages on org', function (t) {
160 var serverPackages = {
164 var clientPackages = {
165 '@foo/bar': 'read-write',
166 '@foo/util': 'read-only'
168 var uri = '/-/org/myorg/package?format=cli'
169 server.expect('GET', uri, function (req, res) {
170 t.equal(req.method, 'GET')
172 res.json(serverPackages)
174 var params = Object.create(PARAMS)
176 client.access('ls-packages', URI, params, function (error, data) {
177 t.ifError(error, 'no errors')
178 t.same(data, clientPackages)
183 test('ls-packages on user', function (t) {
184 var serverPackages = {
188 var clientPackages = {
189 '@foo/bar': 'read-write',
190 '@foo/util': 'read-only'
192 var firstUri = '/-/org/myorg/package?format=cli'
193 server.expect('GET', firstUri, function (req, res) {
194 t.equal(req.method, 'GET')
196 res.json({error: 'not found'})
198 var secondUri = '/-/user/myorg/package?format=cli'
199 server.expect('GET', secondUri, function (req, res) {
200 t.equal(req.method, 'GET')
202 res.json(serverPackages)
204 var params = Object.create(PARAMS)
206 client.access('ls-packages', URI, params, function (error, data) {
207 t.ifError(error, 'no errors')
208 t.same(data, clientPackages)
213 test('ls-collaborators', function (t) {
214 var serverCollaborators = {
215 'myorg:myteam': 'write',
216 'myorg:anotherteam': 'read'
218 var clientCollaborators = {
219 'myorg:myteam': 'read-write',
220 'myorg:anotherteam': 'read-only'
222 var uri = '/-/package/%40foo%2Fbar/collaborators?format=cli'
223 server.expect('GET', uri, function (req, res) {
224 t.equal(req.method, 'GET')
226 res.json(serverCollaborators)
228 client.access('ls-collaborators', URI, PARAMS, function (error, data) {
229 t.ifError(error, 'no errors')
230 t.same(data, clientCollaborators)
235 test('ls-collaborators w/scope', function (t) {
236 var serverCollaborators = {
237 'myorg:myteam': 'write',
238 'myorg:anotherteam': 'read'
240 var clientCollaborators = {
241 'myorg:myteam': 'read-write',
242 'myorg:anotherteam': 'read-only'
244 var uri = '/-/package/%40foo%2Fbar/collaborators?format=cli&user=zkat'
245 server.expect('GET', uri, function (req, res) {
246 t.equal(req.method, 'GET')
248 res.json(serverCollaborators)
250 var params = Object.create(PARAMS)
252 client.access('ls-collaborators', URI, params, function (error, data) {
253 t.ifError(error, 'no errors')
254 t.same(data, clientCollaborators)
259 test('ls-collaborators w/o scope', function (t) {
260 var serverCollaborators = {
261 'myorg:myteam': 'write',
262 'myorg:anotherteam': 'read'
264 var clientCollaborators = {
265 'myorg:myteam': 'read-write',
266 'myorg:anotherteam': 'read-only'
268 var uri = '/-/package/bar/collaborators?format=cli&user=zkat'
269 server.expect('GET', uri, function (req, res) {
270 t.equal(req.method, 'GET')
272 res.json(serverCollaborators)
274 var params = Object.create(UNSCOPED)
276 client.access('ls-collaborators', URI, params, function (error, data) {
277 t.ifError(error, 'no errors')
278 t.same(data, clientCollaborators)
283 test('access command base validation', function (t) {
284 t.throws(function () {
285 client.access(undefined, URI, PARAMS, nop)
286 }, 'command is required')
287 t.throws(function () {
288 client.access('whoops', URI, PARAMS, nop)
289 }, 'command must be a valid subcommand')
290 commands.forEach(function (cmd) {
291 t.throws(function () {
292 client.access(cmd, undefined, PARAMS, nop)
293 }, 'registry URI is required')
294 t.throws(function () {
295 client.access(cmd, URI, undefined, nop)
296 }, 'params is required')
297 t.throws(function () {
298 client.access(cmd, URI, '', nop)
299 }, 'params must be an object')
300 t.throws(function () {
301 client.access(cmd, URI, {scope: 'o', team: 't'}, nop)
302 }, 'auth is required')
303 t.throws(function () {
304 client.access(cmd, URI, {auth: 5, scope: 'o', team: 't'}, nop)
305 }, 'auth must be an object')
306 t.throws(function () {
307 client.access(cmd, URI, PARAMS, {})
308 }, 'callback must be a function')
309 t.throws(function () {
310 client.access(cmd, URI, PARAMS, undefined)
311 }, 'callback is required')
313 'public', 'restricted'
315 t.throws(function () {
316 var params = Object.create(PARAMS)
317 params.package = null
318 client.access(cmd, URI, params, nop)
319 }, 'package is required')
320 t.throws(function () {
321 var params = Object.create(PARAMS)
322 params.package = 'underscore'
323 client.access(cmd, URI, params, nop)
324 }, 'only scoped packages are allowed')
326 if (contains(['grant', 'revoke', 'ls-packages'], cmd)) {
327 t.throws(function () {
328 var params = Object.create(PARAMS)
330 client.access(cmd, URI, params, nop)
331 }, 'scope is required')
333 if (contains(['grant', 'revoke'], cmd)) {
334 t.throws(function () {
335 var params = Object.create(PARAMS)
337 client.access(cmd, URI, params, nop)
338 }, 'team is required')
340 if (cmd === 'grant') {
341 t.throws(function () {
342 var params = Object.create(PARAMS)
343 params.permissions = null
344 client.access(cmd, URI, params, nop)
345 }, 'permissions are required')
346 t.throws(function () {
347 var params = Object.create(PARAMS)
348 params.permissions = 'idkwhat'
349 client.access(cmd, URI, params, nop)
350 }, 'permissions must be either read-only or read-write')
356 test('cleanup', function (t) {
361 function onJsonReq (req, cb) {
363 req.setEncoding('utf8')
364 req.on('data', function (data) { buffer += data })
365 req.on('end', function () { cb(buffer ? JSON.parse(buffer) : undefined) })
368 function contains (arr, item) {
369 return arr.indexOf(item) !== -1