2 var path = require('path')
3 var mkdirp = require('mkdirp')
4 var rimraf = require('rimraf')
5 var mr = require('npm-registry-mock')
7 var test = require('tap').test
8 var common = require('../common-tap.js')
10 var pkg = path.resolve(__dirname, 'access')
18 test('setup', function (t) {
19 mkdirp(pkg, function (er) {
20 t.ifError(er, pkg + ' made successfully')
22 mr({port: common.port}, function (err, s) {
23 t.ifError(err, 'registry mocked successfully')
27 path.join(pkg, 'package.json'),
28 JSON.stringify(scoped),
30 t.ifError(er, 'wrote package.json')
38 test('npm access public on current package', function (t) {
39 server.post('/-/package/%40scoped%2Fpkg/access', JSON.stringify({
47 '--registry', common.registry,
48 '--loglevel', 'silent'
52 function (er, code, stdout, stderr) {
53 t.ifError(er, 'npm access')
54 t.equal(code, 0, 'exited OK')
55 t.equal(stderr, '', 'no error output')
60 test('npm access public when no package passed and no package.json', function (t) {
61 // need to simulate a missing package.json
62 var missing = path.join(__dirname, 'access-public-missing-guard')
63 mkdirp.sync(path.join(missing, 'node_modules'))
68 '--registry', common.registry
72 function (er, code, stdout, stderr) {
73 t.ifError(er, 'npm access')
74 t.match(stderr, /no package name passed to command and no package.json found/)
80 test('npm access public when no package passed and invalid package.json', function (t) {
81 // need to simulate a missing package.json
82 var invalid = path.join(__dirname, 'access-public-invalid-package')
83 mkdirp.sync(path.join(invalid, 'node_modules'))
84 // it's hard to force `read-package-json` to break w/o ENOENT, but this will do it
85 fs.writeFileSync(path.join(invalid, 'package.json'), '{\n')
90 '--registry', common.registry
94 function (er, code, stdout, stderr) {
95 t.ifError(er, 'npm access')
96 t.match(stderr, /Failed to parse json/)
102 test('npm access restricted on current package', function (t) {
103 server.post('/-/package/%40scoped%2Fpkg/access', JSON.stringify({
111 '--registry', common.registry,
112 '--loglevel', 'silent'
116 function (er, code, stdout, stderr) {
117 t.ifError(er, 'npm access')
118 t.equal(code, 0, 'exited OK')
119 t.equal(stderr, '', 'no error output')
124 test('npm access on named package', function (t) {
125 server.post('/-/package/%40scoped%2Fanother/access', {
133 'public', '@scoped/another',
134 '--registry', common.registry,
135 '--loglevel', 'silent'
138 function (er, code, stdout, stderr) {
139 t.ifError(er, 'npm access')
140 t.equal(code, 0, 'exited OK')
141 t.equal(stderr, '', 'no error output')
148 test('npm change access on unscoped package', function (t) {
152 'restricted', 'yargs',
153 '--registry', common.registry
156 function (er, code, stdout, stderr) {
157 t.ok(code, 'exited with Error')
159 stderr, /access commands are only accessible for scoped packages/)
165 test('npm access grant read-only', function (t) {
166 server.put('/-/team/myorg/myteam/package', {
167 permissions: 'read-only',
168 package: '@scoped/another'
175 'grant', 'read-only',
178 '--registry', common.registry
181 function (er, code, stdout, stderr) {
182 t.ifError(er, 'npm access grant')
183 t.equal(code, 0, 'exited with Error')
189 test('npm access grant read-write', function (t) {
190 server.put('/-/team/myorg/myteam/package', {
191 permissions: 'read-write',
192 package: '@scoped/another'
199 'grant', 'read-write',
202 '--registry', common.registry
205 function (er, code, stdout, stderr) {
206 t.ifError(er, 'npm access grant')
207 t.equal(code, 0, 'exited with Error')
213 test('npm access grant others', function (t) {
220 '--registry', common.registry
223 function (er, code, stdout, stderr) {
224 t.ok(code, 'exited with Error')
225 t.matches(stderr, /read-only/)
226 t.matches(stderr, /read-write/)
232 test('npm access revoke', function (t) {
233 server.delete('/-/team/myorg/myteam/package', {
234 package: '@scoped/another'
244 '--registry', common.registry
247 function (er, code, stdout, stderr) {
248 t.ifError(er, 'npm access grant')
249 t.equal(code, 0, 'exited with Error')
255 test('npm access ls-packages with no team', function (t) {
256 var serverPackages = {
260 var clientPackages = {
261 '@foo/bar': 'read-write',
262 '@foo/util': 'read-only'
265 '/-/org/username/package?format=cli'
266 ).reply(200, serverPackages)
271 '--registry', common.registry
274 function (er, code, stdout, stderr) {
275 t.ifError(er, 'npm access ls-packages')
276 t.same(JSON.parse(stdout), clientPackages)
282 test('npm access ls-packages on team', function (t) {
283 var serverPackages = {
287 var clientPackages = {
288 '@foo/bar': 'read-write',
289 '@foo/util': 'read-only'
292 '/-/team/myorg/myteam/package?format=cli'
293 ).reply(200, serverPackages)
299 '--registry', common.registry
302 function (er, code, stdout, stderr) {
303 t.ifError(er, 'npm access ls-packages')
304 t.same(JSON.parse(stdout), clientPackages)
310 test('npm access ls-packages on org', function (t) {
311 var serverPackages = {
315 var clientPackages = {
316 '@foo/bar': 'read-write',
317 '@foo/util': 'read-only'
320 '/-/org/myorg/package?format=cli'
321 ).reply(200, serverPackages)
327 '--registry', common.registry
330 function (er, code, stdout, stderr) {
331 t.ifError(er, 'npm access ls-packages')
332 t.same(JSON.parse(stdout), clientPackages)
338 test('npm access ls-packages on user', function (t) {
339 var serverPackages = {
343 var clientPackages = {
344 '@foo/bar': 'read-write',
345 '@foo/util': 'read-only'
348 '/-/org/myorg/package?format=cli'
349 ).reply(404, {error: 'nope'})
351 '/-/user/myorg/package?format=cli'
352 ).reply(200, serverPackages)
358 '--registry', common.registry
361 function (er, code, stdout, stderr) {
362 t.ifError(er, 'npm access ls-packages')
363 t.same(JSON.parse(stdout), clientPackages)
369 test('npm access ls-packages with no package specified or package.json', function (t) {
370 // need to simulate a missing package.json
371 var missing = path.join(__dirname, 'access-missing-guard')
372 mkdirp.sync(path.join(missing, 'node_modules'))
374 var serverPackages = {
378 var clientPackages = {
379 '@foo/bar': 'read-write',
380 '@foo/util': 'read-only'
383 '/-/org/myorg/package?format=cli'
384 ).reply(404, {error: 'nope'})
386 '/-/user/myorg/package?format=cli'
387 ).reply(200, serverPackages)
393 '--registry', common.registry
396 function (er, code, stdout, stderr) {
397 t.ifError(er, 'npm access ls-packages')
398 t.same(JSON.parse(stdout), clientPackages)
405 test('npm access ls-collaborators on current', function (t) {
406 var serverCollaborators = {
407 'myorg:myteam': 'write',
408 'myorg:anotherteam': 'read'
410 var clientCollaborators = {
411 'myorg:myteam': 'read-write',
412 'myorg:anotherteam': 'read-only'
415 '/-/package/%40scoped%2Fpkg/collaborators?format=cli'
416 ).reply(200, serverCollaborators)
421 '--registry', common.registry
424 function (er, code, stdout, stderr) {
425 t.ifError(er, 'npm access ls-collaborators')
426 t.same(JSON.parse(stdout), clientCollaborators)
432 test('npm access ls-collaborators on package', function (t) {
433 var serverCollaborators = {
434 'myorg:myteam': 'write',
435 'myorg:anotherteam': 'read'
437 var clientCollaborators = {
438 'myorg:myteam': 'read-write',
439 'myorg:anotherteam': 'read-only'
442 '/-/package/%40scoped%2Fanother/collaborators?format=cli'
443 ).reply(200, serverCollaborators)
449 '--registry', common.registry
452 function (er, code, stdout, stderr) {
453 t.ifError(er, 'npm access ls-collaborators')
454 t.same(JSON.parse(stdout), clientCollaborators)
460 test('npm access ls-collaborators on current w/user filter', function (t) {
461 var serverCollaborators = {
462 'myorg:myteam': 'write',
463 'myorg:anotherteam': 'read'
465 var clientCollaborators = {
466 'myorg:myteam': 'read-write',
467 'myorg:anotherteam': 'read-only'
470 '/-/package/%40scoped%2Fanother/collaborators?format=cli&user=zkat'
471 ).reply(200, serverCollaborators)
478 '--registry', common.registry
481 function (er, code, stdout, stderr) {
482 t.ifError(er, 'npm access ls-collaborators')
483 t.same(JSON.parse(stdout), clientCollaborators)
489 test('npm access edit', function (t) {
493 'edit', '@scoped/another',
494 '--registry', common.registry
497 function (er, code, stdout, stderr) {
498 t.ok(code, 'exited with Error')
499 t.match(stderr, /edit subcommand is not implemented yet/)
505 test('npm access blerg', function (t) {
509 'blerg', '@scoped/another',
510 '--registry', common.registry
513 function (er, code, stdout, stderr) {
514 t.ok(code, 'exited with Error')
515 t.matches(stderr, /Usage:/)
521 test('cleanup', function (t) {