Welcome to mirror list, hosted at ThFree Co, Russian Federation.

access.js « commands « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fdf132aff97f37cd4165d186aa15295e6666db47 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
const t = require('tap')

const { real: mockNpm } = require('../../fixtures/mock-npm.js')

const { Npm } = mockNpm(t)
const npm = new Npm()

const prefix = t.testdir({})

t.before(async () => {
  await npm.load()
  npm.prefix = prefix
})

t.test('completion', async t => {
  const access = await npm.cmd('access')
  const testComp = (argv, expect) => {
    const res = access.completion({ conf: { argv: { remain: argv } } })
    t.resolves(res, expect, argv.join(' '))
  }

  testComp(['npm', 'access'], [
    'public', 'restricted', 'grant', 'revoke', 'ls-packages',
    'ls-collaborators', 'edit', '2fa-required', '2fa-not-required',
  ])
  testComp(['npm', 'access', 'grant'], ['read-only', 'read-write'])
  testComp(['npm', 'access', 'grant', 'read-only'], [])
  testComp(['npm', 'access', 'public'], [])
  testComp(['npm', 'access', 'restricted'], [])
  testComp(['npm', 'access', 'revoke'], [])
  testComp(['npm', 'access', 'ls-packages'], [])
  testComp(['npm', 'access', 'ls-collaborators'], [])
  testComp(['npm', 'access', 'edit'], [])
  testComp(['npm', 'access', '2fa-required'], [])
  testComp(['npm', 'access', '2fa-not-required'], [])
  testComp(['npm', 'access', 'revoke'], [])

  await t.rejects(
    access.completion({ conf: { argv: { remain: ['npm', 'access', 'foobar'] } } }),
    { message: 'foobar not recognized' }
  )
})

t.test('subcommand required', async t => {
  const access = await npm.cmd('access')
  await t.rejects(
    npm.exec('access', []),
    access.usageError('Subcommand is required.')
  )
})

t.test('unrecognized subcommand', async t => {
  await t.rejects(
    npm.exec('access', ['blerg']),
    /Usage: blerg is not a recognized subcommand/,
    'should throw EUSAGE on missing subcommand'
  )
})

t.test('edit', async t => {
  await t.rejects(
    npm.exec('access', ['edit', '@scoped/another']),
    /edit subcommand is not implemented yet/,
    'should throw not implemented yet error'
  )
})

t.test('access public on unscoped package', async t => {
  t.teardown(() => {
    npm.prefix = prefix
  })
  const testdir = t.testdir({
    'package.json': JSON.stringify({
      name: 'npm-access-public-pkg',
    }),
  })
  npm.prefix = testdir
  await t.rejects(
    npm.exec('access', ['public']),
    /Usage: This command is only available for scoped packages/,
    'should throw scoped-restricted error'
  )
})

t.test('access public on scoped package', async t => {
  t.plan(2)
  const { Npm } = mockNpm(t, {
    libnpmaccess: {
      public: (pkg, { registry }) => {
        t.equal(pkg, name, 'should use pkg name ref')
        t.equal(
          registry,
          'https://registry.npmjs.org/',
          'should forward correct options'
        )
        return true
      },
    },
  })
  const npm = new Npm()
  await npm.load()
  const name = '@scoped/npm-access-public-pkg'
  const testdir = t.testdir({
    'package.json': JSON.stringify({ name }),
  })
  npm.prefix = testdir
  await npm.exec('access', ['public'])
})

t.test('access public on missing package.json', async t => {
  await t.rejects(
    npm.exec('access', ['public']),
    /no package name passed to command and no package.json found/,
    'should throw no package.json found error'
  )
})

t.test('access public on invalid package.json', async t => {
  t.teardown(() => {
    npm.prefix = prefix
  })
  const testdir = t.testdir({
    'package.json': '{\n',
    node_modules: {},
  })
  npm.prefix = testdir
  await t.rejects(
    npm.exec('access', ['public']),
    { code: 'EJSONPARSE' },
    'should throw failed to parse package.json'
  )
})

