This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81afb2c
commit 6a9eee8
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict' | ||
|
||
module.exports = org | ||
|
||
var assert = require('assert') | ||
var url = require('url') | ||
|
||
var subcommands = {} | ||
|
||
function org (subcommand, uri, params, cb) { | ||
orgAssertions(subcommand, uri, params, cb) | ||
return subcommands[subcommand].call(this, uri, params, cb) | ||
} | ||
|
||
subcommands.set = subcommands.add = function (uri, params, cb) { | ||
return this.request(apiUri(uri, 'org', params.org, 'user'), { | ||
method: 'PUT', | ||
auth: params.auth, | ||
body: JSON.stringify({ | ||
user: params.user, | ||
role: params.role | ||
}) | ||
}, cb) | ||
} | ||
|
||
subcommands.rm = function (uri, params, cb) { | ||
return this.request(apiUri(uri, 'org', params.org, 'user'), { | ||
method: 'DELETE', | ||
auth: params.auth, | ||
body: JSON.stringify({ | ||
user: params.user | ||
}) | ||
}, cb) | ||
} | ||
|
||
subcommands.ls = function (uri, params, cb) { | ||
return this.request(apiUri(uri, 'org', params.org, 'user'), { | ||
method: 'GET', | ||
auth: params.auth | ||
}, cb) | ||
} | ||
|
||
function apiUri (registryUri) { | ||
var path = Array.prototype.slice.call(arguments, 1) | ||
.map(encodeURIComponent) | ||
.join('/') | ||
return url.resolve(registryUri, '-/' + path) | ||
} | ||
|
||
function orgAssertions (subcommand, uri, params, cb) { | ||
assert(subcommand, 'subcommand is required') | ||
assert(subcommands.hasOwnProperty(subcommand), | ||
'org subcommand must be one of ' + Object.keys(subcommands)) | ||
assert(typeof uri === 'string', 'registry URI is required') | ||
assert(typeof params === 'object', 'params are required') | ||
assert(typeof params.auth === 'object', 'auth is required') | ||
assert(!cb || typeof cb === 'function', 'callback must be a function') | ||
assert(typeof params.org === 'string', 'org name is required') | ||
if (subcommand === 'rm' || subcommand === 'add' || subcommand === 'set') { | ||
assert(typeof params.user === 'string', 'user is required') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict' | ||
|
||
var test = require('tap').test | ||
|
||
var server = require('./lib/server.js') | ||
var common = require('./lib/common.js') | ||
var client = common.freshClient() | ||
|
||
var URI = 'http://localhost:1337' | ||
var PARAMS = { | ||
auth: { | ||
token: 'foo' | ||
}, | ||
org: 'myorg', | ||
user: 'myuser' | ||
} | ||
|
||
test('org: add user', function (t) { | ||
server.expect('PUT', '/-/org/myorg/user', function (req, res) { | ||
t.equal(req.method, 'PUT') | ||
onJsonReq(req, function (json) { | ||
t.same(json, {user: 'myuser'}) | ||
res.statusCode = 200 | ||
res.json({result: 'anything'}) | ||
}) | ||
}) | ||
|
||
client.org('add', URI, PARAMS, function (err, data) { | ||
t.same(err, null) | ||
t.same(data, {result: 'anything'}) | ||
t.done() | ||
}) | ||
}) | ||
|
||
test('org: rm user', function (t) { | ||
server.expect('DELETE', '/-/org/myorg/user', function (req, res) { | ||
t.equal(req.method, 'DELETE') | ||
onJsonReq(req, function (json) { | ||
t.same(json, {user: 'myuser'}) | ||
res.statusCode = 200 | ||
res.json({result: 'anything'}) | ||
}) | ||
}) | ||
|
||
client.org('rm', URI, PARAMS, function (err, data) { | ||
t.same(err, null) | ||
t.same(data, {result: 'anything'}) | ||
t.done() | ||
}) | ||
}) | ||
|
||
test('org: rm user (400)', function (t) { | ||
server.expect('DELETE', '/-/org/myorg/user', function (req, res) { | ||
t.equal(req.method, 'DELETE') | ||
onJsonReq(req, function (json) { | ||
t.same(json, {user: 'myuser'}) | ||
res.statusCode = 400 | ||
res.json({result: 'anything'}) | ||
}) | ||
}) | ||
|
||
client.org('rm', URI, PARAMS, function (err, data) { | ||
t.ok(err) | ||
t.same(data, {result: 'anything'}) | ||
t.done() | ||
}) | ||
}) | ||
|
||
test('org: ls', function (t) { | ||
server.expect('GET', '/-/org/myorg/user', function (req, res) { | ||
t.equal(req.method, 'GET') | ||
onJsonReq(req, function () { | ||
res.statusCode = 200 | ||
res.json({result: 'anything'}) | ||
}) | ||
}) | ||
|
||
client.org('ls', URI, PARAMS, function (err, data) { | ||
t.same(err, null) | ||
t.same(data, {result: 'anything'}) | ||
t.done() | ||
}) | ||
}) | ||
|
||
test('cleanup', function (t) { | ||
server.close() | ||
t.end() | ||
}) | ||
|
||
function onJsonReq (req, cb) { | ||
var buffer = '' | ||
req.setEncoding('utf8') | ||
req.on('data', function (data) { buffer += data }) | ||
req.on('end', function () { cb(buffer ? JSON.parse(buffer) : undefined) }) | ||
} |