• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const fs = require('fs')
4const path = require('path')
5const mkdirp = require('mkdirp')
6const rimraf = require('rimraf')
7const mr = require('npm-registry-mock')
8
9const test = require('tap').test
10const common = require('../common-tap.js')
11
12const pkg = common.pkg
13
14let server
15
16const scoped = {
17  name: '@scoped/pkg',
18  version: '1.1.1'
19}
20
21test('setup', function (t) {
22  mr({port: common.port}, function (err, s) {
23    t.ifError(err, 'registry mocked successfully')
24    server = s
25
26    fs.writeFile(
27      path.join(pkg, 'package.json'),
28      JSON.stringify(scoped),
29      function (er) {
30        t.ifError(er, 'wrote package.json')
31        t.end()
32      }
33    )
34  })
35})
36
37test('npm access public on current package', function (t) {
38  server.post('/-/package/%40scoped%2Fpkg/access', JSON.stringify({
39    access: 'public'
40  })).reply(200, {
41    accessChanged: true
42  })
43  common.npm(
44    [
45      'access',
46      'public',
47      '--registry', common.registry,
48      '--loglevel', 'silent'
49    ], {
50      cwd: pkg
51    },
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')
56      t.end()
57    }
58  )
59})
60
61test('npm access public when no package passed and no package.json', function (t) {
62  // need to simulate a missing package.json
63  var missing = path.join(pkg, 'access-public-missing-guard')
64  mkdirp.sync(path.join(missing, 'node_modules'))
65
66  common.npm([
67    'access',
68    'public',
69    '--registry', common.registry
70  ], {
71    cwd: missing
72  },
73  function (er, code, stdout, stderr) {
74    t.ifError(er, 'npm access')
75    t.match(stderr, /no package name passed to command and no package.json found/)
76    rimraf(missing, t.end)
77  })
78})
79
80test('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(pkg, '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')
86
87  common.npm([
88    'access',
89    'public',
90    '--registry', common.registry
91  ], {
92    cwd: invalid
93  },
94  function (er, code, stdout, stderr) {
95    t.ifError(er, 'npm access')
96    t.match(stderr, /Failed to parse json/)
97    rimraf(invalid, t.end)
98  })
99})
100
101test('npm access restricted on current package', function (t) {
102  server.post('/-/package/%40scoped%2Fpkg/access', JSON.stringify({
103    access: 'restricted'
104  })).reply(200, {
105    accessChanged: true
106  })
107  common.npm(
108    [
109      'access',
110      'restricted',
111      '--registry', common.registry,
112      '--loglevel', 'silent'
113    ], {
114      cwd: pkg
115    },
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')
120      t.end()
121    }
122  )
123})
124
125test('npm access on named package', function (t) {
126  server.post('/-/package/%40scoped%2Fanother/access', {
127    access: 'public'
128  }).reply(200, {
129    accessChaged: true
130  })
131  common.npm(
132    [
133      'access',
134      'public', '@scoped/another',
135      '--registry', common.registry,
136      '--loglevel', 'silent'
137    ],
138    { cwd: pkg },
139    function (er, code, stdout, stderr) {
140      t.ifError(er, 'npm access')
141      t.equal(code, 0, 'exited OK')
142      t.equal(stderr, '', 'no error output')
143
144      t.end()
145    }
146  )
147})
148
149test('npm change access on unscoped package', function (t) {
150  common.npm(
151    [
152      'access',
153      'restricted', 'yargs',
154      '--registry', common.registry
155    ],
156    { cwd: pkg },
157    function (er, code, stdout, stderr) {
158      t.ok(code, 'exited with Error')
159      t.matches(
160        stderr, /only available for scoped packages/)
161      t.end()
162    }
163  )
164})
165
166test('npm access grant read-only', function (t) {
167  server.filteringRequestBody((body) => {
168    const data = JSON.parse(body)
169    t.deepEqual(data, {
170      permissions: 'read-only',
171      package: '@scoped/another'
172    }, 'got the right body')
173    return true
174  })
175  server.put('/-/team/myorg/myteam/package', true).reply(201)
176  common.npm(
177    [
178      'access',
179      'grant', 'read-only',
180      'myorg:myteam',
181      '@scoped/another',
182      '--registry', common.registry
183    ],
184    { cwd: pkg },
185    function (er, code, stdout, stderr) {
186      t.ifError(er, 'npm access grant')
187      t.equal(code, 0, 'exited with Error')
188      t.end()
189    }
190  )
191})
192
193test('npm access grant read-write', function (t) {
194  server.filteringRequestBody((body) => {
195    const data = JSON.parse(body)
196    t.deepEqual(data, {
197      permissions: 'read-write',
198      package: '@scoped/another'
199    }, 'got the right body')
200    return true
201  })
202  server.put('/-/team/myorg/myteam/package', true).reply(201)
203  common.npm(
204    [
205      'access',
206      'grant', 'read-write',
207      'myorg:myteam',
208      '@scoped/another',
209      '--registry', common.registry
210    ],
211    { cwd: pkg },
212    function (er, code, stdout, stderr) {
213      t.ifError(er, 'npm access grant')
214      t.equal(code, 0, 'exited with Error')
215      t.end()
216    }
217  )
218})
219
220test('npm access grant read-write on unscoped package', function (t) {
221  server.filteringRequestBody((body) => {
222    const data = JSON.parse(body)
223    t.deepEqual(data, {
224      permissions: 'read-write',
225      package: 'another'
226    }, 'got the right body')
227    return true
228  })
229  server.put('/-/team/myorg/myteam/package', true).reply(201)
230  common.npm(
231    [
232      'access',
233      'grant', 'read-write',
234      'myorg:myteam',
235      'another',
236      '--registry', common.registry
237    ],
238    { cwd: pkg },
239    function (er, code, stdout, stderr) {
240      t.ifError(er, 'npm access grant')
241      t.equal(code, 0, 'exited with Error')
242      t.end()
243    }
244  )
245})
246
247test('npm access grant others', function (t) {
248  common.npm(
249    [
250      'access',
251      'grant', 'rerere',
252      'myorg:myteam',
253      '@scoped/another',
254      '--registry', common.registry
255    ],
256    { cwd: pkg },
257    function (er, code, stdout, stderr) {
258      t.ok(code, 'exited with Error')
259      t.matches(stderr, /read-only/)
260      t.matches(stderr, /read-write/)
261      t.end()
262    }
263  )
264})
265
266test('npm access revoke', function (t) {
267  server.delete('/-/team/myorg/myteam/package', {
268    package: '@scoped/another'
269  }).reply(200, {
270    accessChaged: true
271  })
272  common.npm(
273    [
274      'access',
275      'revoke',
276      'myorg:myteam',
277      '@scoped/another',
278      '--registry', common.registry
279    ],
280    { cwd: pkg },
281    function (er, code, stdout, stderr) {
282      t.ifError(er, 'npm access grant')
283      t.equal(code, 0, 'exited with Error')
284      t.end()
285    }
286  )
287})
288
289test('npm access ls-packages with no team', function (t) {
290  var serverPackages = {
291    '@foo/bar': 'write',
292    '@foo/util': 'read'
293  }
294  var clientPackages = {
295    '@foo/bar': 'read-write',
296    '@foo/util': 'read-only'
297  }
298  server.get(
299    '/-/org/username/package?format=cli'
300  ).reply(200, serverPackages)
301  common.npm(
302    [
303      'access',
304      'ls-packages',
305      '--registry', common.registry
306    ],
307    { cwd: pkg },
308    function (er, code, stdout, stderr) {
309      t.ifError(er, 'npm access ls-packages')
310      t.same(JSON.parse(stdout), clientPackages)
311      t.end()
312    }
313  )
314})
315
316test('npm access ls-packages on team', function (t) {
317  var serverPackages = {
318    '@foo/bar': 'write',
319    '@foo/util': 'read'
320  }
321  var clientPackages = {
322    '@foo/bar': 'read-write',
323    '@foo/util': 'read-only'
324  }
325  server.get(
326    '/-/team/myorg/myteam/package?format=cli'
327  ).reply(200, serverPackages)
328  common.npm(
329    [
330      'access',
331      'ls-packages',
332      'myorg:myteam',
333      '--registry', common.registry
334    ],
335    { cwd: pkg },
336    function (er, code, stdout, stderr) {
337      t.ifError(er, 'npm access ls-packages')
338      t.same(JSON.parse(stdout), clientPackages)
339      t.end()
340    }
341  )
342})
343
344test('npm access ls-packages on org', function (t) {
345  var serverPackages = {
346    '@foo/bar': 'write',
347    '@foo/util': 'read'
348  }
349  var clientPackages = {
350    '@foo/bar': 'read-write',
351    '@foo/util': 'read-only'
352  }
353  server.get(
354    '/-/org/myorg/package?format=cli'
355  ).reply(200, serverPackages)
356  common.npm(
357    [
358      'access',
359      'ls-packages',
360      'myorg',
361      '--registry', common.registry
362    ],
363    { cwd: pkg },
364    function (er, code, stdout, stderr) {
365      t.ifError(er, 'npm access ls-packages')
366      t.same(JSON.parse(stdout), clientPackages)
367      t.end()
368    }
369  )
370})
371
372test('npm access ls-packages on user', function (t) {
373  var serverPackages = {
374    '@foo/bar': 'write',
375    '@foo/util': 'read'
376  }
377  var clientPackages = {
378    '@foo/bar': 'read-write',
379    '@foo/util': 'read-only'
380  }
381  server.get(
382    '/-/org/myorg/package?format=cli'
383  ).reply(404, {error: 'nope'})
384  server.get(
385    '/-/user/myorg/package?format=cli'
386  ).reply(200, serverPackages)
387  common.npm(
388    [
389      'access',
390      'ls-packages',
391      'myorg',
392      '--registry', common.registry
393    ],
394    { cwd: pkg },
395    function (er, code, stdout, stderr) {
396      t.ifError(er, 'npm access ls-packages')
397      t.same(JSON.parse(stdout), clientPackages)
398      t.end()
399    }
400  )
401})
402
403test('npm access ls-packages with no package specified or package.json', function (t) {
404  // need to simulate a missing package.json
405  var missing = path.join(pkg, 'access-missing-guard')
406  mkdirp.sync(path.join(missing, 'node_modules'))
407
408  var serverPackages = {
409    '@foo/bar': 'write',
410    '@foo/util': 'read'
411  }
412  var clientPackages = {
413    '@foo/bar': 'read-write',
414    '@foo/util': 'read-only'
415  }
416  server.get(
417    '/-/org/myorg/package?format=cli'
418  ).reply(404, {error: 'nope'})
419  server.get(
420    '/-/user/myorg/package?format=cli'
421  ).reply(200, serverPackages)
422  common.npm(
423    [
424      'access',
425      'ls-packages',
426      'myorg',
427      '--registry', common.registry
428    ],
429    { cwd: missing },
430    function (er, code, stdout, stderr) {
431      t.ifError(er, 'npm access ls-packages')
432      t.same(JSON.parse(stdout), clientPackages)
433      rimraf(missing, t.end)
434    }
435  )
436})
437
438test('npm access ls-collaborators on current', function (t) {
439  var serverCollaborators = {
440    'myorg:myteam': 'write',
441    'myorg:anotherteam': 'read'
442  }
443  var clientCollaborators = {
444    'myorg:myteam': 'read-write',
445    'myorg:anotherteam': 'read-only'
446  }
447  server.get(
448    '/-/package/%40scoped%2Fpkg/collaborators?format=cli'
449  ).reply(200, serverCollaborators)
450  common.npm(
451    [
452      'access',
453      'ls-collaborators',
454      '--registry', common.registry
455    ],
456    { cwd: pkg },
457    function (er, code, stdout, stderr) {
458      t.ifError(er, 'npm access ls-collaborators')
459      t.same(JSON.parse(stdout), clientCollaborators)
460      t.end()
461    }
462  )
463})
464
465test('npm access ls-collaborators on package', function (t) {
466  var serverCollaborators = {
467    'myorg:myteam': 'write',
468    'myorg:anotherteam': 'read'
469  }
470  var clientCollaborators = {
471    'myorg:myteam': 'read-write',
472    'myorg:anotherteam': 'read-only'
473  }
474  server.get(
475    '/-/package/%40scoped%2Fanother/collaborators?format=cli'
476  ).reply(200, serverCollaborators)
477  common.npm(
478    [
479      'access',
480      'ls-collaborators',
481      '@scoped/another',
482      '--registry', common.registry
483    ],
484    { cwd: pkg },
485    function (er, code, stdout, stderr) {
486      t.ifError(er, 'npm access ls-collaborators')
487      t.same(JSON.parse(stdout), clientCollaborators)
488      t.end()
489    }
490  )
491})
492
493test('npm access ls-collaborators on unscoped', function (t) {
494  var serverCollaborators = {
495    'myorg:myteam': 'write',
496    'myorg:anotherteam': 'read'
497  }
498  var clientCollaborators = {
499    'myorg:myteam': 'read-write',
500    'myorg:anotherteam': 'read-only'
501  }
502  server.get(
503    '/-/package/pkg/collaborators?format=cli'
504  ).reply(200, serverCollaborators)
505  common.npm(
506    [
507      'access',
508      'ls-collaborators',
509      'pkg',
510      '--registry', common.registry
511    ],
512    { cwd: pkg },
513    function (er, code, stdout, stderr) {
514      t.ifError(er, 'npm access ls-collaborators')
515      t.same(JSON.parse(stdout), clientCollaborators)
516      t.end()
517    }
518  )
519})
520
521test('npm access ls-collaborators on current w/user filter', function (t) {
522  var serverCollaborators = {
523    'myorg:myteam': 'write',
524    'myorg:anotherteam': 'read'
525  }
526  var clientCollaborators = {
527    'myorg:myteam': 'read-write',
528    'myorg:anotherteam': 'read-only'
529  }
530  server.get(
531    '/-/package/%40scoped%2Fanother/collaborators?format=cli&user=zkat'
532  ).reply(200, serverCollaborators)
533  common.npm(
534    [
535      'access',
536      'ls-collaborators',
537      '@scoped/another',
538      'zkat',
539      '--registry', common.registry
540    ],
541    { cwd: pkg },
542    function (er, code, stdout, stderr) {
543      t.ifError(er, 'npm access ls-collaborators')
544      t.same(JSON.parse(stdout), clientCollaborators)
545      t.end()
546    }
547  )
548})
549
550test('npm access edit', function (t) {
551  common.npm(
552    [
553      'access',
554      'edit', '@scoped/another',
555      '--registry', common.registry
556    ],
557    { cwd: pkg },
558    function (er, code, stdout, stderr) {
559      t.ok(code, 'exited with Error')
560      t.match(stderr, /edit subcommand is not implemented yet/)
561      t.end()
562    }
563  )
564})
565
566test('npm access blerg', function (t) {
567  common.npm(
568    [
569      'access',
570      'blerg', '@scoped/another',
571      '--registry', common.registry
572    ],
573    { cwd: pkg },
574    function (er, code, stdout, stderr) {
575      t.ok(code, 'exited with Error')
576      t.matches(stderr, /Usage:/)
577      t.end()
578    }
579  )
580})
581
582test('cleanup', function (t) {
583  t.pass('cleaned up')
584  server.done()
585  server.close()
586  t.end()
587})
588