Skip to content
Snippets Groups Projects
Commit b4a728b6 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-4321 impl PR comments

parent cc716ee6
No related branches found
No related tags found
No related merge requests found
Showing
with 88 additions and 69 deletions
import { ApiRootResource, ApiRootService } from '@alfa-client/api-root-shared'; import { ApiRootResource, ApiRootService } from '@alfa-client/api-root-shared';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { AuthService } from '../common/auth/auth.service';
import { StateResource } from '@alfa-client/tech-shared'; import { StateResource } from '@alfa-client/tech-shared';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { AuthenticationService } from 'libs/authentication/src/lib/authentication.service';
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
...@@ -16,13 +16,13 @@ export class AppComponent implements OnInit { ...@@ -16,13 +16,13 @@ export class AppComponent implements OnInit {
public apiRootStateResource$: Observable<StateResource<ApiRootResource>>; public apiRootStateResource$: Observable<StateResource<ApiRootResource>>;
constructor( constructor(
public authService: AuthService, public authenticationService: AuthenticationService,
private apiRootService: ApiRootService, private apiRootService: ApiRootService,
private router: Router, private router: Router,
) {} ) {}
ngOnInit(): void { ngOnInit(): void {
this.authService.login().then(() => this.doAfterLoggedIn()); this.authenticationService.login().then(() => this.doAfterLoggedIn());
} }
doAfterLoggedIn(): void { doAfterLoggedIn(): void {
......
...@@ -20,7 +20,7 @@ import { UserProfileButtonContainerComponent } from '../common/user-profile-butt ...@@ -20,7 +20,7 @@ import { UserProfileButtonContainerComponent } from '../common/user-profile-butt
import { AdminSettingsModule } from '@admin-client/admin-settings'; import { AdminSettingsModule } from '@admin-client/admin-settings';
import { PostfachNavigationItemComponent } from '../pages/postfach/postfach-navigation-item/postfach-navigation-item.component'; import { PostfachNavigationItemComponent } from '../pages/postfach/postfach-navigation-item/postfach-navigation-item.component';
import { OAuthModule } from 'angular-oauth2-oidc'; import { OAuthModule } from 'angular-oauth2-oidc';
import { HttpUnauthorizedInterceptor } from '../common/interceptor/http-unauthorized.interceptor'; import { HttpUnauthorizedInterceptor } from 'libs/authentication/src/lib/http-unauthorized.interceptor';
@NgModule({ @NgModule({
declarations: [ declarations: [
......
<div class="dropdown"> <div class="dropdown">
<button (click)="showDropDown()" class="dropbtn" data-test-id="drop-down-button">{{ currentUserInitials }}</button> <button (click)="showDropDown()" class="dropbtn" data-test-id="drop-down-button">
{{ currentUserInitials }}
</button>
<div id="myDropdown" class="dropdown-content"> <div id="myDropdown" class="dropdown-content">
<span style="cursor: pointer" (click)="authService.logout()" data-test-id="logout">Abmelden</span> <span style="cursor: pointer" (click)="authenticationService.logout()" data-test-id="logout"
>Abmelden</span
>
</div> </div>
</div> </div>
...@@ -6,15 +6,15 @@ import { ...@@ -6,15 +6,15 @@ import {
} from '@alfa-client/test-utils'; } from '@alfa-client/test-utils';
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { AuthService } from '../auth/auth.service';
import { UserProfileButtonContainerComponent } from './user-profile.button-container.component'; import { UserProfileButtonContainerComponent } from './user-profile.button-container.component';
import { getDataTestIdOf } from 'libs/tech-shared/test/data-test'; import { getDataTestIdOf } from 'libs/tech-shared/test/data-test';
import { AuthenticationService } from 'authentication';
describe('UserProfileButtonContainerComponent', () => { describe('UserProfileButtonContainerComponent', () => {
let component: UserProfileButtonContainerComponent; let component: UserProfileButtonContainerComponent;
let fixture: ComponentFixture<UserProfileButtonContainerComponent>; let fixture: ComponentFixture<UserProfileButtonContainerComponent>;
const authService: Mock<AuthService> = mock(AuthService); const authenticationService: Mock<AuthenticationService> = mock(AuthenticationService);
const dropDownButton: string = getDataTestIdOf('drop-down-button'); const dropDownButton: string = getDataTestIdOf('drop-down-button');
const logout: string = getDataTestIdOf('logout'); const logout: string = getDataTestIdOf('logout');
...@@ -25,8 +25,8 @@ describe('UserProfileButtonContainerComponent', () => { ...@@ -25,8 +25,8 @@ describe('UserProfileButtonContainerComponent', () => {
imports: [RouterTestingModule], imports: [RouterTestingModule],
providers: [ providers: [
{ {
provide: AuthService, provide: AuthenticationService,
useValue: authService, useValue: authenticationService,
}, },
], ],
}).compileComponents(); }).compileComponents();
...@@ -46,7 +46,7 @@ describe('UserProfileButtonContainerComponent', () => { ...@@ -46,7 +46,7 @@ describe('UserProfileButtonContainerComponent', () => {
it('should call authService to get current user initials', () => { it('should call authService to get current user initials', () => {
component.ngOnInit(); component.ngOnInit();
expect(authService.getCurrentUserInitials).toHaveBeenCalled(); expect(authenticationService.getCurrentUserInitials).toHaveBeenCalled();
}); });
}); });
...@@ -74,7 +74,7 @@ describe('UserProfileButtonContainerComponent', () => { ...@@ -74,7 +74,7 @@ describe('UserProfileButtonContainerComponent', () => {
it('should call authService logout', () => { it('should call authService logout', () => {
dispatchEventFromFixture(fixture, logout, 'click'); dispatchEventFromFixture(fixture, logout, 'click');
expect(authService.logout).toHaveBeenCalled(); expect(authenticationService.logout).toHaveBeenCalled();
}); });
}); });
}); });
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { AuthService } from '../auth/auth.service'; import { AuthenticationService } from 'libs/authentication/src/lib/authentication.service';
@Component({ @Component({
selector: 'user-profile-button-container', selector: 'user-profile-button-container',
...@@ -9,10 +9,10 @@ import { AuthService } from '../auth/auth.service'; ...@@ -9,10 +9,10 @@ import { AuthService } from '../auth/auth.service';
export class UserProfileButtonContainerComponent implements OnInit { export class UserProfileButtonContainerComponent implements OnInit {
public currentUserInitials: string; public currentUserInitials: string;
constructor(public authService: AuthService) {} constructor(public authenticationService: AuthenticationService) {}
ngOnInit(): void { ngOnInit(): void {
this.currentUserInitials = this.authService.getCurrentUserInitials(); this.currentUserInitials = this.authenticationService.getCurrentUserInitials();
} }
public showDropDown(): void { public showDropDown(): void {
......
export * from './lib/authentication.module'; export * from './lib/authentication.module';
export * from './lib/authentication.service';
import { AuthenticationModule } from './authentication.module';
import { TestBed } from '@angular/core/testing';
describe('AuthenticationModule', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [AuthenticationModule],
}).compileComponents();
});
it('should create', () => {
expect(AuthenticationModule).toBeDefined();
});
});
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { HttpUnauthorizedInterceptor } from './http-unauthorized.interceptor';
@NgModule({ @NgModule({
imports: [CommonModule], imports: [CommonModule],
providers: [HttpUnauthorizedInterceptor],
}) })
export class AuthenticationModule {} export class AuthenticationModule {}
import { Mock, mock, useFromMock } from '@alfa-client/test-utils'; import { Mock, mock, useFromMock } from '@alfa-client/test-utils';
import { AuthService } from './auth.service';
import { UserProfileResource } from '@alfa-client/user-profile-shared'; import { UserProfileResource } from '@alfa-client/user-profile-shared';
import { createUserProfileResource } from '../../../../../libs/user-profile-shared/test/user-profile'; import { createUserProfileResource } from '../../../../libs/user-profile-shared/test/user-profile';
import { AuthConfig, OAuthService } from 'angular-oauth2-oidc'; import { AuthConfig, OAuthService } from 'angular-oauth2-oidc';
import { fakeAsync, tick } from '@angular/core/testing'; import { fakeAsync, tick } from '@angular/core/testing';
import { createAuthConfig } from './auth.test'; import { AuthenticationService } from './authentication.service';
import { createAuthConfig } from '../../test/authentication.test';
import { createEnvironment } from '../../../environment-shared/test/environment';
describe('AuthService', () => { describe('AuthenticationService', () => {
let service: AuthService; let service: AuthenticationService;
let oAuthService: Mock<OAuthService>; let oAuthService: Mock<OAuthService>;
let environmentConfig; let environmentConfig;
...@@ -16,9 +17,9 @@ describe('AuthService', () => { ...@@ -16,9 +17,9 @@ describe('AuthService', () => {
...mock(OAuthService), ...mock(OAuthService),
loadDiscoveryDocumentAndLogin: jest.fn().mockResolvedValue(() => Promise.resolve()), loadDiscoveryDocumentAndLogin: jest.fn().mockResolvedValue(() => Promise.resolve()),
}; };
environmentConfig = {}; environmentConfig = createEnvironment();
service = new AuthService(useFromMock(oAuthService), environmentConfig); service = new AuthenticationService(useFromMock(oAuthService), environmentConfig);
}); });
describe('login', () => { describe('login', () => {
...@@ -46,13 +47,13 @@ describe('AuthService', () => { ...@@ -46,13 +47,13 @@ describe('AuthService', () => {
expect(oAuthService.setupAutomaticSilentRefresh).toHaveBeenCalled(); expect(oAuthService.setupAutomaticSilentRefresh).toHaveBeenCalled();
}); });
it('should set tokenValidator', () => { // it('should set tokenValidator', () => {
oAuthService.tokenValidationHandler = null; // oAuthService.tokenValidationHandler = null;
service.proceedWithLogin(Promise.resolve); // service.proceedWithLogin(Promise.resolve);
expect(oAuthService.tokenValidationHandler).not.toBeNull(); // expect(oAuthService.tokenValidationHandler).not.toBeNull();
}); // });
it('should load discovery document and login', () => { it('should load discovery document and login', () => {
service.proceedWithLogin(Promise.resolve); service.proceedWithLogin(Promise.resolve);
...@@ -89,8 +90,8 @@ describe('AuthService', () => { ...@@ -89,8 +90,8 @@ describe('AuthService', () => {
it('should update currentUser', () => { it('should update currentUser', () => {
service.setCurrentUser(); service.setCurrentUser();
expect(service.currentUserResource().firstName).toEqual('Marco'); expect(service.currentUserResource.firstName).toEqual('Marco');
expect(service.currentUserResource().lastName).toEqual('Polo'); expect(service.currentUserResource.lastName).toEqual('Polo');
}); });
}); });
...@@ -101,7 +102,7 @@ describe('AuthService', () => { ...@@ -101,7 +102,7 @@ describe('AuthService', () => {
firstName: 'Marco', firstName: 'Marco',
lastName: 'Polo', lastName: 'Polo',
}; };
service.currentUserResource.set(userProfile); service.currentUserResource = userProfile;
const currentUserInitials: string = service.getCurrentUserInitials(); const currentUserInitials: string = service.getCurrentUserInitials();
......
import { ENVIRONMENT_CONFIG, Environment } from '@alfa-client/environment-shared'; import { ENVIRONMENT_CONFIG, Environment } from '@alfa-client/environment-shared';
import { Inject, Injectable, WritableSignal, signal } from '@angular/core'; import { Inject, Injectable } from '@angular/core';
import { OAuthService, AuthConfig } from 'angular-oauth2-oidc'; import { OAuthService, AuthConfig } from 'angular-oauth2-oidc';
import { JwksValidationHandler } from 'angular-oauth2-oidc-jwks'; import { JwksValidationHandler } from 'angular-oauth2-oidc-jwks';
import { UserProfileResource } from 'libs/user-profile-shared/src/lib/user-profile.model'; import { UserProfileResource } from 'libs/user-profile-shared/src/lib/user-profile.model';
import { getUserNameInitials } from 'libs/user-profile-shared/src/lib/user-profile.util'; import { getUserNameInitials } from 'libs/user-profile-shared/src/lib/user-profile.util';
@Injectable({ providedIn: 'root' }) @Injectable({ providedIn: 'root' })
export class AuthService { export class AuthenticationService {
currentUserResource: WritableSignal<UserProfileResource> = signal<UserProfileResource>(undefined); currentUserResource: UserProfileResource;
constructor( constructor(
private oAuthService: OAuthService, private oAuthService: OAuthService,
...@@ -43,7 +43,7 @@ export class AuthService { ...@@ -43,7 +43,7 @@ export class AuthService {
scope: 'openid profile', scope: 'openid profile',
requireHttps: false, requireHttps: false,
responseType: 'code', responseType: 'code',
showDebugInformation: true, showDebugInformation: false,
}; };
} }
...@@ -53,11 +53,11 @@ export class AuthService { ...@@ -53,11 +53,11 @@ export class AuthService {
firstName: claims['given_name'], firstName: claims['given_name'],
lastName: claims['family_name'], lastName: claims['family_name'],
}; };
this.currentUserResource.set(userResource); this.currentUserResource = userResource;
} }
public getCurrentUserInitials(): string { public getCurrentUserInitials(): string {
return getUserNameInitials(this.currentUserResource()); return getUserNameInitials(this.currentUserResource);
} }
public logout(): void { public logout(): void {
......
...@@ -2,14 +2,14 @@ import { Mock, mock } from '@alfa-client/test-utils'; ...@@ -2,14 +2,14 @@ import { Mock, mock } from '@alfa-client/test-utils';
import { TestBed } from '@angular/core/testing'; import { TestBed } from '@angular/core/testing';
import { MatDialogModule } from '@angular/material/dialog'; import { MatDialogModule } from '@angular/material/dialog';
import { HttpUnauthorizedInterceptor } from './http-unauthorized.interceptor'; import { HttpUnauthorizedInterceptor } from './http-unauthorized.interceptor';
import { AuthService } from '../auth/auth.service';
import { HttpErrorResponse, HttpHandler, HttpRequest } from '@angular/common/http'; import { HttpErrorResponse, HttpHandler, HttpRequest } from '@angular/common/http';
import { Subject, isEmpty } from 'rxjs'; import { Subject, isEmpty } from 'rxjs';
import { AuthenticationService } from './authentication.service';
describe('HttpUnauthorizedInterceptor', () => { describe('HttpUnauthorizedInterceptor', () => {
let interceptor: HttpUnauthorizedInterceptor; let interceptor: HttpUnauthorizedInterceptor;
const authService: Mock<AuthService> = mock(AuthService); const authenticationService: Mock<AuthenticationService> = mock(AuthenticationService);
beforeEach(() => beforeEach(() =>
TestBed.configureTestingModule({ TestBed.configureTestingModule({
...@@ -17,8 +17,8 @@ describe('HttpUnauthorizedInterceptor', () => { ...@@ -17,8 +17,8 @@ describe('HttpUnauthorizedInterceptor', () => {
providers: [ providers: [
HttpUnauthorizedInterceptor, HttpUnauthorizedInterceptor,
{ {
provide: AuthService, provide: AuthenticationService,
useValue: authService, useValue: authenticationService,
}, },
], ],
}), }),
...@@ -56,7 +56,7 @@ describe('HttpUnauthorizedInterceptor', () => { ...@@ -56,7 +56,7 @@ describe('HttpUnauthorizedInterceptor', () => {
it('should call logout on authService ', () => { it('should call logout on authService ', () => {
interceptor.handleError(unauthorizedError); interceptor.handleError(unauthorizedError);
expect(authService.logout).toHaveBeenCalled(); expect(authenticationService.logout).toHaveBeenCalled();
}); });
it('should return EMPTY', (done) => { it('should return EMPTY', (done) => {
......
...@@ -9,11 +9,11 @@ import { Injectable } from '@angular/core'; ...@@ -9,11 +9,11 @@ import { Injectable } from '@angular/core';
import { isUnauthorized } from '@alfa-client/tech-shared'; import { isUnauthorized } from '@alfa-client/tech-shared';
import { EMPTY, Observable } from 'rxjs'; import { EMPTY, Observable } from 'rxjs';
import { catchError } from 'rxjs/operators'; import { catchError } from 'rxjs/operators';
import { AuthService } from '../auth/auth.service'; import { AuthenticationService } from './authentication.service';
@Injectable() @Injectable()
export class HttpUnauthorizedInterceptor implements HttpInterceptor { export class HttpUnauthorizedInterceptor implements HttpInterceptor {
constructor(private authService: AuthService) {} constructor(private authenticationService: AuthenticationService) {}
public intercept( public intercept(
request: HttpRequest<unknown>, request: HttpRequest<unknown>,
...@@ -26,7 +26,7 @@ export class HttpUnauthorizedInterceptor implements HttpInterceptor { ...@@ -26,7 +26,7 @@ export class HttpUnauthorizedInterceptor implements HttpInterceptor {
handleError(error: HttpErrorResponse): Observable<any> { handleError(error: HttpErrorResponse): Observable<any> {
if (isUnauthorized(error.status)) { if (isUnauthorized(error.status)) {
this.authService.logout(); this.authenticationService.logout();
return EMPTY; return EMPTY;
} }
throw error; throw error;
......
// @ts-expect-error https://thymikee.github.io/jest-preset-angular/docs/getting-started/test-environment
globalThis.ngJest = {
testEnvironmentOptions: {
errorOnUnknownElements: true,
errorOnUnknownProperties: true,
},
};
import 'jest-preset-angular/setup-jest'; import 'jest-preset-angular/setup-jest';
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';
getTestBed().resetTestEnvironment();
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {
teardown: { destroyAfterEach: false },
});
{ {
"compilerOptions": { "extends": "../../tsconfig.base.json",
"target": "es2022",
"useDefineForClassFields": false,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [], "files": [],
"include": [], "include": [],
"references": [ "references": [
...@@ -19,11 +10,7 @@ ...@@ -19,11 +10,7 @@
"path": "./tsconfig.spec.json" "path": "./tsconfig.spec.json"
} }
], ],
"extends": "../../tsconfig.base.json", "compilerOptions": {
"angularCompilerOptions": { "target": "es2020"
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
} }
} }
...@@ -8,5 +8,5 @@ ...@@ -8,5 +8,5 @@
"types": [] "types": []
}, },
"exclude": ["src/**/*.spec.ts", "src/test-setup.ts", "jest.config.ts", "src/**/*.test.ts"], "exclude": ["src/**/*.spec.ts", "src/test-setup.ts", "jest.config.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"] "include": ["src/**/*.ts", "test/authentication.test.ts"]
} }
...@@ -7,5 +7,11 @@ ...@@ -7,5 +7,11 @@
"types": ["jest", "node"] "types": ["jest", "node"]
}, },
"files": ["src/test-setup.ts"], "files": ["src/test-setup.ts"],
"include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"] "include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts",
"test/authentication.test.ts"
]
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment