Browse Source

update: oauth package tests

pull/24530/head
sumeyye 2 weeks ago
parent
commit
34a30e02c7
  1. 4
      npm/ng-packs/packages/oauth/jest.config.ts
  2. 20
      npm/ng-packs/packages/oauth/src/lib/tests/api.interceptor.spec.ts
  3. 4
      npm/ng-packs/packages/oauth/src/lib/tests/auth.guard.spec.ts
  4. 17
      npm/ng-packs/packages/oauth/src/lib/tests/initial-utils.spec.ts
  5. 2
      npm/ng-packs/packages/oauth/src/lib/utils/check-access-token.ts

4
npm/ng-packs/packages/oauth/jest.config.ts

@ -1,4 +1,8 @@
/* eslint-disable */ /* eslint-disable */
/**
* @deprecated use vitest instead of jest
* @see https://vitest.dev/guide/migration.html#jest
*/
export default { export default {
displayName: 'oauth', displayName: 'oauth',
preset: '../../jest.preset.js', preset: '../../jest.preset.js',

20
npm/ng-packs/packages/oauth/src/lib/tests/api.interceptor.spec.ts

@ -1,8 +1,8 @@
import { HttpRequest } from '@angular/common/http'; import { HttpRequest } from '@angular/common/http';
import { SpyObject } from '@ngneat/spectator'; import { SpyObject } from '@ngneat/spectator';
import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest'; import { createServiceFactory, SpectatorService } from '@ngneat/spectator/vitest';
import { OAuthService } from 'angular-oauth2-oidc'; import { OAuthService } from 'angular-oauth2-oidc';
import { Subject, timer } from 'rxjs'; import { Subject } from 'rxjs';
import { HttpWaitService, SessionStateService, TENANT_KEY } from '@abp/ng.core'; import { HttpWaitService, SessionStateService, TENANT_KEY } from '@abp/ng.core';
import { OAuthApiInterceptor } from '../interceptors'; import { OAuthApiInterceptor } from '../interceptors';
@ -29,7 +29,7 @@ describe('ApiInterceptor', () => {
httpWaitService = spectator.inject(HttpWaitService); httpWaitService = spectator.inject(HttpWaitService);
}); });
it('should add headers to http request', done => { it('should add headers to http request', () => {
oauthService.getAccessToken.andReturn('ey892mkwa8^2jk'); oauthService.getAccessToken.andReturn('ey892mkwa8^2jk');
sessionState.getLanguage.andReturn('tr'); sessionState.getLanguage.andReturn('tr');
sessionState.getTenant.andReturn({ id: 'Volosoft', name: 'Volosoft' }); sessionState.getTenant.andReturn({ id: 'Volosoft', name: 'Volosoft' });
@ -42,7 +42,6 @@ describe('ApiInterceptor', () => {
expect(req.headers.get('Authorization')).toEqual('Bearer ey892mkwa8^2jk'); expect(req.headers.get('Authorization')).toEqual('Bearer ey892mkwa8^2jk');
expect(req.headers.get('Accept-Language')).toEqual('tr'); expect(req.headers.get('Accept-Language')).toEqual('tr');
expect(req.headers.get(testTenantKey)).toEqual('Volosoft'); expect(req.headers.get(testTenantKey)).toEqual('Volosoft');
done();
return handleRes$; return handleRes$;
}, },
}; };
@ -53,9 +52,9 @@ describe('ApiInterceptor', () => {
handleRes$.complete(); handleRes$.complete();
}); });
it('should call http wait services add request and delete request', done => { it('should call http wait services add request and delete request', () => {
const spyAddRequest = jest.spyOn(httpWaitService, 'addRequest'); const spyAddRequest = vi.spyOn(httpWaitService, 'addRequest');
const spyDeleteRequest = jest.spyOn(httpWaitService, 'deleteRequest'); const spyDeleteRequest = vi.spyOn(httpWaitService, 'deleteRequest');
const request = new HttpRequest('GET', 'https://abp.io'); const request = new HttpRequest('GET', 'https://abp.io');
const handleRes$ = new Subject<void>(); const handleRes$ = new Subject<void>();
@ -71,10 +70,7 @@ describe('ApiInterceptor', () => {
handleRes$.next(); handleRes$.next();
handleRes$.complete(); handleRes$.complete();
timer(0).subscribe(() => { expect(spyAddRequest).toHaveBeenCalled();
expect(spyAddRequest).toHaveBeenCalled(); expect(spyDeleteRequest).toHaveBeenCalled();
expect(spyDeleteRequest).toHaveBeenCalled();
done();
});
}); });
}); });

