feat: init

This commit is contained in:
2025-01-30 20:02:28 +08:00
parent da0d9855da
commit 1785df25e2
125 changed files with 8601 additions and 4725 deletions

View File

@@ -1,19 +1,20 @@
import { TestBed } from '@/testing';
import {
HTTP_INTERCEPTORS,
HttpClient,
provideHttpClient,
withInterceptors,
withInterceptorsFromDi,
} from '@angular/common/http';
} from '@ngify/http';
import {
HttpTestingController,
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed, waitForAsync } from '@angular/core/testing';
import { mockProvider } from '../../test/auto-mock';
} from '@ngify/http/testing';
import { vi } from 'vitest';
import { AuthStateService } from '../auth-state/auth-state.service';
import { ConfigurationService } from '../config/config.service';
import { LoggerService } from '../logging/logger.service';
import { mockProvider } from '../testing/mock';
import { AuthInterceptor, authInterceptor } from './auth.interceptor';
import { ClosestMatchingRouteService } from './closest-matching-route.service';
@@ -85,18 +86,22 @@ describe(`AuthHttpInterceptor`, () => {
});
function runTests(): void {
it('should add an Authorization header when route matches and token is present', waitForAsync(() => {
it('should add an Authorization header when route matches and token is present', async () => {
const actionUrl = `https://jsonplaceholder.typicode.com/`;
spyOn(configurationService, 'getAllConfigurations').and.returnValue([
vi.spyOn(configurationService, 'getAllConfigurations').mockReturnValue([
{
secureRoutes: [actionUrl],
configId: 'configId1',
},
]);
spyOn(authStateService, 'getAccessToken').and.returnValue('thisIsAToken');
spyOn(configurationService, 'hasAtLeastOneConfig').and.returnValue(true);
vi.spyOn(authStateService, 'getAccessToken').mockReturnValue(
'thisIsAToken'
);
vi.spyOn(configurationService, 'hasAtLeastOneConfig').mockReturnValue(
true
);
httpClient.get(actionUrl).subscribe((response) => {
expect(response).toBeTruthy();
@@ -108,18 +113,22 @@ describe(`AuthHttpInterceptor`, () => {
httpRequest.flush('something');
httpTestingController.verify();
}));
});
it('should not add an Authorization header when `secureRoutes` is not given', waitForAsync(() => {
it('should not add an Authorization header when `secureRoutes` is not given', async () => {
const actionUrl = `https://jsonplaceholder.typicode.com/`;
spyOn(configurationService, 'getAllConfigurations').and.returnValue([
vi.spyOn(configurationService, 'getAllConfigurations').mockReturnValue([
{
configId: 'configId1',
},
]);
spyOn(authStateService, 'getAccessToken').and.returnValue('thisIsAToken');
spyOn(configurationService, 'hasAtLeastOneConfig').and.returnValue(true);
vi.spyOn(authStateService, 'getAccessToken').mockReturnValue(
'thisIsAToken'
);
vi.spyOn(configurationService, 'hasAtLeastOneConfig').mockReturnValue(
true
);
httpClient.get(actionUrl).subscribe((response) => {
expect(response).toBeTruthy();
@@ -131,20 +140,24 @@ describe(`AuthHttpInterceptor`, () => {
httpRequest.flush('something');
httpTestingController.verify();
}));
});
it('should not add an Authorization header when no routes configured', waitForAsync(() => {
it('should not add an Authorization header when no routes configured', async () => {
const actionUrl = `https://jsonplaceholder.typicode.com/`;
spyOn(configurationService, 'getAllConfigurations').and.returnValue([
vi.spyOn(configurationService, 'getAllConfigurations').mockReturnValue([
{
secureRoutes: [],
configId: 'configId1',
},
]);
spyOn(configurationService, 'hasAtLeastOneConfig').and.returnValue(true);
spyOn(authStateService, 'getAccessToken').and.returnValue('thisIsAToken');
vi.spyOn(configurationService, 'hasAtLeastOneConfig').mockReturnValue(
true
);
vi.spyOn(authStateService, 'getAccessToken').mockReturnValue(
'thisIsAToken'
);
httpClient.get(actionUrl).subscribe((response) => {
expect(response).toBeTruthy();
@@ -156,19 +169,21 @@ describe(`AuthHttpInterceptor`, () => {
httpRequest.flush('something');
httpTestingController.verify();
}));
});
it('should not add an Authorization header when no routes configured', waitForAsync(() => {
it('should not add an Authorization header when no routes configured', async () => {
const actionUrl = `https://jsonplaceholder.typicode.com/`;
spyOn(configurationService, 'getAllConfigurations').and.returnValue([
vi.spyOn(configurationService, 'getAllConfigurations').mockReturnValue([
{
secureRoutes: [],
configId: 'configId1',
},
]);
spyOn(configurationService, 'hasAtLeastOneConfig').and.returnValue(true);
vi.spyOn(configurationService, 'hasAtLeastOneConfig').mockReturnValue(
true
);
httpClient.get(actionUrl).subscribe((response) => {
expect(response).toBeTruthy();
@@ -180,20 +195,22 @@ describe(`AuthHttpInterceptor`, () => {
httpRequest.flush('something');
httpTestingController.verify();
}));
});
it('should not add an Authorization header when route is configured but no token is present', waitForAsync(() => {
it('should not add an Authorization header when route is configured but no token is present', async () => {
const actionUrl = `https://jsonplaceholder.typicode.com/`;
spyOn(configurationService, 'getAllConfigurations').and.returnValue([
vi.spyOn(configurationService, 'getAllConfigurations').mockReturnValue([
{
secureRoutes: [actionUrl],
configId: 'configId1',
},
]);
spyOn(configurationService, 'hasAtLeastOneConfig').and.returnValue(true);
spyOn(authStateService, 'getAccessToken').and.returnValue('');
vi.spyOn(configurationService, 'hasAtLeastOneConfig').mockReturnValue(
true
);
vi.spyOn(authStateService, 'getAccessToken').mockReturnValue('');
httpClient.get(actionUrl).subscribe((response) => {
expect(response).toBeTruthy();
@@ -205,12 +222,14 @@ describe(`AuthHttpInterceptor`, () => {
httpRequest.flush('something');
httpTestingController.verify();
}));
});
it('should not add an Authorization header when no config is present', waitForAsync(() => {
it('should not add an Authorization header when no config is present', async () => {
const actionUrl = `https://jsonplaceholder.typicode.com/`;
spyOn(configurationService, 'hasAtLeastOneConfig').and.returnValue(false);
vi.spyOn(configurationService, 'hasAtLeastOneConfig').mockReturnValue(
false
);
httpClient.get(actionUrl).subscribe((response) => {
expect(response).toBeTruthy();
@@ -222,22 +241,24 @@ describe(`AuthHttpInterceptor`, () => {
httpRequest.flush('something');
httpTestingController.verify();
}));
});
it('should not add an Authorization header when no configured route is matching the request', waitForAsync(() => {
spyOn(configurationService, 'hasAtLeastOneConfig').and.returnValue(true);
it('should not add an Authorization header when no configured route is matching the request', async () => {
vi.spyOn(configurationService, 'hasAtLeastOneConfig').mockReturnValue(
true
);
const actionUrl = `https://jsonplaceholder.typicode.com/`;
spyOn(configurationService, 'getAllConfigurations').and.returnValue([
vi.spyOn(configurationService, 'getAllConfigurations').mockReturnValue([
{
secureRoutes: [actionUrl],
configId: 'configId1',
},
]);
spyOn(
vi.spyOn(
closestMatchingRouteService,
'getConfigIdForClosestMatchingRoute'
).and.returnValue({
).mockReturnValue({
matchingRoute: null,
matchingConfig: null,
});
@@ -252,18 +273,22 @@ describe(`AuthHttpInterceptor`, () => {
httpRequest.flush('something');
httpTestingController.verify();
}));
});
it('should add an Authorization header when multiple routes are configured and token is present', waitForAsync(() => {
it('should add an Authorization header when multiple routes are configured and token is present', async () => {
const actionUrl = `https://jsonplaceholder.typicode.com/`;
const actionUrl2 = `https://some-other-url.com/`;
spyOn(configurationService, 'getAllConfigurations').and.returnValue([
vi.spyOn(configurationService, 'getAllConfigurations').mockReturnValue([
{ secureRoutes: [actionUrl, actionUrl2], configId: 'configId1' },
]);
spyOn(authStateService, 'getAccessToken').and.returnValue('thisIsAToken');
spyOn(configurationService, 'hasAtLeastOneConfig').and.returnValue(true);
vi.spyOn(authStateService, 'getAccessToken').mockReturnValue(
'thisIsAToken'
);
vi.spyOn(configurationService, 'hasAtLeastOneConfig').mockReturnValue(
true
);
httpClient.get(actionUrl).subscribe((response) => {
expect(response).toBeTruthy();
@@ -284,6 +309,6 @@ describe(`AuthHttpInterceptor`, () => {
httpRequest.flush('something');
httpRequest2.flush('something');
httpTestingController.verify();
}));
});
}
});

View File

@@ -1,6 +1,7 @@
import { TestBed } from '@angular/core/testing';
import { mockProvider } from '../../test/auto-mock';
import { TestBed } from '@/testing';
import { vi } from 'vitest';
import { LoggerService } from '../logging/logger.service';
import { mockProvider } from '../testing/mock';
import { ClosestMatchingRouteService } from './closest-matching-route.service';
describe('ClosestMatchingRouteService', () => {