From 22f110d7ad162a86f7df4eef2c38b890aa3e0c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinan=20=C3=96zt=C3=BCrk?= Date: Fri, 16 Feb 2024 18:21:24 +0300 Subject: [PATCH] Create unit test fors AuthErrorFilterService --- .../tests/oauth-error-filter.service.spec.ts | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 npm/ng-packs/packages/oauth/src/lib/tests/oauth-error-filter.service.spec.ts diff --git a/npm/ng-packs/packages/oauth/src/lib/tests/oauth-error-filter.service.spec.ts b/npm/ng-packs/packages/oauth/src/lib/tests/oauth-error-filter.service.spec.ts new file mode 100644 index 00000000000..c8af0f934d1 --- /dev/null +++ b/npm/ng-packs/packages/oauth/src/lib/tests/oauth-error-filter.service.spec.ts @@ -0,0 +1,121 @@ +import { createServiceFactory, SpectatorService } from '@ngneat/spectator'; +import { OAuthErrorFilterService } from '../services'; +import { AuthErrorEvent, AuthErrorFilter } from '@abp/ng.core'; +import { OAuthErrorEvent } from 'angular-oauth2-oidc'; + +const ids = { + firstFilter: 'firstFilter', + secondFilter: 'secondFilter', +}; +type Reason = object & { error: { grant_type: string | undefined; }; }; + + +describe('AuthService', () => { + let spectator: SpectatorService; + let oAuthErrorFilterService: OAuthErrorFilterService; + const createService = createServiceFactory(OAuthErrorFilterService); + + let firstFilter: AuthErrorFilter = { + id: '', + executable: Boolean(), + execute: () => Boolean(), + }; + let secondFilter: AuthErrorFilter = { + id: '', + executable: Boolean(), + execute: () => Boolean(), + }; + + beforeEach(() => { + spectator = createService(); + oAuthErrorFilterService = spectator.inject(OAuthErrorFilterService); + firstFilter = { + id: ids.firstFilter, + executable: true, + execute: (event: AuthErrorEvent) => { + const { reason } = event; + const { + error: { grant_type }, + } = (reason || {}); + + return !!grant_type && grant_type === ids.firstFilter; + }, + }; + + secondFilter = { + id: ids.secondFilter, + executable: true, + execute: (event: AuthErrorEvent) => { + const { reason } = event; + const { + error: { grant_type }, + } = (reason || {}); + + return !!grant_type && grant_type === ids.secondFilter; + }, + }; + }); + + it('should add filter to filter service', () => { + oAuthErrorFilterService.add(firstFilter); + + expect(oAuthErrorFilterService.filters()).toContain(firstFilter); + expect(oAuthErrorFilterService.filters()).toHaveLength(1); + + oAuthErrorFilterService.add(secondFilter); + expect(oAuthErrorFilterService.filters()).toHaveLength(2); + }); + + it('should be able to get item by id', () => { + oAuthErrorFilterService.add(firstFilter); + + expect(oAuthErrorFilterService.get(ids.firstFilter)).toBe(firstFilter); + }); + + it('should be able to patch item', () => { + oAuthErrorFilterService.add(firstFilter); + oAuthErrorFilterService.patch({ id: ids.firstFilter, executable: false }); + + expect(oAuthErrorFilterService.get(ids.firstFilter)?.executable).toBe(false); + }); + + it('should be able to remove item', () => { + oAuthErrorFilterService.add(firstFilter); + oAuthErrorFilterService.add(secondFilter); + + oAuthErrorFilterService.remove(ids.firstFilter); + expect(oAuthErrorFilterService.filters()).not.toContain(firstFilter); + expect(oAuthErrorFilterService.filters()).toContain(secondFilter); + expect(oAuthErrorFilterService.filters()).toHaveLength(1); + }); + + it('if any execute method returns true, it should return the true value.', () => { + oAuthErrorFilterService.add(firstFilter); + oAuthErrorFilterService.add(secondFilter); + + const event = { + reason: { + error: { + grant_type: ids.secondFilter, + }, + }, + } as OAuthErrorEvent; + + expect(oAuthErrorFilterService.run(event)).toBe(true); + }); + + it('if none of execute method returns true, it should return the false.', () => { + oAuthErrorFilterService.add(firstFilter); + oAuthErrorFilterService.add(secondFilter); + + const event = { + reason: { + error: { + grant_type: 'random-event', + }, + }, + } as OAuthErrorEvent; + + expect(oAuthErrorFilterService.run(event)).toBe(false); + }); +}); \ No newline at end of file