From 40e2176883bd66de64b82616d30d606170b7f2e2 Mon Sep 17 00:00:00 2001 From: Quentin Date: Tue, 8 Oct 2024 22:27:57 +0200 Subject: [PATCH] Upgrade keycloak-js to v26 --- .../domain/AngularOauth2ModuleFactory.java | 18 ------------------ .../client/angular/core/jest.conf.js.mustache | 2 +- .../client/angular/core/tsconfig.json | 2 +- .../auth/oauth2-auth.service.spec.ts.mustache | 11 +++++++++-- .../app/auth/oauth2-auth.service.ts.mustache | 17 ++++++++--------- .../generator/dependencies/common/package.json | 2 +- .../domain/AngularOauth2ModuleFactoryTest.java | 4 ---- 7 files changed, 20 insertions(+), 36 deletions(-) diff --git a/src/main/java/tech/jhipster/lite/generator/client/angular/security/oauth2/domain/AngularOauth2ModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/client/angular/security/oauth2/domain/AngularOauth2ModuleFactory.java index 56605042890..9e91a8b3e86 100644 --- a/src/main/java/tech/jhipster/lite/generator/client/angular/security/oauth2/domain/AngularOauth2ModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/client/angular/security/oauth2/domain/AngularOauth2ModuleFactory.java @@ -60,13 +60,6 @@ public class AngularOauth2ModuleFactory { private static final ElementReplacer MENU_NEEDLE = lineAfterRegex(""); - private static final String TEST_IMPORTS = - """ - import { By } from '@angular/platform-browser'; - import LoginComponent from './login/login.component';"""; - - private static final ElementReplacer TEST_NEEDLE = lineAfterRegex("^\\s+it\\('should have appName',[^}]+\\}\\);"); - private static final String LOGIN_IMPORT = "import LoginComponent from './login/login.component';"; private static final String OAUTH2_AUTH_SERVICE_IMPORT = @@ -93,13 +86,6 @@ public class AngularOauth2ModuleFactory { private static final ElementReplacer INJECT_NEEDLE = text("import { Component, OnInit, signal } from '@angular/core';"); - private static final String LOGIN_COMPONENT_TEST = - """ - - it('should display login component', () => { - expect(fixture.debugElement.query(By.directive(LoginComponent))).toBeTruthy(); - });"""; - private static final String HTTP_AUTH_INTERCEPTOR_IMPORT = "import { httpAuthInterceptor } from './app/auth/http-auth.interceptor';"; private static final JHipsterSource SOURCE = from("client/angular/security/oauth2/src/main/webapp/app"); @@ -156,10 +142,6 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) { .add(APPNAME_NEEDLE, INJECT_OAUTH2_AUTH_SERVICE) .add(lineAfterRegex("this.appName.set\\('" + properties.projectBaseName().name() + "'\\);"), INIT_AUTHENTICATION) .and() - .in(path("src/main/webapp/app/app.component.spec.ts")) - .add(lineAfterRegex("from '@angular/router';"), TEST_IMPORTS) - .add(TEST_NEEDLE, LOGIN_COMPONENT_TEST.indent(indentation.spacesCount() * 2)) - .and() .in(path("src/main/webapp/app/app.component.html")) .add(MENU_NEEDLE, indentation.spaces() + "") .and() diff --git a/src/main/resources/generator/client/angular/core/jest.conf.js.mustache b/src/main/resources/generator/client/angular/core/jest.conf.js.mustache index e7027f0af98..747e6c3c2ec 100644 --- a/src/main/resources/generator/client/angular/core/jest.conf.js.mustache +++ b/src/main/resources/generator/client/angular/core/jest.conf.js.mustache @@ -5,7 +5,7 @@ const { } = require('./tsconfig.json'); module.exports = { - transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$|dayjs/esm)'], + transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$|dayjs/esm|keycloak-js)'], resolver: 'jest-preset-angular/build/resolvers/ng-jest-resolver.js', roots: ['', `/${baseUrl}`], modulePaths: [`/${baseUrl}`], diff --git a/src/main/resources/generator/client/angular/core/tsconfig.json b/src/main/resources/generator/client/angular/core/tsconfig.json index 5e2f4145a75..9d7c06d9ce4 100644 --- a/src/main/resources/generator/client/angular/core/tsconfig.json +++ b/src/main/resources/generator/client/angular/core/tsconfig.json @@ -14,7 +14,7 @@ "esModuleInterop": true, "experimentalDecorators": true, "useDefineForClassFields": false, - "moduleResolution": "node", + "moduleResolution": "bundler", "importHelpers": true, "target": "ES2022", "module": "ES2022", diff --git a/src/main/resources/generator/client/angular/security/oauth2/src/main/webapp/app/auth/oauth2-auth.service.spec.ts.mustache b/src/main/resources/generator/client/angular/security/oauth2/src/main/webapp/app/auth/oauth2-auth.service.spec.ts.mustache index f3be4fbc74f..281b84e1f2e 100644 --- a/src/main/resources/generator/client/angular/security/oauth2/src/main/webapp/app/auth/oauth2-auth.service.spec.ts.mustache +++ b/src/main/resources/generator/client/angular/security/oauth2/src/main/webapp/app/auth/oauth2-auth.service.spec.ts.mustache @@ -1,7 +1,8 @@ import { TestBed } from '@angular/core/testing'; -import Keycloak, { KeycloakInitOptions } from 'keycloak-js'; +import Keycloak, { KeycloakConfig, KeycloakInitOptions } from 'keycloak-js'; import { lastValueFrom } from 'rxjs'; import { Oauth2AuthService } from './oauth2-auth.service'; +import { environment } from '../../environments/environment'; import SpyInstance = jest.SpyInstance; jest.mock('keycloak-js', () => ({ @@ -43,13 +44,19 @@ describe('Oauth2 Auth Service', () => { let consoleDebugMock: SpyInstance; let consoleErrorMock: SpyInstance; + const config: KeycloakConfig = { + url: environment.keycloak.url, + realm: environment.keycloak.realm, + clientId: environment.keycloak.client_id, + }; + beforeEach(() => { TestBed.configureTestingModule({ imports: [], }); service = TestBed.inject(Oauth2AuthService); - keycloakInstance = new Keycloak(); + keycloakInstance = new Keycloak(config); consoleDebugMock = jest.spyOn(console, 'debug').mockImplementation(); consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(); diff --git a/src/main/resources/generator/client/angular/security/oauth2/src/main/webapp/app/auth/oauth2-auth.service.ts.mustache b/src/main/resources/generator/client/angular/security/oauth2/src/main/webapp/app/auth/oauth2-auth.service.ts.mustache index 6af3fdf1e75..1940cfcd92f 100644 --- a/src/main/resources/generator/client/angular/security/oauth2/src/main/webapp/app/auth/oauth2-auth.service.ts.mustache +++ b/src/main/resources/generator/client/angular/security/oauth2/src/main/webapp/app/auth/oauth2-auth.service.ts.mustache @@ -7,7 +7,13 @@ const MIN_TOKEN_VALIDITY_SECONDS = 70; @Injectable({ providedIn: 'root' }) export class Oauth2AuthService { - private keycloak = new Keycloak(); + config: KeycloakConfig = { + url: environment.keycloak.url, + realm: environment.keycloak.realm, + clientId: environment.keycloak.client_id, + }; + + private readonly keycloak = new Keycloak(this.config); get token(): string | undefined { return this.keycloak.token; @@ -18,14 +24,7 @@ export class Oauth2AuthService { } initAuthentication(): Observable { - const config: KeycloakConfig = { - url: environment.keycloak.url, - realm: environment.keycloak.realm, - clientId: environment.keycloak.client_id, - }; - this.keycloak = new Keycloak(config); - - return from(this.keycloak.init({ onLoad: 'login-required', checkLoginIframe: false })).pipe( + return from(this.keycloak.init({ onLoad: 'login-required', checkLoginIframe: false })).pipe( tap(authenticated => { if (!authenticated) { window.location.reload(); diff --git a/src/main/resources/generator/dependencies/common/package.json b/src/main/resources/generator/dependencies/common/package.json index b6cefa75584..40ce4f4e239 100644 --- a/src/main/resources/generator/dependencies/common/package.json +++ b/src/main/resources/generator/dependencies/common/package.json @@ -29,7 +29,7 @@ "i18next-http-backend": "2.6.2", "jest": "29.7.0", "jsdom": "25.0.1", - "keycloak-js": "25.0.6", + "keycloak-js": "26.0.0", "lint-staged": "15.2.10", "mkdirp": "3.0.1", "node": "20.18.0", diff --git a/src/test/java/tech/jhipster/lite/generator/client/angular/security/oauth2/domain/AngularOauth2ModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/client/angular/security/oauth2/domain/AngularOauth2ModuleFactoryTest.java index 1334687bff9..95cb398a6b4 100644 --- a/src/test/java/tech/jhipster/lite/generator/client/angular/security/oauth2/domain/AngularOauth2ModuleFactoryTest.java +++ b/src/test/java/tech/jhipster/lite/generator/client/angular/security/oauth2/domain/AngularOauth2ModuleFactoryTest.java @@ -81,10 +81,6 @@ private static JHipsterModuleAsserter assertAngularOAuthModule(ModuleFile angula .containing("") .and() .hasFile("src/main/webapp/app/app.component.spec.ts") - .containing("import { By } from '@angular/platform-browser';") - .containing("import LoginComponent from './login/login.component';") - .containing("LoginComponent") - .containing("it('should display login component', () => {") .and(); }