Skip to content

Commit ee3308a

Browse files
wraithgarfritzy
authored andcommitted
fix: remove dead code from get-identity
1 parent 124df81 commit ee3308a

File tree

11 files changed

+159
-375
lines changed

11 files changed

+159
-375
lines changed

lib/utils/get-identity.js

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,24 @@
11
const npmFetch = require('npm-registry-fetch')
22

3-
const needsAuthError = (msg) =>
4-
Object.assign(new Error(msg), { code: 'ENEEDAUTH' })
5-
6-
module.exports = async (npm, opts = {}) => {
3+
module.exports = async (npm, opts) => {
74
const { registry } = opts
8-
if (!registry) {
9-
throw Object.assign(new Error('No registry specified.'), { code: 'ENOREGISTRY' })
10-
}
115

126
// First, check if we have a user/pass-based auth
137
const creds = npm.config.getCredentialsByURI(registry)
14-
const { username: usernameFromURI, token } = creds
8+
if (creds.username) {
9+
return creds.username
10+
}
1511

16-
if (usernameFromURI) {
17-
// Found username; return it
18-
return usernameFromURI
19-
} else if (token) {
20-
// No username, but we have a token; fetch the username from registry
21-
const registryData = await npmFetch.json('/-/whoami', {
22-
...opts,
23-
})
24-
const { username: usernameFromRegistry } = registryData
25-
// Retrieved username from registry; return it
26-
if (usernameFromRegistry) {
27-
return usernameFromRegistry
28-
} else {
29-
// Didn't get username from registry; bad token
30-
throw needsAuthError(
31-
'Your auth token is no longer valid. Please login again.'
32-
)
33-
}
34-
} else {
35-
// At this point, if they have a credentials object, it doesn't have a
36-
// token or auth in it. Probably just the default registry.
37-
throw needsAuthError('This command requires you to be logged in.')
12+
// No username, but we have a token; fetch the username from registry
13+
if (creds.token) {
14+
const registryData = await npmFetch.json('/-/whoami', { ...opts })
15+
return registryData.username
3816
}
17+
18+
// At this point, even if they have a credentials object, it doesn't have a
19+
// valid token.
20+
throw Object.assign(
21+
new Error('This command requires you to be logged in.'),
22+
{ code: 'ENEEDAUTH' }
23+
)
3924
}

tap-snapshots/test/lib/utils/npm-usage.js.test.cjs renamed to tap-snapshots/test/lib/npm.js.test.cjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Make sure to inspect the output below. Do not ignore changes!
66
*/
77
'use strict'
8-
exports[`test/lib/utils/npm-usage.js TAP usage basic usage > must match snapshot 1`] = `
8+
exports[`test/lib/npm.js TAP usage basic usage > must match snapshot 1`] = `
99
npm <command>
1010
1111
Usage:
@@ -41,7 +41,7 @@ Configuration fields: npm help 7 config
4141
npm@{VERSION} {BASEDIR}
4242
`
4343

44-
exports[`test/lib/utils/npm-usage.js TAP usage set process.stdout.columns columns=0 > must match snapshot 1`] = `
44+
exports[`test/lib/npm.js TAP usage set process.stdout.columns columns=0 > must match snapshot 1`] = `
4545
npm <command>
4646
4747
Usage:
@@ -77,7 +77,7 @@ Configuration fields: npm help 7 config
7777
npm@{VERSION} {BASEDIR}
7878
`
7979

80-
exports[`test/lib/utils/npm-usage.js TAP usage set process.stdout.columns columns=90 > must match snapshot 1`] = `
80+
exports[`test/lib/npm.js TAP usage set process.stdout.columns columns=90 > must match snapshot 1`] = `
8181
npm <command>
8282
8383
Usage:
@@ -113,7 +113,7 @@ Configuration fields: npm help 7 config
113113
npm@{VERSION} {BASEDIR}
114114
`
115115

116-
exports[`test/lib/utils/npm-usage.js TAP usage with browser > must match snapshot 1`] = `
116+
exports[`test/lib/npm.js TAP usage with browser > must match snapshot 1`] = `
117117
npm <command>
118118
119119
Usage:
@@ -149,7 +149,7 @@ Configuration fields: npm help 7 config
149149
npm@{VERSION} {BASEDIR}
150150
`
151151

152-
exports[`test/lib/utils/npm-usage.js TAP usage with long > must match snapshot 1`] = `
152+
exports[`test/lib/npm.js TAP usage with long > must match snapshot 1`] = `
153153
npm <command>
154154
155155
Usage:

test/fixtures/mock-registry.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ class MockRegistry {
192192
}).reply(200, { ...manifest, users })
193193
}
194194

