11cb0ef41Sopenharmony_ciconst t = require('tap') 21cb0ef41Sopenharmony_ciconst fs = require('fs') 31cb0ef41Sopenharmony_ciconst path = require('path') 41cb0ef41Sopenharmony_ciconst ini = require('ini') 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm.js') 71cb0ef41Sopenharmony_ciconst mockGlobals = require('@npmcli/mock-globals') 81cb0ef41Sopenharmony_ciconst MockRegistry = require('@npmcli/mock-registry') 91cb0ef41Sopenharmony_ciconst stream = require('stream') 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciconst mockLogin = async (t, { stdin: stdinLines, registry: registryUrl, ...options } = {}) => { 121cb0ef41Sopenharmony_ci let stdin 131cb0ef41Sopenharmony_ci if (stdinLines) { 141cb0ef41Sopenharmony_ci stdin = new stream.PassThrough() 151cb0ef41Sopenharmony_ci for (const l of stdinLines) { 161cb0ef41Sopenharmony_ci stdin.write(l + '\n') 171cb0ef41Sopenharmony_ci } 181cb0ef41Sopenharmony_ci mockGlobals(t, { 191cb0ef41Sopenharmony_ci 'process.stdin': stdin, 201cb0ef41Sopenharmony_ci 'process.stdout': new stream.PassThrough(), // to quiet readline 211cb0ef41Sopenharmony_ci }, { replace: true }) 221cb0ef41Sopenharmony_ci } 231cb0ef41Sopenharmony_ci const mock = await loadMockNpm(t, { 241cb0ef41Sopenharmony_ci ...options, 251cb0ef41Sopenharmony_ci command: 'login', 261cb0ef41Sopenharmony_ci }) 271cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 281cb0ef41Sopenharmony_ci tap: t, 291cb0ef41Sopenharmony_ci registry: registryUrl ?? mock.npm.config.get('registry'), 301cb0ef41Sopenharmony_ci }) 311cb0ef41Sopenharmony_ci return { 321cb0ef41Sopenharmony_ci registry, 331cb0ef41Sopenharmony_ci stdin, 341cb0ef41Sopenharmony_ci rc: () => ini.parse(fs.readFileSync(path.join(mock.home, '.npmrc'), 'utf8')), 351cb0ef41Sopenharmony_ci ...mock, 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci} 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_cit.test('usage', async t => { 401cb0ef41Sopenharmony_ci const { login } = await loadMockNpm(t, { command: 'login' }) 411cb0ef41Sopenharmony_ci t.match(login.usage, 'login', 'usage has command name in it') 421cb0ef41Sopenharmony_ci}) 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_cit.test('legacy', t => { 451cb0ef41Sopenharmony_ci t.test('basic login', async t => { 461cb0ef41Sopenharmony_ci const { npm, registry, login, rc } = await mockLogin(t, { 471cb0ef41Sopenharmony_ci stdin: ['test-user', 'test-password'], 481cb0ef41Sopenharmony_ci config: { 'auth-type': 'legacy' }, 491cb0ef41Sopenharmony_ci homeDir: { 501cb0ef41Sopenharmony_ci '.npmrc': [ 511cb0ef41Sopenharmony_ci '//registry.npmjs.org/:_authToken=user', 521cb0ef41Sopenharmony_ci '//registry.npmjs.org/:always-auth=user', 531cb0ef41Sopenharmony_ci '//registry.npmjs.org/:email=test-email-old@npmjs.org', 541cb0ef41Sopenharmony_ci ].join('\n'), 551cb0ef41Sopenharmony_ci }, 561cb0ef41Sopenharmony_ci }) 571cb0ef41Sopenharmony_ci registry.couchlogin({ 581cb0ef41Sopenharmony_ci username: 'test-user', 591cb0ef41Sopenharmony_ci password: 'test-password', 601cb0ef41Sopenharmony_ci token: 'npm_test-token', 611cb0ef41Sopenharmony_ci }) 621cb0ef41Sopenharmony_ci await login.exec([]) 631cb0ef41Sopenharmony_ci t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') 641cb0ef41Sopenharmony_ci t.same(rc(), { 651cb0ef41Sopenharmony_ci '//registry.npmjs.org/:_authToken': 'npm_test-token', 661cb0ef41Sopenharmony_ci email: 'test-email-old@npmjs.org', 671cb0ef41Sopenharmony_ci }, 'should only have token and un-nerfed old email') 681cb0ef41Sopenharmony_ci }) 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci t.test('scoped login default registry', async t => { 711cb0ef41Sopenharmony_ci const { npm, registry, login, rc } = await mockLogin(t, { 721cb0ef41Sopenharmony_ci stdin: ['test-user', 'test-password'], 731cb0ef41Sopenharmony_ci config: { 741cb0ef41Sopenharmony_ci 'auth-type': 'legacy', 751cb0ef41Sopenharmony_ci scope: '@npmcli', 761cb0ef41Sopenharmony_ci }, 771cb0ef41Sopenharmony_ci }) 781cb0ef41Sopenharmony_ci registry.couchlogin({ 791cb0ef41Sopenharmony_ci username: 'test-user', 801cb0ef41Sopenharmony_ci password: 'test-password', 811cb0ef41Sopenharmony_ci token: 'npm_test-token', 821cb0ef41Sopenharmony_ci }) 831cb0ef41Sopenharmony_ci await login.exec([]) 841cb0ef41Sopenharmony_ci t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') 851cb0ef41Sopenharmony_ci t.same(npm.config.get('@npmcli:registry'), 'https://registry.npmjs.org/') 861cb0ef41Sopenharmony_ci t.same(rc(), { 871cb0ef41Sopenharmony_ci '//registry.npmjs.org/:_authToken': 'npm_test-token', 881cb0ef41Sopenharmony_ci '@npmcli:registry': 'https://registry.npmjs.org/', 891cb0ef41Sopenharmony_ci }, 'should only have token and scope:registry') 901cb0ef41Sopenharmony_ci }) 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci t.test('scoped login scoped registry', async t => { 931cb0ef41Sopenharmony_ci const { npm, registry, login, rc } = await mockLogin(t, { 941cb0ef41Sopenharmony_ci stdin: ['test-user', 'test-password'], 951cb0ef41Sopenharmony_ci registry: 'https://diff-registry.npmjs.org', 961cb0ef41Sopenharmony_ci config: { 971cb0ef41Sopenharmony_ci 'auth-type': 'legacy', 981cb0ef41Sopenharmony_ci scope: '@npmcli', 991cb0ef41Sopenharmony_ci }, 1001cb0ef41Sopenharmony_ci homeDir: { 1011cb0ef41Sopenharmony_ci '.npmrc': '@npmcli:registry=https://diff-registry.npmjs.org', 1021cb0ef41Sopenharmony_ci }, 1031cb0ef41Sopenharmony_ci }) 1041cb0ef41Sopenharmony_ci registry.couchlogin({ 1051cb0ef41Sopenharmony_ci username: 'test-user', 1061cb0ef41Sopenharmony_ci password: 'test-password', 1071cb0ef41Sopenharmony_ci token: 'npm_test-token', 1081cb0ef41Sopenharmony_ci }) 1091cb0ef41Sopenharmony_ci await login.exec([]) 1101cb0ef41Sopenharmony_ci t.same(npm.config.get('//diff-registry.npmjs.org/:_authToken'), 'npm_test-token') 1111cb0ef41Sopenharmony_ci t.same(npm.config.get('@npmcli:registry'), 'https://diff-registry.npmjs.org') 1121cb0ef41Sopenharmony_ci t.same(rc(), { 1131cb0ef41Sopenharmony_ci '@npmcli:registry': 'https://diff-registry.npmjs.org', 1141cb0ef41Sopenharmony_ci '//diff-registry.npmjs.org/:_authToken': 'npm_test-token', 1151cb0ef41Sopenharmony_ci }, 'should only have token and scope:registry') 1161cb0ef41Sopenharmony_ci }) 1171cb0ef41Sopenharmony_ci t.end() 1181cb0ef41Sopenharmony_ci}) 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_cit.test('web', t => { 1211cb0ef41Sopenharmony_ci t.test('basic login', async t => { 1221cb0ef41Sopenharmony_ci const { npm, registry, login, rc } = await mockLogin(t, { 1231cb0ef41Sopenharmony_ci config: { 'auth-type': 'web' }, 1241cb0ef41Sopenharmony_ci }) 1251cb0ef41Sopenharmony_ci registry.weblogin({ token: 'npm_test-token' }) 1261cb0ef41Sopenharmony_ci await login.exec([]) 1271cb0ef41Sopenharmony_ci t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') 1281cb0ef41Sopenharmony_ci t.same(rc(), { 1291cb0ef41Sopenharmony_ci '//registry.npmjs.org/:_authToken': 'npm_test-token', 1301cb0ef41Sopenharmony_ci }) 1311cb0ef41Sopenharmony_ci }) 1321cb0ef41Sopenharmony_ci t.test('server error', async t => { 1331cb0ef41Sopenharmony_ci const { registry, login } = await mockLogin(t, { 1341cb0ef41Sopenharmony_ci config: { 'auth-type': 'web' }, 1351cb0ef41Sopenharmony_ci }) 1361cb0ef41Sopenharmony_ci registry.nock.post(registry.fullPath('/-/v1/login')) 1371cb0ef41Sopenharmony_ci .reply(503, {}) 1381cb0ef41Sopenharmony_ci await t.rejects( 1391cb0ef41Sopenharmony_ci login.exec([]), 1401cb0ef41Sopenharmony_ci { message: /503/ } 1411cb0ef41Sopenharmony_ci ) 1421cb0ef41Sopenharmony_ci }) 1431cb0ef41Sopenharmony_ci t.test('fallback', async t => { 1441cb0ef41Sopenharmony_ci const { npm, registry, login } = await mockLogin(t, { 1451cb0ef41Sopenharmony_ci stdin: ['test-user', 'test-password'], 1461cb0ef41Sopenharmony_ci config: { 'auth-type': 'web' }, 1471cb0ef41Sopenharmony_ci }) 1481cb0ef41Sopenharmony_ci registry.nock.post(registry.fullPath('/-/v1/login')) 1491cb0ef41Sopenharmony_ci .reply(404, {}) 1501cb0ef41Sopenharmony_ci registry.couchlogin({ 1511cb0ef41Sopenharmony_ci username: 'test-user', 1521cb0ef41Sopenharmony_ci password: 'test-password', 1531cb0ef41Sopenharmony_ci token: 'npm_test-token', 1541cb0ef41Sopenharmony_ci }) 1551cb0ef41Sopenharmony_ci await login.exec([]) 1561cb0ef41Sopenharmony_ci t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') 1571cb0ef41Sopenharmony_ci }) 1581cb0ef41Sopenharmony_ci t.end() 1591cb0ef41Sopenharmony_ci}) 160