| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | import io = require('@actions/io'); | 
					
						
							|  |  |  | import fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  | import os = require('os'); | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | import path = require('path'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  | // make the os.homedir() call be local to the tests
 | 
					
						
							|  |  |  | jest.doMock('os', () => { | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     homedir: jest.fn(() => __dirname) | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import * as auth from '../src/auth'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  | const m2Dir = path.join(__dirname, auth.M2_DIR); | 
					
						
							|  |  |  | const settingsFile = path.join(m2Dir, auth.SETTINGS_FILE); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | describe('auth tests', () => { | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  |   beforeEach(async () => { | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  |     await io.rmRF(m2Dir); | 
					
						
							|  |  |  |   }, 300000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   afterAll(async () => { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       await io.rmRF(m2Dir); | 
					
						
							|  |  |  |     } catch { | 
					
						
							|  |  |  |       console.log('Failed to remove test directories'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, 100000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-10 10:03:33 -08:00
										 |  |  |   it('creates settings.xml in alternate locations', async () => { | 
					
						
							|  |  |  |     const id = 'packages'; | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     const username = 'UNAMI'; | 
					
						
							|  |  |  |     const password = 'TOLKIEN'; | 
					
						
							| 
									
										
										
										
											2019-12-10 10:03:33 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const altHome = path.join(__dirname, 'runner', 'settings'); | 
					
						
							|  |  |  |     const altSettingsFile = path.join(altHome, auth.SETTINGS_FILE); | 
					
						
							| 
									
										
										
										
											2019-12-10 13:02:21 -08:00
										 |  |  |     process.env[`INPUT_SETTINGS-PATH`] = altHome; | 
					
						
							| 
									
										
										
										
											2019-12-10 10:03:33 -08:00
										 |  |  |     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 13:02:21 -08:00
										 |  |  |     delete process.env[`INPUT_SETTINGS-PATH`]; | 
					
						
							| 
									
										
										
										
											2019-12-10 10:03:33 -08:00
										 |  |  |     await io.rmRF(altHome); | 
					
						
							|  |  |  |   }, 100000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  |   it('creates settings.xml with username and password', async () => { | 
					
						
							| 
									
										
										
										
											2019-11-28 12:40:08 -08:00
										 |  |  |     const id = 'packages'; | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     const username = 'UNAME'; | 
					
						
							|  |  |  |     const password = 'TOKEN'; | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-28 12:40:08 -08:00
										 |  |  |     await auth.configAuthentication(id, username, password); | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(fs.existsSync(m2Dir)).toBe(true); | 
					
						
							|  |  |  |     expect(fs.existsSync(settingsFile)).toBe(true); | 
					
						
							|  |  |  |     expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual( | 
					
						
							| 
									
										
										
										
											2019-11-28 12:40:08 -08:00
										 |  |  |       auth.generate(id, username, password) | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  |     ); | 
					
						
							|  |  |  |   }, 100000); | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-06 14:46:35 -05:00
										 |  |  |   it('overwrites existing settings.xml files', async () => { | 
					
						
							|  |  |  |     const id = 'packages'; | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     const username = 'USERNAME'; | 
					
						
							|  |  |  |     const password = 'PASSWORD'; | 
					
						
							| 
									
										
										
										
											2019-12-06 14:46:35 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fs.mkdirSync(m2Dir, {recursive: true}); | 
					
						
							| 
									
										
										
										
											2019-12-10 09:26:42 -08:00
										 |  |  |     fs.writeFileSync(settingsFile, 'FAKE FILE'); | 
					
						
							| 
									
										
										
										
											2019-12-06 14:46:35 -05: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-12-19 11:28:11 -08:00
										 |  |  |     await auth.configAuthentication('FOO'); | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     expect(fs.existsSync(m2Dir)).toBe(true); | 
					
						
							|  |  |  |     expect(fs.existsSync(settingsFile)).toBe(true); | 
					
						
							|  |  |  |     expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual( | 
					
						
							| 
									
										
										
										
											2019-12-19 14:18:54 -08:00
										 |  |  |       auth.generate('FOO', auth.DEFAULT_USERNAME, auth.DEFAULT_PASSWORD) | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     await auth.configAuthentication(undefined, 'BAR', undefined); | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     expect(fs.existsSync(m2Dir)).toBe(true); | 
					
						
							|  |  |  |     expect(fs.existsSync(settingsFile)).toBe(true); | 
					
						
							|  |  |  |     expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual( | 
					
						
							| 
									
										
										
										
											2019-12-19 14:18:54 -08:00
										 |  |  |       auth.generate(auth.DEFAULT_ID, 'BAR', auth.DEFAULT_PASSWORD) | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     await auth.configAuthentication(undefined, undefined, 'BAZ'); | 
					
						
							| 
									
										
										
										
											2019-11-28 12:40:08 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     expect(fs.existsSync(m2Dir)).toBe(true); | 
					
						
							|  |  |  |     expect(fs.existsSync(settingsFile)).toBe(true); | 
					
						
							|  |  |  |     expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual( | 
					
						
							| 
									
										
										
										
											2019-12-19 14:18:54 -08:00
										 |  |  |       auth.generate(auth.DEFAULT_ID, auth.DEFAULT_USERNAME, 'BAZ') | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2019-11-28 12:40:08 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     await auth.configAuthentication(); | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     expect(fs.existsSync(m2Dir)).toBe(true); | 
					
						
							|  |  |  |     expect(fs.existsSync(settingsFile)).toBe(true); | 
					
						
							|  |  |  |     expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual( | 
					
						
							| 
									
										
										
										
											2019-12-19 14:18:54 -08:00
										 |  |  |       auth.generate( | 
					
						
							|  |  |  |         auth.DEFAULT_ID, | 
					
						
							|  |  |  |         auth.DEFAULT_USERNAME, | 
					
						
							|  |  |  |         auth.DEFAULT_PASSWORD | 
					
						
							|  |  |  |       ) | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  |   }, 100000); | 
					
						
							| 
									
										
										
										
											2019-12-10 09:26:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('escapes invalid XML inputs', () => { | 
					
						
							|  |  |  |     const id = 'packages'; | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |     const username = 'USER'; | 
					
						
							| 
									
										
										
										
											2019-12-10 09:26:51 -08:00
										 |  |  |     const password = '&<>"\'\'"><&'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(auth.generate(id, username, password)).toEqual(`
 | 
					
						
							|  |  |  |   <settings> | 
					
						
							|  |  |  |       <servers> | 
					
						
							|  |  |  |         <server> | 
					
						
							|  |  |  |           <id>${id}</id> | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |           <username>\${env.${username}}</username> | 
					
						
							|  |  |  |           <password>\${env.&<>"''"><&}</password> | 
					
						
							| 
									
										
										
										
											2019-12-10 09:26:51 -08:00
										 |  |  |         </server> | 
					
						
							|  |  |  |       </servers> | 
					
						
							|  |  |  |   </settings> | 
					
						
							|  |  |  |   `);
 | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | }); |