/
npmrc.spec.ts
56 lines (48 loc) · 1.82 KB
/
npmrc.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
import { getName, mocked } from '../../../test/util';
import { setAdminConfig } from '../../config/admin';
import * as _sanitize from '../../util/sanitize';
import { getNpmrc, setNpmrc } from './npmrc';
jest.mock('../../util/sanitize');
const sanitize = mocked(_sanitize);
describe(getName(__filename), () => {
beforeEach(() => {
setNpmrc('');
setAdminConfig();
jest.resetAllMocks();
});
it('sanitize _auth', () => {
setNpmrc('_auth=test');
expect(sanitize.add).toHaveBeenCalledWith('test');
expect(sanitize.add).toHaveBeenCalledTimes(1);
});
it('sanitize _authtoken', () => {
// eslint-disable-next-line no-template-curly-in-string
setNpmrc('//registry.test.com:_authToken=test\n_authToken=${NPM_TOKEN}');
expect(sanitize.add).toHaveBeenCalledWith('test');
expect(sanitize.add).toHaveBeenCalledTimes(1);
});
it('sanitize _password', () => {
setNpmrc(
`registry=https://test.org\n//test.org/:username=test\n//test.org/:_password=dGVzdA==`
);
expect(sanitize.add).toHaveBeenNthCalledWith(1, 'dGVzdA==');
expect(sanitize.add).toHaveBeenNthCalledWith(2, 'test');
expect(sanitize.add).toHaveBeenNthCalledWith(3, 'dGVzdDp0ZXN0');
expect(sanitize.add).toHaveBeenCalledTimes(3);
});
it('sanitize _authtoken with high trust', () => {
setAdminConfig({ trustLevel: 'high' });
process.env.TEST_TOKEN = 'test';
setNpmrc(
// eslint-disable-next-line no-template-curly-in-string
'//registry.test.com:_authToken=${TEST_TOKEN}\n_authToken=\nregistry=http://localhost'
);
expect(sanitize.add).toHaveBeenCalledWith('test');
expect(sanitize.add).toHaveBeenCalledTimes(1);
});
it('ignores localhost', () => {
setNpmrc(`registry=http://localhost`);
expect(sanitize.add).toHaveBeenCalledTimes(0);
expect(getNpmrc()).toBeNull();
});
});