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
|
const t = require('tap')
t.test('throws ENOREGISTRY when no registry option is provided', async (t) => {
t.plan(2)
const getIdentity = t.mock('../../../lib/utils/get-identity.js')
try {
await getIdentity({})
} catch (err) {
t.equal(err.code, 'ENOREGISTRY', 'assigns the appropriate error code')
t.equal(err.message, 'No registry specified.', 'returns the correct error message')
}
})
t.test('returns username from uri when provided', async (t) => {
t.plan(1)
const getIdentity = t.mock('../../../lib/utils/get-identity.js')
const npm = {
config: {
getCredentialsByURI: () => {
return { username: 'foo' }
},
},
}
const identity = await getIdentity(npm, { registry: 'https://registry.npmjs.org' })
t.equal(identity, 'foo', 'returns username from uri')
})
t.test('calls registry whoami when token is provided', async (t) => {
t.plan(3)
const options = {
registry: 'https://registry.npmjs.org',
token: 'thisisnotreallyatoken',
}
const getIdentity = t.mock('../../../lib/utils/get-identity.js', {
'npm-registry-fetch': {
json: (path, opts) => {
t.equal(path, '/-/whoami', 'calls whoami')
t.same(opts, options, 'passes through provided options')
return { username: 'foo' }
},
},
})
const npm = {
config: {
getCredentialsByURI: () => options,
},
}
const identity = await getIdentity(npm, options)
t.equal(identity, 'foo', 'fetched username from registry')
})
t.test('throws ENEEDAUTH when response does not include a username', async (t) => {
t.plan(3)
const options = {
registry: 'https://registry.npmjs.org',
token: 'thisisnotreallyatoken',
}
const getIdentity = t.mock('../../../lib/utils/get-identity.js', {
'npm-registry-fetch': {
json: (path, opts) => {
t.equal(path, '/-/whoami', 'calls whoami')
t.same(opts, options, 'passes through provided options')
return {}
},
},
})
const npm = {
config: {
getCredentialsByURI: () => options,
},
}
try {
await getIdentity(npm, options)
} catch (err) {
t.equal(err.code, 'ENEEDAUTH', 'throws correct error code')
}
})
t.test('throws ENEEDAUTH when neither username nor token is configured', async (t) => {
t.plan(1)
const getIdentity = t.mock('../../../lib/utils/get-identity.js', {
})
const npm = {
config: {
getCredentialsByURI: () => ({}),
},
}
try {
await getIdentity(npm, { registry: 'https://registry.npmjs.org' })
} catch (err) {
t.equal(err.code, 'ENEEDAUTH', 'throws correct error code')
}
})
|