11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst fs = require('fs/promises')
31cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
41cb0ef41Sopenharmony_ciconst MockRegistry = require('@npmcli/mock-registry')
51cb0ef41Sopenharmony_ciconst { join } = require('path')
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cit.test('token logout - user config', async t => {
81cb0ef41Sopenharmony_ci  const { npm, home, logs } = await loadMockNpm(t, {
91cb0ef41Sopenharmony_ci    homeDir: {
101cb0ef41Sopenharmony_ci      '.npmrc': [
111cb0ef41Sopenharmony_ci        '//registry.npmjs.org/:_authToken=@foo/',
121cb0ef41Sopenharmony_ci        'other-config=true',
131cb0ef41Sopenharmony_ci      ].join('\n'),
141cb0ef41Sopenharmony_ci    },
151cb0ef41Sopenharmony_ci  })
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  const mockRegistry = new MockRegistry({ tap: t, registry: 'https://registry.npmjs.org/' })
181cb0ef41Sopenharmony_ci  mockRegistry.logout('@foo/')
191cb0ef41Sopenharmony_ci  await npm.exec('logout', [])
201cb0ef41Sopenharmony_ci  t.equal(
211cb0ef41Sopenharmony_ci    logs.verbose.find(l => l[0] === 'logout')[1],
221cb0ef41Sopenharmony_ci    'clearing token for https://registry.npmjs.org/',
231cb0ef41Sopenharmony_ci    'should log message with correct registry'
241cb0ef41Sopenharmony_ci  )
251cb0ef41Sopenharmony_ci  const userRc = await fs.readFile(join(home, '.npmrc'), 'utf-8')
261cb0ef41Sopenharmony_ci  t.equal(userRc.trim(), 'other-config=true')
271cb0ef41Sopenharmony_ci})
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cit.test('token scoped logout - user config', async t => {
301cb0ef41Sopenharmony_ci  const { npm, home, logs } = await loadMockNpm(t, {
311cb0ef41Sopenharmony_ci    config: {
321cb0ef41Sopenharmony_ci      scope: '@myscope',
331cb0ef41Sopenharmony_ci    },
341cb0ef41Sopenharmony_ci    homeDir: {
351cb0ef41Sopenharmony_ci      '.npmrc': [
361cb0ef41Sopenharmony_ci        '//diff-registry.npmjs.com/:_authToken=@bar/',
371cb0ef41Sopenharmony_ci        '//registry.npmjs.org/:_authToken=@foo/',
381cb0ef41Sopenharmony_ci        '@myscope:registry=https://diff-registry.npmjs.com/',
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci      ].join('\n'),
411cb0ef41Sopenharmony_ci    },
421cb0ef41Sopenharmony_ci  })
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  const mockRegistry = new MockRegistry({ tap: t, registry: 'https://diff-registry.npmjs.com/' })
451cb0ef41Sopenharmony_ci  mockRegistry.logout('@bar/')
461cb0ef41Sopenharmony_ci  await npm.exec('logout', [])
471cb0ef41Sopenharmony_ci  t.equal(
481cb0ef41Sopenharmony_ci    logs.verbose.find(l => l[0] === 'logout')[1],
491cb0ef41Sopenharmony_ci    'clearing token for https://diff-registry.npmjs.com/',
501cb0ef41Sopenharmony_ci    'should log message with correct registry'
511cb0ef41Sopenharmony_ci  )
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  const userRc = await fs.readFile(join(home, '.npmrc'), 'utf-8')
541cb0ef41Sopenharmony_ci  t.equal(userRc.trim(), '//registry.npmjs.org/:_authToken=@foo/')
551cb0ef41Sopenharmony_ci})
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_cit.test('user/pass logout - user config', async t => {
581cb0ef41Sopenharmony_ci  const { npm, home, logs } = await loadMockNpm(t, {
591cb0ef41Sopenharmony_ci    homeDir: {
601cb0ef41Sopenharmony_ci      '.npmrc': [
611cb0ef41Sopenharmony_ci        '//registry.npmjs.org/:username=foo',
621cb0ef41Sopenharmony_ci        '//registry.npmjs.org/:_password=bar',
631cb0ef41Sopenharmony_ci        'other-config=true',
641cb0ef41Sopenharmony_ci      ].join('\n'),
651cb0ef41Sopenharmony_ci    },
661cb0ef41Sopenharmony_ci  })
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  await npm.exec('logout', [])
691cb0ef41Sopenharmony_ci  t.equal(
701cb0ef41Sopenharmony_ci    logs.verbose.find(l => l[0] === 'logout')[1],
711cb0ef41Sopenharmony_ci    'clearing user credentials for https://registry.npmjs.org/',
721cb0ef41Sopenharmony_ci    'should log message with correct registry'
731cb0ef41Sopenharmony_ci  )
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  const userRc = await fs.readFile(join(home, '.npmrc'), 'utf-8')
761cb0ef41Sopenharmony_ci  t.equal(userRc.trim(), 'other-config=true')
771cb0ef41Sopenharmony_ci})
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_cit.test('missing credentials', async t => {
801cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t)
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  await t.rejects(
831cb0ef41Sopenharmony_ci    npm.exec('logout', []),
841cb0ef41Sopenharmony_ci    {
851cb0ef41Sopenharmony_ci      code: 'ENEEDAUTH',
861cb0ef41Sopenharmony_ci      message: /not logged in to https:\/\/registry.npmjs.org\/, so can't log out!/,
871cb0ef41Sopenharmony_ci    },
881cb0ef41Sopenharmony_ci    'should reject with expected error code'
891cb0ef41Sopenharmony_ci  )
901cb0ef41Sopenharmony_ci})
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_cit.test('ignore invalid scoped registry config', async t => {
931cb0ef41Sopenharmony_ci  const { npm, home, logs } = await loadMockNpm(t, {
941cb0ef41Sopenharmony_ci    config: { scope: '@myscope' },
951cb0ef41Sopenharmony_ci    homeDir: {
961cb0ef41Sopenharmony_ci      '.npmrc': [
971cb0ef41Sopenharmony_ci        '//registry.npmjs.org/:_authToken=@foo/',
981cb0ef41Sopenharmony_ci        'other-config=true',
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci      ].join('\n'),
1011cb0ef41Sopenharmony_ci    },
1021cb0ef41Sopenharmony_ci  })
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  const mockRegistry = new MockRegistry({ tap: t, registry: 'https://registry.npmjs.org/' })
1051cb0ef41Sopenharmony_ci  mockRegistry.logout('@foo/')
1061cb0ef41Sopenharmony_ci  await npm.exec('logout', [])
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  t.equal(
1091cb0ef41Sopenharmony_ci    logs.verbose.find(l => l[0] === 'logout')[1],
1101cb0ef41Sopenharmony_ci    'clearing token for https://registry.npmjs.org/',
1111cb0ef41Sopenharmony_ci    'should log message with correct registry'
1121cb0ef41Sopenharmony_ci  )
1131cb0ef41Sopenharmony_ci  const userRc = await fs.readFile(join(home, '.npmrc'), 'utf-8')
1141cb0ef41Sopenharmony_ci  t.equal(userRc.trim(), 'other-config=true')
1151cb0ef41Sopenharmony_ci})
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_cit.test('token logout - project config', async t => {
1181cb0ef41Sopenharmony_ci  const { npm, home, logs, prefix } = await loadMockNpm(t, {
1191cb0ef41Sopenharmony_ci    homeDir: {
1201cb0ef41Sopenharmony_ci      '.npmrc': [
1211cb0ef41Sopenharmony_ci        '//registry.npmjs.org/:_authToken=@foo/',
1221cb0ef41Sopenharmony_ci        'other-config=true',
1231cb0ef41Sopenharmony_ci      ].join('\n'),
1241cb0ef41Sopenharmony_ci    },
1251cb0ef41Sopenharmony_ci    prefixDir: {
1261cb0ef41Sopenharmony_ci      '.npmrc': [
1271cb0ef41Sopenharmony_ci        '//registry.npmjs.org/:_authToken=@bar/',
1281cb0ef41Sopenharmony_ci        'other-config=true',
1291cb0ef41Sopenharmony_ci      ].join('\n'),
1301cb0ef41Sopenharmony_ci    },
1311cb0ef41Sopenharmony_ci  })
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci  const mockRegistry = new MockRegistry({ tap: t, registry: 'https://registry.npmjs.org/' })
1341cb0ef41Sopenharmony_ci  mockRegistry.logout('@bar/')
1351cb0ef41Sopenharmony_ci  await npm.exec('logout', [])
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci  t.equal(
1381cb0ef41Sopenharmony_ci    logs.verbose.find(l => l[0] === 'logout')[1],
1391cb0ef41Sopenharmony_ci    'clearing token for https://registry.npmjs.org/',
1401cb0ef41Sopenharmony_ci    'should log message with correct registry'
1411cb0ef41Sopenharmony_ci  )
1421cb0ef41Sopenharmony_ci  const userRc = await fs.readFile(join(home, '.npmrc'), 'utf-8')
1431cb0ef41Sopenharmony_ci  t.equal(userRc.trim(), [
1441cb0ef41Sopenharmony_ci    '//registry.npmjs.org/:_authToken=@foo/',
1451cb0ef41Sopenharmony_ci    'other-config=true',
1461cb0ef41Sopenharmony_ci  ].join('\n'), 'leaves user config alone')
1471cb0ef41Sopenharmony_ci  t.equal(
1481cb0ef41Sopenharmony_ci    logs.verbose.find(l => l[0] === 'logout')[1],
1491cb0ef41Sopenharmony_ci    'clearing token for https://registry.npmjs.org/',
1501cb0ef41Sopenharmony_ci    'should log message with correct registry'
1511cb0ef41Sopenharmony_ci  )
1521cb0ef41Sopenharmony_ci  const projectRc = await fs.readFile(join(prefix, '.npmrc'), 'utf-8')
1531cb0ef41Sopenharmony_ci  t.equal(projectRc.trim(), 'other-config=true', 'removes project config')
1541cb0ef41Sopenharmony_ci})
155