• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var test = require('tap').test
2
3var npmconf = require('../../lib/config/core.js')
4var common = require('../common-config.js')
5
6var URI = 'https://registry.lvh.me:8661/'
7
8test('getting scope with no credentials set', function (t) {
9  npmconf.load({}, function (er, conf) {
10    t.ifError(er, 'configuration loaded')
11
12    var basic = conf.getCredentialsByURI(URI)
13    t.equal(basic.scope, '//registry.lvh.me:8661/', 'nerfed URL extracted')
14
15    t.end()
16  })
17})
18
19test('trying to set credentials with no URI', function (t) {
20  npmconf.load(common.builtin, function (er, conf) {
21    t.ifError(er, 'configuration loaded')
22
23    t.throws(function () {
24      conf.setCredentialsByURI()
25    }, 'enforced missing URI')
26
27    t.end()
28  })
29})
30
31test('trying to clear credentials with no URI', function (t) {
32  npmconf.load(common.builtin, function (er, conf) {
33    t.ifError(er, 'configuration loaded')
34
35    t.throws(function () {
36      conf.clearCredentialsByURI()
37    }, 'enforced missing URI')
38
39    t.end()
40  })
41})
42
43test('set with missing credentials object', function (t) {
44  npmconf.load(common.builtin, function (er, conf) {
45    t.ifError(er, 'configuration loaded')
46
47    t.throws(function () {
48      conf.setCredentialsByURI(URI)
49    }, 'enforced missing credentials')
50
51    t.end()
52  })
53})
54
55test('set with empty credentials object', function (t) {
56  npmconf.load(common.builtin, function (er, conf) {
57    t.ifError(er, 'configuration loaded')
58
59    t.throws(function () {
60      conf.setCredentialsByURI(URI, {})
61    }, 'enforced missing credentials')
62
63    t.end()
64  })
65})
66
67test('set with token', function (t) {
68  npmconf.load(common.builtin, function (er, conf) {
69    t.ifError(er, 'configuration loaded')
70
71    t.doesNotThrow(function () {
72      conf.setCredentialsByURI(URI, { token: 'simple-token' })
73    }, 'needs only token')
74
75    var expected = {
76      scope: '//registry.lvh.me:8661/',
77      token: 'simple-token',
78      username: undefined,
79      password: undefined,
80      email: undefined,
81      auth: undefined,
82      alwaysAuth: false
83    }
84
85    t.same(conf.getCredentialsByURI(URI), expected, 'got bearer token and scope')
86
87    t.end()
88  })
89})
90
91test('clear with token', function (t) {
92  npmconf.load(common.builtin, function (er, conf) {
93    t.ifError(er, 'configuration loaded')
94
95    t.doesNotThrow(function () {
96      conf.setCredentialsByURI(URI, { token: 'simple-token' })
97    }, 'needs only token')
98
99    t.doesNotThrow(function () {
100      conf.clearCredentialsByURI(URI)
101    }, 'needs only URI')
102
103    t.notOk(conf.getCredentialsByURI(URI).token, 'token all gone')
104
105    t.end()
106  })
107})
108
109test('set with missing username', function (t) {
110  npmconf.load(common.builtin, function (er, conf) {
111    t.ifError(er, 'configuration loaded')
112
113    var credentials = {
114      password: 'password',
115      email: 'ogd@aoaioxxysz.net'
116    }
117
118    t.throws(function () {
119      conf.setCredentialsByURI(URI, credentials)
120    }, 'enforced missing email')
121
122    t.end()
123  })
124})
125
126test('set with missing password', function (t) {
127  npmconf.load(common.builtin, function (er, conf) {
128    t.ifError(er, 'configuration loaded')
129
130    var credentials = {
131      username: 'username',
132      email: 'ogd@aoaioxxysz.net'
133    }
134
135    t.throws(function () {
136      conf.setCredentialsByURI(URI, credentials)
137    }, 'enforced missing email')
138
139    t.end()
140  })
141})
142
143test('set with missing email', function (t) {
144  npmconf.load(common.builtin, function (er, conf) {
145    t.ifError(er, 'configuration loaded')
146
147    var credentials = {
148      username: 'username',
149      password: 'password'
150    }
151
152    t.throws(function () {
153      conf.setCredentialsByURI(URI, credentials)
154    }, 'enforced missing email')
155
156    t.end()
157  })
158})
159
160test('set with old-style credentials', function (t) {
161  npmconf.load(common.builtin, function (er, conf) {
162    t.ifError(er, 'configuration loaded')
163
164    var credentials = {
165      username: 'username',
166      password: 'password',
167      email: 'ogd@aoaioxxysz.net'
168    }
169
170    t.doesNotThrow(function () {
171      conf.setCredentialsByURI(URI, credentials)
172    }, 'requires all of username, password, and email')
173
174    var expected = {
175      scope: '//registry.lvh.me:8661/',
176      token: undefined,
177      username: 'username',
178      password: 'password',
179      email: 'ogd@aoaioxxysz.net',
180      auth: 'dXNlcm5hbWU6cGFzc3dvcmQ=',
181      alwaysAuth: false
182    }
183
184    t.same(conf.getCredentialsByURI(URI), expected, 'got credentials')
185
186    t.end()
187  })
188})
189
190test('clear with old-style credentials', function (t) {
191  npmconf.load(common.builtin, function (er, conf) {
192    t.ifError(er, 'configuration loaded')
193
194    var credentials = {
195      username: 'username',
196      password: 'password',
197      email: 'ogd@aoaioxxysz.net'
198    }
199
200    t.doesNotThrow(function () {
201      conf.setCredentialsByURI(URI, credentials)
202    }, 'requires all of username, password, and email')
203
204    t.doesNotThrow(function () {
205      conf.clearCredentialsByURI(URI)
206    }, 'clearing only required URI')
207
208    t.notOk(conf.getCredentialsByURI(URI).username, 'username cleared')
209    t.notOk(conf.getCredentialsByURI(URI).password, 'password cleared')
210
211    t.end()
212  })
213})
214
215test('get old-style credentials for default registry', function (t) {
216  npmconf.load(common.builtin, function (er, conf) {
217    var actual = conf.getCredentialsByURI(conf.get('registry'))
218    var expected = {
219      scope: '//registry.npmjs.org/',
220      token: undefined,
221      password: 'password',
222      username: 'username',
223      email: 'i@izs.me',
224      auth: 'dXNlcm5hbWU6cGFzc3dvcmQ=',
225      alwaysAuth: false
226    }
227    t.same(actual, expected)
228    t.end()
229  })
230})
231
232test('set with always-auth enabled', function (t) {
233  npmconf.load(common.builtin, function (er, conf) {
234    t.ifError(er, 'configuration loaded')
235
236    var credentials = {
237      username: 'username',
238      password: 'password',
239      email: 'ogd@aoaioxxysz.net',
240      alwaysAuth: true
241    }
242
243    conf.setCredentialsByURI(URI, credentials)
244
245    var expected = {
246      scope: '//registry.lvh.me:8661/',
247      token: undefined,
248      username: 'username',
249      password: 'password',
250      email: 'ogd@aoaioxxysz.net',
251      auth: 'dXNlcm5hbWU6cGFzc3dvcmQ=',
252      alwaysAuth: true
253    }
254
255    t.same(conf.getCredentialsByURI(URI), expected, 'got credentials')
256
257    t.end()
258  })
259})
260
261test('set with always-auth disabled', function (t) {
262  npmconf.load(common.builtin, function (er, conf) {
263    t.ifError(er, 'configuration loaded')
264
265    var credentials = {
266      username: 'username',
267      password: 'password',
268      email: 'ogd@aoaioxxysz.net',
269      alwaysAuth: false
270    }
271
272    conf.setCredentialsByURI(URI, credentials)
273
274    var expected = {
275      scope: '//registry.lvh.me:8661/',
276      token: undefined,
277      username: 'username',
278      password: 'password',
279      email: 'ogd@aoaioxxysz.net',
280      auth: 'dXNlcm5hbWU6cGFzc3dvcmQ=',
281      alwaysAuth: false
282    }
283
284    t.same(conf.getCredentialsByURI(URI), expected, 'got credentials')
285
286    t.end()
287  })
288})
289
290test('set with global always-auth enabled', function (t) {
291  npmconf.load(common.builtin, function (er, conf) {
292    t.ifError(er, 'configuration loaded')
293    var original = conf.get('always-auth')
294    conf.set('always-auth', true)
295
296    var credentials = {
297      username: 'username',
298      password: 'password',
299      email: 'ogd@aoaioxxysz.net'
300    }
301
302    conf.setCredentialsByURI(URI, credentials)
303
304    var expected = {
305      scope: '//registry.lvh.me:8661/',
306      token: undefined,
307      username: 'username',
308      password: 'password',
309      email: 'ogd@aoaioxxysz.net',
310      auth: 'dXNlcm5hbWU6cGFzc3dvcmQ=',
311      alwaysAuth: true
312    }
313
314    t.same(conf.getCredentialsByURI(URI), expected, 'got credentials')
315
316    conf.set('always-auth', original)
317    t.end()
318  })
319})
320
321test('set with global always-auth disabled', function (t) {
322  npmconf.load(common.builtin, function (er, conf) {
323    t.ifError(er, 'configuration loaded')
324    var original = conf.get('always-auth')
325    conf.set('always-auth', false)
326
327    var credentials = {
328      username: 'username',
329      password: 'password',
330      email: 'ogd@aoaioxxysz.net'
331    }
332
333    conf.setCredentialsByURI(URI, credentials)
334
335    var expected = {
336      scope: '//registry.lvh.me:8661/',
337      token: undefined,
338      username: 'username',
339      password: 'password',
340      email: 'ogd@aoaioxxysz.net',
341      auth: 'dXNlcm5hbWU6cGFzc3dvcmQ=',
342      alwaysAuth: false
343    }
344
345    t.same(conf.getCredentialsByURI(URI), expected, 'got credentials')
346
347    conf.set('always-auth', original)
348    t.end()
349  })
350})
351