Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit c1f1f43

Browse files
chrisdickinsonzkat
authored andcommitted
feat(org): add org.{add,set,rm,ls} (#155)
Credit: @chrisdickinson PR-URL: #155 Reviewed-By: @zkat
1 parent 81afb2c commit c1f1f43

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ function RegClient (config) {
6060
client.get = require('./lib/get')
6161
client.initialize = require('./lib/initialize')
6262
client.logout = require('./lib/logout')
63+
client.org = require('./lib/org')
6364
client.ping = require('./lib/ping')
6465
client.publish = require('./lib/publish')
6566
client.request = require('./lib/request')

lib/org.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict'
2+
3+
module.exports = org
4+
5+
var assert = require('assert')
6+
var url = require('url')
7+
8+
var subcommands = {}
9+
10+
function org (subcommand, uri, params, cb) {
11+
orgAssertions(subcommand, uri, params, cb)
12+
return subcommands[subcommand].call(this, uri, params, cb)
13+
}
14+
15+
subcommands.set = subcommands.add = function (uri, params, cb) {
16+
return this.request(apiUri(uri, 'org', params.org, 'user'), {
17+
method: 'PUT',
18+
auth: params.auth,
19+
body: JSON.stringify({
20+
user: params.user,
21+
role: params.role
22+
})
23+
}, cb)
24+
}
25+
26+
subcommands.rm = function (uri, params, cb) {
27+
return this.request(apiUri(uri, 'org', params.org, 'user'), {
28+
method: 'DELETE',
29+
auth: params.auth,
30+
body: JSON.stringify({
31+
user: params.user
32+
})
33+
}, cb)
34+
}
35+
36+
subcommands.ls = function (uri, params, cb) {
37+
return this.request(apiUri(uri, 'org', params.org, 'user'), {
38+
method: 'GET',
39+
auth: params.auth
40+
}, cb)
41+
}
42+
43+
function apiUri (registryUri) {
44+
var path = Array.prototype.slice.call(arguments, 1)
45+
.map(encodeURIComponent)
46+
.join('/')
47+
return url.resolve(registryUri, '-/' + path)
48+
}
49+
50+
function orgAssertions (subcommand, uri, params, cb) {
51+
assert(subcommand, 'subcommand is required')
52+
assert(subcommands.hasOwnProperty(subcommand),
53+
'org subcommand must be one of ' + Object.keys(subcommands))
54+
assert(typeof uri === 'string', 'registry URI is required')
55+
assert(typeof params === 'object', 'params are required')
56+
assert(typeof params.auth === 'object', 'auth is required')
57+
assert(!cb || typeof cb === 'function', 'callback must be a function')
58+
assert(typeof params.org === 'string', 'org name is required')
59+
if (subcommand === 'rm' || subcommand === 'add' || subcommand === 'set') {
60+
assert(typeof params.user === 'string', 'user is required')
61+
}
62+
}

test/org.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict'
2+
3+
var test = require('tap').test
4+
5+
var server = require('./lib/server.js')
6+
var common = require('./lib/common.js')
7+
var client = common.freshClient()
8+
9+
var URI = 'http://localhost:1337'
10+
var PARAMS = {
11+
auth: {
12+
token: 'foo'
13+
},
14+
org: 'myorg',
15+
user: 'myuser'
16+
}
17+
18+
test('org: add user', function (t) {
19+
server.expect('PUT', '/-/org/myorg/user', function (req, res) {
20+
t.equal(req.method, 'PUT')
21+
onJsonReq(req, function (json) {
22+
t.same(json, {user: 'myuser'})
23+
res.statusCode = 200
24+
res.json({result: 'anything'})
25+
})
26+
})
27+
28+
client.org('add', URI, PARAMS, function (err, data) {
29+
t.same(err, null)
30+
t.same(data, {result: 'anything'})
31+
t.done()
32+
})
33+
})
34+
35+
test('org: rm user', function (t) {
36+
server.expect('DELETE', '/-/org/myorg/user', function (req, res) {
37+
t.equal(req.method, 'DELETE')
38+
onJsonReq(req, function (json) {
39+
t.same(json, {user: 'myuser'})
40+
res.statusCode = 200
41+
res.json({result: 'anything'})
42+
})
43+
})
44+
45+
client.org('rm', URI, PARAMS, function (err, data) {
46+
t.same(err, null)
47+
t.same(data, {result: 'anything'})
48+
t.done()
49+
})
50+
})
51+
52+
test('org: rm user (400)', function (t) {
53+
server.expect('DELETE', '/-/org/myorg/user', function (req, res) {
54+
t.equal(req.method, 'DELETE')
55+
onJsonReq(req, function (json) {
56+
t.same(json, {user: 'myuser'})
57+
res.statusCode = 400
58+
res.json({result: 'anything'})
59+
})
60+
})
61+
62+
client.org('rm', URI, PARAMS, function (err, data) {
63+
t.ok(err)
64+
t.same(data, {result: 'anything'})
65+
t.done()
66+
})
67+
})
68+
69+
test('org: ls', function (t) {
70+
server.expect('GET', '/-/org/myorg/user', function (req, res) {
71+
t.equal(req.method, 'GET')
72+
onJsonReq(req, function () {
73+
res.statusCode = 200
74+
res.json({username: 'role', username2: 'admin'})
75+
})
76+
})
77+
78+
client.org('ls', URI, PARAMS, function (err, data) {
79+
t.same(err, null)
80+
t.same(data, {username: 'role', username2: 'admin'})
81+
t.done()
82+
})
83+
})
84+
85+
test('cleanup', function (t) {
86+
server.close()
87+
t.end()
88+
})
89+
90+
function onJsonReq (req, cb) {
91+
var buffer = ''
92+
req.setEncoding('utf8')
93+
req.on('data', function (data) { buffer += data })
94+
req.on('end', function () { cb(buffer ? JSON.parse(buffer) : undefined) })
95+
}

0 commit comments

Comments
 (0)