|
5 | 5 |
|
6 | 6 | describe('OAuthProvider', function() {
|
7 | 7 | var defaults = {
|
| 8 | + authorizePath: '/oauth2/authorize', |
8 | 9 | baseUrl: 'https://api.website.com',
|
9 | 10 | clientId: 'CLIENT_ID',
|
| 11 | + clientSecret: 'CLIENT_SECRET', |
10 | 12 | grantPath: '/oauth2/token',
|
11 |
| - revokePath: '/oauth2/revoke', |
12 |
| - clientSecret: 'CLIENT_SECRET' |
| 13 | + redirectUrl: 'https://website.com', |
| 14 | + revokePath: '/oauth2/revoke' |
13 | 15 | };
|
14 | 16 |
|
15 | 17 | describe('configure()', function() {
|
@@ -48,6 +50,25 @@ describe('OAuthProvider', function() {
|
48 | 50 | }
|
49 | 51 | });
|
50 | 52 |
|
| 53 | + it('should throw an error if `authorizePath` param is empty', function() { |
| 54 | + try { |
| 55 | + provider.configure(_.defaults({ authorizePath: null }, defaults)); |
| 56 | + |
| 57 | + should.fail(); |
| 58 | + } catch(e) { |
| 59 | + e.should.be.an.instanceOf(Error); |
| 60 | + e.message.should.match(/authorizePath/); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + it('should add facing slash from `authorizePath`', function() { |
| 65 | + var config = provider.configure(_.defaults({ |
| 66 | + authorizePath: 'oauth2/authorize' |
| 67 | + }, defaults)); |
| 68 | + |
| 69 | + config.authorizePath.should.equal('/oauth2/authorize'); |
| 70 | + }); |
| 71 | + |
51 | 72 | it('should throw an error if `baseUrl` param is empty', function() {
|
52 | 73 | try {
|
53 | 74 | provider.configure(_.omit(defaults, 'baseUrl'));
|
@@ -137,6 +158,94 @@ describe('OAuthProvider', function() {
|
137 | 158 | OAuthToken.removeToken();
|
138 | 159 | }));
|
139 | 160 |
|
| 161 | + describe('authorize()', function() { |
| 162 | + var data = { |
| 163 | + client_id: defaults.clientId, |
| 164 | + response_type: 'code', |
| 165 | + scope: 'foo:bar', |
| 166 | + state: 'state_hash' |
| 167 | + }; |
| 168 | + |
| 169 | + it('should throw an error if `clientId` is missing', inject(function(OAuth) { |
| 170 | + try { |
| 171 | + OAuth.authorize(); |
| 172 | + |
| 173 | + should.fail(); |
| 174 | + } catch(e) { |
| 175 | + e.should.be.an.instanceOf(Error); |
| 176 | + e.message.should.match(/clientId/); |
| 177 | + } |
| 178 | + })); |
| 179 | + |
| 180 | + it('should call `queryString.stringify`', inject(function(OAuth) { |
| 181 | + sinon.spy(queryString, 'stringify'); |
| 182 | + |
| 183 | + OAuth.authorize(data.client_id, data.scope, data.state); |
| 184 | + |
| 185 | + queryString.stringify.callCount.should.equal(1); |
| 186 | + queryString.stringify.firstCall.args.should.have.lengthOf(1); |
| 187 | + queryString.stringify.firstCall.args[0].should.eql({ |
| 188 | + client_id: data.client_id, |
| 189 | + response_type: 'code', |
| 190 | + scope: data.scope, |
| 191 | + state: data.state |
| 192 | + }); |
| 193 | + |
| 194 | + queryString.stringify.restore(); |
| 195 | + })); |
| 196 | + |
| 197 | + it('should return an error if request response doesn\'t contain a `redirectUri` attribute', inject(function($httpBackend, OAuth) { |
| 198 | + $httpBackend.expectGET(`${defaults.baseUrl}${defaults.authorizePath}?${queryString.stringify(data)}`) |
| 199 | + .respond(200, { redirectUri: `${defaults.redirectUrl}` }); |
| 200 | + |
| 201 | + OAuth.authorize(data.client_id, data.scope, data.state).then(function(response) { |
| 202 | + response.data.should.have.property('redirectUri'); |
| 203 | + }); |
| 204 | + |
| 205 | + $httpBackend.flush(); |
| 206 | + |
| 207 | + $httpBackend.verifyNoOutstandingExpectation(); |
| 208 | + $httpBackend.verifyNoOutstandingRequest(); |
| 209 | + })); |
| 210 | + |
| 211 | + it('should return an `error` and `error_description` parameters if scope is invalid ', inject(function($httpBackend, OAuth) { |
| 212 | + $httpBackend.expectGET(`${defaults.baseUrl}${defaults.authorizePath}?${queryString.stringify(data)}`) |
| 213 | + .respond(200, { redirectUri: `${defaults.redirectUrl}?error=invalid_scope&error_description=The%20requested%20scope%20is%20invalid` }); |
| 214 | + |
| 215 | + OAuth.authorize(data.client_id, data.scope, data.state).then(function(response) { |
| 216 | + response.data.should.have.property('redirectUri'); |
| 217 | + response.data.redirectUri.should.match(/error=/); |
| 218 | + response.data.redirectUri.should.match(/error_description=/); |
| 219 | + }); |
| 220 | + |
| 221 | + $httpBackend.flush(); |
| 222 | + |
| 223 | + $httpBackend.verifyNoOutstandingExpectation(); |
| 224 | + $httpBackend.verifyNoOutstandingRequest(); |
| 225 | + })); |
| 226 | + |
| 227 | + it('should return an `code` and `state` parameters if scope is valid', inject(function($httpBackend, OAuth) { |
| 228 | + var redirectUri = `${defaults.redirectUrl}?code=foo&state=${data.state}`; |
| 229 | + _.merge(data, { scope: 'foobar' }); |
| 230 | + |
| 231 | + $httpBackend.expectGET(`${defaults.baseUrl}${defaults.authorizePath}?${queryString.stringify(data)}`) |
| 232 | + .respond(200, { redirectUri: redirectUri }); |
| 233 | + |
| 234 | + OAuth.authorize(data.client_id, data.scope, data.state).then(function(response) { |
| 235 | + response.data.should.have.property('redirectUri'); |
| 236 | + response.data.redirectUri.should.match(/code=/); |
| 237 | + response.data.redirectUri.should.match(/state=/); |
| 238 | + response.data.redirectUri.should.match(new RegExp(`state=${data.state}`)); |
| 239 | + }); |
| 240 | + |
| 241 | + $httpBackend.flush(); |
| 242 | + |
| 243 | + $httpBackend.verifyNoOutstandingExpectation(); |
| 244 | + $httpBackend.verifyNoOutstandingRequest(); |
| 245 | + })); |
| 246 | + |
| 247 | + }); |
| 248 | + |
140 | 249 | describe('isAuthenticated()', function() {
|
141 | 250 | it('should be true when there is a stored `token` cookie', inject(function(OAuth, OAuthToken) {
|
142 | 251 | OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });
|
|
0 commit comments