import { HttpRequest } from '@angular/common/http'; import { GCMockModule } from '@core/mocks/gc-module.mock'; import { environment } from '@environment'; import { Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { expect, spy } from 'chai'; import { of, take } from 'rxjs'; import { TokenInterceptor } from './token-interceptor.service'; @DescribeAngularService(TokenInterceptor, { imports: [GCMockModule] }) export class TokenInterceptorServiceSpec implements Spec { @TestCase('should know when to process a request') testShouldProcess (service: TokenInterceptor) { const shouldProcess = service['checkShouldProcessRequest']('/api/some/gc/endpoint'); expect(shouldProcess).to.be.true; } @TestCase('should know when not to process a request') testShouldNotProcess (service: TokenInterceptor) { const shouldProcess = service['checkShouldProcessRequest']('/assets/some/ui/file'); expect(shouldProcess).to.be.false; } @TestCase('should append leading slash') testBuildUpFullAPIURL (service: TokenInterceptor) { const originalPath = 'api/asdf/asdf'; const request = new HttpRequest('GET', originalPath); const newRequest = service['buildUpFullAPIURL'](request); const newURL = newRequest.url; expect(newURL).to.equal(`${environment.apiUrl}/${originalPath}`); } @TestCase('should not append leading slash') testNotBuildUpFullAPIURL (service: TokenInterceptor) { const originalPath = '/api/asdf/asdf'; const request = new HttpRequest('GET', originalPath); const newRequest = service['buildUpFullAPIURL'](request); const newURL = newRequest.url; expect(newURL).to.equal(`${environment.apiUrl}${originalPath}`); } @TestCase('should know when to append token') testShouldAppendToken (service: TokenInterceptor) { const shouldAddToken = service['checkShouldAppendToken']('/api/some/auth/endpoint'); expect(shouldAddToken).to.be.true; } @TestCase('should know when not to append token') testShouldNotAppendToken (service: TokenInterceptor) { const shouldAddToken = service['checkShouldAppendToken'](`${environment.apiUrl}/api/token/manager`); expect(shouldAddToken).to.be.false; } @TestCase('should know when to append token for overrides') testShouldNotAppendTokenForRevoking (service: TokenInterceptor) { const shouldAddTokenRevokeForClient = service['checkShouldAppendToken']('/api/token/RevokeForClient'); expect(shouldAddTokenRevokeForClient).to.be.true; const shouldAddTokenRevokeAll = service['checkShouldAppendToken']('/api/token/RevokeAll'); expect(shouldAddTokenRevokeAll).to.be.true; const shouldAddTokenScanToken = service['checkShouldAppendToken']('/api/token/FileScanToken'); expect(shouldAddTokenScanToken).to.be.true; } @TestCase('should be able to append the auth token') async testShouldAppendAuthToken (service: TokenInterceptor) { const request = new HttpRequest('GET', '/api/some/auth/endpoint'); const token = 'some-auth-token'; service['tokenService']['getLatestToken'] = async () => token; const newRequest = await service['appendToken'](request); const authHeader = newRequest.headers.get('Authorization'); expect(authHeader).to.equal(`Bearer ${token}`); } @TestCase('should not append auth header if token not present') async testShouldNotAppendAuthToken (service: TokenInterceptor) { const request = new HttpRequest('GET', '/api/some/auth/endpoint'); service['tokenService']['getLatestToken'] = async () => ''; const newRequest = await service['appendToken'](request); const authHeader = newRequest.headers.get('Authorization'); expect(authHeader).to.be.null; } @TestCase('should call relevant functions in intercept') async testShouldCallRelevantFunctionsOnIntercept (service: TokenInterceptor) { const request = new HttpRequest('GET', '/api/some/auth/endpoint'); const checkProcessRequestSpy = spy.on(service, 'checkShouldProcessRequest', () => true); const buildUpFullAPIURLSpy = spy.on(service, 'buildUpFullAPIURL', (r) => r); const appendTokenSpy = spy.on(service, 'appendToken', async (r) => r); await service.intercept(request, { handle: (r: HttpRequest) => of(r) } as any) .pipe(take(1)) .toPromise(); expect(checkProcessRequestSpy).to.have.been.called(); expect(buildUpFullAPIURLSpy).to.have.been.called(); expect(appendTokenSpy).to.have.been.called(); } }