|
| 1 | +import io = require('@actions/io'); |
| 2 | +import fs = require('fs'); |
| 3 | +import os = require('os'); |
| 4 | +import path = require('path'); |
| 5 | + |
| 6 | +// make the os.homedir() call be local to the tests |
| 7 | +jest.doMock('os', () => { |
| 8 | + return { |
| 9 | + homedir: jest.fn(() => __dirname) |
| 10 | + }; |
| 11 | +}); |
| 12 | + |
| 13 | +import * as auth from '../src/auth'; |
| 14 | + |
| 15 | +const m2Dir = path.join(__dirname, auth.M2_DIR); |
| 16 | +const settingsFile = path.join(m2Dir, auth.SETTINGS_FILE); |
| 17 | + |
| 18 | +describe('auth tests', () => { |
| 19 | + beforeEach(async () => { |
| 20 | + await io.rmRF(m2Dir); |
| 21 | + }, 300000); |
| 22 | + |
| 23 | + afterAll(async () => { |
| 24 | + try { |
| 25 | + await io.rmRF(m2Dir); |
| 26 | + } catch { |
| 27 | + console.log('Failed to remove test directories'); |
| 28 | + } |
| 29 | + }, 100000); |
| 30 | + |
| 31 | + it('creates settings.xml in alternate locations', async () => { |
| 32 | + const id = 'packages'; |
| 33 | + const username = 'UNAMI'; |
| 34 | + const password = 'TOLKIEN'; |
| 35 | + |
| 36 | + const altHome = path.join(__dirname, 'runner', 'settings'); |
| 37 | + const altSettingsFile = path.join(altHome, auth.SETTINGS_FILE); |
| 38 | + process.env[`INPUT_SETTINGS-PATH`] = altHome; |
| 39 | + await io.rmRF(altHome); // ensure it doesn't already exist |
| 40 | + |
| 41 | + await auth.configAuthentication(id, username, password); |
| 42 | + |
| 43 | + expect(fs.existsSync(m2Dir)).toBe(false); |
| 44 | + expect(fs.existsSync(settingsFile)).toBe(false); |
| 45 | + |
| 46 | + expect(fs.existsSync(altHome)).toBe(true); |
| 47 | + expect(fs.existsSync(altSettingsFile)).toBe(true); |
| 48 | + expect(fs.readFileSync(altSettingsFile, 'utf-8')).toEqual( |
| 49 | + auth.generate(id, username, password) |
| 50 | + ); |
| 51 | + |
| 52 | + delete process.env[`INPUT_SETTINGS-PATH`]; |
| 53 | + await io.rmRF(altHome); |
| 54 | + }, 100000); |
| 55 | + |
| 56 | + it('creates settings.xml with username and password', async () => { |
| 57 | + const id = 'packages'; |
| 58 | + const username = 'UNAME'; |
| 59 | + const password = 'TOKEN'; |
| 60 | + |
| 61 | + await auth.configAuthentication(id, username, password); |
| 62 | + |
| 63 | + expect(fs.existsSync(m2Dir)).toBe(true); |
| 64 | + expect(fs.existsSync(settingsFile)).toBe(true); |
| 65 | + expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual( |
| 66 | + auth.generate(id, username, password) |
| 67 | + ); |
| 68 | + }, 100000); |
| 69 | + |
| 70 | + it('overwrites existing settings.xml files', async () => { |
| 71 | + const id = 'packages'; |
| 72 | + const username = 'USERNAME'; |
| 73 | + const password = 'PASSWORD'; |
| 74 | + |
| 75 | + fs.mkdirSync(m2Dir, {recursive: true}); |
| 76 | + fs.writeFileSync(settingsFile, 'FAKE FILE'); |
| 77 | + expect(fs.existsSync(m2Dir)).toBe(true); |
| 78 | + expect(fs.existsSync(settingsFile)).toBe(true); |
| 79 | + |
| 80 | + await auth.configAuthentication(id, username, password); |
| 81 | + |
| 82 | + expect(fs.existsSync(m2Dir)).toBe(true); |
| 83 | + expect(fs.existsSync(settingsFile)).toBe(true); |
| 84 | + expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual( |
| 85 | + auth.generate(id, username, password) |
| 86 | + ); |
| 87 | + }, 100000); |
| 88 | + |
| 89 | + it('does not create settings.xml without required parameters', async () => { |
| 90 | + await auth.configAuthentication('FOO'); |
| 91 | + |
| 92 | + expect(fs.existsSync(m2Dir)).toBe(true); |
| 93 | + expect(fs.existsSync(settingsFile)).toBe(true); |
| 94 | + expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual( |
| 95 | + auth.generate('FOO', auth.DEFAULT_USERNAME, auth.DEFAULT_PASSWORD) |
| 96 | + ); |
| 97 | + |
| 98 | + await auth.configAuthentication(undefined, 'BAR', undefined); |
| 99 | + |
| 100 | + expect(fs.existsSync(m2Dir)).toBe(true); |
| 101 | + expect(fs.existsSync(settingsFile)).toBe(true); |
| 102 | + expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual( |
| 103 | + auth.generate(auth.DEFAULT_ID, 'BAR', auth.DEFAULT_PASSWORD) |
| 104 | + ); |
| 105 | + |
| 106 | + await auth.configAuthentication(undefined, undefined, 'BAZ'); |
| 107 | + |
| 108 | + expect(fs.existsSync(m2Dir)).toBe(true); |
| 109 | + expect(fs.existsSync(settingsFile)).toBe(true); |
| 110 | + expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual( |
| 111 | + auth.generate(auth.DEFAULT_ID, auth.DEFAULT_USERNAME, 'BAZ') |
| 112 | + ); |
| 113 | + |
| 114 | + await auth.configAuthentication(); |
| 115 | + |
| 116 | + expect(fs.existsSync(m2Dir)).toBe(true); |
| 117 | + expect(fs.existsSync(settingsFile)).toBe(true); |
| 118 | + expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual( |
| 119 | + auth.generate( |
| 120 | + auth.DEFAULT_ID, |
| 121 | + auth.DEFAULT_USERNAME, |
| 122 | + auth.DEFAULT_PASSWORD |
| 123 | + ) |
| 124 | + ); |
| 125 | + }, 100000); |
| 126 | + |
| 127 | + it('escapes invalid XML inputs', () => { |
| 128 | + const id = 'packages'; |
| 129 | + const username = 'USER'; |
| 130 | + const password = '&<>"\'\'"><&'; |
| 131 | + |
| 132 | + expect(auth.generate(id, username, password)).toEqual(` |
| 133 | + <settings> |
| 134 | + <servers> |
| 135 | + <server> |
| 136 | + <id>${id}</id> |
| 137 | + <username>\${env.${username}}</username> |
| 138 | + <password>\${env.&<>"''"><&}</password> |
| 139 | + </server> |
| 140 | + </servers> |
| 141 | + </settings> |
| 142 | + `); |
| 143 | + }); |
| 144 | +}); |
0 commit comments