fix: fix observable

This commit is contained in:
2025-01-31 03:23:45 +08:00
parent 733b697ee2
commit 316361bd3c
45 changed files with 1084 additions and 1355 deletions

View File

@@ -1,5 +1,5 @@
import { TestBed } from '@/testing';
import { of, throwError } from 'rxjs';
import { lastValueFrom, of, throwError } from 'rxjs';
import { vi } from 'vitest';
import { DataService } from '../../api/data.service';
import { LoggerService } from '../../logging/logger.service';
@@ -59,16 +59,14 @@ describe('AuthWellKnownDataService', () => {
const urlWithoutSuffix = 'myUrl';
const urlWithSuffix = `${urlWithoutSuffix}/.well-known/openid-configuration`;
(service as any)
.getWellKnownDocument(urlWithoutSuffix, { configId: 'configId1' })
.subscribe(() => {
expect(dataServiceSpy).toHaveBeenCalledExactlyOnceWith(
urlWithSuffix,
{
configId: 'configId1',
}
);
});
await lastValueFrom(
(service as any).getWellKnownDocument(urlWithoutSuffix, {
configId: 'configId1',
})
);
expect(dataServiceSpy).toHaveBeenCalledExactlyOnceWith(urlWithSuffix, {
configId: 'configId1',
});
});
it('should not add suffix if it does exist on current url', async () => {
@@ -77,16 +75,14 @@ describe('AuthWellKnownDataService', () => {
.mockReturnValue(of(null));
const urlWithSuffix = `myUrl/.well-known/openid-configuration`;
(service as any)
.getWellKnownDocument(urlWithSuffix, { configId: 'configId1' })
.subscribe(() => {
expect(dataServiceSpy).toHaveBeenCalledExactlyOnceWith(
urlWithSuffix,
{
configId: 'configId1',
}
);
});
await lastValueFrom(
(service as any).getWellKnownDocument(urlWithSuffix, {
configId: 'configId1',
})
);
expect(dataServiceSpy).toHaveBeenCalledExactlyOnceWith(urlWithSuffix, {
configId: 'configId1',
});
});
it('should not add suffix if it does exist in the middle of current url', async () => {
@@ -95,16 +91,14 @@ describe('AuthWellKnownDataService', () => {
.mockReturnValue(of(null));
const urlWithSuffix = `myUrl/.well-known/openid-configuration/and/some/more/stuff`;
(service as any)
.getWellKnownDocument(urlWithSuffix, { configId: 'configId1' })
.subscribe(() => {
expect(dataServiceSpy).toHaveBeenCalledExactlyOnceWith(
urlWithSuffix,
{
configId: 'configId1',
}
);
});
await lastValueFrom(
(service as any).getWellKnownDocument(urlWithSuffix, {
configId: 'configId1',
})
);
expect(dataServiceSpy).toHaveBeenCalledExactlyOnceWith(urlWithSuffix, {
configId: 'configId1',
});
});
it('should use the custom suffix provided in the config', async () => {
@@ -114,20 +108,16 @@ describe('AuthWellKnownDataService', () => {
const urlWithoutSuffix = `myUrl`;
const urlWithSuffix = `${urlWithoutSuffix}/.well-known/test-openid-configuration`;
(service as any)
.getWellKnownDocument(urlWithoutSuffix, {
await lastValueFrom(
(service as any).getWellKnownDocument(urlWithoutSuffix, {
configId: 'configId1',
authWellknownUrlSuffix: '/.well-known/test-openid-configuration',
})
.subscribe(() => {
expect(dataServiceSpy).toHaveBeenCalledExactlyOnceWith(
urlWithSuffix,
{
configId: 'configId1',
authWellknownUrlSuffix: '/.well-known/test-openid-configuration',
}
);
});
);
expect(dataServiceSpy).toHaveBeenCalledExactlyOnceWith(urlWithSuffix, {
configId: 'configId1',
authWellknownUrlSuffix: '/.well-known/test-openid-configuration',
});
});
it('should retry once', async () => {
@@ -193,16 +183,15 @@ describe('AuthWellKnownDataService', () => {
const spy = vi.spyOn(service as any, 'getWellKnownDocument')();
service
.getWellKnownEndPointsForConfig({
const result = await lastValueFrom(
service.getWellKnownEndPointsForConfig({
configId: 'configId1',
authWellknownEndpointUrl: 'any-url',
})
.subscribe((result) => {
expect(spy).toHaveBeenCalled();
expect((result as any).jwks_uri).toBeUndefined();
expect(result.jwksUri).toBe('jwks_uri');
});
);
expect(spy).toHaveBeenCalled();
expect((result as any).jwks_uri).toBeUndefined();
expect(result.jwksUri).toBe('jwks_uri');
});
it('throws error and logs if no authwellknownUrl is given', async () => {
@@ -234,8 +223,8 @@ describe('AuthWellKnownDataService', () => {
jwksUri: DUMMY_WELL_KNOWN_DOCUMENT.jwks_uri,
};
service
.getWellKnownEndPointsForConfig({
const result = await lastValueFrom(
service.getWellKnownEndPointsForConfig({
configId: 'configId1',
authWellknownEndpointUrl: 'any-url',
authWellknownEndpoints: {
@@ -243,9 +232,8 @@ describe('AuthWellKnownDataService', () => {
revocationEndpoint: 'config-revocationEndpoint',
},
})
.subscribe((result) => {
expect(result).toEqual(jasmine.objectContaining(expected));
});
);
expect(result).toEqual(jasmine.objectContaining(expected));
});
});
});

View File

@@ -1,5 +1,5 @@
import { TestBed, mockImplementationWhenArgsEqual } from '@/testing';
import { of, throwError } from 'rxjs';
import { lastValueFrom, of, throwError } from 'rxjs';
import { vi } from 'vitest';
import { EventTypes } from '../../public-events/event-types';
import { PublicEventsService } from '../../public-events/public-events.service';
@@ -57,13 +57,12 @@ describe('AuthWellKnownService', () => {
() => ({ issuer: 'anything' })
);
service
.queryAndStoreAuthWellKnownEndPoints({ configId: 'configId1' })
.subscribe((result) => {
expect(storagePersistenceService.read).not.toHaveBeenCalled();
expect(dataServiceSpy).toHaveBeenCalled();
expect(result).toEqual({ issuer: 'anything' });
});
const result = await lastValueFrom(
service.queryAndStoreAuthWellKnownEndPoints({ configId: 'configId1' })
);
expect(storagePersistenceService.read).not.toHaveBeenCalled();
expect(dataServiceSpy).toHaveBeenCalled();
expect(result).toEqual({ issuer: 'anything' });
});
it('getAuthWellKnownEndPoints stored the result if http call is made', async () => {
@@ -78,13 +77,12 @@ describe('AuthWellKnownService', () => {
);
const storeSpy = vi.spyOn(service, 'storeWellKnownEndpoints');
service
.queryAndStoreAuthWellKnownEndPoints({ configId: 'configId1' })
.subscribe((result) => {
expect(dataServiceSpy).toHaveBeenCalled();
expect(storeSpy).toHaveBeenCalled();
expect(result).toEqual({ issuer: 'anything' });
});
const result = await lastValueFrom(
service.queryAndStoreAuthWellKnownEndPoints({ configId: 'configId1' })
);
expect(dataServiceSpy).toHaveBeenCalled();
expect(storeSpy).toHaveBeenCalled();
expect(result).toEqual({ issuer: 'anything' });
});
it('throws `ConfigLoadingFailed` event when error happens from http', async () => {

View File

@@ -1,5 +1,5 @@
import { TestBed } from '@/testing';
import { of } from 'rxjs';
import { lastValueFrom, of } from 'rxjs';
import { vi } from 'vitest';
import { LoggerService } from '../logging/logger.service';
import { EventTypes } from '../public-events/event-types';
@@ -93,10 +93,11 @@ describe('Configuration Service', () => {
};
const spy = vi.spyOn(configService as any, 'loadConfigs');
configService.getOpenIDConfiguration('configId1').subscribe((config) => {
expect(config).toBeTruthy();
expect(spy).not.toHaveBeenCalled();
});
const config = await lastValueFrom(
configService.getOpenIDConfiguration('configId1')
);
expect(config).toBeTruthy();
expect(spy).not.toHaveBeenCalled();
});
it(`if config is NOT already saved 'loadConfigs' is called`, async () => {
@@ -107,10 +108,11 @@ describe('Configuration Service', () => {
vi.spyOn(configValidationService, 'validateConfig').mockReturnValue(true);
configService.getOpenIDConfiguration('configId1').subscribe((config) => {
expect(config).toBeTruthy();
expect(spy).toHaveBeenCalled();
});
const config = await lastValueFrom(
configService.getOpenIDConfiguration('configId1')
);
expect(config).toBeTruthy();
expect(spy).toHaveBeenCalled();
});
it('returns null if config is not valid', async () => {
@@ -124,12 +126,13 @@ describe('Configuration Service', () => {
);
const consoleSpy = vi.spyOn(console, 'warn');
configService.getOpenIDConfiguration('configId1').subscribe((config) => {
expect(config).toBeNull();
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
`[oidc-client-rx] No configuration found for config id 'configId1'.`
);
});
const config = await lastValueFrom(
configService.getOpenIDConfiguration('configId1')
);
expect(config).toBeNull();
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
`[oidc-client-rx] No configuration found for config id 'configId1'.`
);
});
it('returns null if configs are stored but not existing ID is passed', async () => {
@@ -138,11 +141,10 @@ describe('Configuration Service', () => {
configId2: { configId: 'configId2' },
};
configService
.getOpenIDConfiguration('notExisting')
.subscribe((config) => {
expect(config).toBeNull();
});
const config = await lastValueFrom(
configService.getOpenIDConfiguration('notExisting')
);
expect(config).toBeNull();
});
it('sets authWellKnownEndPoints on config if authWellKnownEndPoints is stored', async () => {
@@ -158,12 +160,13 @@ describe('Configuration Service', () => {
issuer: 'auth-well-known',
});
configService.getOpenIDConfiguration('configId1').subscribe((config) => {
expect(config?.authWellknownEndpoints).toEqual({
issuer: 'auth-well-known',
});
expect(consoleSpy).not.toHaveBeenCalled();
const config = await lastValueFrom(
configService.getOpenIDConfiguration('configId1')
);
expect(config?.authWellknownEndpoints).toEqual({
issuer: 'auth-well-known',
});
expect(consoleSpy).not.toHaveBeenCalled();
});
it('fires ConfigLoaded if authWellKnownEndPoints is stored', async () => {
@@ -179,12 +182,11 @@ describe('Configuration Service', () => {
const spy = vi.spyOn(publicEventsService, 'fireEvent');
configService.getOpenIDConfiguration('configId1').subscribe(() => {
expect(spy).toHaveBeenCalledExactlyOnceWith(
EventTypes.ConfigLoaded,
expect.anything()
);
});
await lastValueFrom(configService.getOpenIDConfiguration('configId1'));
expect(spy).toHaveBeenCalledExactlyOnceWith(
EventTypes.ConfigLoaded,
expect.anything()
);
});
it('stores, uses and fires event when authwellknownendpoints are passed', async () => {
@@ -207,19 +209,20 @@ describe('Configuration Service', () => {
'storeWellKnownEndpoints'
);
configService.getOpenIDConfiguration('configId1').subscribe((config) => {
expect(config).toBeTruthy();
expect(fireEventSpy).toHaveBeenCalledExactlyOnceWith(
EventTypes.ConfigLoaded,
expect.anything()
);
expect(storeWellKnownEndpointsSpy).toHaveBeenCalledExactlyOnceWith(
config as OpenIdConfiguration,
{
issuer: 'auth-well-known',
}
);
});
const config = await lastValueFrom(
configService.getOpenIDConfiguration('configId1')
);
expect(config).toBeTruthy();
expect(fireEventSpy).toHaveBeenCalledExactlyOnceWith(
EventTypes.ConfigLoaded,
expect.anything()
);
expect(storeWellKnownEndpointsSpy).toHaveBeenCalledExactlyOnceWith(
config as OpenIdConfiguration,
{
issuer: 'auth-well-known',
}
);
});
});
@@ -234,10 +237,11 @@ describe('Configuration Service', () => {
vi.spyOn(configValidationService, 'validateConfig').mockReturnValue(true);
configService.getOpenIDConfigurations('configId1').subscribe((result) => {
expect(result.allConfigs.length).toEqual(2);
expect(result.currentConfig).toBeTruthy();
});
const result = await lastValueFrom(
configService.getOpenIDConfigurations('configId1')
);
expect(result.allConfigs.length).toEqual(2);
expect(result.currentConfig).toBeTruthy();
});
it('created configId when configId is not set', async () => {
@@ -250,15 +254,14 @@ describe('Configuration Service', () => {
vi.spyOn(configValidationService, 'validateConfig').mockReturnValue(true);
configService.getOpenIDConfigurations().subscribe((result) => {
expect(result.allConfigs.length).toEqual(2);
const allConfigIds = result.allConfigs.map((x) => x.configId);
expect(allConfigIds).toEqual(['0-clientId1', '1-clientId2']);
expect(result.currentConfig).toBeTruthy();
expect(result.currentConfig?.configId).toBeTruthy();
});
const result = await lastValueFrom(
configService.getOpenIDConfigurations()
);
expect(result.allConfigs.length).toEqual(2);
const allConfigIds = result.allConfigs.map((x) => x.configId);
expect(allConfigIds).toEqual(['0-clientId1', '1-clientId2']);
expect(result.currentConfig).toBeTruthy();
expect(result.currentConfig?.configId).toBeTruthy();
});
it('returns empty array if config is not valid', async () => {
@@ -273,12 +276,9 @@ describe('Configuration Service', () => {
false
);
configService
.getOpenIDConfigurations()
.subscribe(({ allConfigs, currentConfig }) => {
expect(allConfigs).toEqual([]);
expect(currentConfig).toBeNull();
});
await lastValueFrom(configService.getOpenIDConfigurations());
expect(allConfigs).toEqual([]);
expect(currentConfig).toBeNull();
});
});

View File

@@ -1,5 +1,5 @@
import { waitForAsync } from '@/testing';
import { of } from 'rxjs';
import { lastValueFrom, of } from 'rxjs';
import type { OpenIdConfiguration } from '../openid-configuration';
import { StsConfigHttpLoader, StsConfigStaticLoader } from './config-loader';
@@ -16,9 +16,8 @@ describe('ConfigLoader', () => {
const result$ = loader.loadConfigs();
result$.subscribe((result) => {
expect(Array.isArray(result)).toBeTruthy();
});
const result = await lastValueFrom(result$);
expect(Array.isArray(result)).toBeTruthy();
});
it('returns an array if only one config is passed', async () => {
@@ -28,9 +27,8 @@ describe('ConfigLoader', () => {
const result$ = loader.loadConfigs();
result$.subscribe((result) => {
expect(Array.isArray(result)).toBeTruthy();
});
const result = await lastValueFrom(result$);
expect(Array.isArray(result)).toBeTruthy();
});
});
});
@@ -46,11 +44,10 @@ describe('ConfigLoader', () => {
const result$ = loader.loadConfigs();
result$.subscribe((result) => {
expect(Array.isArray(result)).toBeTruthy();
expect(result[0].configId).toBe('configId1');
expect(result[1].configId).toBe('configId2');
});
const result = await lastValueFrom(result$);
expect(Array.isArray(result)).toBeTruthy();
expect(result[0].configId).toBe('configId1');
expect(result[1].configId).toBe('configId2');
});
it('returns an array if an observable with a config array is passed', async () => {
@@ -62,11 +59,10 @@ describe('ConfigLoader', () => {
const result$ = loader.loadConfigs();
result$.subscribe((result) => {
expect(Array.isArray(result)).toBeTruthy();
expect(result[0].configId).toBe('configId1');
expect(result[1].configId).toBe('configId2');
});
const result = await lastValueFrom(result$);
expect(Array.isArray(result)).toBeTruthy();
expect(result[0].configId).toBe('configId1');
expect(result[1].configId).toBe('configId2');
});
it('returns an array if only one config is passed', async () => {
@@ -76,10 +72,9 @@ describe('ConfigLoader', () => {
const result$ = loader.loadConfigs();
result$.subscribe((result) => {
expect(Array.isArray(result)).toBeTruthy();
expect(result[0].configId).toBe('configId1');
});
const result = await lastValueFrom(result$);
expect(Array.isArray(result)).toBeTruthy();
expect(result[0].configId).toBe('configId1');
});
});
});