feat: init
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
import { TestBed, waitForAsync } from '@angular/core/testing';
|
||||
import { TestBed } from '@/testing';
|
||||
import { of } from 'rxjs';
|
||||
import { mockProvider } from '../../../test/auto-mock';
|
||||
import { vi } from 'vitest';
|
||||
import { CheckAuthService } from '../../auth-state/check-auth.service';
|
||||
import { AuthWellKnownService } from '../../config/auth-well-known/auth-well-known.service';
|
||||
import { LoggerService } from '../../logging/logger.service';
|
||||
import { mockProvider } from '../../testing/mock';
|
||||
import { RedirectService } from '../../utils/redirect/redirect.service';
|
||||
import { UrlService } from '../../utils/url/url.service';
|
||||
import { LoginResponse } from '../login-response';
|
||||
import { PopupResult } from '../popup/popup-result';
|
||||
import type { LoginResponse } from '../login-response';
|
||||
import type { PopupResult } from '../popup/popup-result';
|
||||
import { PopUpService } from '../popup/popup.service';
|
||||
import { ResponseTypeValidationService } from '../response-type-validation/response-type-validation.service';
|
||||
import { ParLoginService } from './par-login.service';
|
||||
import { ParResponse } from './par-response';
|
||||
import type { ParResponse } from './par-response';
|
||||
import { ParService } from './par.service';
|
||||
|
||||
describe('ParLoginService', () => {
|
||||
@@ -60,33 +61,33 @@ describe('ParLoginService', () => {
|
||||
});
|
||||
|
||||
describe('loginPar', () => {
|
||||
it('does nothing if it has an invalid response type', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('does nothing if it has an invalid response type', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(false);
|
||||
const loggerSpy = spyOn(loggerService, 'logError');
|
||||
).mockReturnValue(false);
|
||||
const loggerSpy = vi.spyOn(loggerService, 'logError');
|
||||
|
||||
const result = service.loginPar({});
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
expect(loggerSpy).toHaveBeenCalled();
|
||||
}));
|
||||
});
|
||||
|
||||
it('calls parService.postParRequest without custom params when no custom params are passed', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('calls parService.postParRequest without custom params when no custom params are passed', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(true);
|
||||
).mockReturnValue(true);
|
||||
|
||||
spyOn(
|
||||
vi.spyOn(
|
||||
authWellKnownService,
|
||||
'queryAndStoreAuthWellKnownEndPoints'
|
||||
).and.returnValue(of({}));
|
||||
).mockReturnValue(of({}));
|
||||
|
||||
const spy = spyOn(parService, 'postParRequest').and.returnValue(
|
||||
of({ requestUri: 'requestUri' } as ParResponse)
|
||||
);
|
||||
const spy = vi
|
||||
.spyOn(parService, 'postParRequest')
|
||||
.mockReturnValue(of({ requestUri: 'requestUri' } as ParResponse));
|
||||
|
||||
const result = service.loginPar({
|
||||
authWellknownEndpointUrl: 'authWellknownEndpoint',
|
||||
@@ -95,69 +96,69 @@ describe('ParLoginService', () => {
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
expect(spy).toHaveBeenCalled();
|
||||
}));
|
||||
});
|
||||
|
||||
it('calls parService.postParRequest with custom params when custom params are passed', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('calls parService.postParRequest with custom params when custom params are passed', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(true);
|
||||
).mockReturnValue(true);
|
||||
const config = {
|
||||
authWellknownEndpointUrl: 'authWellknownEndpoint',
|
||||
responseType: 'stubValue',
|
||||
};
|
||||
|
||||
spyOn(
|
||||
vi.spyOn(
|
||||
authWellKnownService,
|
||||
'queryAndStoreAuthWellKnownEndPoints'
|
||||
).and.returnValue(of({}));
|
||||
).mockReturnValue(of({}));
|
||||
|
||||
const spy = spyOn(parService, 'postParRequest').and.returnValue(
|
||||
of({ requestUri: 'requestUri' } as ParResponse)
|
||||
);
|
||||
const spy = vi
|
||||
.spyOn(parService, 'postParRequest')
|
||||
.mockReturnValue(of({ requestUri: 'requestUri' } as ParResponse));
|
||||
|
||||
const result = service.loginPar(config, {
|
||||
customParams: { some: 'thing' },
|
||||
});
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
expect(spy).toHaveBeenCalledOnceWith(config, {
|
||||
expect(spy).toHaveBeenCalledExactlyOnceWith(config, {
|
||||
customParams: { some: 'thing' },
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('returns undefined and logs error when no url could be created', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('returns undefined and logs error when no url could be created', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(true);
|
||||
).mockReturnValue(true);
|
||||
const config = {
|
||||
authWellknownEndpointUrl: 'authWellknownEndpoint',
|
||||
responseType: 'stubValue',
|
||||
};
|
||||
|
||||
spyOn(
|
||||
vi.spyOn(
|
||||
authWellKnownService,
|
||||
'queryAndStoreAuthWellKnownEndPoints'
|
||||
).and.returnValue(of({}));
|
||||
).mockReturnValue(of({}));
|
||||
|
||||
spyOn(parService, 'postParRequest').and.returnValue(
|
||||
vi.spyOn(parService, 'postParRequest').mockReturnValue(
|
||||
of({ requestUri: 'requestUri' } as ParResponse)
|
||||
);
|
||||
spyOn(urlService, 'getAuthorizeParUrl').and.returnValue('');
|
||||
const spy = spyOn(loggerService, 'logError');
|
||||
vi.spyOn(urlService, 'getAuthorizeParUrl').mockReturnValue('');
|
||||
const spy = vi.spyOn(loggerService, 'logError');
|
||||
|
||||
const result = service.loginPar(config);
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
}));
|
||||
});
|
||||
|
||||
it('calls redirect service redirectTo when url could be created', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('calls redirect service redirectTo when url could be created', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(true);
|
||||
).mockReturnValue(true);
|
||||
const config = {
|
||||
authWellknownEndpointUrl: 'authWellknownEndpoint',
|
||||
responseType: 'stubValue',
|
||||
@@ -165,42 +166,46 @@ describe('ParLoginService', () => {
|
||||
|
||||
const authOptions = {};
|
||||
|
||||
spyOn(
|
||||
vi.spyOn(
|
||||
authWellKnownService,
|
||||
'queryAndStoreAuthWellKnownEndPoints'
|
||||
).and.returnValue(of({}));
|
||||
).mockReturnValue(of({}));
|
||||
|
||||
spyOn(parService, 'postParRequest').and.returnValue(
|
||||
vi.spyOn(parService, 'postParRequest').mockReturnValue(
|
||||
of({ requestUri: 'requestUri' } as ParResponse)
|
||||
);
|
||||
spyOn(urlService, 'getAuthorizeParUrl').and.returnValue('some-par-url');
|
||||
const spy = spyOn(redirectService, 'redirectTo');
|
||||
vi.spyOn(urlService, 'getAuthorizeParUrl').mockReturnValue(
|
||||
'some-par-url'
|
||||
);
|
||||
const spy = vi.spyOn(redirectService, 'redirectTo');
|
||||
|
||||
service.loginPar(config, authOptions);
|
||||
|
||||
expect(spy).toHaveBeenCalledOnceWith('some-par-url');
|
||||
}));
|
||||
expect(spy).toHaveBeenCalledExactlyOnceWith('some-par-url');
|
||||
});
|
||||
|
||||
it('calls urlHandler when URL is passed', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('calls urlHandler when URL is passed', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(true);
|
||||
).mockReturnValue(true);
|
||||
const config = {
|
||||
authWellknownEndpointUrl: 'authWellknownEndpoint',
|
||||
responseType: 'stubValue',
|
||||
};
|
||||
|
||||
spyOn(
|
||||
vi.spyOn(
|
||||
authWellKnownService,
|
||||
'queryAndStoreAuthWellKnownEndPoints'
|
||||
).and.returnValue(of({}));
|
||||
).mockReturnValue(of({}));
|
||||
|
||||
spyOn(parService, 'postParRequest').and.returnValue(
|
||||
vi.spyOn(parService, 'postParRequest').mockReturnValue(
|
||||
of({ requestUri: 'requestUri' } as ParResponse)
|
||||
);
|
||||
spyOn(urlService, 'getAuthorizeParUrl').and.returnValue('some-par-url');
|
||||
const redirectToSpy = spyOn(redirectService, 'redirectTo');
|
||||
vi.spyOn(urlService, 'getAuthorizeParUrl').mockReturnValue(
|
||||
'some-par-url'
|
||||
);
|
||||
const redirectToSpy = vi.spyOn(redirectService, 'redirectTo');
|
||||
const spy = jasmine.createSpy();
|
||||
const urlHandler = (url: any): void => {
|
||||
spy(url);
|
||||
@@ -208,18 +213,18 @@ describe('ParLoginService', () => {
|
||||
|
||||
service.loginPar(config, { urlHandler });
|
||||
|
||||
expect(spy).toHaveBeenCalledOnceWith('some-par-url');
|
||||
expect(spy).toHaveBeenCalledExactlyOnceWith('some-par-url');
|
||||
expect(redirectToSpy).not.toHaveBeenCalled();
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
describe('loginWithPopUpPar', () => {
|
||||
it('does nothing if it has an invalid response type', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('does nothing if it has an invalid response type', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(false);
|
||||
const loggerSpy = spyOn(loggerService, 'logError');
|
||||
).mockReturnValue(false);
|
||||
const loggerSpy = vi.spyOn(loggerService, 'logError');
|
||||
const config = {};
|
||||
const allConfigs = [config];
|
||||
|
||||
@@ -229,27 +234,27 @@ describe('ParLoginService', () => {
|
||||
expect(err.message).toBe('Invalid response type!');
|
||||
},
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('calls parService.postParRequest without custom params when no custom params are passed', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('calls parService.postParRequest without custom params when no custom params are passed', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(true);
|
||||
).mockReturnValue(true);
|
||||
const config = {
|
||||
authWellknownEndpointUrl: 'authWellknownEndpoint',
|
||||
responseType: 'stubValue',
|
||||
};
|
||||
const allConfigs = [config];
|
||||
|
||||
spyOn(
|
||||
vi.spyOn(
|
||||
authWellKnownService,
|
||||
'queryAndStoreAuthWellKnownEndPoints'
|
||||
).and.returnValue(of({}));
|
||||
).mockReturnValue(of({}));
|
||||
|
||||
const spy = spyOn(parService, 'postParRequest').and.returnValue(
|
||||
of({ requestUri: 'requestUri' } as ParResponse)
|
||||
);
|
||||
const spy = vi
|
||||
.spyOn(parService, 'postParRequest')
|
||||
.mockReturnValue(of({ requestUri: 'requestUri' } as ParResponse));
|
||||
|
||||
service.loginWithPopUpPar(config, allConfigs).subscribe({
|
||||
error: (err) => {
|
||||
@@ -259,27 +264,27 @@ describe('ParLoginService', () => {
|
||||
);
|
||||
},
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('calls parService.postParRequest with custom params when custom params are passed', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('calls parService.postParRequest with custom params when custom params are passed', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(true);
|
||||
).mockReturnValue(true);
|
||||
const config = {
|
||||
authWellknownEndpointUrl: 'authWellknownEndpoint',
|
||||
responseType: 'stubValue',
|
||||
};
|
||||
const allConfigs = [config];
|
||||
|
||||
spyOn(
|
||||
vi.spyOn(
|
||||
authWellKnownService,
|
||||
'queryAndStoreAuthWellKnownEndPoints'
|
||||
).and.returnValue(of({}));
|
||||
).mockReturnValue(of({}));
|
||||
|
||||
const spy = spyOn(parService, 'postParRequest').and.returnValue(
|
||||
of({ requestUri: 'requestUri' } as ParResponse)
|
||||
);
|
||||
const spy = vi
|
||||
.spyOn(parService, 'postParRequest')
|
||||
.mockReturnValue(of({ requestUri: 'requestUri' } as ParResponse));
|
||||
|
||||
service
|
||||
.loginWithPopUpPar(config, allConfigs, {
|
||||
@@ -287,7 +292,7 @@ describe('ParLoginService', () => {
|
||||
})
|
||||
.subscribe({
|
||||
error: (err) => {
|
||||
expect(spy).toHaveBeenCalledOnceWith(config, {
|
||||
expect(spy).toHaveBeenCalledExactlyOnceWith(config, {
|
||||
customParams: { some: 'thing' },
|
||||
});
|
||||
expect(err.message).toBe(
|
||||
@@ -295,29 +300,29 @@ describe('ParLoginService', () => {
|
||||
);
|
||||
},
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('returns undefined and logs error when no URL could be created', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('returns undefined and logs error when no URL could be created', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(true);
|
||||
).mockReturnValue(true);
|
||||
const config = {
|
||||
authWellknownEndpointUrl: 'authWellknownEndpoint',
|
||||
responseType: 'stubValue',
|
||||
};
|
||||
const allConfigs = [config];
|
||||
|
||||
spyOn(
|
||||
vi.spyOn(
|
||||
authWellKnownService,
|
||||
'queryAndStoreAuthWellKnownEndPoints'
|
||||
).and.returnValue(of({}));
|
||||
).mockReturnValue(of({}));
|
||||
|
||||
spyOn(parService, 'postParRequest').and.returnValue(
|
||||
vi.spyOn(parService, 'postParRequest').mockReturnValue(
|
||||
of({ requestUri: 'requestUri' } as ParResponse)
|
||||
);
|
||||
spyOn(urlService, 'getAuthorizeParUrl').and.returnValue('');
|
||||
const spy = spyOn(loggerService, 'logError');
|
||||
vi.spyOn(urlService, 'getAuthorizeParUrl').mockReturnValue('');
|
||||
const spy = vi.spyOn(loggerService, 'logError');
|
||||
|
||||
service
|
||||
.loginWithPopUpPar(config, allConfigs, {
|
||||
@@ -331,46 +336,52 @@ describe('ParLoginService', () => {
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
},
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('calls popupService openPopUp when URL could be created', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('calls popupService openPopUp when URL could be created', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(true);
|
||||
).mockReturnValue(true);
|
||||
const config = {
|
||||
authWellknownEndpointUrl: 'authWellknownEndpoint',
|
||||
responseType: 'stubValue',
|
||||
};
|
||||
const allConfigs = [config];
|
||||
|
||||
spyOn(
|
||||
vi.spyOn(
|
||||
authWellKnownService,
|
||||
'queryAndStoreAuthWellKnownEndPoints'
|
||||
).and.returnValue(of({}));
|
||||
).mockReturnValue(of({}));
|
||||
|
||||
spyOn(parService, 'postParRequest').and.returnValue(
|
||||
vi.spyOn(parService, 'postParRequest').mockReturnValue(
|
||||
of({ requestUri: 'requestUri' } as ParResponse)
|
||||
);
|
||||
spyOn(urlService, 'getAuthorizeParUrl').and.returnValue('some-par-url');
|
||||
spyOn(checkAuthService, 'checkAuth').and.returnValue(
|
||||
vi.spyOn(urlService, 'getAuthorizeParUrl').mockReturnValue(
|
||||
'some-par-url'
|
||||
);
|
||||
vi.spyOn(checkAuthService, 'checkAuth').mockReturnValue(
|
||||
of({} as LoginResponse)
|
||||
);
|
||||
spyOnProperty(popupService, 'result$').and.returnValue(
|
||||
vi.spyOnProperty(popupService, 'result$').mockReturnValue(
|
||||
of({} as PopupResult)
|
||||
);
|
||||
const spy = spyOn(popupService, 'openPopUp');
|
||||
const spy = vi.spyOn(popupService, 'openPopUp');
|
||||
|
||||
service.loginWithPopUpPar(config, allConfigs).subscribe(() => {
|
||||
expect(spy).toHaveBeenCalledOnceWith('some-par-url', undefined, config);
|
||||
expect(spy).toHaveBeenCalledExactlyOnceWith(
|
||||
'some-par-url',
|
||||
undefined,
|
||||
config
|
||||
);
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('returns correct properties if URL is received', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('returns correct properties if URL is received', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(true);
|
||||
).mockReturnValue(true);
|
||||
const config = {
|
||||
authWellknownEndpointUrl: 'authWellknownEndpoint',
|
||||
responseType: 'stubValue',
|
||||
@@ -378,34 +389,40 @@ describe('ParLoginService', () => {
|
||||
};
|
||||
const allConfigs = [config];
|
||||
|
||||
spyOn(
|
||||
vi.spyOn(
|
||||
authWellKnownService,
|
||||
'queryAndStoreAuthWellKnownEndPoints'
|
||||
).and.returnValue(of({}));
|
||||
).mockReturnValue(of({}));
|
||||
|
||||
spyOn(parService, 'postParRequest').and.returnValue(
|
||||
vi.spyOn(parService, 'postParRequest').mockReturnValue(
|
||||
of({ requestUri: 'requestUri' } as ParResponse)
|
||||
);
|
||||
spyOn(urlService, 'getAuthorizeParUrl').and.returnValue('some-par-url');
|
||||
|
||||
const checkAuthSpy = spyOn(checkAuthService, 'checkAuth').and.returnValue(
|
||||
of({
|
||||
isAuthenticated: true,
|
||||
configId: 'configId1',
|
||||
idToken: '',
|
||||
userData: { any: 'userData' },
|
||||
accessToken: 'anyAccessToken',
|
||||
})
|
||||
vi.spyOn(urlService, 'getAuthorizeParUrl').mockReturnValue(
|
||||
'some-par-url'
|
||||
);
|
||||
|
||||
const checkAuthSpy = vi
|
||||
.spyOn(checkAuthService, 'checkAuth')
|
||||
.mockReturnValue(
|
||||
of({
|
||||
isAuthenticated: true,
|
||||
configId: 'configId1',
|
||||
idToken: '',
|
||||
userData: { any: 'userData' },
|
||||
accessToken: 'anyAccessToken',
|
||||
})
|
||||
);
|
||||
const popupResult: PopupResult = {
|
||||
userClosed: false,
|
||||
receivedUrl: 'someUrl',
|
||||
};
|
||||
|
||||
spyOnProperty(popupService, 'result$').and.returnValue(of(popupResult));
|
||||
vi.spyOnProperty(popupService, 'result$').mockReturnValue(
|
||||
of(popupResult)
|
||||
);
|
||||
|
||||
service.loginWithPopUpPar(config, allConfigs).subscribe((result) => {
|
||||
expect(checkAuthSpy).toHaveBeenCalledOnceWith(
|
||||
expect(checkAuthSpy).toHaveBeenCalledExactlyOnceWith(
|
||||
config,
|
||||
allConfigs,
|
||||
'someUrl'
|
||||
@@ -419,13 +436,13 @@ describe('ParLoginService', () => {
|
||||
accessToken: 'anyAccessToken',
|
||||
});
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('returns correct properties if popup was closed by user', waitForAsync(() => {
|
||||
spyOn(
|
||||
it('returns correct properties if popup was closed by user', async () => {
|
||||
vi.spyOn(
|
||||
responseTypeValidationService,
|
||||
'hasConfigValidResponseType'
|
||||
).and.returnValue(true);
|
||||
).mockReturnValue(true);
|
||||
const config = {
|
||||
authWellknownEndpointUrl: 'authWellknownEndpoint',
|
||||
responseType: 'stubValue',
|
||||
@@ -433,20 +450,24 @@ describe('ParLoginService', () => {
|
||||
};
|
||||
const allConfigs = [config];
|
||||
|
||||
spyOn(
|
||||
vi.spyOn(
|
||||
authWellKnownService,
|
||||
'queryAndStoreAuthWellKnownEndPoints'
|
||||
).and.returnValue(of({}));
|
||||
).mockReturnValue(of({}));
|
||||
|
||||
spyOn(parService, 'postParRequest').and.returnValue(
|
||||
vi.spyOn(parService, 'postParRequest').mockReturnValue(
|
||||
of({ requestUri: 'requestUri' } as ParResponse)
|
||||
);
|
||||
spyOn(urlService, 'getAuthorizeParUrl').and.returnValue('some-par-url');
|
||||
vi.spyOn(urlService, 'getAuthorizeParUrl').mockReturnValue(
|
||||
'some-par-url'
|
||||
);
|
||||
|
||||
const checkAuthSpy = spyOn(checkAuthService, 'checkAuth');
|
||||
const checkAuthSpy = vi.spyOn(checkAuthService, 'checkAuth');
|
||||
const popupResult = { userClosed: true } as PopupResult;
|
||||
|
||||
spyOnProperty(popupService, 'result$').and.returnValue(of(popupResult));
|
||||
vi.spyOnProperty(popupService, 'result$').mockReturnValue(
|
||||
of(popupResult)
|
||||
);
|
||||
|
||||
service.loginWithPopUpPar(config, allConfigs).subscribe((result) => {
|
||||
expect(checkAuthSpy).not.toHaveBeenCalled();
|
||||
@@ -459,6 +480,6 @@ describe('ParLoginService', () => {
|
||||
accessToken: '',
|
||||
});
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import { inject, Injectable } from 'injection-js';
|
||||
import { Observable, of, throwError } from 'rxjs';
|
||||
import { Injectable, inject } from 'injection-js';
|
||||
import { type Observable, of, throwError } from 'rxjs';
|
||||
import { switchMap, take } from 'rxjs/operators';
|
||||
import { AuthOptions } from '../../auth-options';
|
||||
import type { AuthOptions } from '../../auth-options';
|
||||
import { CheckAuthService } from '../../auth-state/check-auth.service';
|
||||
import { AuthWellKnownService } from '../../config/auth-well-known/auth-well-known.service';
|
||||
import { OpenIdConfiguration } from '../../config/openid-configuration';
|
||||
import type { OpenIdConfiguration } from '../../config/openid-configuration';
|
||||
import { LoggerService } from '../../logging/logger.service';
|
||||
import { RedirectService } from '../../utils/redirect/redirect.service';
|
||||
import { UrlService } from '../../utils/url/url.service';
|
||||
import { LoginResponse } from '../login-response';
|
||||
import { PopupOptions } from '../popup/popup-options';
|
||||
import { PopupResult } from '../popup/popup-result';
|
||||
import type { LoginResponse } from '../login-response';
|
||||
import type { PopupOptions } from '../popup/popup-options';
|
||||
import type { PopupResult } from '../popup/popup-result';
|
||||
import { PopUpService } from '../popup/popup.service';
|
||||
import { ResponseTypeValidationService } from '../response-type-validation/response-type-validation.service';
|
||||
import { ParResponse } from './par-response';
|
||||
import type { ParResponse } from './par-response';
|
||||
import { ParService } from './par.service';
|
||||
|
||||
@Injectable()
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { HttpHeaders } from '@angular/common/http';
|
||||
import { TestBed, waitForAsync } from '@angular/core/testing';
|
||||
import { TestBed } from '@/testing';
|
||||
import { HttpHeaders } from '@ngify/http';
|
||||
import { of, throwError } from 'rxjs';
|
||||
import { mockProvider } from '../../../test/auto-mock';
|
||||
import { createRetriableStream } from '../../../test/create-retriable-stream.helper';
|
||||
import { vi } from 'vitest';
|
||||
import { DataService } from '../../api/data.service';
|
||||
import { LoggerService } from '../../logging/logger.service';
|
||||
import { StoragePersistenceService } from '../../storage/storage-persistence.service';
|
||||
import { createRetriableStream } from '../../testing/create-retriable-stream.helper';
|
||||
import { mockProvider } from '../../testing/mock';
|
||||
import { UrlService } from '../../utils/url/url.service';
|
||||
import { ParService } from './par.service';
|
||||
|
||||
@@ -40,13 +41,15 @@ describe('ParService', () => {
|
||||
});
|
||||
|
||||
describe('postParRequest', () => {
|
||||
it('throws error if authWellKnownEndPoints does not exist in storage', waitForAsync(() => {
|
||||
spyOn(urlService, 'createBodyForParCodeFlowRequest').and.returnValue(
|
||||
it('throws error if authWellKnownEndPoints does not exist in storage', async () => {
|
||||
vi.spyOn(urlService, 'createBodyForParCodeFlowRequest').mockReturnValue(
|
||||
of(null)
|
||||
);
|
||||
spyOn(storagePersistenceService, 'read')
|
||||
.withArgs('authWellKnownEndPoints', { configId: 'configId1' })
|
||||
.and.returnValue(null);
|
||||
mockImplementationWhenArgsEqual(
|
||||
vi.spyOn(storagePersistenceService, 'read'),
|
||||
['authWellKnownEndPoints', { configId: 'configId1' }],
|
||||
() => null
|
||||
);
|
||||
service.postParRequest({ configId: 'configId1' }).subscribe({
|
||||
error: (err) => {
|
||||
expect(err.message).toBe(
|
||||
@@ -54,15 +57,17 @@ describe('ParService', () => {
|
||||
);
|
||||
},
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('throws error if par endpoint does not exist in storage', waitForAsync(() => {
|
||||
spyOn(urlService, 'createBodyForParCodeFlowRequest').and.returnValue(
|
||||
it('throws error if par endpoint does not exist in storage', async () => {
|
||||
vi.spyOn(urlService, 'createBodyForParCodeFlowRequest').mockReturnValue(
|
||||
of(null)
|
||||
);
|
||||
spyOn(storagePersistenceService, 'read')
|
||||
.withArgs('authWellKnownEndPoints', { configId: 'configId1' })
|
||||
.and.returnValue({ some: 'thing' });
|
||||
mockImplementationWhenArgsEqual(
|
||||
vi.spyOn(storagePersistenceService, 'read'),
|
||||
['authWellKnownEndPoints', { configId: 'configId1' }],
|
||||
() => ({ some: 'thing' })
|
||||
);
|
||||
service.postParRequest({ configId: 'configId1' }).subscribe({
|
||||
error: (err) => {
|
||||
expect(err.message).toBe(
|
||||
@@ -70,77 +75,87 @@ describe('ParService', () => {
|
||||
);
|
||||
},
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('calls data service with correct params', waitForAsync(() => {
|
||||
spyOn(urlService, 'createBodyForParCodeFlowRequest').and.returnValue(
|
||||
it('calls data service with correct params', async () => {
|
||||
vi.spyOn(urlService, 'createBodyForParCodeFlowRequest').mockReturnValue(
|
||||
of('some-url123')
|
||||
);
|
||||
spyOn(storagePersistenceService, 'read')
|
||||
.withArgs('authWellKnownEndPoints', { configId: 'configId1' })
|
||||
.and.returnValue({ parEndpoint: 'parEndpoint' });
|
||||
mockImplementationWhenArgsEqual(
|
||||
vi.spyOn(storagePersistenceService, 'read'),
|
||||
['authWellKnownEndPoints', { configId: 'configId1' }],
|
||||
() => ({ parEndpoint: 'parEndpoint' })
|
||||
);
|
||||
|
||||
const dataServiceSpy = spyOn(dataService, 'post').and.returnValue(of({}));
|
||||
const dataServiceSpy = vi
|
||||
.spyOn(dataService, 'post')
|
||||
.mockReturnValue(of({}));
|
||||
|
||||
service.postParRequest({ configId: 'configId1' }).subscribe(() => {
|
||||
expect(dataServiceSpy).toHaveBeenCalledOnceWith(
|
||||
expect(dataServiceSpy).toHaveBeenCalledExactlyOnceWith(
|
||||
'parEndpoint',
|
||||
'some-url123',
|
||||
{ configId: 'configId1' },
|
||||
jasmine.any(HttpHeaders)
|
||||
expect.any(HttpHeaders)
|
||||
);
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('Gives back correct object properties', waitForAsync(() => {
|
||||
spyOn(urlService, 'createBodyForParCodeFlowRequest').and.returnValue(
|
||||
it('Gives back correct object properties', async () => {
|
||||
vi.spyOn(urlService, 'createBodyForParCodeFlowRequest').mockReturnValue(
|
||||
of('some-url456')
|
||||
);
|
||||
spyOn(storagePersistenceService, 'read')
|
||||
.withArgs('authWellKnownEndPoints', { configId: 'configId1' })
|
||||
.and.returnValue({ parEndpoint: 'parEndpoint' });
|
||||
spyOn(dataService, 'post').and.returnValue(
|
||||
mockImplementationWhenArgsEqual(
|
||||
vi.spyOn(storagePersistenceService, 'read'),
|
||||
['authWellKnownEndPoints', { configId: 'configId1' }],
|
||||
() => ({ parEndpoint: 'parEndpoint' })
|
||||
);
|
||||
vi.spyOn(dataService, 'post').mockReturnValue(
|
||||
of({ expires_in: 123, request_uri: 'request_uri' })
|
||||
);
|
||||
service.postParRequest({ configId: 'configId1' }).subscribe((result) => {
|
||||
expect(result).toEqual({ expiresIn: 123, requestUri: 'request_uri' });
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('throws error if data service has got an error', waitForAsync(() => {
|
||||
spyOn(urlService, 'createBodyForParCodeFlowRequest').and.returnValue(
|
||||
it('throws error if data service has got an error', async () => {
|
||||
vi.spyOn(urlService, 'createBodyForParCodeFlowRequest').mockReturnValue(
|
||||
of('some-url789')
|
||||
);
|
||||
spyOn(storagePersistenceService, 'read')
|
||||
.withArgs('authWellKnownEndPoints', { configId: 'configId1' })
|
||||
.and.returnValue({ parEndpoint: 'parEndpoint' });
|
||||
spyOn(dataService, 'post').and.returnValue(
|
||||
mockImplementationWhenArgsEqual(
|
||||
vi.spyOn(storagePersistenceService, 'read'),
|
||||
['authWellKnownEndPoints', { configId: 'configId1' }],
|
||||
() => ({ parEndpoint: 'parEndpoint' })
|
||||
);
|
||||
vi.spyOn(dataService, 'post').mockReturnValue(
|
||||
throwError(() => new Error('ERROR'))
|
||||
);
|
||||
const loggerSpy = spyOn(loggerService, 'logError');
|
||||
const loggerSpy = vi.spyOn(loggerService, 'logError');
|
||||
|
||||
service.postParRequest({ configId: 'configId1' }).subscribe({
|
||||
error: (err) => {
|
||||
expect(err.message).toBe(
|
||||
'There was an error on ParService postParRequest'
|
||||
);
|
||||
expect(loggerSpy).toHaveBeenCalledOnceWith(
|
||||
expect(loggerSpy).toHaveBeenCalledExactlyOnceWith(
|
||||
{ configId: 'configId1' },
|
||||
'There was an error on ParService postParRequest',
|
||||
jasmine.any(Error)
|
||||
expect.any(Error)
|
||||
);
|
||||
},
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('should retry once', waitForAsync(() => {
|
||||
spyOn(urlService, 'createBodyForParCodeFlowRequest').and.returnValue(
|
||||
it('should retry once', async () => {
|
||||
vi.spyOn(urlService, 'createBodyForParCodeFlowRequest').mockReturnValue(
|
||||
of('some-url456')
|
||||
);
|
||||
spyOn(storagePersistenceService, 'read')
|
||||
.withArgs('authWellKnownEndPoints', { configId: 'configId1' })
|
||||
.and.returnValue({ parEndpoint: 'parEndpoint' });
|
||||
spyOn(dataService, 'post').and.returnValue(
|
||||
mockImplementationWhenArgsEqual(
|
||||
vi.spyOn(storagePersistenceService, 'read'),
|
||||
['authWellKnownEndPoints', { configId: 'configId1' }],
|
||||
() => ({ parEndpoint: 'parEndpoint' })
|
||||
);
|
||||
vi.spyOn(dataService, 'post').mockReturnValue(
|
||||
createRetriableStream(
|
||||
throwError(() => new Error('ERROR')),
|
||||
of({ expires_in: 123, request_uri: 'request_uri' })
|
||||
@@ -153,16 +168,18 @@ describe('ParService', () => {
|
||||
expect(res).toEqual({ expiresIn: 123, requestUri: 'request_uri' });
|
||||
},
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('should retry twice', waitForAsync(() => {
|
||||
spyOn(urlService, 'createBodyForParCodeFlowRequest').and.returnValue(
|
||||
it('should retry twice', async () => {
|
||||
vi.spyOn(urlService, 'createBodyForParCodeFlowRequest').mockReturnValue(
|
||||
of('some-url456')
|
||||
);
|
||||
spyOn(storagePersistenceService, 'read')
|
||||
.withArgs('authWellKnownEndPoints', { configId: 'configId1' })
|
||||
.and.returnValue({ parEndpoint: 'parEndpoint' });
|
||||
spyOn(dataService, 'post').and.returnValue(
|
||||
mockImplementationWhenArgsEqual(
|
||||
vi.spyOn(storagePersistenceService, 'read'),
|
||||
['authWellKnownEndPoints', { configId: 'configId1' }],
|
||||
() => ({ parEndpoint: 'parEndpoint' })
|
||||
);
|
||||
vi.spyOn(dataService, 'post').mockReturnValue(
|
||||
createRetriableStream(
|
||||
throwError(() => new Error('ERROR')),
|
||||
throwError(() => new Error('ERROR')),
|
||||
@@ -176,16 +193,18 @@ describe('ParService', () => {
|
||||
expect(res).toEqual({ expiresIn: 123, requestUri: 'request_uri' });
|
||||
},
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('should fail after three tries', waitForAsync(() => {
|
||||
spyOn(urlService, 'createBodyForParCodeFlowRequest').and.returnValue(
|
||||
it('should fail after three tries', async () => {
|
||||
vi.spyOn(urlService, 'createBodyForParCodeFlowRequest').mockReturnValue(
|
||||
of('some-url456')
|
||||
);
|
||||
spyOn(storagePersistenceService, 'read')
|
||||
.withArgs('authWellKnownEndPoints', { configId: 'configId1' })
|
||||
.and.returnValue({ parEndpoint: 'parEndpoint' });
|
||||
spyOn(dataService, 'post').and.returnValue(
|
||||
mockImplementationWhenArgsEqual(
|
||||
vi.spyOn(storagePersistenceService, 'read'),
|
||||
['authWellKnownEndPoints', { configId: 'configId1' }],
|
||||
() => ({ parEndpoint: 'parEndpoint' })
|
||||
);
|
||||
vi.spyOn(dataService, 'post').mockReturnValue(
|
||||
createRetriableStream(
|
||||
throwError(() => new Error('ERROR')),
|
||||
throwError(() => new Error('ERROR')),
|
||||
@@ -199,6 +218,6 @@ describe('ParService', () => {
|
||||
expect(err).toBeTruthy();
|
||||
},
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user