|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | + |
| 7 | +var TokenResponseType = require('../../../lib/response-types/token-response-type'); |
| 8 | +var InvalidArgumentError = require('../../../lib/errors/invalid-argument-error'); |
| 9 | +var should = require('should'); |
| 10 | +var url = require('url'); |
| 11 | + |
| 12 | +/** |
| 13 | + * Test `TokenResponseType` integration. |
| 14 | + */ |
| 15 | + |
| 16 | +describe('TokenResponseType integration', function() { |
| 17 | + describe('constructor()', function() { |
| 18 | + it('should throw an error if `options.accessTokenLifetime` is missing', function() { |
| 19 | + try { |
| 20 | + new TokenResponseType(); |
| 21 | + |
| 22 | + should.fail(); |
| 23 | + } catch (e) { |
| 24 | + e.should.be.an.instanceOf(InvalidArgumentError); |
| 25 | + e.message.should.equal('Missing parameter: `accessTokenLifetime`'); |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + it('should set `accessTokenLifetime`', function() { |
| 30 | + var responseType = new TokenResponseType({ |
| 31 | + accessTokenLifetime: 120, |
| 32 | + model: {} |
| 33 | + }); |
| 34 | + |
| 35 | + responseType.accessTokenLifetime.should.equal(120); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should set the `model`', function() { |
| 39 | + var model = { |
| 40 | + foobar: function() {} |
| 41 | + }; |
| 42 | + var handler = new TokenResponseType({ accessTokenLifetime: 120, model: model }); |
| 43 | + |
| 44 | + handler.model.should.equal(model); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('buildRedirectUri()', function() { |
| 49 | + it('should throw an error if the `redirectUri` is missing', function() { |
| 50 | + var responseType = new TokenResponseType({ |
| 51 | + accessTokenLifetime: 120, |
| 52 | + model: {} |
| 53 | + }); |
| 54 | + |
| 55 | + try { |
| 56 | + responseType.buildRedirectUri(); |
| 57 | + |
| 58 | + should.fail(); |
| 59 | + } catch (e) { |
| 60 | + e.should.be.an.instanceOf(InvalidArgumentError); |
| 61 | + e.message.should.equal('Missing parameter: `redirectUri`'); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + it('should return the new redirect uri and set `access_token` and `state` in the query', function() { |
| 66 | + var responseType = new TokenResponseType({ |
| 67 | + accessTokenLifetime: 120, |
| 68 | + model: {} |
| 69 | + }); |
| 70 | + |
| 71 | + responseType.accessToken = 'foobar-token'; |
| 72 | + var redirectUri = responseType.buildRedirectUri(url.parse('http://example.com/cb')); |
| 73 | + |
| 74 | + url.format(redirectUri).should.equal('http://example.com/cb#access_token=foobar-token'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return the new redirect uri and append `access_token` and `state` in the query', function() { |
| 78 | + var responseType = new TokenResponseType({ |
| 79 | + accessTokenLifetime: 120, |
| 80 | + model: {} |
| 81 | + }); |
| 82 | + |
| 83 | + responseType.accessToken = 'foobar-token'; |
| 84 | + var redirectUri = responseType.buildRedirectUri(url.parse('http://example.com/cb?foo=bar', true)); |
| 85 | + |
| 86 | + url.format(redirectUri).should.equal('http://example.com/cb?foo=bar#access_token=foobar-token'); |
| 87 | + }); |
| 88 | + }); |
| 89 | +}); |
0 commit comments