|
| 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