11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm')
31cb0ef41Sopenharmony_ciconst MockRegistry = require('@npmcli/mock-registry')
41cb0ef41Sopenharmony_ciconst nock = require('nock')
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst username = 'foo'
71cb0ef41Sopenharmony_ciconst auth = { '//registry.npmjs.org/:_authToken': 'test-auth-token' }
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cit.test('npm whoami', async t => {
101cb0ef41Sopenharmony_ci  const { npm, joinedOutput } = await loadMockNpm(t, { config: auth })
111cb0ef41Sopenharmony_ci  const registry = new MockRegistry({
121cb0ef41Sopenharmony_ci    tap: t,
131cb0ef41Sopenharmony_ci    registry: npm.config.get('registry'),
141cb0ef41Sopenharmony_ci    authorization: 'test-auth-token',
151cb0ef41Sopenharmony_ci  })
161cb0ef41Sopenharmony_ci  registry.whoami({ username })
171cb0ef41Sopenharmony_ci  await npm.exec('whoami', [])
181cb0ef41Sopenharmony_ci  t.equal(joinedOutput(), username, 'should print username')
191cb0ef41Sopenharmony_ci})
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cit.test('npm whoami --json', async t => {
221cb0ef41Sopenharmony_ci  const { npm, joinedOutput } = await loadMockNpm(t, {
231cb0ef41Sopenharmony_ci    config: {
241cb0ef41Sopenharmony_ci      json: true,
251cb0ef41Sopenharmony_ci      ...auth,
261cb0ef41Sopenharmony_ci    },
271cb0ef41Sopenharmony_ci  })
281cb0ef41Sopenharmony_ci  const registry = new MockRegistry({
291cb0ef41Sopenharmony_ci    tap: t,
301cb0ef41Sopenharmony_ci    registry: npm.config.get('registry'),
311cb0ef41Sopenharmony_ci    authorization: 'test-auth-token',
321cb0ef41Sopenharmony_ci  })
331cb0ef41Sopenharmony_ci  registry.whoami({ username })
341cb0ef41Sopenharmony_ci  await npm.exec('whoami', [])
351cb0ef41Sopenharmony_ci  t.equal(JSON.parse(joinedOutput()), username, 'should print username')
361cb0ef41Sopenharmony_ci})
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cit.test('npm whoami using mTLS', async t => {
391cb0ef41Sopenharmony_ci  const { npm, joinedOutput } = await loadMockNpm(t, { config: {
401cb0ef41Sopenharmony_ci    '//registry.npmjs.org/:certfile': '/some.cert',
411cb0ef41Sopenharmony_ci    '//registry.npmjs.org/:keyfile': '/some.key',
421cb0ef41Sopenharmony_ci  } })
431cb0ef41Sopenharmony_ci  const registry = new MockRegistry({
441cb0ef41Sopenharmony_ci    tap: t,
451cb0ef41Sopenharmony_ci    registry: npm.config.get('registry'),
461cb0ef41Sopenharmony_ci  })
471cb0ef41Sopenharmony_ci  registry.whoami({ username })
481cb0ef41Sopenharmony_ci  await npm.exec('whoami', [])
491cb0ef41Sopenharmony_ci  t.equal(joinedOutput(), username, 'should print username')
501cb0ef41Sopenharmony_ci})
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_cit.test('credentials from token', async t => {
531cb0ef41Sopenharmony_ci  const { npm, joinedOutput } = await loadMockNpm(t, {
541cb0ef41Sopenharmony_ci    config: {
551cb0ef41Sopenharmony_ci      '//registry.npmjs.org/:username': username,
561cb0ef41Sopenharmony_ci      '//registry.npmjs.org/:_password': 'hunter2',
571cb0ef41Sopenharmony_ci    },
581cb0ef41Sopenharmony_ci  })
591cb0ef41Sopenharmony_ci  await npm.exec('whoami', [])
601cb0ef41Sopenharmony_ci  t.equal(joinedOutput(), username, 'should print username')
611cb0ef41Sopenharmony_ci})
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_cit.test('not logged in', async t => {
641cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, {
651cb0ef41Sopenharmony_ci    config: {
661cb0ef41Sopenharmony_ci      json: true,
671cb0ef41Sopenharmony_ci    },
681cb0ef41Sopenharmony_ci  })
691cb0ef41Sopenharmony_ci  await t.rejects(npm.exec('whoami', []), { code: 'ENEEDAUTH' })
701cb0ef41Sopenharmony_ci})
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_cit.test('non-string username in response', async t => {
731cb0ef41Sopenharmony_ci  nock.disableNetConnect()
741cb0ef41Sopenharmony_ci  t.teardown(() => {
751cb0ef41Sopenharmony_ci    nock.enableNetConnect()
761cb0ef41Sopenharmony_ci  })
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  const server = nock('https://registry.npmjs.org', {
791cb0ef41Sopenharmony_ci    reqheaders: {
801cb0ef41Sopenharmony_ci      authorization: 'Bearer abcd1234',
811cb0ef41Sopenharmony_ci    },
821cb0ef41Sopenharmony_ci  })
831cb0ef41Sopenharmony_ci    .get('/-/whoami')
841cb0ef41Sopenharmony_ci    .reply(200, { username: null })
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, {
871cb0ef41Sopenharmony_ci    config: {
881cb0ef41Sopenharmony_ci      '//registry.npmjs.org/:_authToken': 'abcd1234',
891cb0ef41Sopenharmony_ci    },
901cb0ef41Sopenharmony_ci  })
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  await t.rejects(npm.exec('whoami', []), { code: 'ENEEDAUTH' })
931cb0ef41Sopenharmony_ci  t.ok(server.isDone())
941cb0ef41Sopenharmony_ci})
95