From e77eaaccd3948bf790da061e9f0fe5c9cb8297a2 Mon Sep 17 00:00:00 2001 From: Sergey Dolin Date: Mon, 5 Dec 2022 13:36:23 +0100 Subject: [PATCH] Add unit tests --- __tests__/authutil.test.ts | 81 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/__tests__/authutil.test.ts b/__tests__/authutil.test.ts index 594c6a1..458ceb1 100644 --- a/__tests__/authutil.test.ts +++ b/__tests__/authutil.test.ts @@ -123,6 +123,7 @@ describe('authutil tests', () => { expect(rc['registry']).toBe('https://registry.npmjs.org/'); expect(rc['always-auth']).toBe('true'); }); + it('It is already set the NODE_AUTH_TOKEN export it ', async () => { process.env.NODE_AUTH_TOKEN = 'foobar'; await auth.configAuthentication('npm.pkg.github.com', 'false'); @@ -132,4 +133,84 @@ describe('authutil tests', () => { expect(rc['always-auth']).toBe('false'); expect(process.env.NODE_AUTH_TOKEN).toEqual('foobar'); }); + + it('configAuthentication should overwrite non-scoped with non-scoped', async () => { + fs.writeFileSync(rcFile, 'registry=NNN'); + await auth.configAuthentication('https://registry.npmjs.org/', 'true'); + let contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); + 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'); + let contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); + 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'); + let contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); + 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'); + let contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); + 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'); + let contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); + 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'); + let contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); + 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'); + let contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); + 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'); + let contents = fs.readFileSync(rcFile, {encoding: 'utf8'}); + 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` + ); + }); });