import { Injectable } from '@angular/core'; import { GCMockModule } from '@core/mocks/gc-module.mock'; import { Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { expect } from 'chai'; import { PORTAL_DETERMINATION_SERVICE_MOCK } from '../portal-determination.service.mock'; import { TOKEN_REFRESH_RESOURCES_MOCK } from './token-refresh.resources.mock'; import { TokenService } from './token.service'; @Injectable({ providedIn: 'root' }) @DescribeAngularService(TokenService, { imports: [ GCMockModule ], providers: [ PORTAL_DETERMINATION_SERVICE_MOCK, TOKEN_REFRESH_RESOURCES_MOCK ] }) export class TokenServiceSpec implements Spec { @TestCase('should exist') testExists (service: TokenService) { expect(service).to.exist; } @TestCase('should return invalid on expired tokens') testShouldReturnInvalidTokens (service: TokenService) { // mock the login token service['storage'].jwt = { token: '.' + btoa(JSON.stringify({})), // set it to expire way in the past (makes it an invalid token) expiration: new Date(Date.now() - 100000).toString(), refreshToken: '', refreshTokenExpiration: '', requirePasswordReset: false }; expect(service.hasCurrentValidToken()).to.be.false; } @TestCase('should return valid on unexpired tokens') testShouldReturnValidTokens (service: TokenService) { // mock the login token service['storage'].jwt = { token: '.' + btoa(JSON.stringify({})), // set it to expire way in the future (makes it a valid token) expiration: new Date(Date.now() + 100000).toString(), refreshToken: '', refreshTokenExpiration: '', requirePasswordReset: false }; expect(service.hasCurrentValidToken()).to.be.true; } @TestCase('should return valid unexpired refresh tokens') testReturnValidRefresh (service: TokenService) { // mock the login token service['storage'].jwt = { token: '.' + btoa(JSON.stringify({})), // set it to expire way in the past (makes it an invalid token) expiration: new Date(Date.now() - 100000).toString(), refreshToken: '', // set to expire in future (valid refresh) refreshTokenExpiration: new Date(Date.now() + 100000).toString(), requirePasswordReset: false }; expect(service.hasFutureValidToken()).to.be.true; } @TestCase('should return invalid expired refresh tokens') testReturnInvalidRefresh (service: TokenService) { // mock the login token service['storage'].jwt = { token: '.' + btoa(JSON.stringify({})), // set it to expire way in the past (makes it an invalid token) expiration: new Date(Date.now() - 100000).toString(), refreshToken: '', // set to expire in future (valid refresh) refreshTokenExpiration: new Date(Date.now() - 100000).toString(), requirePasswordReset: false }; expect(service.hasFutureValidToken()).to.be.false; } @TestCase('should return invalid refresh tokens when token is valid') // When token is currently valid, it is not future valid testReturnInvalidRefreshWhenValidToken (service: TokenService) { // mock the login token service['storage'].jwt = { token: '.' + btoa(JSON.stringify({})), // set it to expire way in the future (makes it a valid token) expiration: new Date(Date.now() + 100000).toString(), refreshToken: '', refreshTokenExpiration: '', requirePasswordReset: false }; expect(service.hasFutureValidToken()).to.be.false; } }