setup-java/__tests__/auth.test.ts

129 lines
3.7 KiB
TypeScript
Raw Normal View History

2019-11-16 00:01:13 +00:00
import io = require('@actions/io');
import fs = require('fs');
import os = require('os');
2019-11-16 00:01:13 +00:00
import path = require('path');
// make the os.homedir() call be local to the tests
jest.doMock('os', () => {
return {
homedir: jest.fn(() => __dirname)
};
});
2019-11-16 00:01:13 +00:00
import * as auth from '../src/auth';
const m2Dir = path.join(__dirname, auth.M2_DIR);
const settingsFile = path.join(m2Dir, auth.SETTINGS_FILE);
2019-11-16 00:01:13 +00:00
describe('auth tests', () => {
beforeEach(async () => {
2019-11-16 00:01:13 +00:00
await io.rmRF(m2Dir);
}, 300000);
afterAll(async () => {
try {
await io.rmRF(m2Dir);
} catch {
console.log('Failed to remove test directories');
}
}, 100000);
it('creates settings.xml in alternate locations', async () => {
const id = 'packages';
const username = 'bluebottle';
const password = 'SingleOrigin';
const altHome = path.join(__dirname, 'runner', 'settings');
const altSettingsFile = path.join(altHome, auth.SETTINGS_FILE);
2019-12-10 21:02:21 +00:00
process.env[`INPUT_SETTINGS-PATH`] = altHome;
await io.rmRF(altHome); // ensure it doesn't already exist
await auth.configAuthentication(id, username, password);
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)
);
2019-12-10 21:02:21 +00:00
delete process.env[`INPUT_SETTINGS-PATH`];
await io.rmRF(altHome);
}, 100000);
it('creates settings.xml with username and password', async () => {
2019-11-28 20:40:08 +00:00
const id = 'packages';
2019-11-16 00:01:13 +00:00
const username = 'bluebottle';
const password = 'SingleOrigin';
2019-11-28 20:40:08 +00:00
await auth.configAuthentication(id, username, password);
2019-11-16 00:01:13 +00:00
expect(fs.existsSync(m2Dir)).toBe(true);
expect(fs.existsSync(settingsFile)).toBe(true);
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
2019-11-28 20:40:08 +00:00
auth.generate(id, username, password)
2019-11-16 00:01:13 +00:00
);
}, 100000);
2019-12-06 19:46:35 +00:00
it('overwrites existing settings.xml files', async () => {
const id = 'packages';
const username = 'bluebottle';
const password = 'SingleOrigin';
fs.mkdirSync(m2Dir, {recursive: true});
2019-12-10 17:26:42 +00:00
fs.writeFileSync(settingsFile, 'FAKE FILE');
2019-12-06 19:46:35 +00:00
expect(fs.existsSync(m2Dir)).toBe(true);
expect(fs.existsSync(settingsFile)).toBe(true);
await auth.configAuthentication(id, username, password);
expect(fs.existsSync(m2Dir)).toBe(true);
expect(fs.existsSync(settingsFile)).toBe(true);
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
auth.generate(id, username, password)
);
}, 100000);
it('does not create settings.xml without required parameters', async () => {
2019-11-28 20:40:08 +00:00
await auth.configAuthentication('FOO', '', '');
expect(fs.existsSync(m2Dir)).toBe(false);
expect(fs.existsSync(settingsFile)).toBe(false);
2019-11-28 20:40:08 +00:00
await auth.configAuthentication('', 'BAR', '');
expect(fs.existsSync(m2Dir)).toBe(false);
expect(fs.existsSync(settingsFile)).toBe(false);
2019-11-28 20:40:08 +00:00
await auth.configAuthentication('', '', 'BAZ');
expect(fs.existsSync(m2Dir)).toBe(false);
expect(fs.existsSync(settingsFile)).toBe(false);
await auth.configAuthentication('', '', '');
expect(fs.existsSync(m2Dir)).toBe(false);
expect(fs.existsSync(settingsFile)).toBe(false);
}, 100000);
2019-12-10 17:26:51 +00:00
it('escapes invalid XML inputs', () => {
const id = 'packages';
const username = 'bluebottle';
const password = '&<>"\'\'"><&';
expect(auth.generate(id, username, password)).toEqual(`
<settings>
<servers>
<server>
<id>${id}</id>
<username>${username}</username>
<password>&amp;&lt;&gt;&quot;&apos;&apos;&quot;&gt;&lt;&amp;</password>
</server>
</servers>
</settings>
`);
});
2019-11-16 00:01:13 +00:00
});