Home Reference Source Test Repository

unit/model/access-token.js

  1. import chai from 'chai';
  2. import AccessToken from '../../../src/model/access-token';
  3. import { ACCESS_TOKEN_RESPONSE } from '../../test-data';
  4.  
  5.  
  6. const should = chai.should();
  7.  
  8.  
  9. /** @test {AccessToken} */
  10. describe('AccessToken', () => {
  11. /** @type AccessToken */
  12. let accessToken;
  13. let responseClone;
  14.  
  15.  
  16. beforeEach(() => {
  17. responseClone = JSON.parse(JSON.stringify(ACCESS_TOKEN_RESPONSE));
  18. accessToken = new AccessToken(responseClone);
  19. });
  20.  
  21.  
  22. /** @test {AccessToken#constructor} */
  23. describe('constructor', () => {
  24. it('should not set an expiry date if expires_in is NaN', () => {
  25. responseClone = JSON.parse(JSON.stringify(ACCESS_TOKEN_RESPONSE));
  26. responseClone.expires_in = 'bad_value';
  27. const noExpiryAccessToken = new AccessToken(responseClone);
  28.  
  29. chai.expect(noExpiryAccessToken._expiryDate).to.be.null;
  30. });
  31. });
  32.  
  33.  
  34. /** @test {AccessToken#validate} */
  35. describe('validate', () => {
  36. it('should not throw an error on valid input', () => {
  37. should.not.exist(accessToken.validate());
  38. });
  39. });
  40.  
  41.  
  42. /** @test {AccessToken#isExpired} */
  43. describe('isExpired', () => {
  44. it('should not be expired for test data token', () => {
  45. accessToken.isExpired().should.be.false;
  46. });
  47.  
  48. it('should be expired if the expires_in time is in the past', () => {
  49. responseClone = JSON.parse(JSON.stringify(ACCESS_TOKEN_RESPONSE));
  50. responseClone.expires_in = 0;
  51. const expiredAccessToken = new AccessToken(responseClone);
  52.  
  53. expiredAccessToken.isExpired().should.be.true;
  54. });
  55. });
  56.  
  57.  
  58. /** @test {AccessToken.token} */
  59. describe('token', () => {
  60. it('should match the "access_token" value from the API response', () => {
  61. accessToken.token.should.equal(responseClone.access_token);
  62. });
  63. });
  64.  
  65.  
  66. /** @test {AccessToken.ttl} */
  67. describe('ttl', () => {
  68. it('should convert the "expires_in" value from the API response (which is in seconds) to milliseconds', () => {
  69. accessToken.ttl.should.equal(responseClone.expires_in * 1000);
  70. });
  71. });
  72.  
  73.  
  74. /** @test {AccessToken#toString} */
  75. describe('toString', () => {
  76. it('should match the "access_token" value from the API response', () => {
  77. accessToken.toString().should.equal(responseClone.access_token);
  78. });
  79. });
  80. });