11cb0ef41Sopenharmony_ciconst t = require('tap') 21cb0ef41Sopenharmony_ciconst setupMockNpm = require('../../fixtures/mock-npm') 31cb0ef41Sopenharmony_ciconst tmock = require('../../fixtures/tmock') 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst setupOtplease = async (t, { otp = {}, ...rest }, fn) => { 61cb0ef41Sopenharmony_ci const readUserInfo = { 71cb0ef41Sopenharmony_ci otp: async () => '1234', 81cb0ef41Sopenharmony_ci } 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci const webAuth = async (opener) => { 111cb0ef41Sopenharmony_ci opener() 121cb0ef41Sopenharmony_ci return '1234' 131cb0ef41Sopenharmony_ci } 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci const otplease = tmock(t, '{LIB}/utils/otplease.js', { 161cb0ef41Sopenharmony_ci '{LIB}/utils/read-user-info.js': readUserInfo, 171cb0ef41Sopenharmony_ci '{LIB}/utils/open-url-prompt.js': () => {}, 181cb0ef41Sopenharmony_ci '{LIB}/utils/web-auth': webAuth, 191cb0ef41Sopenharmony_ci }) 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci const { npm } = await setupMockNpm(t, rest) 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci return await otplease(npm, otp, fn) 241cb0ef41Sopenharmony_ci} 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_cit.test('returns function results on success', async (t) => { 271cb0ef41Sopenharmony_ci const result = await setupOtplease(t, {}, () => 'test string') 281cb0ef41Sopenharmony_ci t.equal('test string', result) 291cb0ef41Sopenharmony_ci}) 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cit.test('returns function results on otp success', async (t) => { 321cb0ef41Sopenharmony_ci const fn = ({ otp }) => { 331cb0ef41Sopenharmony_ci if (otp) { 341cb0ef41Sopenharmony_ci return 'success' 351cb0ef41Sopenharmony_ci } 361cb0ef41Sopenharmony_ci throw Object.assign(new Error('nope'), { code: 'EOTP' }) 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci const result = await setupOtplease(t, { 401cb0ef41Sopenharmony_ci globals: { 411cb0ef41Sopenharmony_ci 'process.stdin': { isTTY: true }, 421cb0ef41Sopenharmony_ci 'process.stdout': { isTTY: true }, 431cb0ef41Sopenharmony_ci }, 441cb0ef41Sopenharmony_ci }, fn) 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci t.equal('success', result) 471cb0ef41Sopenharmony_ci}) 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_cit.test('prompts for otp for EOTP', async (t) => { 501cb0ef41Sopenharmony_ci let called = false 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci const fn = async (opts) => { 531cb0ef41Sopenharmony_ci if (!called) { 541cb0ef41Sopenharmony_ci called = true 551cb0ef41Sopenharmony_ci throw Object.assign(new Error('nope'), { code: 'EOTP' }) 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci return opts 581cb0ef41Sopenharmony_ci } 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci const result = await setupOtplease(t, { 611cb0ef41Sopenharmony_ci otp: { some: 'prop' }, 621cb0ef41Sopenharmony_ci globals: { 631cb0ef41Sopenharmony_ci 'process.stdin': { isTTY: true }, 641cb0ef41Sopenharmony_ci 'process.stdout': { isTTY: true }, 651cb0ef41Sopenharmony_ci }, 661cb0ef41Sopenharmony_ci }, fn) 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci t.strictSame(result, { some: 'prop', otp: '1234' }) 691cb0ef41Sopenharmony_ci}) 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_cit.test('returns function results on webauth success', async (t) => { 721cb0ef41Sopenharmony_ci const fn = ({ otp }) => { 731cb0ef41Sopenharmony_ci if (otp) { 741cb0ef41Sopenharmony_ci return 'success' 751cb0ef41Sopenharmony_ci } 761cb0ef41Sopenharmony_ci throw Object.assign(new Error('nope'), { 771cb0ef41Sopenharmony_ci code: 'EOTP', 781cb0ef41Sopenharmony_ci body: { 791cb0ef41Sopenharmony_ci authUrl: 'https://www.example.com/auth', 801cb0ef41Sopenharmony_ci doneUrl: 'https://www.example.com/done', 811cb0ef41Sopenharmony_ci }, 821cb0ef41Sopenharmony_ci }) 831cb0ef41Sopenharmony_ci } 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci const result = await setupOtplease(t, { 861cb0ef41Sopenharmony_ci config: { browser: 'firefox' }, 871cb0ef41Sopenharmony_ci globals: { 881cb0ef41Sopenharmony_ci 'process.stdin': { isTTY: true }, 891cb0ef41Sopenharmony_ci 'process.stdout': { isTTY: true }, 901cb0ef41Sopenharmony_ci }, 911cb0ef41Sopenharmony_ci }, fn) 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci t.equal('success', result) 941cb0ef41Sopenharmony_ci}) 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_cit.test('prompts for otp for 401', async (t) => { 971cb0ef41Sopenharmony_ci let called = false 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci const fn = async (opts) => { 1001cb0ef41Sopenharmony_ci if (!called) { 1011cb0ef41Sopenharmony_ci called = true 1021cb0ef41Sopenharmony_ci throw Object.assign(new Error('nope'), { 1031cb0ef41Sopenharmony_ci code: 'E401', 1041cb0ef41Sopenharmony_ci body: 'one-time pass required', 1051cb0ef41Sopenharmony_ci }) 1061cb0ef41Sopenharmony_ci } 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci return opts 1091cb0ef41Sopenharmony_ci } 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci const result = await setupOtplease(t, { 1121cb0ef41Sopenharmony_ci globals: { 1131cb0ef41Sopenharmony_ci 'process.stdin': { isTTY: true }, 1141cb0ef41Sopenharmony_ci 'process.stdout': { isTTY: true }, 1151cb0ef41Sopenharmony_ci }, 1161cb0ef41Sopenharmony_ci }, fn) 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci t.strictSame(result, { otp: '1234' }) 1191cb0ef41Sopenharmony_ci}) 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_cit.test('does not prompt for non-otp errors', async (t) => { 1221cb0ef41Sopenharmony_ci const fn = async (opts) => { 1231cb0ef41Sopenharmony_ci throw new Error('nope') 1241cb0ef41Sopenharmony_ci } 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci await t.rejects(setupOtplease(t, { 1271cb0ef41Sopenharmony_ci globals: { 1281cb0ef41Sopenharmony_ci 'process.stdin': { isTTY: true }, 1291cb0ef41Sopenharmony_ci 'process.stdout': { isTTY: true }, 1301cb0ef41Sopenharmony_ci }, 1311cb0ef41Sopenharmony_ci }, fn), { message: 'nope' }, 'rejects with the original error') 1321cb0ef41Sopenharmony_ci}) 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_cit.test('does not prompt if stdin or stdout is not a tty', async (t) => { 1351cb0ef41Sopenharmony_ci const fn = async (opts) => { 1361cb0ef41Sopenharmony_ci throw Object.assign(new Error('nope'), { code: 'EOTP' }) 1371cb0ef41Sopenharmony_ci } 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci await t.rejects(setupOtplease(t, { 1401cb0ef41Sopenharmony_ci globals: { 1411cb0ef41Sopenharmony_ci 'process.stdin': { isTTY: false }, 1421cb0ef41Sopenharmony_ci 'process.stdout': { isTTY: false }, 1431cb0ef41Sopenharmony_ci }, 1441cb0ef41Sopenharmony_ci }, fn), { message: 'nope' }, 'rejects with the original error') 1451cb0ef41Sopenharmony_ci}) 146