2022-11-17 14:35:58 +01:00
|
|
|
import os from 'os';
|
2020-03-10 16:51:57 +01:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2020-05-19 15:25:54 +02:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as io from '@actions/io';
|
2019-08-07 00:26:04 +02:00
|
|
|
import * as auth from '../src/authutil';
|
|
|
|
|
2020-03-10 16:51:57 +01:00
|
|
|
let rcFile: string;
|
|
|
|
|
2020-05-19 15:25:54 +02:00
|
|
|
describe('authutil tests', () => {
|
|
|
|
const _runnerDir = path.join(__dirname, 'runner');
|
|
|
|
|
|
|
|
let cnSpy: jest.SpyInstance;
|
|
|
|
let logSpy: jest.SpyInstance;
|
|
|
|
let dbgSpy: jest.SpyInstance;
|
|
|
|
|
2019-08-07 00:26:04 +02:00
|
|
|
beforeAll(async () => {
|
2023-03-08 09:47:38 +01:00
|
|
|
const randPath = path.join(Math.random().toString(36).substring(7));
|
2020-10-01 18:03:22 +02:00
|
|
|
console.log('::stop-commands::stoptoken'); // Disable executing of runner commands when running tests in actions
|
|
|
|
process.env['GITHUB_ENV'] = ''; // Stub out Environment file functionality so we can verify it writes to standard out (toolkit is backwards compatible)
|
2020-05-19 15:25:54 +02:00
|
|
|
const tempDir = path.join(_runnerDir, randPath, 'temp');
|
2019-08-07 00:26:04 +02:00
|
|
|
await io.rmRF(tempDir);
|
|
|
|
await io.mkdirP(tempDir);
|
2020-03-10 16:51:57 +01:00
|
|
|
process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo';
|
|
|
|
process.env['RUNNER_TEMP'] = tempDir;
|
|
|
|
rcFile = path.join(tempDir, '.npmrc');
|
2019-08-07 00:26:04 +02:00
|
|
|
}, 100000);
|
|
|
|
|
2020-05-19 15:25:54 +02:00
|
|
|
beforeEach(async () => {
|
|
|
|
await io.rmRF(rcFile);
|
|
|
|
// if (fs.existsSync(rcFile)) {
|
|
|
|
// fs.unlinkSync(rcFile);
|
|
|
|
// }
|
2019-08-07 00:26:04 +02:00
|
|
|
process.env['INPUT_SCOPE'] = '';
|
2020-05-19 15:25:54 +02:00
|
|
|
|
|
|
|
// writes
|
|
|
|
cnSpy = jest.spyOn(process.stdout, 'write');
|
|
|
|
logSpy = jest.spyOn(console, 'log');
|
|
|
|
dbgSpy = jest.spyOn(core, 'debug');
|
|
|
|
cnSpy.mockImplementation(line => {
|
|
|
|
// uncomment to debug
|
|
|
|
// process.stderr.write('write:' + line + '\n');
|
|
|
|
});
|
|
|
|
logSpy.mockImplementation(line => {
|
|
|
|
// uncomment to debug
|
|
|
|
// process.stderr.write('log:' + line + '\n');
|
|
|
|
});
|
|
|
|
dbgSpy.mockImplementation(msg => {
|
|
|
|
// uncomment to see debug output
|
|
|
|
// process.stderr.write(msg + '\n');
|
|
|
|
});
|
|
|
|
}, 100000);
|
|
|
|
|
|
|
|
function dbg(message: string) {
|
|
|
|
process.stderr.write('dbg::' + message + '::\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
if (_runnerDir) {
|
|
|
|
await io.rmRF(_runnerDir);
|
|
|
|
}
|
2020-10-01 18:03:22 +02:00
|
|
|
console.log('::stoptoken::'); // Re-enable executing of runner commands when running tests in actions
|
2020-05-19 15:25:54 +02:00
|
|
|
}, 100000);
|
|
|
|
|
|
|
|
function readRcFile(rcFile: string) {
|
2023-03-08 09:47:38 +01:00
|
|
|
const rc = {};
|
|
|
|
const contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
|
2020-05-19 15:25:54 +02:00
|
|
|
for (const line of contents.split(os.EOL)) {
|
2023-03-08 09:47:38 +01:00
|
|
|
const parts = line.split('=');
|
2020-05-19 15:25:54 +02:00
|
|
|
if (parts.length == 2) {
|
|
|
|
rc[parts[0].trim()] = parts[1].trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2019-08-07 00:26:04 +02:00
|
|
|
|
|
|
|
it('Sets up npmrc for npmjs', async () => {
|
2019-09-03 16:57:45 +02:00
|
|
|
await auth.configAuthentication('https://registry.npmjs.org/', 'false');
|
2020-05-19 15:25:54 +02:00
|
|
|
|
|
|
|
expect(fs.statSync(rcFile)).toBeDefined();
|
2023-03-08 09:47:38 +01:00
|
|
|
const contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
|
|
|
|
const rc = readRcFile(rcFile);
|
2020-05-19 15:25:54 +02:00
|
|
|
expect(rc['registry']).toBe('https://registry.npmjs.org/');
|
|
|
|
expect(rc['always-auth']).toBe('false');
|
2019-08-07 00:26:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Appends trailing slash to registry', async () => {
|
2019-09-03 16:57:45 +02:00
|
|
|
await auth.configAuthentication('https://registry.npmjs.org', 'false');
|
2019-08-07 00:26:04 +02:00
|
|
|
|
2020-05-19 15:25:54 +02:00
|
|
|
expect(fs.statSync(rcFile)).toBeDefined();
|
2023-03-08 09:47:38 +01:00
|
|
|
const rc = readRcFile(rcFile);
|
2020-05-19 15:25:54 +02:00
|
|
|
expect(rc['registry']).toBe('https://registry.npmjs.org/');
|
|
|
|
expect(rc['always-auth']).toBe('false');
|
2019-08-07 00:26:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Configures scoped npm registries', async () => {
|
|
|
|
process.env['INPUT_SCOPE'] = 'myScope';
|
2019-09-03 16:57:45 +02:00
|
|
|
await auth.configAuthentication('https://registry.npmjs.org', 'false');
|
2019-08-07 00:26:04 +02:00
|
|
|
|
2020-05-19 15:25:54 +02:00
|
|
|
expect(fs.statSync(rcFile)).toBeDefined();
|
2023-03-08 09:47:38 +01:00
|
|
|
const rc = readRcFile(rcFile);
|
2020-05-19 15:25:54 +02:00
|
|
|
expect(rc['@myscope:registry']).toBe('https://registry.npmjs.org/');
|
|
|
|
expect(rc['always-auth']).toBe('false');
|
2019-08-07 00:26:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Automatically configures GPR scope', async () => {
|
2019-09-03 16:57:45 +02:00
|
|
|
await auth.configAuthentication('npm.pkg.github.com', 'false');
|
2019-08-07 00:26:04 +02:00
|
|
|
|
2020-05-19 15:25:54 +02:00
|
|
|
expect(fs.statSync(rcFile)).toBeDefined();
|
2023-03-08 09:47:38 +01:00
|
|
|
const rc = readRcFile(rcFile);
|
2020-05-19 15:25:54 +02:00
|
|
|
expect(rc['@ownername:registry']).toBe('npm.pkg.github.com/');
|
|
|
|
expect(rc['always-auth']).toBe('false');
|
2019-08-07 00:26:04 +02:00
|
|
|
});
|
2019-09-03 16:57:45 +02:00
|
|
|
|
|
|
|
it('Sets up npmrc for always-auth true', async () => {
|
|
|
|
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
|
2020-05-19 15:25:54 +02:00
|
|
|
expect(fs.statSync(rcFile)).toBeDefined();
|
2023-03-08 09:47:38 +01:00
|
|
|
const rc = readRcFile(rcFile);
|
2020-05-19 15:25:54 +02:00
|
|
|
expect(rc['registry']).toBe('https://registry.npmjs.org/');
|
|
|
|
expect(rc['always-auth']).toBe('true');
|
2020-10-22 01:31:20 +02:00
|
|
|
});
|
2022-12-05 13:36:23 +01:00
|
|
|
|
2023-03-08 09:47:38 +01:00
|
|
|
it('is already set the NODE_AUTH_TOKEN export it', async () => {
|
2020-10-22 01:31:20 +02:00
|
|
|
process.env.NODE_AUTH_TOKEN = 'foobar';
|
2020-10-22 01:14:34 +02:00
|
|
|
await auth.configAuthentication('npm.pkg.github.com', 'false');
|
|
|
|
expect(fs.statSync(rcFile)).toBeDefined();
|
2023-03-08 09:47:38 +01:00
|
|
|
const rc = readRcFile(rcFile);
|
2020-10-22 01:14:34 +02:00
|
|
|
expect(rc['@ownername:registry']).toBe('npm.pkg.github.com/');
|
|
|
|
expect(rc['always-auth']).toBe('false');
|
|
|
|
expect(process.env.NODE_AUTH_TOKEN).toEqual('foobar');
|
2020-10-22 01:31:20 +02:00
|
|
|
});
|
2022-12-05 13:36:23 +01:00
|
|
|
|
|
|
|
it('configAuthentication should overwrite non-scoped with non-scoped', async () => {
|
|
|
|
fs.writeFileSync(rcFile, 'registry=NNN');
|
|
|
|
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
|
2023-03-08 09:47:38 +01:00
|
|
|
const contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
|
2022-12-05 13:36:23 +01:00
|
|
|
expect(contents).toBe(
|
|
|
|
`//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('configAuthentication should overwrite only non-scoped', async () => {
|
|
|
|
fs.writeFileSync(rcFile, `registry=NNN${os.EOL}@myscope:registry=MMM`);
|
|
|
|
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
|
2023-03-08 09:47:38 +01:00
|
|
|
const contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
|
2022-12-05 13:36:23 +01:00
|
|
|
expect(contents).toBe(
|
|
|
|
`@myscope:registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('configAuthentication should add non-scoped to scoped', async () => {
|
|
|
|
fs.writeFileSync(rcFile, '@myscope:registry=NNN');
|
|
|
|
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
|
2023-03-08 09:47:38 +01:00
|
|
|
const contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
|
2022-12-05 13:36:23 +01:00
|
|
|
expect(contents).toBe(
|
|
|
|
`@myscope:registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('configAuthentication should overwrite scoped with scoped', async () => {
|
|
|
|
process.env['INPUT_SCOPE'] = 'myscope';
|
|
|
|
fs.writeFileSync(rcFile, `@myscope:registry=NNN`);
|
|
|
|
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
|
2023-03-08 09:47:38 +01:00
|
|
|
const contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
|
2022-12-05 13:36:23 +01:00
|
|
|
expect(contents).toBe(
|
|
|
|
`//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('configAuthentication should overwrite only scoped', async () => {
|
|
|
|
process.env['INPUT_SCOPE'] = 'myscope';
|
|
|
|
fs.writeFileSync(rcFile, `registry=NNN${os.EOL}@myscope:registry=MMM`);
|
|
|
|
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
|
2023-03-08 09:47:38 +01:00
|
|
|
const contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
|
2022-12-05 13:36:23 +01:00
|
|
|
expect(contents).toBe(
|
|
|
|
`registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('configAuthentication should add scoped to non-scoped', async () => {
|
|
|
|
process.env['INPUT_SCOPE'] = 'myscope';
|
|
|
|
fs.writeFileSync(rcFile, `registry=MMM`);
|
|
|
|
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
|
2023-03-08 09:47:38 +01:00
|
|
|
const contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
|
2022-12-05 13:36:23 +01:00
|
|
|
expect(contents).toBe(
|
|
|
|
`registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('configAuthentication should overwrite only one scoped', async () => {
|
|
|
|
process.env['INPUT_SCOPE'] = 'myscope';
|
|
|
|
fs.writeFileSync(
|
|
|
|
rcFile,
|
|
|
|
`@otherscope:registry=NNN${os.EOL}@myscope:registry=MMM`
|
|
|
|
);
|
|
|
|
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
|
2023-03-08 09:47:38 +01:00
|
|
|
const contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
|
2022-12-05 13:36:23 +01:00
|
|
|
expect(contents).toBe(
|
|
|
|
`@otherscope:registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('configAuthentication should add scoped to another scoped', async () => {
|
|
|
|
process.env['INPUT_SCOPE'] = 'myscope';
|
|
|
|
fs.writeFileSync(rcFile, `@otherscope:registry=MMM`);
|
|
|
|
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
|
2023-03-08 09:47:38 +01:00
|
|
|
const contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
|
2022-12-05 13:36:23 +01:00
|
|
|
expect(contents).toBe(
|
|
|
|
`@otherscope:registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
|
|
|
|
);
|
|
|
|
});
|
2019-08-07 00:26:04 +02:00
|
|
|
});
|