Welcome to mirror list, hosted at ThFree Co, Russian Federation.

get-identity.js « utils « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cc713b3378166da7f07ca0483be599a08205f177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const { test } = require('tap')
const requireInject = require('require-inject')

test('throws ENOREGISTRY when no registry option is provided', async (t) => {
  t.plan(2)
  const getIdentity = requireInject('../../../lib/utils/get-identity.js')

  try {
    await getIdentity({})
  } catch (err) {
    t.equal(err.code, 'ENOREGISTRY', 'assigns the appropriate error code')
    t.equal(err.message, 'No registry specified.', 'returns the correct error message')
  }
})

test('returns username from uri when provided', async (t) => {
  t.plan(1)

  const getIdentity = requireInject('../../../lib/utils/get-identity.js')
  const npm = {
    config: {
      getCredentialsByURI: () => {
        return { username: 'foo' }
      },
    },
  }

  const identity = await getIdentity(npm, { registry: 'https://registry.npmjs.org' })
  t.equal(identity, 'foo', 'returns username from uri')
})

test('calls registry whoami when token is provided', async (t) => {
  t.plan(3)

  const options = {
    registry: 'https://registry.npmjs.org',
    token: 'thisisnotreallyatoken',
  }

  const getIdentity = requireInject('../../../lib/utils/get-identity.js', {
    'npm-registry-fetch': {
      json: (path, opts) => {
        t.equal(path, '/-/whoami', 'calls whoami')
        t.same(opts, options, 'passes through provided options')
        return { username: 'foo' }
      },
    },
  })
  const npm = {
    config: {
      getCredentialsByURI: () => options,
    },
  }

  const identity = await getIdentity(npm, options)
  t.equal(identity, 'foo', 'fetched username from registry')
})

test('throws ENEEDAUTH when response does not include a username', async (t) => {
  t.plan(3)

  const options = {
    registry: 'https://registry.npmjs.org',
    token: 'thisisnotreallyatoken',
  }

  const getIdentity = requireInject('../../../lib/utils/get-identity.js', {
    'npm-registry-fetch': {
      json: (path, opts) => {
        t.equal(path, '/-/whoami', 'calls whoami')
        t.same(opts, options, 'passes through provided options')
        return {}
      },
    },
  })
  const npm = {
    config: {
      getCredentialsByURI: () => options,
    },
  }

  try {
    await getIdentity(npm, options)
  } catch (err) {
    t.equal(err.code, 'ENEEDAUTH', 'throws correct error code')
  }
})

test('throws ENEEDAUTH when neither username nor token is configured', async (t) => {
  t.plan(1)
  const getIdentity = requireInject('../../../lib/utils/get-identity.js', {
  })
  const npm = {
    config: {
      getCredentialsByURI: () => ({}),
    },
  }

  try {
    await getIdentity(npm, { registry: 'https://registry.npmjs.org' })
  } catch (err) {
    t.equal(err.code, 'ENEEDAUTH', 'throws correct error code')
  }
})