-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
verify-npm-package-access.spec.ts
153 lines (120 loc) · 5.25 KB
/
verify-npm-package-access.spec.ts
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
jest.mock('libnpmaccess');
import access from 'libnpmaccess';
import { FetchConfig, Project } from '@lerna-lite/core';
import { loggingOutput } from '@lerna-test/helpers/logging-output';
import { verifyNpmPackageAccess } from '../lib/verify-npm-package-access';
import { initFixtureFactory } from '@lerna-test/helpers';
const initFixture = initFixtureFactory(__dirname);
(access.getPackages as unknown as jest.Mock).mockImplementation(() =>
Promise.resolve({
'package-1': 'read-write',
'package-2': 'read-write',
})
);
describe('verifyNpmPackageAccess', () => {
const origConsoleError = console.error;
let cwd;
beforeAll(async () => {
cwd = await initFixture('lifecycle');
});
beforeEach(() => {
console.error = jest.fn();
});
afterEach(() => {
console.error = origConsoleError;
});
test('validates that all packages have read-write permission', async () => {
const packages = await Project.getPackages(cwd);
const opts = { registry: 'https://registry.npmjs.org/' };
await verifyNpmPackageAccess(packages, 'lerna-test', opts as FetchConfig);
expect(access.getPackages).toHaveBeenLastCalledWith(
'lerna-test',
expect.objectContaining({
registry: 'https://registry.npmjs.org/',
fetchRetries: 0,
})
);
});
test('allows unpublished packages to pass', async () => {
const packages = await Project.getPackages(cwd);
const opts = { registry: 'https://registry.npmjs.org/' };
(access.getPackages as unknown as jest.Mock).mockImplementationOnce(() =>
Promise.resolve({
'package-1': 'read-write',
// unpublished packages don't show up in ls-packages
// "package-2": "read-write",
})
);
await verifyNpmPackageAccess(packages, 'lerna-test', opts as FetchConfig);
expect(access.getPackages).toHaveBeenCalled();
});
test('allows null result to pass with warning', async () => {
const packages = await Project.getPackages(cwd);
const opts = { registry: 'https://registry.npmjs.org/' };
(access.getPackages as unknown as jest.Mock).mockImplementationOnce(() =>
// access.getPackages() returns null when _no_ results returned
Promise.resolve(null)
);
await verifyNpmPackageAccess(packages, 'lerna-test', opts as FetchConfig);
const [logMessage] = loggingOutput('warn');
expect(logMessage).toBe(
'The logged-in user does not have any previously-published packages, skipping permission checks...'
);
});
test('throws EACCESS when any package does not have read-write permission', async () => {
const packages = await Project.getPackages(cwd);
const opts = { registry: 'https://registry.npmjs.org/' };
(access.getPackages as unknown as jest.Mock).mockImplementationOnce(() =>
Promise.resolve({
'package-1': 'read-write',
'package-2': 'read-only',
})
);
const result = verifyNpmPackageAccess(packages, 'lerna-test', opts as FetchConfig);
await expect(result).rejects.toThrow(`You do not have write permission required to publish "package-2"`);
expect(console.error).not.toHaveBeenCalled();
});
test('passes when npm Enterprise registry returns E500', async () => {
const packages = await Project.getPackages(cwd);
const registry = 'http://outdated-npm-enterprise.mycompany.com:12345/';
const opts = { registry };
(access.getPackages as unknown as jest.Mock).mockImplementationOnce(() => {
const err = new Error('npm-enterprise-what') as Error & { code: string };
err.code = 'E500';
return Promise.reject(err);
});
await verifyNpmPackageAccess(packages, 'lerna-test', opts as FetchConfig);
const [logMessage] = loggingOutput('warn');
expect(logMessage).toMatch(
`Registry "${registry}" does not support \`npm access ls-packages\`, skipping permission checks...`
);
expect(console.error).not.toHaveBeenCalled();
});
test('passes when Artifactory registry returns E404', async () => {
const packages = await Project.getPackages(cwd);
const registry = 'https://artifactory-partial-implementation.corpnet.mycompany.com/';
const opts = { registry };
(access.getPackages as unknown as jest.Mock).mockImplementationOnce(() => {
const err = new Error('artifactory-why') as Error & { code: string };
err.code = 'E404';
return Promise.reject(err);
});
await verifyNpmPackageAccess(packages, 'lerna-test', opts as FetchConfig);
const [logMessage] = loggingOutput('warn');
expect(logMessage).toMatch(
`Registry "${registry}" does not support \`npm access ls-packages\`, skipping permission checks...`
);
expect(console.error).not.toHaveBeenCalled();
});
test('logs unexpected failure message before throwing EWHOAMI', async () => {
const packages = await Project.getPackages(cwd);
const opts = {};
(access.getPackages as unknown as jest.Mock).mockImplementationOnce(() => {
const err = new Error('gonna-need-a-bigger-boat');
return Promise.reject(err);
});
const result = verifyNpmPackageAccess(packages, 'lerna-test', opts as FetchConfig);
await expect(result).rejects.toThrow('Authentication error. Use `npm whoami` to troubleshoot.');
expect(console.error).toHaveBeenCalledWith('gonna-need-a-bigger-boat');
});
});