@@ -9,12 +9,6 @@ const tnock = require('./util/tnock.js')
9
9
const fetch = require ( '../index.js' )
10
10
const getAuth = require ( '../auth.js' )
11
11
12
- function confFromObj ( obj ) {
13
- const conf = new Map ( )
14
- Object . keys ( obj || { } ) . forEach ( k => { conf . set ( k , obj [ k ] ) } )
15
- return conf
16
- }
17
-
18
12
npmlog . level = process . env . LOGLEVEL || 'silent'
19
13
const OPTS = {
20
14
log : npmlog ,
@@ -25,31 +19,29 @@ const OPTS = {
25
19
minTimeout : 1 ,
26
20
maxTimeout : 10
27
21
} ,
28
- config : confFromObj ( {
29
- registry : 'https://mock.reg/'
30
- } )
22
+ registry : 'https://mock.reg/'
31
23
}
32
24
33
25
test ( 'basic auth' , t => {
34
- const config = new Map ( [
35
- [ ' registry' , 'https://my.custom.registry/here/' ] ,
36
- [ ' username' , 'globaluser' ] ,
37
- [ ' password' , Buffer . from ( 'globalpass' , 'utf8' ) . toString ( 'base64' ) ] ,
38
-
39
- [ '//my.custom.registry/here/:username' , 'user' ] ,
40
- [ '//my.custom.registry/here/:password' , Buffer . from ( 'pass' , 'utf8' ) . toString ( 'base64' ) ] ,
41
- [ '//my.custom.registry/here/:email' , '[email protected] ' ]
42
- ] )
43
- t . deepEqual ( getAuth ( config . get ( ' registry' ) , config ) , {
26
+ const config = {
27
+ registry : 'https://my.custom.registry/here/' ,
28
+ username : 'globaluser' ,
29
+ password : Buffer . from ( 'globalpass' , 'utf8' ) . toString ( 'base64' ) ,
30
+
31
+ '//my.custom.registry/here/:username' : 'user' ,
32
+ '//my.custom.registry/here/:password' : Buffer . from ( 'pass' , 'utf8' ) . toString ( 'base64' ) ,
33
+ '//my.custom.registry/here/:email' : '[email protected] '
34
+ }
35
+ t . deepEqual ( getAuth ( config . registry , config ) , {
44
36
alwaysAuth : false ,
45
37
username : 'user' ,
46
38
password : 'pass' ,
47
39
48
40
} , 'basic auth details generated' )
49
41
50
- const opts = Object . assign ( { } , OPTS , { config} )
42
+ const opts = Object . assign ( { } , OPTS , config )
51
43
const encoded = Buffer . from ( `user:pass` , 'utf8' ) . toString ( 'base64' )
52
- tnock ( t , opts . config . get ( ' registry' ) )
44
+ tnock ( t , opts . registry )
53
45
. matchHeader ( 'authorization' , auth => {
54
46
t . equal ( auth [ 0 ] , `Basic ${ encoded } ` , 'got encoded basic auth' )
55
47
return auth [ 0 ] === `Basic ${ encoded } `
@@ -61,19 +53,19 @@ test('basic auth', t => {
61
53
} )
62
54
63
55
test ( 'token auth' , t => {
64
- const config = new Map ( [
65
- [ 'registry' , 'https://my.custom.registry/here/' ] ,
66
- [ 'token' , 'deadbeef' ] ,
67
- [ '//my.custom.registry/here/:_authToken' , 'c0ffee' ] ,
68
- [ '//my.custom.registry/here/:token' , 'nope' ]
69
- ] )
70
- t . deepEqual ( getAuth ( config . get ( ' registry' ) , config ) , {
56
+ const config = {
57
+ 'registry' : 'https://my.custom.registry/here/' ,
58
+ 'token' : 'deadbeef' ,
59
+ '//my.custom.registry/here/:_authToken' : 'c0ffee' ,
60
+ '//my.custom.registry/here/:token' : 'nope'
61
+ }
62
+ t . deepEqual ( getAuth ( config . registry , config ) , {
71
63
alwaysAuth : false ,
72
64
token : 'c0ffee'
73
65
} , 'correct auth token picked out' )
74
66
75
- const opts = Object . assign ( { } , OPTS , { config} )
76
- tnock ( t , opts . config . get ( ' registry' ) )
67
+ const opts = Object . assign ( { } , OPTS , config )
68
+ tnock ( t , opts . registry )
77
69
. matchHeader ( 'authorization' , auth => {
78
70
t . equal ( auth [ 0 ] , 'Bearer c0ffee' , 'got correct bearer token' )
79
71
return auth [ 0 ] === 'Bearer c0ffee'
@@ -85,18 +77,18 @@ test('token auth', t => {
85
77
} )
86
78
87
79
test ( '_auth auth' , t => {
88
- const config = new Map ( [
89
- [ 'registry' , 'https://my.custom.registry/here/' ] ,
90
- [ '_auth' , 'deadbeef' ] ,
91
- [ '//my.custom.registry/here/:_auth' , 'c0ffee' ]
92
- ] )
93
- t . deepEqual ( getAuth ( config . get ( ' registry' ) , config ) , {
80
+ const config = {
81
+ 'registry' : 'https://my.custom.registry/here/' ,
82
+ '_auth' : 'deadbeef' ,
83
+ '//my.custom.registry/here/:_auth' : 'c0ffee'
84
+ }
85
+ t . deepEqual ( getAuth ( config . registry , config ) , {
94
86
alwaysAuth : false ,
95
87
_auth : 'c0ffee'
96
88
} , 'correct _auth picked out' )
97
89
98
- const opts = Object . assign ( { } , OPTS , { config} )
99
- tnock ( t , opts . config . get ( ' registry' ) )
90
+ const opts = Object . assign ( { } , OPTS , config )
91
+ tnock ( t , opts . registry )
100
92
. matchHeader ( 'authorization' , `Basic c0ffee` )
101
93
. get ( '/hello' )
102
94
. reply ( 200 , '"success"' )
@@ -105,39 +97,39 @@ test('_auth auth', t => {
105
97
} )
106
98
107
99
test ( 'globally-configured auth' , t => {
108
- const basicConfig = new Map ( [
109
- [ 'registry' , 'https://different.registry/' ] ,
110
- [ 'username' , 'globaluser' ] ,
111
- [ 'password' , Buffer . from ( 'globalpass' , 'utf8' ) . toString ( 'base64' ) ] ,
112
-
113
- [ '//my.custom.registry/here/:username' , 'user' ] ,
114
- [ '//my.custom.registry/here/:password' , Buffer . from ( 'pass' , 'utf8' ) . toString ( 'base64' ) ] ,
115
- [ '//my.custom.registry/here/:email' , '[email protected] ' ]
116
- ] )
117
- t . deepEqual ( getAuth ( basicConfig . get ( ' registry' ) , basicConfig ) , {
100
+ const basicConfig = {
101
+ 'registry' : 'https://different.registry/' ,
102
+ 'username' : 'globaluser' ,
103
+ 'password' : Buffer . from ( 'globalpass' , 'utf8' ) . toString ( 'base64' ) ,
104
+
105
+ '//my.custom.registry/here/:username' : 'user' ,
106
+ '//my.custom.registry/here/:password' : Buffer . from ( 'pass' , 'utf8' ) . toString ( 'base64' ) ,
107
+ '//my.custom.registry/here/:email' : '[email protected] '
108
+ }
109
+ t . deepEqual ( getAuth ( basicConfig . registry , basicConfig ) , {
118
110
alwaysAuth : false ,
119
111
username : 'globaluser' ,
120
112
password : 'globalpass' ,
121
113
122
114
} , 'basic auth details generated from global settings' )
123
115
124
- const tokenConfig = new Map ( [
125
- [ 'registry' , 'https://different.registry/' ] ,
126
- [ '_authToken' , 'deadbeef' ] ,
127
- [ '//my.custom.registry/here/:_authToken' , 'c0ffee' ] ,
128
- [ '//my.custom.registry/here/:token' , 'nope' ]
129
- ] )
130
- t . deepEqual ( getAuth ( tokenConfig . get ( ' registry' ) , tokenConfig ) , {
116
+ const tokenConfig = {
117
+ 'registry' : 'https://different.registry/' ,
118
+ '_authToken' : 'deadbeef' ,
119
+ '//my.custom.registry/here/:_authToken' : 'c0ffee' ,
120
+ '//my.custom.registry/here/:token' : 'nope'
121
+ }
122
+ t . deepEqual ( getAuth ( tokenConfig . registry , tokenConfig ) , {
131
123
alwaysAuth : false ,
132
124
token : 'deadbeef'
133
125
} , 'correct global auth token picked out' )
134
126
135
- const _authConfig = new Map ( [
136
- [ 'registry' , 'https://different.registry/' ] ,
137
- [ '_auth' , 'deadbeef' ] ,
138
- [ '//my.custom.registry/here/:_auth' , 'c0ffee' ]
139
- ] )
140
- t . deepEqual ( getAuth ( _authConfig . get ( ' registry' ) , _authConfig ) , {
127
+ const _authConfig = {
128
+ 'registry' : 'https://different.registry/' ,
129
+ '_auth' : 'deadbeef' ,
130
+ '//my.custom.registry/here/:_auth' : 'c0ffee'
131
+ }
132
+ t . deepEqual ( getAuth ( _authConfig . registry , _authConfig ) , {
141
133
alwaysAuth : false ,
142
134
_auth : 'deadbeef'
143
135
} , 'correct global _auth picked out' )
@@ -146,25 +138,25 @@ test('globally-configured auth', t => {
146
138
} )
147
139
148
140
test ( 'otp token passed through' , t => {
149
- const config = new Map ( [
150
- [ 'registry' , 'https://my.custom.registry/here/' ] ,
151
- [ 'token' , 'deadbeef' ] ,
152
- [ 'otp' , '694201' ] ,
153
- [ '//my.custom.registry/here/:_authToken' , 'c0ffee' ] ,
154
- [ '//my.custom.registry/here/:token' , 'nope' ]
155
- ] )
156
- t . deepEqual ( getAuth ( config . get ( ' registry' ) , config ) , {
141
+ const config = {
142
+ 'registry' : 'https://my.custom.registry/here/' ,
143
+ 'token' : 'deadbeef' ,
144
+ 'otp' : '694201' ,
145
+ '//my.custom.registry/here/:_authToken' : 'c0ffee' ,
146
+ '//my.custom.registry/here/:token' : 'nope'
147
+ }
148
+ t . deepEqual ( getAuth ( config . registry , config ) , {
157
149
alwaysAuth : false ,
158
150
token : 'c0ffee' ,
159
151
otp : '694201'
160
152
} , 'correct auth token picked out' )
161
153
162
- const opts = Object . assign ( { } , OPTS , { config} )
163
- tnock ( t , opts . config . get ( ' registry' ) )
154
+ const opts = Object . assign ( { } , OPTS , config )
155
+ tnock ( t , opts . registry )
164
156
. matchHeader ( 'authorization' , `Bearer c0ffee` )
165
157
. matchHeader ( 'npm-otp' , otp => {
166
- t . equal ( otp [ 0 ] , config . get ( ' otp' ) , 'got the right otp token' )
167
- return otp [ 0 ] === config . get ( ' otp' )
158
+ t . equal ( otp [ 0 ] , config . otp , 'got the right otp token' )
159
+ return otp [ 0 ] === config . otp
168
160
} )
169
161
. get ( '/hello' )
170
162
. reply ( 200 , '"success"' )
@@ -173,15 +165,15 @@ test('otp token passed through', t => {
173
165
} )
174
166
175
167
test ( 'different hosts for uri vs registry' , t => {
176
- const config = new Map ( [
177
- [ 'always-auth' , false ] ,
178
- [ 'registry' , 'https://my.custom.registry/here/' ] ,
179
- [ 'token' , 'deadbeef' ] ,
180
- [ '//my.custom.registry/here/:_authToken' , 'c0ffee' ] ,
181
- [ '//my.custom.registry/here/:token' , 'nope' ]
182
- ] )
168
+ const config = {
169
+ 'always-auth' : false ,
170
+ 'registry' : 'https://my.custom.registry/here/' ,
171
+ 'token' : 'deadbeef' ,
172
+ '//my.custom.registry/here/:_authToken' : 'c0ffee' ,
173
+ '//my.custom.registry/here/:token' : 'nope'
174
+ }
183
175
184
- const opts = Object . assign ( { } , OPTS , { config} )
176
+ const opts = Object . assign ( { } , OPTS , config )
185
177
tnock ( t , 'https://some.other.host/' )
186
178
. matchHeader ( 'authorization' , auth => {
187
179
t . notOk ( auth , 'no authorization header was sent' )
@@ -194,15 +186,15 @@ test('different hosts for uri vs registry', t => {
194
186
} )
195
187
196
188
test ( 'http vs https auth sending' , t => {
197
- const config = new Map ( [
198
- [ 'always-auth' , false ] ,
199
- [ 'registry' , 'https://my.custom.registry/here/' ] ,
200
- [ 'token' , 'deadbeef' ] ,
201
- [ '//my.custom.registry/here/:_authToken' , 'c0ffee' ] ,
202
- [ '//my.custom.registry/here/:token' , 'nope' ]
203
- ] )
189
+ const config = {
190
+ 'always-auth' : false ,
191
+ 'registry' : 'https://my.custom.registry/here/' ,
192
+ 'token' : 'deadbeef' ,
193
+ '//my.custom.registry/here/:_authToken' : 'c0ffee' ,
194
+ '//my.custom.registry/here/:token' : 'nope'
195
+ }
204
196
205
- const opts = Object . assign ( { } , OPTS , { config} )
197
+ const opts = Object . assign ( { } , OPTS , config )
206
198
tnock ( t , 'http://my.custom.registry/here/' )
207
199
. matchHeader ( 'authorization' , `Bearer c0ffee` )
208
200
. get ( '/hello' )
@@ -212,19 +204,19 @@ test('http vs https auth sending', t => {
212
204
} )
213
205
214
206
test ( 'always-auth' , t => {
215
- const config = new Map ( [
216
- [ 'registry' , 'https://my.custom.registry/here/' ] ,
217
- [ 'always-auth' , 'true' ] ,
218
- [ 'token' , 'deadbeef' ] ,
219
- [ '//my.custom.registry/here/:_authToken' , 'c0ffee' ] ,
220
- [ '//my.custom.registry/here/:token' , 'nope' ]
221
- ] )
222
- t . deepEqual ( getAuth ( config . get ( ' registry' ) , config ) , {
207
+ const config = {
208
+ 'registry' : 'https://my.custom.registry/here/' ,
209
+ 'always-auth' : 'true' ,
210
+ 'token' : 'deadbeef' ,
211
+ '//my.custom.registry/here/:_authToken' : 'c0ffee' ,
212
+ '//my.custom.registry/here/:token' : 'nope'
213
+ }
214
+ t . deepEqual ( getAuth ( config . registry , config ) , {
223
215
alwaysAuth : true ,
224
216
token : 'c0ffee'
225
217
} , 'correct auth token picked out' )
226
218
227
- const opts = Object . assign ( { } , OPTS , { config} )
219
+ const opts = Object . assign ( { } , OPTS , config )
228
220
tnock ( t , 'https://some.other.host/' )
229
221
. matchHeader ( 'authorization' , `Bearer c0ffee` )
230
222
. get ( '/hello' )
@@ -234,25 +226,25 @@ test('always-auth', t => {
234
226
} )
235
227
236
228
test ( 'scope-based auth' , t => {
237
- const config = new Map ( [
238
- [ 'registry' , 'https://my.custom.registry/here/' ] ,
239
- [ 'scope' , '@myscope' ] ,
240
- [ '@myscope:registry' , 'https://my.custom.registry/here/' ] ,
241
- [ 'token' , 'deadbeef' ] ,
242
- [ '//my.custom.registry/here/:_authToken' , 'c0ffee' ] ,
243
- [ '//my.custom.registry/here/:token' , 'nope' ]
244
- ] )
245
- t . deepEqual ( getAuth ( config . get ( '@myscope:registry' ) , config ) , {
229
+ const config = {
230
+ 'registry' : 'https://my.custom.registry/here/' ,
231
+ 'scope' : '@myscope' ,
232
+ '@myscope:registry' : 'https://my.custom.registry/here/' ,
233
+ 'token' : 'deadbeef' ,
234
+ '//my.custom.registry/here/:_authToken' : 'c0ffee' ,
235
+ '//my.custom.registry/here/:token' : 'nope'
236
+ }
237
+ t . deepEqual ( getAuth ( config [ '@myscope:registry' ] , config ) , {
246
238
alwaysAuth : false ,
247
239
token : 'c0ffee'
248
240
} , 'correct auth token picked out' )
249
- t . deepEqual ( getAuth ( config . get ( '@myscope:registry' ) , config ) , {
241
+ t . deepEqual ( getAuth ( config [ '@myscope:registry' ] , config ) , {
250
242
alwaysAuth : false ,
251
243
token : 'c0ffee'
252
244
} , 'correct auth token picked out without scope config having an @' )
253
245
254
- const opts = Object . assign ( { } , OPTS , { config} )
255
- tnock ( t , opts . config . get ( '@myscope:registry' ) )
246
+ const opts = Object . assign ( { } , OPTS , config )
247
+ tnock ( t , opts [ '@myscope:registry' ] )
256
248
. matchHeader ( 'authorization' , auth => {
257
249
t . equal ( auth [ 0 ] , 'Bearer c0ffee' , 'got correct bearer token for scope' )
258
250
return auth [ 0 ] === 'Bearer c0ffee'
@@ -262,7 +254,8 @@ test('scope-based auth', t => {
262
254
. reply ( 200 , '"success"' )
263
255
return fetch . json ( '/hello' , opts )
264
256
. then ( res => t . equal ( res , 'success' , 'token auth succeeded' ) )
265
- . then ( ( ) => config . set ( 'scope' , 'myscope' ) )
266
- . then ( ( ) => fetch . json ( '/hello' , opts ) )
257
+ . then ( ( ) => fetch . json ( '/hello' , Object . assign ( { } , opts , {
258
+ scope : 'myscope'
259
+ } ) ) )
267
260
. then ( res => t . equal ( res , 'success' , 'token auth succeeded without @ in scope' ) )
268
261
} )
0 commit comments