test/TokenTest.js   A
last analyzed

Complexity

Total Complexity 9
Complexity/F 1

Size

Lines of Code 100
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 9
mnd 0
bc 0
fnc 9
bpm 0
cpm 1
noi 0
1
/* eslint-disable camelcase */
2
3
'use strict';
4
5
const { describe, it } = require('mocha');
6
const { expect } = require('chai');
7
8
const OAuthClientTest = require('../src/OAuthClient');
9
const expectedAccessToken = require('./mocks/bearer-token.json');
10
11
12
let oauthClient = new OAuthClientTest({
13
  clientId: 'clientID',
14
  clientSecret: 'clientSecret',
15
  environment: 'sandbox',
16
  redirectUri: 'http://localhost:8000/callback',
17
});
18
19
describe('Tests for Token', () => {
20
  it('Creates a new token instance', () => {
21
    const token = oauthClient.getToken();
22
    expect(token).to.have.property('realmId');
23
    expect(token).to.have.property('token_type');
24
    expect(token).to.have.property('access_token');
25
    expect(token).to.have.property('refresh_token');
26
    expect(token).to.have.property('expires_in');
27
    expect(token).to.have.property('x_refresh_token_expires_in');
28
    expect(token).to.have.property('latency');
29
    expect(token).to.have.property('id_token');
30
  });
31
32
  it('Set Token using Constructor', () => {
33
    oauthClient = new OAuthClientTest({
34
      clientId: 'clientID',
35
      clientSecret: 'clientSecret',
36
      environment: 'sandbox',
37
      redirectUri: 'http://localhost:8000/callback',
38
      token: expectedAccessToken,
39
    });
40
    const token = oauthClient.getToken();
41
42
    expect(token.access_token).to.equal(expectedAccessToken.access_token);
43
    expect(token.refresh_token).to.equal(expectedAccessToken.refresh_token);
44
    expect(token.token_type).to.equal(expectedAccessToken.token_type);
45
    expect(token.expires_in).to.equal(expectedAccessToken.expires_in);
46
    expect(token.x_refresh_token_expires_in)
47
      .to.equal(expectedAccessToken.x_refresh_token_expires_in);
48
  });
49
50
  it('Set Token using Helper Method', () => {
51
    oauthClient.token.setToken(expectedAccessToken);
52
    const token = oauthClient.getToken();
53
54
    expect(token.access_token).to.equal(expectedAccessToken.access_token);
55
    expect(token.refresh_token).to.equal(expectedAccessToken.refresh_token);
56
    expect(token.token_type).to.equal(expectedAccessToken.token_type);
57
    expect(token.expires_in).to.equal(expectedAccessToken.expires_in);
58
    expect(token.x_refresh_token_expires_in)
59
      .to.equal(expectedAccessToken.x_refresh_token_expires_in);
60
  });
61
62
  it('Get Access Token using Helper Method', () => {
63
    oauthClient.token.setToken(expectedAccessToken);
64
    const accessToken = oauthClient.getToken().accessToken();
65
66
    expect(accessToken).to.deep.equal(expectedAccessToken.access_token);
67
  });
68
69
70
  it('Get Refresh Token using Helper Method', () => {
71
    oauthClient.token.setToken(expectedAccessToken);
72
    const refreshToken = oauthClient.getToken().refreshToken();
73
74
    expect(refreshToken).to.deep.equal(expectedAccessToken.refresh_token);
75
  });
76
77
  it('Get TokenType using Helper Method', () => {
78
    oauthClient.token.setToken(expectedAccessToken);
79
    const tokenType = oauthClient.getToken().tokenType();
80
81
    expect(tokenType).to.deep.equal(expectedAccessToken.token_type);
82
  });
83
84
  it('Get Token using Helper Method', () => {
85
    oauthClient.token.setToken(expectedAccessToken);
86
    const token = oauthClient.getToken().getToken();
87
88
    expect(token).to.be.a('Object');
89
    expect(token.access_token).to.deep.equal('sample_access_token');
90
  });
91
92
  it('Clear Token using Helper Method', () => {
93
    oauthClient.token.setToken(expectedAccessToken);
94
    const token = oauthClient.getToken().clearToken();
95
96
    expect(token.access_token).to.equal('');
97
    expect(token.refresh_token).to.equal('');
98
    expect(token.token_type).to.equal('');
99
    expect(token.expires_in).to.equal(0);
100
    expect(token.x_refresh_token_expires_in).to.equal(0);
101
  });
102
});
103