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 mockAddUser = 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: 'adduser', 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 { adduser } = await loadMockNpm(t, { command: 'adduser' }) 411cb0ef41Sopenharmony_ci t.match(adduser.usage, 'adduser', 'usage has command name in it') 421cb0ef41Sopenharmony_ci}) 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_cit.test('legacy', async t => { 451cb0ef41Sopenharmony_ci t.test('simple adduser', async t => { 461cb0ef41Sopenharmony_ci const { npm, rc, registry, adduser } = await mockAddUser(t, { 471cb0ef41Sopenharmony_ci stdin: ['test-user', 'test-password', 'test-email@npmjs.org'], 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.couchadduser({ 581cb0ef41Sopenharmony_ci username: 'test-user', 591cb0ef41Sopenharmony_ci password: 'test-password', 601cb0ef41Sopenharmony_ci email: 'test-email@npmjs.org', 611cb0ef41Sopenharmony_ci token: 'npm_test-token', 621cb0ef41Sopenharmony_ci }) 631cb0ef41Sopenharmony_ci await adduser.exec([]) 641cb0ef41Sopenharmony_ci t.same(npm.config.get('email'), 'test-email-old@npmjs.org') 651cb0ef41Sopenharmony_ci t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') 661cb0ef41Sopenharmony_ci t.same(rc(), { 671cb0ef41Sopenharmony_ci '//registry.npmjs.org/:_authToken': 'npm_test-token', 681cb0ef41Sopenharmony_ci email: 'test-email-old@npmjs.org', 691cb0ef41Sopenharmony_ci }, 'should only have token and un-nerfed old email') 701cb0ef41Sopenharmony_ci }) 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci t.test('scoped adduser', async t => { 731cb0ef41Sopenharmony_ci const { npm, rc, registry, adduser } = await mockAddUser(t, { 741cb0ef41Sopenharmony_ci stdin: ['test-user', 'test-password', 'test-email@npmjs.org'], 751cb0ef41Sopenharmony_ci config: { 761cb0ef41Sopenharmony_ci 'auth-type': 'legacy', 771cb0ef41Sopenharmony_ci scope: '@myscope', 781cb0ef41Sopenharmony_ci }, 791cb0ef41Sopenharmony_ci }) 801cb0ef41Sopenharmony_ci registry.couchadduser({ 811cb0ef41Sopenharmony_ci username: 'test-user', 821cb0ef41Sopenharmony_ci password: 'test-password', 831cb0ef41Sopenharmony_ci email: 'test-email@npmjs.org', 841cb0ef41Sopenharmony_ci token: 'npm_test-token', 851cb0ef41Sopenharmony_ci }) 861cb0ef41Sopenharmony_ci await adduser.exec([]) 871cb0ef41Sopenharmony_ci t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') 881cb0ef41Sopenharmony_ci t.same(npm.config.get('@myscope:registry'), 'https://registry.npmjs.org/') 891cb0ef41Sopenharmony_ci t.same(rc(), { 901cb0ef41Sopenharmony_ci '//registry.npmjs.org/:_authToken': 'npm_test-token', 911cb0ef41Sopenharmony_ci '@myscope:registry': 'https://registry.npmjs.org/', 921cb0ef41Sopenharmony_ci }, 'should only have token and scope:registry') 931cb0ef41Sopenharmony_ci }) 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci t.test('scoped adduser with valid scoped registry config', async t => { 961cb0ef41Sopenharmony_ci const { npm, rc, registry, adduser } = await mockAddUser(t, { 971cb0ef41Sopenharmony_ci stdin: ['test-user', 'test-password', 'test-email@npmjs.org'], 981cb0ef41Sopenharmony_ci registry: 'https://diff-registry.npmjs.org', 991cb0ef41Sopenharmony_ci homeDir: { 1001cb0ef41Sopenharmony_ci '.npmrc': '@myscope:registry=https://diff-registry.npmjs.org', 1011cb0ef41Sopenharmony_ci }, 1021cb0ef41Sopenharmony_ci config: { 1031cb0ef41Sopenharmony_ci 'auth-type': 'legacy', 1041cb0ef41Sopenharmony_ci scope: '@myscope', 1051cb0ef41Sopenharmony_ci }, 1061cb0ef41Sopenharmony_ci }) 1071cb0ef41Sopenharmony_ci registry.couchadduser({ 1081cb0ef41Sopenharmony_ci username: 'test-user', 1091cb0ef41Sopenharmony_ci password: 'test-password', 1101cb0ef41Sopenharmony_ci email: 'test-email@npmjs.org', 1111cb0ef41Sopenharmony_ci token: 'npm_test-token', 1121cb0ef41Sopenharmony_ci }) 1131cb0ef41Sopenharmony_ci await adduser.exec([]) 1141cb0ef41Sopenharmony_ci t.same(npm.config.get('//diff-registry.npmjs.org/:_authToken'), 'npm_test-token') 1151cb0ef41Sopenharmony_ci t.same(npm.config.get('@myscope:registry'), 'https://diff-registry.npmjs.org') 1161cb0ef41Sopenharmony_ci t.same(rc(), { 1171cb0ef41Sopenharmony_ci '@myscope:registry': 'https://diff-registry.npmjs.org', 1181cb0ef41Sopenharmony_ci '//diff-registry.npmjs.org/:_authToken': 'npm_test-token', 1191cb0ef41Sopenharmony_ci }, 'should only have token and scope:registry') 1201cb0ef41Sopenharmony_ci }) 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci t.test('save config failure', async t => { 1231cb0ef41Sopenharmony_ci const { registry, adduser } = await mockAddUser(t, { 1241cb0ef41Sopenharmony_ci stdin: ['test-user', 'test-password', 'test-email@npmjs.org'], 1251cb0ef41Sopenharmony_ci config: { 'auth-type': 'legacy' }, 1261cb0ef41Sopenharmony_ci homeDir: { 1271cb0ef41Sopenharmony_ci '.npmrc': {}, 1281cb0ef41Sopenharmony_ci }, 1291cb0ef41Sopenharmony_ci }) 1301cb0ef41Sopenharmony_ci registry.couchadduser({ 1311cb0ef41Sopenharmony_ci username: 'test-user', 1321cb0ef41Sopenharmony_ci password: 'test-password', 1331cb0ef41Sopenharmony_ci email: 'test-email@npmjs.org', 1341cb0ef41Sopenharmony_ci token: 'npm_test-token', 1351cb0ef41Sopenharmony_ci }) 1361cb0ef41Sopenharmony_ci await t.rejects(adduser.exec([])) 1371cb0ef41Sopenharmony_ci }) 1381cb0ef41Sopenharmony_ci t.end() 1391cb0ef41Sopenharmony_ci}) 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_cit.test('web', t => { 1421cb0ef41Sopenharmony_ci t.test('basic adduser', async t => { 1431cb0ef41Sopenharmony_ci const { npm, rc, registry, adduser } = await mockAddUser(t, { 1441cb0ef41Sopenharmony_ci config: { 'auth-type': 'web' }, 1451cb0ef41Sopenharmony_ci }) 1461cb0ef41Sopenharmony_ci registry.webadduser({ token: 'npm_test-token' }) 1471cb0ef41Sopenharmony_ci await adduser.exec([]) 1481cb0ef41Sopenharmony_ci t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') 1491cb0ef41Sopenharmony_ci t.same(rc(), { 1501cb0ef41Sopenharmony_ci '//registry.npmjs.org/:_authToken': 'npm_test-token', 1511cb0ef41Sopenharmony_ci }) 1521cb0ef41Sopenharmony_ci }) 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci t.test('server error', async t => { 1551cb0ef41Sopenharmony_ci const { adduser, registry } = await mockAddUser(t, { 1561cb0ef41Sopenharmony_ci config: { 'auth-type': 'web' }, 1571cb0ef41Sopenharmony_ci }) 1581cb0ef41Sopenharmony_ci registry.nock.post(registry.fullPath('/-/v1/login')) 1591cb0ef41Sopenharmony_ci .reply(503, {}) 1601cb0ef41Sopenharmony_ci await t.rejects( 1611cb0ef41Sopenharmony_ci adduser.exec([]), 1621cb0ef41Sopenharmony_ci { message: /503/ } 1631cb0ef41Sopenharmony_ci ) 1641cb0ef41Sopenharmony_ci }) 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci t.test('fallback', async t => { 1671cb0ef41Sopenharmony_ci const { npm, registry, adduser } = await mockAddUser(t, { 1681cb0ef41Sopenharmony_ci stdin: ['test-user', 'test-password', 'test-email@npmjs.org'], 1691cb0ef41Sopenharmony_ci config: { 'auth-type': 'web' }, 1701cb0ef41Sopenharmony_ci }) 1711cb0ef41Sopenharmony_ci registry.nock.post(registry.fullPath('/-/v1/login')) 1721cb0ef41Sopenharmony_ci .reply(404, {}) 1731cb0ef41Sopenharmony_ci registry.couchadduser({ 1741cb0ef41Sopenharmony_ci username: 'test-user', 1751cb0ef41Sopenharmony_ci password: 'test-password', 1761cb0ef41Sopenharmony_ci email: 'test-email@npmjs.org', 1771cb0ef41Sopenharmony_ci token: 'npm_test-token', 1781cb0ef41Sopenharmony_ci }) 1791cb0ef41Sopenharmony_ci await adduser.exec([]) 1801cb0ef41Sopenharmony_ci t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') 1811cb0ef41Sopenharmony_ci }) 1821cb0ef41Sopenharmony_ci t.end() 1831cb0ef41Sopenharmony_ci}) 184