t.test('access restricted on unscoped package', async t => {
  t.teardown(() => {
    npm.prefix = prefix
  })
  const testdir = t.testdir({
    'package.json': JSON.stringify({
      name: 'npm-access-restricted-pkg',
    }),
  })
  npm.prefix = testdir
  await t.rejects(
    npm.exec('access', ['public']),
    /Usage: This command is only available for scoped packages/,
    'should throw scoped-restricted error'
  )
})

t.test('access restricted on scoped package', async t => {
  t.plan(2)
  const { Npm } = mockNpm(t, {
    libnpmaccess: {
      restricted: (pkg, { registry }) => {
        t.equal(pkg, name, 'should use pkg name ref')
        t.equal(
          registry,
          'https://registry.npmjs.org/',
          'should forward correct options'
        )
        return true
      },
    },
  })
  const npm = new Npm()
  await npm.load()
  const name = '@scoped/npm-access-restricted-pkg'
  const testdir = t.testdir({
    'package.json': JSON.stringify({ name }),
  })
  npm.prefix = testdir
  await npm.exec('access', ['restricted'])
})

t.test('access restricted on missing package.json', async t => {
  await t.rejects(
    npm.exec('access', ['restricted']),
    /no package name passed to command and no package.json found/,
    'should throw no package.json found error'
  )
})

t.test('access restricted on invalid package.json', async t => {
  t.teardown(() => {
    npm.prefix = prefix
  })
  const testdir = t.testdir({
    'package.json': '{\n',
    node_modules: {},
  })
  npm.prefix = testdir
  await t.rejects(
    npm.exec('access', ['restricted']),
    { code: 'EJSONPARSE' },
    'should throw failed to parse package.json'
  )
})

t.test('access grant read-only', async t => {
  t.plan(3)
  const { Npm } = mockNpm(t, {
    libnpmaccess: {
      grant: (spec, team, permissions) => {
        t.equal(spec, '@scoped/another', 'should use expected spec')
        t.equal(team, 'myorg:myteam', 'should use expected team')
        t.equal(permissions, 'read-only', 'should forward permissions')
        return true
      },
    },
  })
  const npm = new Npm()
  await npm.exec('access', [
    'grant',
    'read-only',
    'myorg:myteam',
    '@scoped/another',
  ])
})

t.test('access grant read-write', async t => {
  t.plan(3)
  const { Npm } = mockNpm(t, {
    libnpmaccess: {
      grant: (spec, team, permissions) => {
        t.equal(spec, '@scoped/another', 'should use expected spec')
        t.equal(team, 'myorg:myteam', 'should use expected team')
        t.equal(permissions, 'read-write', 'should forward permissions')
        return true
      },
    },
  })
  const npm = new Npm()
  await npm.exec('access', [
    'grant',
    'read-write',
    'myorg:myteam',
    '@scoped/another',
  ])
})

t.test('access grant current cwd', async t => {
  t.plan(3)
  const testdir = t.testdir({
    'package.json': JSON.stringify({
      name: 'yargs',
    }),
  })
  const { Npm } = mockNpm(t, {
    libnpmaccess: {
      grant: (spec, team, permissions) => {
        t.equal(spec, 'yargs', 'should use expected spec')
        t.equal(team, 'myorg:myteam', 'should use expected team')
        t.equal(permissions, 'read-write', 'should forward permissions')
        return true
      },
    },
  })
  const npm = new Npm()
  await npm.load()
  npm.prefix = testdir
  await npm.exec('access', [
    'grant',
    'read-write',
    'myorg:myteam',
  ])
})

t.test('access grant others', async t => {
  await t.rejects(
    npm.exec('access', [
      'grant',
      'rerere',
      'myorg:myteam',
      '@scoped/another',
    ]),
    /Usage: First argument must be either `read-only` or `read-write`./,
    'should throw unrecognized argument error'
  )
})

