2019-11-15 19:01:13 -05:00
|
|
|
import io = require('@actions/io');
|
|
|
|
import fs = require('fs');
|
|
|
|
import path = require('path');
|
2021-08-23 04:31:06 -04:00
|
|
|
import * as core from '@actions/core';
|
2021-04-05 06:02:27 -04:00
|
|
|
import os from 'os';
|
2019-11-15 19:01:13 -05:00
|
|
|
|
|
|
|
import * as auth from '../src/auth';
|
|
|
|
|
2019-11-20 13:25:21 -05:00
|
|
|
const m2Dir = path.join(__dirname, auth.M2_DIR);
|
|
|
|
const settingsFile = path.join(m2Dir, auth.SETTINGS_FILE);
|
|
|
|
|
2019-11-15 19:01:13 -05:00
|
|
|
describe('auth tests', () => {
|
2021-04-05 06:02:27 -04:00
|
|
|
let spyOSHomedir: jest.SpyInstance;
|
2021-08-23 04:31:06 -04:00
|
|
|
let spyInfo: jest.SpyInstance;
|
2021-04-05 06:02:27 -04:00
|
|
|
|
2019-11-20 13:25:21 -05:00
|
|
|
beforeEach(async () => {
|
2019-11-15 19:01:13 -05:00
|
|
|
await io.rmRF(m2Dir);
|
2021-04-05 06:02:27 -04:00
|
|
|
spyOSHomedir = jest.spyOn(os, 'homedir');
|
|
|
|
spyOSHomedir.mockReturnValue(__dirname);
|
2021-08-23 04:31:06 -04:00
|
|
|
spyInfo = jest.spyOn(core, 'info');
|
|
|
|
spyInfo.mockImplementation(() => null);
|
2019-11-15 19:01:13 -05:00
|
|
|
}, 300000);
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
try {
|
|
|
|
await io.rmRF(m2Dir);
|
|
|
|
} catch {
|
|
|
|
console.log('Failed to remove test directories');
|
|
|
|
}
|
2021-04-05 06:02:27 -04:00
|
|
|
jest.resetAllMocks();
|
|
|
|
jest.clearAllMocks();
|
|
|
|
jest.restoreAllMocks();
|
2019-11-15 19:01:13 -05:00
|
|
|
}, 100000);
|
|
|
|
|
2019-12-10 13:03:33 -05:00
|
|
|
it('creates settings.xml in alternate locations', async () => {
|
|
|
|
const id = 'packages';
|
2019-12-19 14:28:11 -05:00
|
|
|
const username = 'UNAMI';
|
|
|
|
const password = 'TOLKIEN';
|
2019-12-10 13:03:33 -05:00
|
|
|
|
|
|
|
const altHome = path.join(__dirname, 'runner', 'settings');
|
|
|
|
const altSettingsFile = path.join(altHome, auth.SETTINGS_FILE);
|
|
|
|
await io.rmRF(altHome); // ensure it doesn't already exist
|
|
|
|
|
2021-04-05 06:02:27 -04:00
|
|
|
await auth.createAuthenticationSettings(id, username, password, altHome, true);
|
2019-12-10 13:03:33 -05:00
|
|
|
|
|
|
|
expect(fs.existsSync(m2Dir)).toBe(false);
|
|
|
|
expect(fs.existsSync(settingsFile)).toBe(false);
|
|
|
|
|
|
|
|
expect(fs.existsSync(altHome)).toBe(true);
|
|
|
|
expect(fs.existsSync(altSettingsFile)).toBe(true);
|
|
|
|
expect(fs.readFileSync(altSettingsFile, 'utf-8')).toEqual(
|
|
|
|
auth.generate(id, username, password)
|
|
|
|
);
|
|
|
|
|
|
|
|
await io.rmRF(altHome);
|
|
|
|
}, 100000);
|
|
|
|
|
2020-05-02 07:33:15 -04:00
|
|
|
it('creates settings.xml with minimal configuration', async () => {
|
2019-11-28 15:40:08 -05:00
|
|
|
const id = 'packages';
|
2019-12-19 14:28:11 -05:00
|
|
|
const username = 'UNAME';
|
|
|
|
const password = 'TOKEN';
|
2019-11-15 19:01:13 -05:00
|
|
|
|
2021-04-05 06:02:27 -04:00
|
|
|
await auth.createAuthenticationSettings(id, username, password, m2Dir, true);
|
2019-11-15 19:01:13 -05:00
|
|
|
|
|
|
|
expect(fs.existsSync(m2Dir)).toBe(true);
|
|
|
|
expect(fs.existsSync(settingsFile)).toBe(true);
|
2021-04-05 06:02:27 -04:00
|
|
|
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(auth.generate(id, username, password));
|
2019-11-15 19:01:13 -05:00
|
|
|
}, 100000);
|
2019-11-20 13:25:21 -05:00
|
|
|
|
2020-05-02 07:33:15 -04:00
|
|
|
it('creates settings.xml with additional configuration', async () => {
|
2019-12-06 14:46:35 -05:00
|
|
|
const id = 'packages';
|
2020-05-02 07:33:15 -04:00
|
|
|
const username = 'UNAME';
|
|
|
|
const password = 'TOKEN';
|
|
|
|
const gpgPassphrase = 'GPG';
|
2019-12-06 14:46:35 -05:00
|
|
|
|
2021-04-05 06:02:27 -04:00
|
|
|
await auth.createAuthenticationSettings(id, username, password, m2Dir, true, gpgPassphrase);
|
2019-12-06 14:46:35 -05:00
|
|
|
|
|
|
|
expect(fs.existsSync(m2Dir)).toBe(true);
|
|
|
|
expect(fs.existsSync(settingsFile)).toBe(true);
|
|
|
|
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
|
2020-05-02 07:33:15 -04:00
|
|
|
auth.generate(id, username, password, gpgPassphrase)
|
2019-12-06 14:46:35 -05:00
|
|
|
);
|
|
|
|
}, 100000);
|
|
|
|
|
2020-05-02 07:33:15 -04:00
|
|
|
it('overwrites existing settings.xml files', async () => {
|
|
|
|
const id = 'packages';
|
|
|
|
const username = 'USERNAME';
|
|
|
|
const password = 'PASSWORD';
|
2019-11-28 15:40:08 -05:00
|
|
|
|
2021-04-05 06:02:27 -04:00
|
|
|
fs.mkdirSync(m2Dir, { recursive: true });
|
2020-05-02 07:33:15 -04:00
|
|
|
fs.writeFileSync(settingsFile, 'FAKE FILE');
|
2019-12-19 14:28:11 -05:00
|
|
|
expect(fs.existsSync(m2Dir)).toBe(true);
|
|
|
|
expect(fs.existsSync(settingsFile)).toBe(true);
|
2019-11-28 15:40:08 -05:00
|
|
|
|
2021-04-05 06:02:27 -04:00
|
|
|
await auth.createAuthenticationSettings(id, username, password, m2Dir, true);
|
2019-11-20 13:25:21 -05:00
|
|
|
|
2019-12-19 14:28:11 -05:00
|
|
|
expect(fs.existsSync(m2Dir)).toBe(true);
|
|
|
|
expect(fs.existsSync(settingsFile)).toBe(true);
|
2021-04-05 06:02:27 -04:00
|
|
|
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(auth.generate(id, username, password));
|
|
|
|
}, 100000);
|
|
|
|
|
|
|
|
it('does not overwrite existing settings.xml files', async () => {
|
|
|
|
const id = 'packages';
|
|
|
|
const username = 'USERNAME';
|
|
|
|
const password = 'PASSWORD';
|
|
|
|
|
|
|
|
fs.mkdirSync(m2Dir, { recursive: true });
|
|
|
|
fs.writeFileSync(settingsFile, 'FAKE FILE');
|
|
|
|
expect(fs.existsSync(m2Dir)).toBe(true);
|
|
|
|
expect(fs.existsSync(settingsFile)).toBe(true);
|
|
|
|
|
|
|
|
await auth.createAuthenticationSettings(id, username, password, m2Dir, false);
|
|
|
|
|
|
|
|
expect(fs.existsSync(m2Dir)).toBe(true);
|
|
|
|
expect(fs.existsSync(settingsFile)).toBe(true);
|
|
|
|
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual('FAKE FILE');
|
2019-11-20 13:25:21 -05:00
|
|
|
}, 100000);
|
2019-12-10 12:26:51 -05:00
|
|
|
|
2020-05-02 07:33:15 -04:00
|
|
|
it('generates valid settings.xml with minimal configuration', () => {
|
2019-12-10 12:26:51 -05:00
|
|
|
const id = 'packages';
|
2019-12-19 14:28:11 -05:00
|
|
|
const username = 'USER';
|
2019-12-10 12:26:51 -05:00
|
|
|
const password = '&<>"\'\'"><&';
|
|
|
|
|
2020-05-02 07:33:15 -04:00
|
|
|
const expectedSettings = `<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
|
|
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
|
|
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
|
|
|
|
<servers>
|
|
|
|
<server>
|
|
|
|
<id>${id}</id>
|
|
|
|
<username>\${env.${username}}</username>
|
|
|
|
<password>\${env.&<>"''"><&}</password>
|
|
|
|
</server>
|
|
|
|
</servers>
|
|
|
|
</settings>`;
|
|
|
|
|
|
|
|
expect(auth.generate(id, username, password)).toEqual(expectedSettings);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('generates valid settings.xml with additional configuration', () => {
|
|
|
|
const id = 'packages';
|
|
|
|
const username = 'USER';
|
|
|
|
const password = '&<>"\'\'"><&';
|
|
|
|
const gpgPassphrase = 'PASSPHRASE';
|
|
|
|
|
|
|
|
const expectedSettings = `<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
|
|
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
|
|
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
|
|
|
|
<servers>
|
|
|
|
<server>
|
|
|
|
<id>${id}</id>
|
|
|
|
<username>\${env.${username}}</username>
|
|
|
|
<password>\${env.&<>"''"><&}</password>
|
|
|
|
</server>
|
|
|
|
<server>
|
|
|
|
<id>gpg.passphrase</id>
|
|
|
|
<passphrase>\${env.${gpgPassphrase}}</passphrase>
|
|
|
|
</server>
|
|
|
|
</servers>
|
|
|
|
</settings>`;
|
|
|
|
|
2021-04-05 06:02:27 -04:00
|
|
|
expect(auth.generate(id, username, password, gpgPassphrase)).toEqual(expectedSettings);
|
2019-12-10 12:26:51 -05:00
|
|
|
});
|
2019-11-15 19:01:13 -05:00
|
|
|
});
|