From 188ee68e82306e2afbb6c2373f7b6e97fd12efe2 Mon Sep 17 00:00:00 2001 From: Art Date: Mon, 13 Sep 2021 21:00:13 +0300 Subject: [PATCH] 121. Testing Interceptor path match (#14) --- .../app/interceptor/auth.interceptor.spec.ts | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 support-portal-frontend/src/app/interceptor/auth.interceptor.spec.ts diff --git a/support-portal-frontend/src/app/interceptor/auth.interceptor.spec.ts b/support-portal-frontend/src/app/interceptor/auth.interceptor.spec.ts new file mode 100644 index 0000000..89b1ef2 --- /dev/null +++ b/support-portal-frontend/src/app/interceptor/auth.interceptor.spec.ts @@ -0,0 +1,47 @@ +import {TestBed} from '@angular/core/testing'; + +import {AuthInterceptor} from './auth.interceptor'; +import {AuthenticationService} from "../service/authentication.service"; + +describe('AuthInterceptor', () => { + beforeEach(() => { + + const spy = jasmine.createSpyObj('AuthenticationService', ['loadToken']); + + TestBed.configureTestingModule({ + providers: [ + AuthInterceptor, + {provide: AuthenticationService, useValue: spy} + ] + }) + }); + + it('should be created', () => { + const interceptor: AuthInterceptor = TestBed.inject(AuthInterceptor); + expect(interceptor).toBeTruthy(); + }); + + it('should not intercept public URLs', () => { + const interceptor: AuthInterceptor = TestBed.inject(AuthInterceptor); + + expect(interceptor.isRequestUrlMatches('http://google.com')).toBeFalsy(); + expect(interceptor.isRequestUrlMatches('http://example.com:8080/user')).toBeFalsy(); + expect(interceptor.isRequestUrlMatches('http://localhost:8080/user/login')).toBeFalsy(); + expect(interceptor.isRequestUrlMatches('http://localhost:8080/user/register')).toBeFalsy(); + expect(interceptor.isRequestUrlMatches('http://localhost:8080/user/image/profile/1234567/avatar.jpg')).toBeFalsy(); + expect(interceptor.isRequestUrlMatches('http://localhost:8080/user/image/profile/1234567')).toBeFalsy(); + expect(interceptor.isRequestUrlMatches('http://localhost:8080/user/art.shyshkin/image/profile')).toBeFalsy(); + }); + + it('should intercept NON-public URLs', () => { + const interceptor: AuthInterceptor = TestBed.inject(AuthInterceptor); + + expect(interceptor.isRequestUrlMatches('http://localhost:8080/user/art.shyshkin')).toBeTruthy(); + expect(interceptor.isRequestUrlMatches('http://localhost:8080/user')).toBeTruthy(); + expect(interceptor.isRequestUrlMatches('http://localhost:8080/user/login/some')).toBeTruthy(); + expect(interceptor.isRequestUrlMatches('http://localhost:8080/user/art.shyshkin/profileImage')).toBeTruthy(); + expect(interceptor.isRequestUrlMatches('http://localhost:8080/user/123mn1mn3m1')).toBeTruthy(); + + }); + +});