4
npm/ng-packs/packages/oauth/src/lib/tests/auth.guard.spec.ts

@ -1,4 +1,4 @@
import { createServiceFactory, SpectatorService, createSpyObject } from '@ngneat/spectator/jest'; import { createServiceFactory, SpectatorService, createSpyObject } from '@ngneat/spectator/vitest';
import { OAuthService } from 'angular-oauth2-oidc'; import { OAuthService } from 'angular-oauth2-oidc';
import { AbpOAuthGuard, abpOAuthGuard } from '../guards/oauth.guard'; import { AbpOAuthGuard, abpOAuthGuard } from '../guards/oauth.guard';
import { AuthService } from '@abp/ng.core'; import { AuthService } from '@abp/ng.core';
@ -40,7 +40,7 @@ describe('AuthGuard', () => {
it('should execute the navigateToLogin method of the authService', () => { it('should execute the navigateToLogin method of the authService', () => {
const authService = spectator.inject(AuthService); const authService = spectator.inject(AuthService);
spectator.inject(OAuthService).hasValidAccessToken.andReturn(false); spectator.inject(OAuthService).hasValidAccessToken.andReturn(false);
const navigateToLoginSpy = jest.spyOn(authService, 'navigateToLogin'); const navigateToLoginSpy = vi.spyOn(authService, 'navigateToLogin');
expect(guard.canActivate(route, state)).toBe(false); expect(guard.canActivate(route, state)).toBe(false);
expect(navigateToLoginSpy).toHaveBeenCalled(); expect(navigateToLoginSpy).toHaveBeenCalled();

17
npm/ng-packs/packages/oauth/src/lib/tests/initial-utils.spec.ts

@ -1,6 +1,6 @@
import { Component, Injector } from '@angular/core'; import { Component, Injector } from '@angular/core';
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest'; import { createComponentFactory, Spectator } from '@ngneat/spectator/vitest';
import { OAuthService } from 'angular-oauth2-oidc'; import { OAuthService, OAuthStorage } from 'angular-oauth2-oidc';
import { import {
CORE_OPTIONS, CORE_OPTIONS,
@ -42,6 +42,15 @@ describe('InitialUtils', () => {
skipGetAppConfiguration: false, skipGetAppConfiguration: false,
}, },
}, },
{
provide: OAuthStorage,
useValue: {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
clear: vi.fn(),
},
},
], ],
}); });
@ -53,8 +62,8 @@ describe('InitialUtils', () => {
let clearOAuthStorageSpy; let clearOAuthStorageSpy;
beforeEach(() => { beforeEach(() => {
injector = spectator.inject(Injector); injector = spectator.inject(Injector);
injectorSpy = jest.spyOn(injector, 'get'); injectorSpy = vi.spyOn(injector, 'get');
clearOAuthStorageSpy = jest.spyOn(clearOAuthStorageDefault, 'clearOAuthStorage'); clearOAuthStorageSpy = vi.spyOn(clearOAuthStorageDefault, 'clearOAuthStorage');
clearOAuthStorageSpy.mockReset(); clearOAuthStorageSpy.mockReset();
}); });

2
npm/ng-packs/packages/oauth/src/lib/utils/check-access-token.ts

@ -7,6 +7,6 @@ export const checkAccessToken: CheckAuthenticationStateFn = function (injector:
const configState = injector.get(ConfigStateService); const configState = injector.get(ConfigStateService);
const oAuth = injector.get(OAuthService); const oAuth = injector.get(OAuthService);
if (oAuth.hasValidAccessToken() && !configState.getDeep('currentUser.id')) { if (oAuth.hasValidAccessToken() && !configState.getDeep('currentUser.id')) {
clearOAuthStorage(this.injector); clearOAuthStorage(injector);
} }
}; };

Loading…
Cancel
Save