195+
ping ({ body = {}, responseCode = 200 } = {}) {
196+
this.nock = this.nock.get('/-/ping?write=true').reply(responseCode, body)
197+
}
198+
195199
async package ({ manifest, times = 1, query, tarballs }) {
196200
let nock = this.nock
197201
const spec = npa(manifest.name)

test/lib/commands/deprecate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ t.test('completion', async t => {
4444

4545
registry.whoami({ statusCode: 404, body: {} })
4646

47-
t.rejects(testComp([], []), { code: 'ENEEDAUTH' })
47+
t.rejects(testComp([], []), { code: 'EINVALIDTYPE' })
4848
})
4949

5050
t.test('no args', async t => {

test/lib/commands/ping.js

Lines changed: 54 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,67 @@
11
const t = require('tap')
2-
const { fake: mockNpm } = require('../../fixtures/mock-npm')
2+
const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
3+
const MockRegistry = require('../../fixtures/mock-registry.js')
34

4-
t.test('pings', async t => {
5-
t.plan(6)
6-
7-
const registry = 'https://registry.npmjs.org'
8-
let noticeCalls = 0
9-
const Ping = t.mock('../../../lib/commands/ping.js', {
10-
'../../../lib/utils/ping.js': function (spec) {
11-
t.equal(spec.registry, registry, 'passes flatOptions')
12-
return {}
13-
},
14-
'proc-log': {
15-
notice: (type, spec) => {
16-
++noticeCalls
17-
if (noticeCalls === 1) {
18-
t.equal(type, 'PING', 'should log a PING')
19-
t.equal(spec, registry, 'should log the registry url')
20-
} else {
21-
t.equal(type, 'PONG', 'should log a PONG')
22-
t.match(spec, /\d+ms/, 'should log the elapsed milliseconds')
23-
}
24-
},
25-
},
26-
})
27-
const npm = mockNpm({
28-
config: { registry },
29-
flatOptions: { registry },
5+
t.test('no details', async t => {
6+
const { npm, logs, joinedOutput } = await loadMockNpm(t)
7+
const registry = new MockRegistry({
8+
tap: t,
9+
registry: npm.config.get('registry'),
3010
})
31-
const ping = new Ping(npm)
32-
33-
await ping.exec([])
34-
t.equal(noticeCalls, 2, 'should have logged 2 lines')
11+
registry.ping()
12+
await npm.exec('ping', [])
13+
t.match(logs.notice, [['PING', 'https://registry.npmjs.org/'], ['PONG', /[0-9]+ms/]])
14+
t.equal(joinedOutput(), '')
3515
})
3616

37-
t.test('pings and logs details', async t => {
38-
t.plan(8)
17+
t.test('with details', async t => {
18+
const { npm, logs, joinedOutput } = await loadMockNpm(t)
19+
const registry = new MockRegistry({
20+
tap: t,
21+
registry: npm.config.get('registry'),
22+
})
23+
registry.ping({ body: { test: true } })
24+
await npm.exec('ping', [])
25+
t.match(logs.notice, [
26+
['PING', 'https://registry.npmjs.org/'],
27+
['PONG', /[0-9]+ms/],
28+
['PONG', '{\n "test": true\n}'],
29+
])
30+
t.match(joinedOutput(), '')
31+
})
3932

40-
const registry = 'https://registry.npmjs.org'
41-
const details = { extra: 'data' }
42-
let noticeCalls = 0
43-
const Ping = t.mock('../../../lib/commands/ping.js', {
44-
'../../../lib/utils/ping.js': function (spec) {
45-
t.equal(spec.registry, registry, 'passes flatOptions')
46-
return details
47-
},
48-
'proc-log': {
49-
notice: (type, spec) => {
50-
++noticeCalls
51-
if (noticeCalls === 1) {
52-
t.equal(type, 'PING', 'should log a PING')
53-
t.equal(spec, registry, 'should log the registry url')
54-
} else if (noticeCalls === 2) {
55-
t.equal(type, 'PONG', 'should log a PONG')
56-
t.match(spec, /\d+ms/, 'should log the elapsed milliseconds')
57-
} else {
58-
t.equal(type, 'PONG', 'should log a PONG')
59-
const parsed = JSON.parse(spec)
60-
t.match(parsed, details, 'should log JSON stringified details')
61-
}
62-
},
63-
},
33+
t.test('valid json', async t => {
34+
const { npm, logs, joinedOutput } = await loadMockNpm(t, {
35+
config: { json: true },
6436
})
65-
const npm = mockNpm({
66-
config: { registry },
67-
flatOptions: { registry },
37+
const registry = new MockRegistry({
38+
tap: t,
39+
registry: npm.config.get('registry'),
40+
})
41+
registry.ping()
42+
await npm.exec('ping', [])
43+
t.match(logs.notice, [['PING', 'https://registry.npmjs.org/'], ['PONG', /[0-9]+ms/]])
44+
t.match(JSON.parse(joinedOutput()), {
45+
registry: npm.config.get('registry'),
46+
time: /[0-9]+/,
47+
details: {},
6848
})
69-
const ping = new Ping(npm)
70-
71-
await ping.exec([])
72-
t.equal(noticeCalls, 3, 'should have logged 3 lines')
7349
})
7450

75-
t.test('pings and returns json', async t => {
76-
t.plan(9)
77-
78-
const registry = 'https://registry.npmjs.org'
79-
const details = { extra: 'data' }
80-
let noticeCalls = 0
81-
const Ping = t.mock('../../../lib/commands/ping.js', {
82-
'../../../lib/utils/ping.js': function (spec) {
83-
t.equal(spec.registry, registry, 'passes flatOptions')
84-
return details
85-
},
86-
'proc-log': {
87-
notice: (type, spec) => {
88-
++noticeCalls
89-
if (noticeCalls === 1) {
90-
t.equal(type, 'PING', 'should log a PING')
91-
t.equal(spec, registry, 'should log the registry url')
92-
} else {
93-
t.equal(type, 'PONG', 'should log a PONG')
94-
t.match(spec, /\d+ms/, 'should log the elapsed milliseconds')
95-
}
96-
},
97-
},
51+
t.test('invalid json', async t => {
52+
const { npm, logs, joinedOutput } = await loadMockNpm(t, {
53+
config: { json: true },
9854
})
99-
const npm = mockNpm({
100-
config: { registry, json: true },
101-
flatOptions: { registry },
102-
output: function (spec) {
103-
const parsed = JSON.parse(spec)
104-
t.equal(parsed.registry, registry, 'returns the correct registry url')
105-
t.match(parsed.details, details, 'prints returned details')
106-
t.type(parsed.time, 'number', 'returns time as a number')
107-
},
55+
const registry = new MockRegistry({
56+
tap: t,
57+
registry: npm.config.get('registry'),
58+
})
59+
registry.ping({ body: '{not: real"json]' })
60+
await npm.exec('ping', [])
61+
t.match(logs.notice, [['PING', 'https://registry.npmjs.org/'], ['PONG', /[0-9]+ms/]])
62+
t.match(JSON.parse(joinedOutput()), {
63+
registry: npm.config.get('registry'),
64+
time: /[0-9]+/,
65+
details: {},
10866
})
109-
const ping = new Ping(npm)
110-
111-
await ping.exec([])
112-
t.equal(noticeCalls, 2, 'should have logged 2 lines')
11367
})

test/lib/commands/whoami.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const MockRegistry = require('../../fixtures/mock-registry.js')
55
const username = 'foo'
66
const auth = { '//registry.npmjs.org/:_authToken': 'test-auth-token' }
77

8-
t.test('npm whoami', async (t) => {
8+
t.test('npm whoami', async t => {
99
const { npm, joinedOutput } = await loadMockNpm(t, { config: auth })
1010
const registry = new MockRegistry({
1111
tap: t,
@@ -17,7 +17,7 @@ t.test('npm whoami', async (t) => {
1717
t.equal(joinedOutput(), username, 'should print username')
1818
})
1919

20-
t.test('npm whoami --json', async (t) => {
20+
t.test('npm whoami --json', async t => {
2121
const { npm, joinedOutput } = await loadMockNpm(t, {
2222
config: {
2323
json: true,
@@ -33,3 +33,23 @@ t.test('npm whoami --json', async (t) => {
3333
await npm.exec('whoami', [])
3434
t.equal(JSON.parse(joinedOutput()), username, 'should print username')
3535
})
36+
37+
t.test('credentials from token', async t => {
38+
const { npm, joinedOutput } = await loadMockNpm(t, {
39+
config: {
40+
'//registry.npmjs.org/:username': username,
41+
'//registry.npmjs.org/:_password': 'hunter2',
42+
},
43+
})
44+
await npm.exec('whoami', [])
45+
t.equal(joinedOutput(), username, 'should print username')
46+
})
47+
48+
t.test('not logged in', async t => {
49+
const { npm } = await loadMockNpm(t, {
50+
config: {
51+
json: true,
52+
},
53+
})
54+
await t.rejects(npm.exec('whoami', []), { code: 'ENEEDAUTH' })
55+
})

test/lib/npm.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,61 @@ t.test('implicit workspace accept', async t => {
654654
})
655655
await t.rejects(mock.npm.exec('org', []), /.*Usage/)
656656
})
657+
658+
t.test('usage', async t => {
659+
const { npm } = await loadMockNpm(t)
660+
t.afterEach(() => {
661+
npm.config.set('viewer', null)
662+
npm.config.set('long', false)
663+
npm.config.set('userconfig', '/some/config/file/.npmrc')
664+
})
665+
const { dirname } = require('path')
666+
const basedir = dirname(dirname(__dirname))
667+
t.cleanSnapshot = str => str.split(basedir).join('{BASEDIR}')
668+
.split(require('../../package.json').version).join('{VERSION}')
669+
670+
npm.config.set('viewer', null)
671+
npm.config.set('long', false)
672+
npm.config.set('userconfig', '/some/config/file/.npmrc')
673+
674+
t.test('basic usage', async t => {
675+
t.matchSnapshot(await npm.usage)
676+
t.end()
677+
})
678+
679+
t.test('with browser', async t => {
680+
npm.config.set('viewer', 'browser')
681+
t.matchSnapshot(await npm.usage)
682+
t.end()
683+
})
684+
685+
t.test('with long', async t => {
686+
npm.config.set('long', true)
687+
t.matchSnapshot(await npm.usage)
688+
t.end()
689+
})
690+
691+
t.test('set process.stdout.columns', async t => {
692+
const { columns } = process.stdout
693+
t.teardown(() => {
694+
Object.defineProperty(process.stdout, 'columns', {
695+
value: columns,
696+
enumerable: true,
697+
configurable: true,
698+
writable: true,
699+
})
700+
})
701+
const cases = [0, 90]
702+
for (const cols of cases) {
703+
t.test(`columns=${cols}`, async t => {
704+
Object.defineProperty(process.stdout, 'columns', {
705+
value: cols,
706+
enumerable: true,
707+
configurable: true,
708+
writable: true,
709+
})
710+
t.matchSnapshot(await npm.usage)
711+
})
712+
}
713+
})
714+
})

0 commit comments

Comments
 (0)