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 { TokenStorageService } from './token-storage.service'; @DescribeAngularService(TokenStorageService, { imports: [GCMockModule] }) export class TokenStorageServiceSpec implements Spec { @TestCase('should be able to store the token') testShouldStoreToken (service: TokenStorageService) { const localStorageKey = service['behavior'].current['userTokenKey']; const token = { token: 'some-jwt', expiration: '', requirePasswordReset: false, refreshToken: '', refreshTokenExpiration: '' }; service.jwt = token; const localStorageToken = JSON.parse(localStorage.getItem(localStorageKey)); expect(localStorageToken.token).to.equal(token.token); } @TestCase('should be able to retrieve stored token') testShouldRetrieveToken (service: TokenStorageService) { const localStorageKey = service['behavior'].current['userTokenKey']; const token = { token: 'some-other-jwt', expiration: '', requirePasswordReset: false, refreshToken: '', refreshTokenExpiration: '' }; localStorage.setItem(localStorageKey, JSON.stringify(token)); const retrievedToken = service.jwt; expect(retrievedToken.token).to.equal(token.token); } @TestCase('should be able to revoke stored token') testShouldRevokeToken (service: TokenStorageService) { const localStorageKey = service['behavior'].current['userTokenKey']; const token = { token: 'some-other-jwt', expiration: '', requirePasswordReset: false, refreshToken: '', refreshTokenExpiration: '' }; localStorage.setItem(localStorageKey, JSON.stringify(token)); service.revoke(); const retrievedToken = localStorage.getItem(localStorageKey); expect(retrievedToken).to.be.null; } @TestCase('should assign default client id') testShouldAssignClientID (service: TokenStorageService) { const localStorageKey = service['behavior'].current['clientIdentifierKey']; const storedKey = localStorage.getItem(localStorageKey); expect(storedKey).to.be.null; const newClientId = service.clientIdentifier; expect(newClientId).not.to.be.null; const clientIdRetrievedAfterAssignment = service.clientIdentifier; expect(newClientId).to.equal(clientIdRetrievedAfterAssignment); } @TestCase('should be able to override client ID') testShouldOverrideClientIdentifier (service: TokenStorageService) { const oldClientId = service.clientIdentifier; const newClientId = 'some-random-client-id'; service.overrideClientIdentifier(newClientId); const clientIdAfterOverride = service.clientIdentifier; expect(oldClientId).to.not.equal(newClientId); expect(clientIdAfterOverride).to.equal(newClientId); } }