Skip to content
This repository was archived by the owner on Feb 25, 2019. It is now read-only.

Commit 0652871

Browse files
Merge pull request #22 from bauglir/add-client-roles-api
Add client roles manipulation
2 parents 98e1283 + 4fcf31c commit 0652871

File tree

3 files changed

+293
-1
lines changed

3 files changed

+293
-1
lines changed

index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var qs = require('qs')
66
var url = require('url')
77
var async = require('async')
88
var request = require('request-promise')
9+
var clientRoles = require('./rest/clientRoles')
910
var clients = require('./rest/clients')
1011
var roles = require('./rest/roles')
1112
var roleScopes = require('./rest/roleScopes')
@@ -35,7 +36,12 @@ function AnvilConnect (options) {
3536
get: clients.get.bind(this),
3637
create: clients.create.bind(this),
3738
update: clients.update.bind(this),
38-
delete: clients.delete.bind(this)
39+
delete: clients.delete.bind(this),
40+
roles: {
41+
list: clientRoles.listRoles.bind(this),
42+
add: clientRoles.addRole.bind(this),
43+
delete: clientRoles.deleteRole.bind(this)
44+
}
3945
}
4046

4147
this.roles = {

rest/clientRoles.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Module dependencies
3+
*/
4+
5+
var request = require('../lib/request')
6+
7+
/**
8+
* List Roles
9+
*/
10+
11+
function listRoles (clientId, options) {
12+
options = options || {}
13+
options.url = '/v1/clients/' + clientId + '/roles'
14+
return request.bind(this)(options)
15+
}
16+
17+
exports.listRoles = listRoles
18+
19+
/**
20+
* Add Role
21+
*/
22+
23+
function addRole (client, role, options) {
24+
options = options || {}
25+
options.url = '/v1/clients/' + client + '/roles/' + role
26+
options.method = 'PUT'
27+
return request.bind(this)(options)
28+
}
29+
30+
exports.addRole = addRole
31+
32+
/**
33+
* Delete Role
34+
*/
35+
36+
function deleteRole (client, role, options) {
37+
options = options || {}
38+
options.url = '/v1/clients/' + client + '/roles/' + role
39+
options.method = 'DELETE'
40+
delete options.json
41+
return request.bind(this)(options)
42+
}
43+
44+
exports.deleteRole = deleteRole

test/rest/clientRolesSpec.coffee

+242
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
# Test dependencies
2+
cwd = process.cwd()
3+
path = require 'path'
4+
chai = require 'chai'
5+
sinon = require 'sinon'
6+
sinonChai = require 'sinon-chai'
7+
expect = chai.expect
8+
nock = require 'nock'
9+
10+
11+
12+
13+
# Assertions
14+
chai.use sinonChai
15+
chai.should()
16+
17+
18+
19+
20+
clientRoles = require path.join(cwd, 'rest', 'clientRoles')
21+
22+
23+
24+
25+
describe 'REST API Client Role Methods', ->
26+
27+
describe 'listRoles', ->
28+
29+
describe 'with a successful response', ->
30+
31+
{promise,success,failure} = {}
32+
33+
before (done) ->
34+
instance =
35+
configuration:
36+
issuer: 'https://connect.anvil.io'
37+
agentOptions: {}
38+
39+
nock.cleanAll()
40+
nock(instance.configuration.issuer)
41+
.get('/v1/clients/authority/roles')
42+
.reply(200, [{name: 'realm'}])
43+
44+
success = sinon.spy -> done()
45+
failure = sinon.spy -> done()
46+
47+
promise = clientRoles.listRoles.bind(instance)('authority', {
48+
token: 'token'
49+
}).then(success, failure)
50+
51+
it 'should return a promise', ->
52+
promise.should.be.instanceof Promise
53+
54+
it 'should provide the roles', ->
55+
success.should.have.been.calledWith sinon.match [{name:'realm'}]
56+
57+
it 'should not catch an error', ->
58+
failure.should.not.have.been.called
59+
60+
61+
describe 'with a failure response', ->
62+
63+
{promise,success,failure} = {}
64+
65+
before (done) ->
66+
instance =
67+
configuration:
68+
issuer: 'https://connect.anvil.io'
69+
tokens:
70+
access_token: 'random'
71+
agentOptions: {}
72+
73+
nock(instance.configuration.issuer)
74+
.get('/v1/clients/authority/roles')
75+
.reply(404, 'Not found')
76+
77+
success = sinon.spy -> done()
78+
failure = sinon.spy -> done()
79+
80+
promise = clientRoles.listRoles.bind(instance)('authority')
81+
.then(success, failure)
82+
83+
after ->
84+
nock.cleanAll()
85+
86+
it 'should return a promise', ->
87+
promise.should.be.instanceof Promise
88+
89+
it 'should not provide the roles', ->
90+
success.should.not.have.been.called
91+
92+
it 'should catch an error', ->
93+
failure.should.have.been.called
94+
95+
96+
97+
98+
describe 'add', ->
99+
100+
describe 'with a successful response', ->
101+
102+
{promise,success,failure} = {}
103+
104+
before (done) ->
105+
instance =
106+
configuration:
107+
issuer: 'https://connect.anvil.io'
108+
agentOptions: {}
109+
110+
nock.cleanAll()
111+
nock(instance.configuration.issuer)
112+
.put('/v1/clients/authority/roles/realm')
113+
.reply(201, {
114+
added: true
115+
})
116+
117+
success = sinon.spy -> done()
118+
failure = sinon.spy -> done()
119+
120+
promise = clientRoles.addRole.bind(instance)('authority', 'realm', {
121+
token: 'token'
122+
}).then(success, failure)
123+
124+
it 'should return a promise', ->
125+
promise.should.be.instanceof Promise
126+
127+
it 'should provide the role', ->
128+
success.should.have.been.calledWith sinon.match { added: true }
129+
130+
it 'should not catch an error', ->
131+
failure.should.not.have.been.called
132+
133+
134+
describe 'with a failure response', ->
135+
136+
{promise,success,failure} = {}
137+
138+
before (done) ->
139+
instance =
140+
configuration:
141+
issuer: 'https://connect.anvil.io'
142+
tokens:
143+
access_token: 'random'
144+
agentOptions: {}
145+
146+
nock.cleanAll()
147+
nock(instance.configuration.issuer)
148+
.put('/v1/clients/invalid/roles/addition')
149+
.reply(400, 'Bad request')
150+
151+
success = sinon.spy -> done()
152+
failure = sinon.spy -> done()
153+
154+
promise = clientRoles.addRole.bind(instance)('invalid', 'addition', {
155+
token: 'token'
156+
}).then(success, failure)
157+
158+
it 'should return a promise', ->
159+
promise.should.be.instanceof Promise
160+
161+
it 'should not provide the role', ->
162+
success.should.not.have.been.called
163+
164+
it 'should catch an error', ->
165+
failure.should.have.been.called
166+
167+
168+
169+
170+
describe 'delete', ->
171+
172+
describe 'with a successful response', ->
173+
174+
{promise,success,failure} = {}
175+
176+
before (done) ->
177+
instance =
178+
configuration:
179+
issuer: 'https://connect.anvil.io'
180+
tokens:
181+
access_token: 'random'
182+
agentOptions: {}
183+
184+
nock.cleanAll()
185+
nock(instance.configuration.issuer)
186+
.delete('/v1/clients/authority/roles/realm')
187+
.reply(204)
188+
189+
success = sinon.spy -> done()
190+
failure = sinon.spy -> done()
191+
192+
promise = clientRoles.deleteRole.bind(instance)('authority', 'realm', {
193+
token: 'token'
194+
}).then(success, failure)
195+
196+
it 'should return a promise', ->
197+
promise.should.be.instanceof Promise
198+
199+
it 'should provide the role', ->
200+
success.should.have.been.called
201+
202+
it 'should not catch an error', ->
203+
failure.should.not.have.been.called
204+
205+
206+
describe 'with a failure response', ->
207+
208+
{promise,success,failure} = {}
209+
210+
before (done) ->
211+
instance =
212+
configuration:
213+
issuer: 'https://connect.anvil.io'
214+
tokens:
215+
access_token: 'random'
216+
agentOptions: {}
217+
218+
nock.cleanAll()
219+
nock(instance.configuration.issuer)
220+
.delete('/v1/clients/invalid/roles/deletion')
221+
.reply(404, 'Not found')
222+
223+
success = sinon.spy -> done()
224+
failure = sinon.spy -> done()
225+
226+
promise = clientRoles.deleteRole.bind(instance)('invalid', 'deletion', {
227+
token: 'token'
228+
}).then(success, failure)
229+
230+
after ->
231+
nock.cleanAll()
232+
233+
it 'should return a promise', ->
234+
promise.should.be.instanceof Promise
235+
236+
it 'should not provide the role', ->
237+
success.should.not.have.been.called
238+
239+
it 'should catch an error', ->
240+
failure.should.have.been.called
241+
242+

0 commit comments

Comments
 (0)