t.test('access grant missing team args', async t => {
  await t.rejects(
    npm.exec('access', [
      'grant',
      'read-only',
      undefined,
      '@scoped/another',
    ]),
    /Usage: `<scope:team>` argument is required./,
    'should throw missing argument error'
  )
})

t.test('access grant malformed team arg', async t => {
  await t.rejects(
    npm.exec('access', [
      'grant',
      'read-only',
      'foo',
      '@scoped/another',
    ]),
    /Usage: Second argument used incorrect format.\n/,
    'should throw malformed arg error'
  )
})

t.test('access 2fa-required/2fa-not-required', async t => {
  t.plan(2)
  const { Npm } = mockNpm(t, {
    libnpmaccess: {
      tfaRequired: (spec) => {
        t.equal(spec, '@scope/pkg', 'should use expected spec')
        return true
      },
      tfaNotRequired: (spec) => {
        t.equal(spec, 'unscoped-pkg', 'should use expected spec')
        return true
      },
    },
  })
  const npm = new Npm()

  await npm.exec('access', ['2fa-required', '@scope/pkg'])
  await npm.exec('access', ['2fa-not-required', 'unscoped-pkg'])
})

t.test('access revoke', async t => {
  t.plan(2)
  const { Npm } = mockNpm(t, {
    libnpmaccess: {
      revoke: (spec, team) => {
        t.equal(spec, '@scoped/another', 'should use expected spec')
        t.equal(team, 'myorg:myteam', 'should use expected team')
        return true
      },
    },
  })
  const npm = new Npm()
  await npm.exec('access', [
    'revoke',
    'myorg:myteam',
    '@scoped/another',
  ])
})

t.test('access revoke missing team args', async t => {
  await t.rejects(
    npm.exec('access', [
      'revoke',
      undefined,
      '@scoped/another',
    ]),
    /Usage: `<scope:team>` argument is required./,
    'should throw missing argument error'
  )
})

t.test('access revoke malformed team arg', async t => {
  await t.rejects(
    npm.exec('access', [
      'revoke',
      'foo',
      '@scoped/another',
    ]),
    /Usage: First argument used incorrect format.\n/,
    'should throw malformed arg error'
  )
})

t.test('npm access ls-packages with no team', async t => {
  t.plan(1)
  const { Npm } = mockNpm(t, {
    libnpmaccess: {
      lsPackages: (entity) => {
        t.equal(entity, 'foo', 'should use expected entity')
        return {}
      },
    },
    '../../lib/utils/get-identity.js': () => Promise.resolve('foo'),
  })
  const npm = new Npm()
  await npm.exec('access', ['ls-packages'])
})

t.test('access ls-packages on team', async t => {
  t.plan(1)
  const { Npm } = mockNpm(t, {
    libnpmaccess: {
      lsPackages: (entity) => {
        t.equal(entity, 'myorg:myteam', 'should use expected entity')
        return {}
      },
    },
  })
  const npm = new Npm()
  await npm.exec('access', [
    'ls-packages',
    'myorg:myteam',
  ])
})

t.test('access ls-collaborators on current', async t => {
  t.plan(1)
  const testdir = t.testdir({
    'package.json': JSON.stringify({
      name: 'yargs',
    }),
  })
  const { Npm } = mockNpm(t, {
    libnpmaccess: {
      lsCollaborators: (spec) => {
        t.equal(spec, 'yargs', 'should use expected spec')
        return {}
      },
    },
  })
  const npm = new Npm()
  await npm.load()
  npm.prefix = testdir
  await npm.exec('access', ['ls-collaborators'])
})

t.test('access ls-collaborators on spec', async t => {
  t.plan(1)
  const { Npm } = mockNpm(t, {
    libnpmaccess: {
      lsCollaborators: (spec) => {
        t.equal(spec, 'yargs', 'should use expected spec')
        return {}
      },
    },
  })
  const npm = new Npm()
  await npm.exec('access', [
    'ls-collaborators',
    'yargs',
  ])
})