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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
|
const requireInject = require('require-inject')
const t = require('tap')
let result = ''
let readLocalPkgResponse = null
const noop = () => null
const npm = {
flatOptions: {},
output: (msg) => {
result = result ? `${result}\n${msg}` : msg
},
}
const npmFetch = { json: noop }
const npmlog = { error: noop, info: noop, verbose: noop }
const pacote = { packument: noop }
const mocks = {
npmlog,
'npm-registry-fetch': npmFetch,
pacote,
'../../lib/utils/otplease.js': async (opts, fn) => fn({ otp: '123456', opts }),
'../../lib/utils/read-local-package.js': async () => readLocalPkgResponse,
'../../lib/utils/usage.js': () => 'usage instructions',
}
const npmcliMaintainers = [
{ email: 'quitlahok@gmail.com', name: 'nlf' },
{ email: 'ruyadorno@hotmail.com', name: 'ruyadorno' },
{ email: 'darcy@darcyclarke.me', name: 'darcyclarke' },
{ email: 'i@izs.me', name: 'isaacs' },
]
const Owner = requireInject('../../lib/owner.js', mocks)
const owner = new Owner(npm)
t.test('owner no args', t => {
result = ''
t.teardown(() => {
result = ''
})
owner.exec([], err => {
t.match(err, /usage instructions/, 'should not error out on empty locations')
t.end()
})
})
t.test('owner ls no args', t => {
t.plan(4)
result = ''
readLocalPkgResponse = '@npmcli/map-workspaces'
pacote.packument = async (spec, opts) => {
t.equal(spec.name, '@npmcli/map-workspaces', 'should use expect pkg name')
t.match(
opts,
{
...npm.flatOptions,
fullMetadata: true,
},
'should forward expected options to pacote.packument'
)
return { maintainers: npmcliMaintainers }
}
t.teardown(() => {
result = ''
pacote.packument = noop
readLocalPkgResponse = null
})
owner.exec(['ls'], err => {
t.ifError(err, 'npm owner ls no args')
t.matchSnapshot(result, 'should output owners of cwd package')
})
})
t.test('owner ls no args no cwd package', t => {
result = ''
t.teardown(() => {
result = ''
npmlog.error = noop
})
owner.exec(['ls'], err => {
t.match(err, /usage instructions/, 'should throw usage instructions if no cwd package available')
t.end()
})
})
t.test('owner ls fails to retrieve packument', t => {
t.plan(4)
result = ''
readLocalPkgResponse = '@npmcli/map-workspaces'
pacote.packument = () => {
throw new Error('ERR')
}
npmlog.error = (title, msg, pkgName) => {
t.equal(title, 'owner ls', 'should list npm owner ls title')
t.equal(msg, "Couldn't get owner data", 'should use expected msg')
t.equal(pkgName, '@npmcli/map-workspaces', 'should use pkg name')
}
t.teardown(() => {
result = ''
npmlog.error = noop
pacote.packument = noop
})
owner.exec(['ls'], err => {
t.match(
err,
/ERR/,
'should throw unknown error'
)
})
})
t.test('owner ls <pkg>', t => {
t.plan(4)
result = ''
pacote.packument = async (spec, opts) => {
t.equal(spec.name, '@npmcli/map-workspaces', 'should use expect pkg name')
t.match(
opts,
{
...npm.flatOptions,
fullMetadata: true,
},
'should forward expected options to pacote.packument'
)
return { maintainers: npmcliMaintainers }
}
t.teardown(() => {
result = ''
pacote.packument = noop
})
owner.exec(['ls', '@npmcli/map-workspaces'], err => {
t.ifError(err, 'npm owner ls <pkg>')
t.matchSnapshot(result, 'should output owners of <pkg>')
})
})
t.test('owner ls <pkg> no maintainers', t => {
result = ''
pacote.packument = async (spec, opts) => {
return { maintainers: [] }
}
t.teardown(() => {
result = ''
pacote.packument = noop
})
owner.exec(['ls', '@npmcli/map-workspaces'], err => {
t.ifError(err, 'npm owner ls <pkg> no maintainers')
t.equal(result, 'no admin found', 'should output no admint found msg')
t.end()
})
})
t.test('owner add <user> <pkg>', t => {
t.plan(9)
result = ''
npmFetch.json = async (uri, opts) => {
// retrieve user info from couchdb request
if (uri === '/-/user/org.couchdb.user:foo') {
t.ok('should request user info')
t.match(opts, { ...npm.flatOptions }, 'should use expected opts')
return {
_id: 'org.couchdb.user:foo',
email: 'foo@github.com',
name: 'foo',
}
} else if (uri === '/@npmcli%2fmap-workspaces/-rev/1-foobaaa1') {
t.ok('should put changed owner')
t.match(opts, {
...npm.flatOptions,
method: 'PUT',
body: {
_rev: '1-foobaaa1',
maintainers: npmcliMaintainers,
},
otp: '123456',
spec: {
name: '@npmcli/map-workspaces',
},
}, 'should use expected opts')
t.deepEqual(
opts.body.maintainers,
[
...npmcliMaintainers,
{
name: 'foo',
email: 'foo@github.com',
},
],
'should contain expected new owners, adding requested user'
)
return {}
} else
t.fail(`unexpected fetch json call to uri: ${uri}`)
}
pacote.packument = async (spec, opts) => {
t.equal(spec.name, '@npmcli/map-workspaces', 'should use expect pkg name')
t.match(
opts,
{
...npm.flatOptions,
fullMetadata: true,
},
'should forward expected options to pacote.packument'
)
return {
_rev: '1-foobaaa1',
maintainers: npmcliMaintainers,
}
}
t.teardown(() => {
result = ''
npmFetch.json = noop
pacote.packument = noop
})
owner.exec(['add', 'foo', '@npmcli/map-workspaces'], err => {
t.ifError(err, 'npm owner add <user> <pkg>')
t.equal(result, '+ foo (@npmcli/map-workspaces)', 'should output add result')
})
})
t.test('owner add <user> cwd package', t => {
result = ''
readLocalPkgResponse = '@npmcli/map-workspaces'
npmFetch.json = async (uri, opts) => {
// retrieve user info from couchdb request
if (uri === '/-/user/org.couchdb.user:foo') {
return {
_id: 'org.couchdb.user:foo',
email: 'foo@github.com',
name: 'foo',
}
} else if (uri === '/@npmcli%2fmap-workspaces/-rev/1-foobaaa1')
return {}
else
t.fail(`unexpected fetch json call to uri: ${uri}`)
}
pacote.packument = async (spec, opts) => ({
_rev: '1-foobaaa1',
maintainers: npmcliMaintainers,
})
t.teardown(() => {
result = ''
readLocalPkgResponse = null
npmFetch.json = noop
pacote.packument = noop
})
owner.exec(['add', 'foo'], err => {
t.ifError(err, 'npm owner add <user> cwd package')
t.equal(result, '+ foo (@npmcli/map-workspaces)', 'should output add result')
t.end()
})
})
t.test('owner add <user> <pkg> already an owner', t => {
t.plan(3)
result = ''
npmlog.info = (title, msg) => {
t.equal(title, 'owner add', 'should use expected title')
t.equal(
msg,
'Already a package owner: ruyadorno <ruyadorno@hotmail.com>',
'should log already package owner info message'
)
}
npmFetch.json = async (uri, opts) => {
// retrieve user info from couchdb request
if (uri === '/-/user/org.couchdb.user:ruyadorno') {
return {
_id: 'org.couchdb.user:ruyadorno',
email: 'ruyadorno@hotmail.com',
name: 'ruyadorno',
}
} else
t.fail(`unexpected fetch json call to uri: ${uri}`)
}
pacote.packument = async (spec, opts) => {
return {
_rev: '1-foobaaa1',
maintainers: npmcliMaintainers,
}
}
t.teardown(() => {
result = ''
npmlog.info = noop
npmFetch.json = noop
pacote.packument = noop
})
owner.exec(['add', 'ruyadorno', '@npmcli/map-workspaces'], err => {
t.ifError(err, 'npm owner add <user> <pkg> already an owner')
})
})
t.test('owner add <user> <pkg> fails to retrieve user', t => {
result = ''
readLocalPkgResponse =
npmFetch.json = async (uri, opts) => {
// retrieve borked user info from couchdb request
if (uri === '/-/user/org.couchdb.user:foo')
return { ok: false }
else if (uri === '/@npmcli%2fmap-workspaces/-rev/1-foobaaa1')
return {}
else
t.fail(`unexpected fetch json call to uri: ${uri}`)
}
pacote.packument = async (spec, opts) => ({
_rev: '1-foobaaa1',
maintainers: npmcliMaintainers,
})
t.teardown(() => {
result = ''
readLocalPkgResponse = null
npmFetch.json = noop
pacote.packument = noop
})
owner.exec(['add', 'foo', '@npmcli/map-workspaces'], err => {
t.match(
err,
/Error: Couldn't get user data for foo: {"ok":false}/,
'should throw user data error'
)
t.equal(err.code, 'EOWNERUSER', 'should have expected error code')
t.end()
})
})
t.test('owner add <user> <pkg> fails to PUT updates', t => {
result = ''
npmFetch.json = async (uri, opts) => {
// retrieve user info from couchdb request
if (uri === '/-/user/org.couchdb.user:foo') {
return {
_id: 'org.couchdb.user:foo',
email: 'foo@github.com',
name: 'foo',
}
} else if (uri === '/@npmcli%2fmap-workspaces/-rev/1-foobaaa1') {
return {
error: {
status: '418',
message: "I'm a teapot",
},
}
} else
t.fail(`unexpected fetch json call to uri: ${uri}`)
}
pacote.packument = async (spec, opts) => ({
_rev: '1-foobaaa1',
maintainers: npmcliMaintainers,
})
t.teardown(() => {
result = ''
npmFetch.json = noop
pacote.packument = noop
})
owner.exec(['add', 'foo', '@npmcli/map-workspaces'], err => {
t.match(
err.message,
/Failed to update package/,
'should throw failed to update package error'
)
t.equal(err.code, 'EOWNERMUTATE', 'should have expected error code')
t.end()
})
})
t.test('owner add <user> <pkg> fails to retrieve user info', t => {
t.plan(3)
result = ''
npmlog.error = (title, msg) => {
t.equal(title, 'owner mutate', 'should use expected title')
t.equal(msg, 'Error getting user data for foo')
}
npmFetch.json = async (uri, opts) => {
// retrieve user info from couchdb request
if (uri === '/-/user/org.couchdb.user:foo') {
throw Object.assign(
new Error("I'm a teapot"),
{ status: 418 }
)
} else
t.fail(`unexpected fetch json call to uri: ${uri}`)
}
pacote.packument = async (spec, opts) => ({
_rev: '1-foobaaa1',
maintainers: npmcliMaintainers,
})
t.teardown(() => {
result = ''
npmlog.error = noop
npmFetch.json = noop
pacote.packument = noop
})
owner.exec(['add', 'foo', '@npmcli/map-workspaces'], err => {
t.match(
err.message,
"I'm a teapot",
'should throw server error response'
)
})
})
t.test('owner add <user> <pkg> no previous maintainers property from server', t => {
result = ''
npmFetch.json = async (uri, opts) => {
// retrieve user info from couchdb request
if (uri === '/-/user/org.couchdb.user:foo') {
return {
_id: 'org.couchdb.user:foo',
email: 'foo@github.com',
name: 'foo',
}
} else if (uri === '/@npmcli%2fno-owners-pkg/-rev/1-foobaaa1')
return {}
else
t.fail(`unexpected fetch json call to uri: ${uri}`)
}
pacote.packument = async (spec, opts) => {
return {
_rev: '1-foobaaa1',
maintainers: null,
}
}
t.teardown(() => {
result = ''
npmFetch.json = noop
pacote.packument = noop
})
owner.exec(['add', 'foo', '@npmcli/no-owners-pkg'], err => {
t.ifError(err, 'npm owner add <user> <pkg>')
t.equal(result, '+ foo (@npmcli/no-owners-pkg)', 'should output add result')
t.end()
})
})
t.test('owner add no user', t => {
result = ''
t.teardown(() => {
result = ''
})
owner.exec(['add'], err => {
t.match(err, /usage instructions/, 'should throw usage instructions if user provided')
t.end()
})
})
t.test('owner add <user> no cwd package', t => {
result = ''
t.teardown(() => {
result = ''
})
owner.exec(['add', 'foo'], err => {
t.match(err, /usage instructions/, 'should throw usage instructions if no user provided')
t.end()
})
})
t.test('owner rm <user> <pkg>', t => {
t.plan(9)
result = ''
npmFetch.json = async (uri, opts) => {
// retrieve user info from couchdb request
if (uri === '/-/user/org.couchdb.user:ruyadorno') {
t.ok('should request user info')
t.match(opts, { ...npm.flatOptions }, 'should use expected opts')
return {
_id: 'org.couchdb.user:ruyadorno',
email: 'ruyadorno@hotmail.com',
name: 'ruyadorno',
}
} else if (uri === '/@npmcli%2fmap-workspaces/-rev/1-foobaaa1') {
t.ok('should put changed owner')
t.match(opts, {
...npm.flatOptions,
method: 'PUT',
body: {
_rev: '1-foobaaa1',
},
otp: '123456',
spec: {
name: '@npmcli/map-workspaces',
},
}, 'should use expected opts')
t.deepEqual(
opts.body.maintainers,
npmcliMaintainers.filter(m => m.name !== 'ruyadorno'),
'should contain expected new owners, removing requested user'
)
return {}
} else
t.fail(`unexpected fetch json call to: ${uri}`)
}
pacote.packument = async (spec, opts) => {
t.equal(spec.name, '@npmcli/map-workspaces', 'should use expect pkg name')
t.match(
opts,
{
...npm.flatOptions,
fullMetadata: true,
},
'should forward expected options to pacote.packument'
)
return {
_rev: '1-foobaaa1',
maintainers: npmcliMaintainers,
}
}
t.teardown(() => {
result = ''
npmFetch.json = noop
pacote.packument = noop
})
owner.exec(['rm', 'ruyadorno', '@npmcli/map-workspaces'], err => {
t.ifError(err, 'npm owner rm <user> <pkg>')
t.equal(result, '- ruyadorno (@npmcli/map-workspaces)', 'should output rm result')
})
})
t.test('owner rm <user> <pkg> not a current owner', t => {
t.plan(3)
result = ''
npmlog.info = (title, msg) => {
t.equal(title, 'owner rm', 'should log expected title')
t.equal(msg, 'Not a package owner: foo', 'should log.info not a package owner msg')
}
npmFetch.json = async (uri, opts) => {
// retrieve user info from couchdb request
if (uri === '/-/user/org.couchdb.user:foo') {
return {
_id: 'org.couchdb.user:foo',
email: 'foo@github.com',
name: 'foo',
}
} else if (uri === '/@npmcli%2fmap-workspaces/-rev/1-foobaaa1')
return {}
else
t.fail(`unexpected fetch json call to: ${uri}`)
}
pacote.packument = async (spec, opts) => {
return {
_rev: '1-foobaaa1',
maintainers: npmcliMaintainers,
}
}
t.teardown(() => {
result = ''
npmlog.info = noop
npmFetch.json = noop
pacote.packument = noop
})
owner.exec(['rm', 'foo', '@npmcli/map-workspaces'], err => {
t.ifError(err, 'npm owner rm <user> <pkg> not a current owner')
})
})
t.test('owner rm <user> cwd package', t => {
result = ''
readLocalPkgResponse = '@npmcli/map-workspaces'
npmFetch.json = async (uri, opts) => {
// retrieve user info from couchdb request
if (uri === '/-/user/org.couchdb.user:ruyadorno') {
return {
_id: 'org.couchdb.user:ruyadorno',
email: 'ruyadorno@hotmail.com',
name: 'ruyadorno',
}
} else if (uri === '/@npmcli%2fmap-workspaces/-rev/1-foobaaa1')
return {}
else
t.fail(`unexpected fetch json call to uri: ${uri}`)
}
pacote.packument = async (spec, opts) => ({
_rev: '1-foobaaa1',
maintainers: npmcliMaintainers,
})
t.teardown(() => {
result = ''
readLocalPkgResponse = null
npmFetch.json = noop
pacote.packument = noop
})
owner.exec(['rm', 'ruyadorno'], err => {
t.ifError(err, 'npm owner rm <user> cwd package')
t.equal(result, '- ruyadorno (@npmcli/map-workspaces)', 'should output rm result')
t.end()
})
})
t.test('owner rm <user> only user', t => {
result = ''
readLocalPkgResponse = 'ipt'
npmFetch.json = async (uri, opts) => {
// retrieve user info from couchdb request
if (uri === '/-/user/org.couchdb.user:ruyadorno') {
return {
_id: 'org.couchdb.user:ruyadorno',
email: 'ruyadorno@hotmail.com',
name: 'ruyadorno',
}
} else
t.fail(`unexpected fetch json call to uri: ${uri}`)
}
pacote.packument = async (spec, opts) => ({
_rev: '1-foobaaa1',
maintainers: [{
name: 'ruyadorno',
email: 'ruyadorno@hotmail.com',
}],
})
t.teardown(() => {
result = ''
readLocalPkgResponse = null
npmFetch.json = noop
pacote.packument = noop
})
owner.exec(['rm', 'ruyadorno'], err => {
t.equal(
err.message,
'Cannot remove all owners of a package. Add someone else first.',
'should throw unable to remove unique owner message'
)
t.equal(err.code, 'EOWNERRM', 'should have expected error code')
t.end()
})
})
t.test('owner rm no user', t => {
result = ''
t.teardown(() => {
result = ''
})
owner.exec(['rm'], err => {
t.match(err, /usage instructions/, 'should throw usage instructions if no user provided to rm')
t.end()
})
})
t.test('owner rm <user> no cwd package', t => {
result = ''
t.teardown(() => {
result = ''
})
owner.exec(['rm', 'foo'], err => {
t.match(err, /usage instructions/, 'should throw usage instructions if no user provided to rm')
t.end()
})
})
t.test('completion', async t => {
const testComp = async (argv, expect) => {
const res = await owner.completion({ conf: { argv: { remain: argv } } })
t.strictSame(res, expect, argv.join(' '))
}
await Promise.all([
testComp(['npm', 'foo'], []),
testComp(['npm', 'owner'], ['add', 'rm', 'ls']),
testComp(['npm', 'owner', 'add'], []),
testComp(['npm', 'owner', 'ls'], []),
testComp(['npm', 'owner', 'rm', 'foo'], []),
])
// npm owner rm completion is async
t.test('completion npm owner rm', async t => {
t.plan(2)
readLocalPkgResponse = '@npmcli/map-workspaces'
pacote.packument = async spec => {
t.equal(spec.name, readLocalPkgResponse, 'should use package spec')
return {
maintainers: npmcliMaintainers,
}
}
t.teardown(() => {
readLocalPkgResponse = null
pacote.packument = noop
})
const res = await owner.completion({ conf: { argv: { remain: ['npm', 'owner', 'rm'] } } })
t.strictSame(res,
['nlf', 'ruyadorno', 'darcyclarke', 'isaacs'],
'should return list of current owners'
)
})
t.test('completion npm owner rm no cwd package', async t => {
const res = await owner.completion({ conf: { argv: { remain: ['npm', 'owner', 'rm'] } } })
t.strictSame(res, [], 'should have no owners to autocomplete if not cwd package')
t.end()
})
t.test('completion npm owner rm no owners found', async t => {
t.plan(2)
readLocalPkgResponse = '@npmcli/map-workspaces'
pacote.packument = async spec => {
t.equal(spec.name, readLocalPkgResponse, 'should use package spec')
return {
maintainers: [],
}
}
t.teardown(() => {
readLocalPkgResponse = null
pacote.packument = noop
})
const res = await owner.completion({ conf: { argv: { remain: ['npm', 'owner', 'rm'] } } })
t.strictSame(res, [], 'should return no owners if not found')
})
t.end()
})
|