11cb0ef41Sopenharmony_ciconst t = require('tap') 21cb0ef41Sopenharmony_ciconst mockNpm = require('../../fixtures/mock-npm') 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst mockToken = async (t, { profile, getCredentialsByURI, readUserInfo, ...opts } = {}) => { 51cb0ef41Sopenharmony_ci const mocks = {} 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci if (profile) { 81cb0ef41Sopenharmony_ci mocks['npm-profile'] = profile 91cb0ef41Sopenharmony_ci } 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci if (readUserInfo) { 121cb0ef41Sopenharmony_ci mocks['{LIB}/utils/read-user-info.js'] = readUserInfo 131cb0ef41Sopenharmony_ci } 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci const mock = await mockNpm(t, { 161cb0ef41Sopenharmony_ci ...opts, 171cb0ef41Sopenharmony_ci command: 'token', 181cb0ef41Sopenharmony_ci mocks, 191cb0ef41Sopenharmony_ci }) 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci // XXX: replace with mock registry 221cb0ef41Sopenharmony_ci if (getCredentialsByURI) { 231cb0ef41Sopenharmony_ci mock.npm.config.getCredentialsByURI = getCredentialsByURI 241cb0ef41Sopenharmony_ci } 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci return mock 271cb0ef41Sopenharmony_ci} 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_cit.test('completion', async t => { 301cb0ef41Sopenharmony_ci const { token } = await mockToken(t) 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci const testComp = (argv, expect) => { 331cb0ef41Sopenharmony_ci t.resolveMatch(token.completion({ conf: { argv: { remain: argv } } }), expect, argv.join(' ')) 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci testComp(['npm', 'token'], ['list', 'revoke', 'create']) 371cb0ef41Sopenharmony_ci testComp(['npm', 'token', 'list'], []) 381cb0ef41Sopenharmony_ci testComp(['npm', 'token', 'revoke'], []) 391cb0ef41Sopenharmony_ci testComp(['npm', 'token', 'create'], []) 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci t.rejects(token.completion({ conf: { argv: { remain: ['npm', 'token', 'foobar'] } } }), { 421cb0ef41Sopenharmony_ci message: 'foobar not recognize', 431cb0ef41Sopenharmony_ci }) 441cb0ef41Sopenharmony_ci}) 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_cit.test('token foobar', async t => { 471cb0ef41Sopenharmony_ci const { token } = await mockToken(t) 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci await t.rejects(token.exec(['foobar']), /foobar is not a recognized subcommand/) 501cb0ef41Sopenharmony_ci}) 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_cit.test('token list', async t => { 531cb0ef41Sopenharmony_ci const now = new Date().toISOString() 541cb0ef41Sopenharmony_ci const tokens = [ 551cb0ef41Sopenharmony_ci { 561cb0ef41Sopenharmony_ci key: 'abcd1234abcd1234', 571cb0ef41Sopenharmony_ci token: 'efgh5678efgh5678', 581cb0ef41Sopenharmony_ci cidr_whitelist: null, 591cb0ef41Sopenharmony_ci readonly: false, 601cb0ef41Sopenharmony_ci created: now, 611cb0ef41Sopenharmony_ci updated: now, 621cb0ef41Sopenharmony_ci }, 631cb0ef41Sopenharmony_ci { 641cb0ef41Sopenharmony_ci key: 'abcd1256', 651cb0ef41Sopenharmony_ci token: 'hgfe8765', 661cb0ef41Sopenharmony_ci cidr_whitelist: ['192.168.1.1/32'], 671cb0ef41Sopenharmony_ci readonly: true, 681cb0ef41Sopenharmony_ci created: now, 691cb0ef41Sopenharmony_ci updated: now, 701cb0ef41Sopenharmony_ci }, 711cb0ef41Sopenharmony_ci ] 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci const { token, joinedOutput } = await mockToken(t, { 741cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org', otp: '123456' }, 751cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 761cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 771cb0ef41Sopenharmony_ci return { token: 'thisisnotarealtoken' } 781cb0ef41Sopenharmony_ci }, 791cb0ef41Sopenharmony_ci profile: { 801cb0ef41Sopenharmony_ci listTokens: conf => { 811cb0ef41Sopenharmony_ci t.same(conf.auth, { token: 'thisisnotarealtoken', otp: '123456' }) 821cb0ef41Sopenharmony_ci return tokens 831cb0ef41Sopenharmony_ci }, 841cb0ef41Sopenharmony_ci }, 851cb0ef41Sopenharmony_ci }) 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci await token.exec([]) 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci const lines = joinedOutput().split(/\r?\n/) 901cb0ef41Sopenharmony_ci t.match(lines[3], ' abcd123 ', 'includes the trimmed key') 911cb0ef41Sopenharmony_ci t.match(lines[3], ' efgh56… ', 'includes the trimmed token') 921cb0ef41Sopenharmony_ci t.match(lines[3], ` ${now.slice(0, 10)} `, 'includes the trimmed creation timestamp') 931cb0ef41Sopenharmony_ci t.match(lines[3], ' no ', 'includes the "no" string for readonly state') 941cb0ef41Sopenharmony_ci t.match(lines[5], ' abcd125 ', 'includes the trimmed key') 951cb0ef41Sopenharmony_ci t.match(lines[5], ' hgfe87… ', 'includes the trimmed token') 961cb0ef41Sopenharmony_ci t.match(lines[5], ` ${now.slice(0, 10)} `, 'includes the trimmed creation timestamp') 971cb0ef41Sopenharmony_ci t.match(lines[5], ' yes ', 'includes the "no" string for readonly state') 981cb0ef41Sopenharmony_ci t.match(lines[5], ` ${tokens[1].cidr_whitelist.join(',')} `, 'includes the cidr whitelist') 991cb0ef41Sopenharmony_ci}) 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_cit.test('token list json output', async t => { 1021cb0ef41Sopenharmony_ci const now = new Date().toISOString() 1031cb0ef41Sopenharmony_ci const tokens = [ 1041cb0ef41Sopenharmony_ci { 1051cb0ef41Sopenharmony_ci key: 'abcd1234abcd1234', 1061cb0ef41Sopenharmony_ci token: 'efgh5678efgh5678', 1071cb0ef41Sopenharmony_ci cidr_whitelist: null, 1081cb0ef41Sopenharmony_ci readonly: false, 1091cb0ef41Sopenharmony_ci created: now, 1101cb0ef41Sopenharmony_ci updated: now, 1111cb0ef41Sopenharmony_ci }, 1121cb0ef41Sopenharmony_ci ] 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci const { token, joinedOutput } = await mockToken(t, { 1151cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org', json: true }, 1161cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 1171cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 1181cb0ef41Sopenharmony_ci return { username: 'foo', password: 'bar' } 1191cb0ef41Sopenharmony_ci }, 1201cb0ef41Sopenharmony_ci profile: { 1211cb0ef41Sopenharmony_ci listTokens: conf => { 1221cb0ef41Sopenharmony_ci t.same( 1231cb0ef41Sopenharmony_ci conf.auth, 1241cb0ef41Sopenharmony_ci { basic: { username: 'foo', password: 'bar' } }, 1251cb0ef41Sopenharmony_ci 'passes the correct auth' 1261cb0ef41Sopenharmony_ci ) 1271cb0ef41Sopenharmony_ci return tokens 1281cb0ef41Sopenharmony_ci }, 1291cb0ef41Sopenharmony_ci }, 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci }) 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci await token.exec(['list']) 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci const parsed = JSON.parse(joinedOutput()) 1361cb0ef41Sopenharmony_ci t.match(parsed, tokens, 'prints the json parsed tokens') 1371cb0ef41Sopenharmony_ci}) 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_cit.test('token list parseable output', async t => { 1401cb0ef41Sopenharmony_ci const now = new Date().toISOString() 1411cb0ef41Sopenharmony_ci const tokens = [ 1421cb0ef41Sopenharmony_ci { 1431cb0ef41Sopenharmony_ci key: 'abcd1234abcd1234', 1441cb0ef41Sopenharmony_ci token: 'efgh5678efgh5678', 1451cb0ef41Sopenharmony_ci cidr_whitelist: null, 1461cb0ef41Sopenharmony_ci readonly: false, 1471cb0ef41Sopenharmony_ci created: now, 1481cb0ef41Sopenharmony_ci updated: now, 1491cb0ef41Sopenharmony_ci }, 1501cb0ef41Sopenharmony_ci { 1511cb0ef41Sopenharmony_ci key: 'efgh5678ijkl9101', 1521cb0ef41Sopenharmony_ci token: 'hgfe8765', 1531cb0ef41Sopenharmony_ci cidr_whitelist: ['192.168.1.1/32'], 1541cb0ef41Sopenharmony_ci readonly: true, 1551cb0ef41Sopenharmony_ci created: now, 1561cb0ef41Sopenharmony_ci updated: now, 1571cb0ef41Sopenharmony_ci }, 1581cb0ef41Sopenharmony_ci ] 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci const { token, joinedOutput } = await mockToken(t, { 1611cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org', parseable: true }, 1621cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 1631cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 1641cb0ef41Sopenharmony_ci return { auth: Buffer.from('foo:bar').toString('base64') } 1651cb0ef41Sopenharmony_ci }, 1661cb0ef41Sopenharmony_ci profile: { 1671cb0ef41Sopenharmony_ci listTokens: conf => { 1681cb0ef41Sopenharmony_ci t.same( 1691cb0ef41Sopenharmony_ci conf.auth, 1701cb0ef41Sopenharmony_ci { basic: { username: 'foo', password: 'bar' } }, 1711cb0ef41Sopenharmony_ci 'passes the correct auth' 1721cb0ef41Sopenharmony_ci ) 1731cb0ef41Sopenharmony_ci return tokens 1741cb0ef41Sopenharmony_ci }, 1751cb0ef41Sopenharmony_ci }, 1761cb0ef41Sopenharmony_ci }) 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci await token.exec(['list']) 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci const lines = joinedOutput().split(/\r?\n/) 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ci t.equal( 1831cb0ef41Sopenharmony_ci lines[0], 1841cb0ef41Sopenharmony_ci ['key', 'token', 'created', 'readonly', 'CIDR whitelist'].join('\t'), 1851cb0ef41Sopenharmony_ci 'prints header' 1861cb0ef41Sopenharmony_ci ) 1871cb0ef41Sopenharmony_ci 1881cb0ef41Sopenharmony_ci t.equal( 1891cb0ef41Sopenharmony_ci lines[1], 1901cb0ef41Sopenharmony_ci [tokens[0].key, tokens[0].token, tokens[0].created, tokens[0].readonly, ''].join('\t'), 1911cb0ef41Sopenharmony_ci 'prints token info' 1921cb0ef41Sopenharmony_ci ) 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ci t.equal( 1951cb0ef41Sopenharmony_ci lines[2], 1961cb0ef41Sopenharmony_ci [ 1971cb0ef41Sopenharmony_ci tokens[1].key, 1981cb0ef41Sopenharmony_ci tokens[1].token, 1991cb0ef41Sopenharmony_ci tokens[1].created, 2001cb0ef41Sopenharmony_ci tokens[1].readonly, 2011cb0ef41Sopenharmony_ci tokens[1].cidr_whitelist.join(','), 2021cb0ef41Sopenharmony_ci ].join('\t'), 2031cb0ef41Sopenharmony_ci 'prints token info' 2041cb0ef41Sopenharmony_ci ) 2051cb0ef41Sopenharmony_ci}) 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_cit.test('token revoke', async t => { 2081cb0ef41Sopenharmony_ci const { token, joinedOutput } = await mockToken(t, { 2091cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org' }, 2101cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 2111cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 2121cb0ef41Sopenharmony_ci return {} 2131cb0ef41Sopenharmony_ci }, 2141cb0ef41Sopenharmony_ci profile: { 2151cb0ef41Sopenharmony_ci listTokens: conf => { 2161cb0ef41Sopenharmony_ci t.same(conf.auth, {}, 'passes the correct empty auth') 2171cb0ef41Sopenharmony_ci return Promise.resolve([{ key: 'abcd1234' }]) 2181cb0ef41Sopenharmony_ci }, 2191cb0ef41Sopenharmony_ci removeToken: key => { 2201cb0ef41Sopenharmony_ci t.equal(key, 'abcd1234', 'deletes the correct token') 2211cb0ef41Sopenharmony_ci }, 2221cb0ef41Sopenharmony_ci }, 2231cb0ef41Sopenharmony_ci }) 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ci await token.exec(['rm', 'abcd']) 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ci t.equal(joinedOutput(), 'Removed 1 token') 2281cb0ef41Sopenharmony_ci}) 2291cb0ef41Sopenharmony_ci 2301cb0ef41Sopenharmony_cit.test('token revoke multiple tokens', async t => { 2311cb0ef41Sopenharmony_ci const { token, joinedOutput } = await mockToken(t, { 2321cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org' }, 2331cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 2341cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 2351cb0ef41Sopenharmony_ci return { token: 'thisisnotarealtoken' } 2361cb0ef41Sopenharmony_ci }, 2371cb0ef41Sopenharmony_ci profile: { 2381cb0ef41Sopenharmony_ci listTokens: () => Promise.resolve([{ key: 'abcd1234' }, { key: 'efgh5678' }]), 2391cb0ef41Sopenharmony_ci removeToken: key => { 2401cb0ef41Sopenharmony_ci // this will run twice 2411cb0ef41Sopenharmony_ci t.ok(['abcd1234', 'efgh5678'].includes(key), 'deletes the correct token') 2421cb0ef41Sopenharmony_ci }, 2431cb0ef41Sopenharmony_ci }, 2441cb0ef41Sopenharmony_ci }) 2451cb0ef41Sopenharmony_ci 2461cb0ef41Sopenharmony_ci await token.exec(['revoke', 'abcd', 'efgh']) 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ci t.equal(joinedOutput(), 'Removed 2 tokens') 2491cb0ef41Sopenharmony_ci}) 2501cb0ef41Sopenharmony_ci 2511cb0ef41Sopenharmony_cit.test('token revoke json output', async t => { 2521cb0ef41Sopenharmony_ci const { token, joinedOutput } = await mockToken(t, { 2531cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org', json: true }, 2541cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 2551cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 2561cb0ef41Sopenharmony_ci return { token: 'thisisnotarealtoken' } 2571cb0ef41Sopenharmony_ci }, 2581cb0ef41Sopenharmony_ci profile: { 2591cb0ef41Sopenharmony_ci listTokens: () => Promise.resolve([{ key: 'abcd1234' }]), 2601cb0ef41Sopenharmony_ci removeToken: key => { 2611cb0ef41Sopenharmony_ci t.equal(key, 'abcd1234', 'deletes the correct token') 2621cb0ef41Sopenharmony_ci }, 2631cb0ef41Sopenharmony_ci }, 2641cb0ef41Sopenharmony_ci 2651cb0ef41Sopenharmony_ci }) 2661cb0ef41Sopenharmony_ci 2671cb0ef41Sopenharmony_ci await token.exec(['delete', 'abcd']) 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ci const parsed = JSON.parse(joinedOutput()) 2701cb0ef41Sopenharmony_ci t.same(parsed, ['abcd1234'], 'logs the token as json') 2711cb0ef41Sopenharmony_ci}) 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_cit.test('token revoke parseable output', async t => { 2741cb0ef41Sopenharmony_ci const { token, joinedOutput } = await mockToken(t, { 2751cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org', parseable: true }, 2761cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 2771cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 2781cb0ef41Sopenharmony_ci return { token: 'thisisnotarealtoken' } 2791cb0ef41Sopenharmony_ci }, 2801cb0ef41Sopenharmony_ci profile: { 2811cb0ef41Sopenharmony_ci listTokens: () => Promise.resolve([{ key: 'abcd1234' }]), 2821cb0ef41Sopenharmony_ci removeToken: key => { 2831cb0ef41Sopenharmony_ci t.equal(key, 'abcd1234', 'deletes the correct token') 2841cb0ef41Sopenharmony_ci }, 2851cb0ef41Sopenharmony_ci }, 2861cb0ef41Sopenharmony_ci }) 2871cb0ef41Sopenharmony_ci 2881cb0ef41Sopenharmony_ci await token.exec(['remove', 'abcd']) 2891cb0ef41Sopenharmony_ci 2901cb0ef41Sopenharmony_ci t.equal(joinedOutput(), 'abcd1234', 'logs the token as a string') 2911cb0ef41Sopenharmony_ci}) 2921cb0ef41Sopenharmony_ci 2931cb0ef41Sopenharmony_cit.test('token revoke by token', async t => { 2941cb0ef41Sopenharmony_ci const { token, joinedOutput } = await mockToken(t, { 2951cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org' }, 2961cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 2971cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 2981cb0ef41Sopenharmony_ci return { token: 'thisisnotarealtoken' } 2991cb0ef41Sopenharmony_ci }, 3001cb0ef41Sopenharmony_ci profile: { 3011cb0ef41Sopenharmony_ci listTokens: () => Promise.resolve([{ key: 'abcd1234', token: 'efgh5678' }]), 3021cb0ef41Sopenharmony_ci removeToken: key => { 3031cb0ef41Sopenharmony_ci t.equal(key, 'efgh5678', 'passes through user input') 3041cb0ef41Sopenharmony_ci }, 3051cb0ef41Sopenharmony_ci }, 3061cb0ef41Sopenharmony_ci }) 3071cb0ef41Sopenharmony_ci 3081cb0ef41Sopenharmony_ci await token.exec(['rm', 'efgh5678']) 3091cb0ef41Sopenharmony_ci t.equal(joinedOutput(), 'Removed 1 token') 3101cb0ef41Sopenharmony_ci}) 3111cb0ef41Sopenharmony_ci 3121cb0ef41Sopenharmony_cit.test('token revoke requires an id', async t => { 3131cb0ef41Sopenharmony_ci const { token } = await mockToken(t) 3141cb0ef41Sopenharmony_ci 3151cb0ef41Sopenharmony_ci await t.rejects(token.exec(['rm']), /`<tokenKey>` argument is required/) 3161cb0ef41Sopenharmony_ci}) 3171cb0ef41Sopenharmony_ci 3181cb0ef41Sopenharmony_cit.test('token revoke ambiguous id errors', async t => { 3191cb0ef41Sopenharmony_ci const { token } = await mockToken(t, { 3201cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org' }, 3211cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 3221cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 3231cb0ef41Sopenharmony_ci return { token: 'thisisnotarealtoken' } 3241cb0ef41Sopenharmony_ci }, 3251cb0ef41Sopenharmony_ci profile: { 3261cb0ef41Sopenharmony_ci listTokens: () => Promise.resolve([{ key: 'abcd1234' }, { key: 'abcd5678' }]), 3271cb0ef41Sopenharmony_ci }, 3281cb0ef41Sopenharmony_ci }) 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ci await t.rejects(token.exec(['rm', 'abcd']), /Token ID "abcd" was ambiguous/) 3311cb0ef41Sopenharmony_ci}) 3321cb0ef41Sopenharmony_ci 3331cb0ef41Sopenharmony_cit.test('token revoke unknown id errors', async t => { 3341cb0ef41Sopenharmony_ci const { token } = await mockToken(t, { 3351cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org' }, 3361cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 3371cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 3381cb0ef41Sopenharmony_ci return { token: 'thisisnotarealtoken' } 3391cb0ef41Sopenharmony_ci }, 3401cb0ef41Sopenharmony_ci profile: { 3411cb0ef41Sopenharmony_ci listTokens: () => Promise.resolve([{ key: 'abcd1234' }]), 3421cb0ef41Sopenharmony_ci }, 3431cb0ef41Sopenharmony_ci }) 3441cb0ef41Sopenharmony_ci 3451cb0ef41Sopenharmony_ci await t.rejects(token.exec(['rm', 'efgh']), /Unknown token id or value "efgh"./) 3461cb0ef41Sopenharmony_ci}) 3471cb0ef41Sopenharmony_ci 3481cb0ef41Sopenharmony_cit.test('token create', async t => { 3491cb0ef41Sopenharmony_ci const now = new Date().toISOString() 3501cb0ef41Sopenharmony_ci const password = 'thisisnotreallyapassword' 3511cb0ef41Sopenharmony_ci 3521cb0ef41Sopenharmony_ci const { token, joinedOutput } = await mockToken(t, { 3531cb0ef41Sopenharmony_ci config: { 3541cb0ef41Sopenharmony_ci registry: 'https://registry.npmjs.org', 3551cb0ef41Sopenharmony_ci cidr: ['10.0.0.0/8', '192.168.1.0/24'], 3561cb0ef41Sopenharmony_ci }, 3571cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 3581cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 3591cb0ef41Sopenharmony_ci return { token: 'thisisnotarealtoken' } 3601cb0ef41Sopenharmony_ci }, 3611cb0ef41Sopenharmony_ci readUserInfo: { 3621cb0ef41Sopenharmony_ci password: () => Promise.resolve(password), 3631cb0ef41Sopenharmony_ci }, 3641cb0ef41Sopenharmony_ci profile: { 3651cb0ef41Sopenharmony_ci createToken: (pw, readonly, cidr) => { 3661cb0ef41Sopenharmony_ci t.equal(pw, password) 3671cb0ef41Sopenharmony_ci t.equal(readonly, false) 3681cb0ef41Sopenharmony_ci t.same(cidr, ['10.0.0.0/8', '192.168.1.0/24'], 'defaults to empty array') 3691cb0ef41Sopenharmony_ci return { 3701cb0ef41Sopenharmony_ci key: 'abcd1234', 3711cb0ef41Sopenharmony_ci token: 'efgh5678', 3721cb0ef41Sopenharmony_ci created: now, 3731cb0ef41Sopenharmony_ci updated: now, 3741cb0ef41Sopenharmony_ci readonly: false, 3751cb0ef41Sopenharmony_ci cidr_whitelist: [], 3761cb0ef41Sopenharmony_ci } 3771cb0ef41Sopenharmony_ci }, 3781cb0ef41Sopenharmony_ci }, 3791cb0ef41Sopenharmony_ci 3801cb0ef41Sopenharmony_ci }) 3811cb0ef41Sopenharmony_ci 3821cb0ef41Sopenharmony_ci await token.exec(['create']) 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci const lines = joinedOutput().split(/\r?\n/) 3851cb0ef41Sopenharmony_ci t.match(lines[1], 'token') 3861cb0ef41Sopenharmony_ci t.match(lines[1], 'efgh5678', 'prints the whole token') 3871cb0ef41Sopenharmony_ci t.match(lines[3], 'created') 3881cb0ef41Sopenharmony_ci t.match(lines[3], now, 'prints the correct timestamp') 3891cb0ef41Sopenharmony_ci t.match(lines[5], 'readonly') 3901cb0ef41Sopenharmony_ci t.match(lines[5], 'false', 'prints the readonly flag') 3911cb0ef41Sopenharmony_ci t.match(lines[7], 'cidr_whitelist') 3921cb0ef41Sopenharmony_ci}) 3931cb0ef41Sopenharmony_ci 3941cb0ef41Sopenharmony_cit.test('token create json output', async t => { 3951cb0ef41Sopenharmony_ci const now = new Date().toISOString() 3961cb0ef41Sopenharmony_ci const password = 'thisisnotreallyapassword' 3971cb0ef41Sopenharmony_ci 3981cb0ef41Sopenharmony_ci const { token } = await mockToken(t, { 3991cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org', json: true }, 4001cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 4011cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 4021cb0ef41Sopenharmony_ci return { token: 'thisisnotarealtoken' } 4031cb0ef41Sopenharmony_ci }, 4041cb0ef41Sopenharmony_ci readUserInfo: { 4051cb0ef41Sopenharmony_ci password: () => Promise.resolve(password), 4061cb0ef41Sopenharmony_ci }, 4071cb0ef41Sopenharmony_ci profile: { 4081cb0ef41Sopenharmony_ci createToken: (pw, readonly, cidr) => { 4091cb0ef41Sopenharmony_ci t.equal(pw, password) 4101cb0ef41Sopenharmony_ci t.equal(readonly, false) 4111cb0ef41Sopenharmony_ci t.same(cidr, [], 'defaults to empty array') 4121cb0ef41Sopenharmony_ci return { 4131cb0ef41Sopenharmony_ci key: 'abcd1234', 4141cb0ef41Sopenharmony_ci token: 'efgh5678', 4151cb0ef41Sopenharmony_ci created: now, 4161cb0ef41Sopenharmony_ci updated: now, 4171cb0ef41Sopenharmony_ci readonly: false, 4181cb0ef41Sopenharmony_ci cidr_whitelist: [], 4191cb0ef41Sopenharmony_ci } 4201cb0ef41Sopenharmony_ci }, 4211cb0ef41Sopenharmony_ci }, 4221cb0ef41Sopenharmony_ci output: spec => { 4231cb0ef41Sopenharmony_ci t.type(spec, 'string', 'outputs a string') 4241cb0ef41Sopenharmony_ci const parsed = JSON.parse(spec) 4251cb0ef41Sopenharmony_ci t.same( 4261cb0ef41Sopenharmony_ci parsed, 4271cb0ef41Sopenharmony_ci { token: 'efgh5678', created: now, readonly: false, cidr_whitelist: [] }, 4281cb0ef41Sopenharmony_ci 'outputs the correct object' 4291cb0ef41Sopenharmony_ci ) 4301cb0ef41Sopenharmony_ci }, 4311cb0ef41Sopenharmony_ci }) 4321cb0ef41Sopenharmony_ci 4331cb0ef41Sopenharmony_ci await token.exec(['create']) 4341cb0ef41Sopenharmony_ci}) 4351cb0ef41Sopenharmony_ci 4361cb0ef41Sopenharmony_cit.test('token create parseable output', async t => { 4371cb0ef41Sopenharmony_ci const now = new Date().toISOString() 4381cb0ef41Sopenharmony_ci const password = 'thisisnotreallyapassword' 4391cb0ef41Sopenharmony_ci 4401cb0ef41Sopenharmony_ci const { token, joinedOutput } = await mockToken(t, { 4411cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org', parseable: true }, 4421cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 4431cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 4441cb0ef41Sopenharmony_ci return { token: 'thisisnotarealtoken' } 4451cb0ef41Sopenharmony_ci }, 4461cb0ef41Sopenharmony_ci readUserInfo: { 4471cb0ef41Sopenharmony_ci password: () => Promise.resolve(password), 4481cb0ef41Sopenharmony_ci }, 4491cb0ef41Sopenharmony_ci profile: { 4501cb0ef41Sopenharmony_ci createToken: (pw, readonly, cidr) => { 4511cb0ef41Sopenharmony_ci t.equal(pw, password) 4521cb0ef41Sopenharmony_ci t.equal(readonly, false) 4531cb0ef41Sopenharmony_ci t.same(cidr, [], 'defaults to empty array') 4541cb0ef41Sopenharmony_ci return { 4551cb0ef41Sopenharmony_ci key: 'abcd1234', 4561cb0ef41Sopenharmony_ci token: 'efgh5678', 4571cb0ef41Sopenharmony_ci created: now, 4581cb0ef41Sopenharmony_ci updated: now, 4591cb0ef41Sopenharmony_ci readonly: false, 4601cb0ef41Sopenharmony_ci cidr_whitelist: [], 4611cb0ef41Sopenharmony_ci } 4621cb0ef41Sopenharmony_ci }, 4631cb0ef41Sopenharmony_ci }, 4641cb0ef41Sopenharmony_ci }) 4651cb0ef41Sopenharmony_ci 4661cb0ef41Sopenharmony_ci await token.exec(['create']) 4671cb0ef41Sopenharmony_ci 4681cb0ef41Sopenharmony_ci const spec = joinedOutput().split(/\r?\n/) 4691cb0ef41Sopenharmony_ci 4701cb0ef41Sopenharmony_ci t.match(spec[0], 'token\tefgh5678', 'prints the token') 4711cb0ef41Sopenharmony_ci t.match(spec[1], `created\t${now}`, 'prints the created timestamp') 4721cb0ef41Sopenharmony_ci t.match(spec[2], 'readonly\tfalse', 'prints the readonly flag') 4731cb0ef41Sopenharmony_ci t.match(spec[3], 'cidr_whitelist\t', 'prints the cidr whitelist') 4741cb0ef41Sopenharmony_ci}) 4751cb0ef41Sopenharmony_ci 4761cb0ef41Sopenharmony_cit.test('token create ipv6 cidr', async t => { 4771cb0ef41Sopenharmony_ci const password = 'thisisnotreallyapassword' 4781cb0ef41Sopenharmony_ci 4791cb0ef41Sopenharmony_ci const { token } = await mockToken(t, { 4801cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org', cidr: '::1/128' }, 4811cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 4821cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 4831cb0ef41Sopenharmony_ci return { token: 'thisisnotarealtoken' } 4841cb0ef41Sopenharmony_ci }, 4851cb0ef41Sopenharmony_ci readUserInfo: { 4861cb0ef41Sopenharmony_ci password: () => Promise.resolve(password), 4871cb0ef41Sopenharmony_ci }, 4881cb0ef41Sopenharmony_ci }) 4891cb0ef41Sopenharmony_ci 4901cb0ef41Sopenharmony_ci await t.rejects( 4911cb0ef41Sopenharmony_ci token.exec(['create']), 4921cb0ef41Sopenharmony_ci { 4931cb0ef41Sopenharmony_ci code: 'EINVALIDCIDR', 4941cb0ef41Sopenharmony_ci message: /CIDR whitelist can only contain IPv4 addresses, ::1\/128 is IPv6/, 4951cb0ef41Sopenharmony_ci }, 4961cb0ef41Sopenharmony_ci 'returns correct error' 4971cb0ef41Sopenharmony_ci ) 4981cb0ef41Sopenharmony_ci}) 4991cb0ef41Sopenharmony_ci 5001cb0ef41Sopenharmony_cit.test('token create invalid cidr', async t => { 5011cb0ef41Sopenharmony_ci const password = 'thisisnotreallyapassword' 5021cb0ef41Sopenharmony_ci 5031cb0ef41Sopenharmony_ci const { token } = await mockToken(t, { 5041cb0ef41Sopenharmony_ci config: { registry: 'https://registry.npmjs.org', cidr: 'apple/cider' }, 5051cb0ef41Sopenharmony_ci getCredentialsByURI: uri => { 5061cb0ef41Sopenharmony_ci t.equal(uri, 'https://registry.npmjs.org/', 'requests correct registry') 5071cb0ef41Sopenharmony_ci return { token: 'thisisnotarealtoken' } 5081cb0ef41Sopenharmony_ci }, 5091cb0ef41Sopenharmony_ci readUserInfo: { 5101cb0ef41Sopenharmony_ci password: () => Promise.resolve(password), 5111cb0ef41Sopenharmony_ci }, 5121cb0ef41Sopenharmony_ci }) 5131cb0ef41Sopenharmony_ci 5141cb0ef41Sopenharmony_ci await t.rejects( 5151cb0ef41Sopenharmony_ci token.exec(['create']), 5161cb0ef41Sopenharmony_ci { code: 'EINVALIDCIDR', message: /CIDR whitelist contains invalid CIDR entry: apple\/cider/ }, 5171cb0ef41Sopenharmony_ci 'returns correct error' 5181cb0ef41Sopenharmony_ci ) 5191cb0ef41Sopenharmony_ci}) 520