]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/npm-registry-client/test/access.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / npm-registry-client / test / access.js
1 var test = require('tap').test
2
3 var server = require('./lib/server.js')
4 var common = require('./lib/common.js')
5 var client = common.freshClient()
6
7 function nop () {}
8
9 var URI = 'http://localhost:1337'
10 var PARAMS = {
11   auth: { token: 'foo' },
12   scope: 'myorg',
13   team: 'myteam',
14   package: '@foo/bar',
15   permissions: 'read-write'
16 }
17 var UNSCOPED = {
18   auth: { token: 'foo' },
19   scope: 'myorg',
20   team: 'myteam',
21   package: 'bar',
22   permissions: 'read-write'
23 }
24
25 var commands = [
26   'public', 'restricted', 'grant', 'revoke', 'ls-packages', 'ls-collaborators'
27 ]
28
29 test('access public', function (t) {
30   server.expect('POST', '/-/package/%40foo%2Fbar/access', function (req, res) {
31     t.equal(req.method, 'POST')
32     onJsonReq(req, function (json) {
33       t.deepEqual(json, { access: 'public' })
34       res.statusCode = 200
35       res.json({ accessChanged: true })
36     })
37   })
38   var params = Object.create(PARAMS)
39   params.package = '@foo/bar'
40   client.access('public', URI, params, function (error, data) {
41     t.ifError(error, 'no errors')
42     t.ok(data.accessChanged, 'access level set')
43     t.end()
44   })
45 })
46
47 test('access restricted', function (t) {
48   server.expect('POST', '/-/package/%40foo%2Fbar/access', function (req, res) {
49     t.equal(req.method, 'POST')
50     onJsonReq(req, function (json) {
51       t.deepEqual(json, { access: 'restricted' })
52       res.statusCode = 200
53       res.json({ accessChanged: true })
54     })
55   })
56   client.access('restricted', URI, PARAMS, function (error, data) {
57     t.ifError(error, 'no errors')
58     t.ok(data.accessChanged, 'access level set')
59     t.end()
60   })
61 })
62
63 test('access grant basic', function (t) {
64   server.expect('PUT', '/-/team/myorg/myteam/package', function (req, res) {
65     t.equal(req.method, 'PUT')
66     onJsonReq(req, function (json) {
67       t.deepEqual(json, {
68         permissions: PARAMS.permissions,
69         package: PARAMS.package
70       })
71       res.statusCode = 201
72       res.json({ accessChanged: true })
73     })
74   })
75   client.access('grant', URI, PARAMS, function (error, data) {
76     t.ifError(error, 'no errors')
77     t.ok(data.accessChanged, 'access level set')
78     t.end()
79   })
80 })
81
82 test('access grant basic unscoped', function (t) {
83   server.expect('PUT', '/-/team/myorg/myteam/package', function (req, res) {
84     t.equal(req.method, 'PUT')
85     onJsonReq(req, function (json) {
86       t.deepEqual(json, {
87         permissions: UNSCOPED.permissions,
88         package: UNSCOPED.package
89       })
90       res.statusCode = 201
91       res.json({ accessChanged: true })
92     })
93   })
94   client.access('grant', URI, UNSCOPED, function (error, data) {
95     t.ifError(error, 'no errors')
96     t.ok(data.accessChanged, 'access level set')
97     t.end()
98   })
99 })
100
101 test('access revoke basic', function (t) {
102   server.expect('DELETE', '/-/team/myorg/myteam/package', function (req, res) {
103     t.equal(req.method, 'DELETE')
104     onJsonReq(req, function (json) {
105       t.deepEqual(json, {
106         package: PARAMS.package
107       })
108       res.statusCode = 200
109       res.json({ accessChanged: true })
110     })
111   })
112   client.access('revoke', URI, PARAMS, function (error, data) {
113     t.ifError(error, 'no errors')
114     t.ok(data.accessChanged, 'access level set')
115     t.end()
116   })
117 })
118
119 test('access revoke basic unscoped', function (t) {
120   server.expect('DELETE', '/-/team/myorg/myteam/package', function (req, res) {
121     t.equal(req.method, 'DELETE')
122     onJsonReq(req, function (json) {
123       t.deepEqual(json, {
124         package: UNSCOPED.package
125       })
126       res.statusCode = 200
127       res.json({ accessChanged: true })
128     })
129   })
130   client.access('revoke', URI, UNSCOPED, function (error, data) {
131     t.ifError(error, 'no errors')
132     t.ok(data.accessChanged, 'access level set')
133     t.end()
134   })
135 })
136
137 test('ls-packages on team', function (t) {
138   var serverPackages = {
139     '@foo/bar': 'write',
140     '@foo/util': 'read'
141   }
142   var clientPackages = {
143     '@foo/bar': 'read-write',
144     '@foo/util': 'read-only'
145   }
146   var uri = '/-/team/myorg/myteam/package?format=cli'
147   server.expect('GET', uri, function (req, res) {
148     t.equal(req.method, 'GET')
149     res.statusCode = 200
150     res.json(serverPackages)
151   })
152   client.access('ls-packages', URI, PARAMS, function (error, data) {
153     t.ifError(error, 'no errors')
154     t.same(data, clientPackages)
155     t.end()
156   })
157 })
158
159 test('ls-packages on org', function (t) {
160   var serverPackages = {
161     '@foo/bar': 'write',
162     '@foo/util': 'read'
163   }
164   var clientPackages = {
165     '@foo/bar': 'read-write',
166     '@foo/util': 'read-only'
167   }
168   var uri = '/-/org/myorg/package?format=cli'
169   server.expect('GET', uri, function (req, res) {
170     t.equal(req.method, 'GET')
171     res.statusCode = 200
172     res.json(serverPackages)
173   })
174   var params = Object.create(PARAMS)
175   params.team = null
176   client.access('ls-packages', URI, params, function (error, data) {
177     t.ifError(error, 'no errors')
178     t.same(data, clientPackages)
179     t.end()
180   })
181 })
182
183 test('ls-packages on user', function (t) {
184   var serverPackages = {
185     '@foo/bar': 'write',
186     '@foo/util': 'read'
187   }
188   var clientPackages = {
189     '@foo/bar': 'read-write',
190     '@foo/util': 'read-only'
191   }
192   var firstUri = '/-/org/myorg/package?format=cli'
193   server.expect('GET', firstUri, function (req, res) {
194     t.equal(req.method, 'GET')
195     res.statusCode = 404
196     res.json({error: 'not found'})
197   })
198   var secondUri = '/-/user/myorg/package?format=cli'
199   server.expect('GET', secondUri, function (req, res) {
200     t.equal(req.method, 'GET')
201     res.statusCode = 200
202     res.json(serverPackages)
203   })
204   var params = Object.create(PARAMS)
205   params.team = null
206   client.access('ls-packages', URI, params, function (error, data) {
207     t.ifError(error, 'no errors')
208     t.same(data, clientPackages)
209     t.end()
210   })
211 })
212
213 test('ls-collaborators', function (t) {
214   var serverCollaborators = {
215     'myorg:myteam': 'write',
216     'myorg:anotherteam': 'read'
217   }
218   var clientCollaborators = {
219     'myorg:myteam': 'read-write',
220     'myorg:anotherteam': 'read-only'
221   }
222   var uri = '/-/package/%40foo%2Fbar/collaborators?format=cli'
223   server.expect('GET', uri, function (req, res) {
224     t.equal(req.method, 'GET')
225     res.statusCode = 200
226     res.json(serverCollaborators)
227   })
228   client.access('ls-collaborators', URI, PARAMS, function (error, data) {
229     t.ifError(error, 'no errors')
230     t.same(data, clientCollaborators)
231     t.end()
232   })
233 })
234
235 test('ls-collaborators w/scope', function (t) {
236   var serverCollaborators = {
237     'myorg:myteam': 'write',
238     'myorg:anotherteam': 'read'
239   }
240   var clientCollaborators = {
241     'myorg:myteam': 'read-write',
242     'myorg:anotherteam': 'read-only'
243   }
244   var uri = '/-/package/%40foo%2Fbar/collaborators?format=cli&user=zkat'
245   server.expect('GET', uri, function (req, res) {
246     t.equal(req.method, 'GET')
247     res.statusCode = 200
248     res.json(serverCollaborators)
249   })
250   var params = Object.create(PARAMS)
251   params.user = 'zkat'
252   client.access('ls-collaborators', URI, params, function (error, data) {
253     t.ifError(error, 'no errors')
254     t.same(data, clientCollaborators)
255     t.end()
256   })
257 })
258
259 test('ls-collaborators w/o scope', function (t) {
260   var serverCollaborators = {
261     'myorg:myteam': 'write',
262     'myorg:anotherteam': 'read'
263   }
264   var clientCollaborators = {
265     'myorg:myteam': 'read-write',
266     'myorg:anotherteam': 'read-only'
267   }
268   var uri = '/-/package/bar/collaborators?format=cli&user=zkat'
269   server.expect('GET', uri, function (req, res) {
270     t.equal(req.method, 'GET')
271     res.statusCode = 200
272     res.json(serverCollaborators)
273   })
274   var params = Object.create(UNSCOPED)
275   params.user = 'zkat'
276   client.access('ls-collaborators', URI, params, function (error, data) {
277     t.ifError(error, 'no errors')
278     t.same(data, clientCollaborators)
279     t.end()
280   })
281 })
282
283 test('access command base validation', function (t) {
284   t.throws(function () {
285     client.access(undefined, URI, PARAMS, nop)
286   }, 'command is required')
287   t.throws(function () {
288     client.access('whoops', URI, PARAMS, nop)
289   }, 'command must be a valid subcommand')
290   commands.forEach(function (cmd) {
291     t.throws(function () {
292       client.access(cmd, undefined, PARAMS, nop)
293     }, 'registry URI is required')
294     t.throws(function () {
295       client.access(cmd, URI, undefined, nop)
296     }, 'params is required')
297     t.throws(function () {
298       client.access(cmd, URI, '', nop)
299     }, 'params must be an object')
300     t.throws(function () {
301       client.access(cmd, URI, {scope: 'o', team: 't'}, nop)
302     }, 'auth is required')
303     t.throws(function () {
304       client.access(cmd, URI, {auth: 5, scope: 'o', team: 't'}, nop)
305     }, 'auth must be an object')
306     t.throws(function () {
307       client.access(cmd, URI, PARAMS, {})
308     }, 'callback must be a function')
309     t.throws(function () {
310       client.access(cmd, URI, PARAMS, undefined)
311     }, 'callback is required')
312     if (contains([
313       'public', 'restricted'
314     ], cmd)) {
315       t.throws(function () {
316         var params = Object.create(PARAMS)
317         params.package = null
318         client.access(cmd, URI, params, nop)
319       }, 'package is required')
320       t.throws(function () {
321         var params = Object.create(PARAMS)
322         params.package = 'underscore'
323         client.access(cmd, URI, params, nop)
324       }, 'only scoped packages are allowed')
325     }
326     if (contains(['grant', 'revoke', 'ls-packages'], cmd)) {
327       t.throws(function () {
328         var params = Object.create(PARAMS)
329         params.scope = null
330         client.access(cmd, URI, params, nop)
331       }, 'scope is required')
332     }
333     if (contains(['grant', 'revoke'], cmd)) {
334       t.throws(function () {
335         var params = Object.create(PARAMS)
336         params.team = null
337         client.access(cmd, URI, params, nop)
338       }, 'team is required')
339     }
340     if (cmd === 'grant') {
341       t.throws(function () {
342         var params = Object.create(PARAMS)
343         params.permissions = null
344         client.access(cmd, URI, params, nop)
345       }, 'permissions are required')
346       t.throws(function () {
347         var params = Object.create(PARAMS)
348         params.permissions = 'idkwhat'
349         client.access(cmd, URI, params, nop)
350       }, 'permissions must be either read-only or read-write')
351     }
352   })
353   t.end()
354 })
355
356 test('cleanup', function (t) {
357   server.close()
358   t.end()
359 })
360
361 function onJsonReq (req, cb) {
362   var buffer = ''
363   req.setEncoding('utf8')
364   req.on('data', function (data) { buffer += data })
365   req.on('end', function () { cb(buffer ? JSON.parse(buffer) : undefined) })
366 }
367
368 function contains (arr, item) {
369   return arr.indexOf(item) !== -1
370 }