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

logout.js « commands « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ee01e7500d1412dad8e4b68982ac913b5aaa4b29 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const t = require('tap')
const { fake: mockNpm } = require('../../fixtures/mock-npm')

const config = {
  registry: 'https://registry.npmjs.org/',
  scope: '',
}
const flatOptions = {
  registry: 'https://registry.npmjs.org/',
  scope: '',
}
const npm = mockNpm({ config, flatOptions })
let result = null

const mockLogout = (otherMocks) => {
  const Logout = t.mock('../../../lib/commands/logout.js', {
    'npm-registry-fetch': (url, opts) => {
      result = { url, opts }
    },
    ...otherMocks,
  })
  return new Logout(npm)
}

t.afterEach(() => {
  delete flatOptions.token
  result = null
  config.clearCredentialsByURI = null
  config.delete = null
  config.save = null
})

t.test('token logout', async t => {
  t.plan(5)

  flatOptions['//registry.npmjs.org/:_authToken'] = '@foo/'

  npm.config.clearCredentialsByURI = registry => {
    t.equal(
      registry,
      'https://registry.npmjs.org/',
      'should clear credentials from the expected registry'
    )
  }

  npm.config.save = type => {
    t.equal(type, 'user', 'should save to user config')
  }

  const logout = mockLogout({
    'proc-log': {
      verbose: (title, msg) => {
        t.equal(title, 'logout', 'should have correcct log prefix')
        t.equal(
          msg,
          'clearing token for https://registry.npmjs.org/',
          'should log message with correct registry'
        )
      },
    },
  })

  await logout.exec([])

  t.same(
    result,
    {
      url: '/-/user/token/%40foo%2F',
      opts: {
        registry: 'https://registry.npmjs.org/',
        scope: '',
        '//registry.npmjs.org/:_authToken': '@foo/',
        method: 'DELETE',
        ignoreBody: true,
      },
    },
    'should call npm-registry-fetch with expected values'
  )
})

t.test('token scoped logout', async t => {
  t.teardown(() => {
    config.scope = ''
    delete flatOptions['//diff-registry.npmjs.com/:_authToken']
    delete flatOptions['//registry.npmjs.org/:_authToken']
    delete config['@myscope:registry']
    delete flatOptions.scope
    result = null
    config.clearCredentialsByURI = null
    config.delete = null
    config.save = null
  })

  t.plan(7)

  flatOptions['//diff-registry.npmjs.com/:_authToken'] = '@bar/'
  flatOptions['//registry.npmjs.org/:_authToken'] = '@foo/'
  config.scope = '@myscope'
  config['@myscope:registry'] = 'https://diff-registry.npmjs.com/'
  flatOptions.scope = '@myscope'
  flatOptions['@myscope:registry'] = 'https://diff-registry.npmjs.com/'

  npm.config.clearCredentialsByURI = registry => {
    t.equal(
      registry,
      'https://diff-registry.npmjs.com/',
      'should clear credentials from the expected registry'
    )
  }

  npm.config.delete = (ref, type) => {
    t.equal(ref, '@myscope:registry', 'should delete scoped registyr from config')
    t.equal(type, 'user', 'should delete from user config')
  }

  npm.config.save = type => {
    t.equal(type, 'user', 'should save to user config')
  }

  const logout = mockLogout({
    'proc-log': {
      verbose: (title, msg) => {
        t.equal(title, 'logout', 'should have correcct log prefix')
        t.equal(
          msg,
          'clearing token for https://diff-registry.npmjs.com/',
          'should log message with correct registry'
        )
      },
    },
  })

  await logout.exec([])

  t.same(
    result,
    {
      url: '/-/user/token/%40bar%2F',
      opts: {
        registry: 'https://registry.npmjs.org/',
        '@myscope:registry': 'https://diff-registry.npmjs.com/',
        scope: '@myscope',
        '//registry.npmjs.org/:_authToken': '@foo/', // <- removed by npm-registry-fetch
        '//diff-registry.npmjs.com/:_authToken': '@bar/',
        method: 'DELETE',
        ignoreBody: true,
      },
    },
    'should call npm-registry-fetch with expected values'
  )
})

t.test('user/pass logout', async t => {
  t.teardown(() => {
    delete flatOptions['//registry.npmjs.org/:username']
    delete flatOptions['//registry.npmjs.org/:_password']
    npm.config.clearCredentialsByURI = null
    npm.config.save = null
  })
  t.plan(2)

  flatOptions['//registry.npmjs.org/:username'] = 'foo'
  flatOptions['//registry.npmjs.org/:_password'] = 'bar'

  npm.config.clearCredentialsByURI = () => null
  npm.config.save = () => null

  const logout = mockLogout({
    'proc-log': {
      verbose: (title, msg) => {
        t.equal(title, 'logout', 'should have correct log prefix')
        t.equal(
          msg,
          'clearing user credentials for https://registry.npmjs.org/',
          'should log message with correct registry'
        )
      },
    },
  })

  await logout.exec([])
})

t.test('missing credentials', async t => {
  const logout = mockLogout()

  await t.rejects(
    logout.exec([]),
    {
      code: 'ENEEDAUTH',
      message: /not logged in to https:\/\/registry.npmjs.org\/, so can't log out!/,
    },
    'should throw with expected error code'
  )
})

t.test('ignore invalid scoped registry config', async t => {
  t.teardown(() => {
    delete flatOptions.token
    result = null
    config.clearCredentialsByURI = null
    config.delete = null
    config.save = null
  })
  t.plan(4)

  flatOptions['//registry.npmjs.org/:_authToken'] = '@foo/'
  config.scope = '@myscope'
  flatOptions['@myscope:registry'] = ''

  npm.config.clearCredentialsByURI = registry => {
    t.equal(
      registry,
      'https://registry.npmjs.org/',
      'should clear credentials from the expected registry'
    )
  }

  npm.config.delete = () => null
  npm.config.save = () => null

  const logout = mockLogout({
    'proc-log': {
      verbose: (title, msg) => {
        t.equal(title, 'logout', 'should have correcct log prefix')
        t.equal(
          msg,
          'clearing token for https://registry.npmjs.org/',
          'should log message with correct registry'
        )
      },
    },
  })

  await logout.exec([])

  t.same(
    result,
    {
      url: '/-/user/token/%40foo%2F',
      opts: {
        '//registry.npmjs.org/:_authToken': '@foo/',
        registry: 'https://registry.npmjs.org/',
        '@myscope:registry': '',
        method: 'DELETE',
        ignoreBody: true,
      },
    },
    'should call npm-registry-fetch with expected values'
  )
})