Skip to content

Commit

Permalink
Upgrade keycloak-js to v26
Browse files Browse the repository at this point in the history
  • Loading branch information
qmonmert committed Oct 8, 2024
1 parent bf6057f commit 40e2176
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ public class AngularOauth2ModuleFactory {

private static final ElementReplacer MENU_NEEDLE = lineAfterRegex("<span.+id=\\\"menu-space-separator\\\".*></span>");

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 =
Expand All @@ -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");
Expand Down Expand Up @@ -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() + "<jhi-login></jhi-login>")
.and()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: ['<rootDir>', `<rootDir>/${baseUrl}`],
modulePaths: [`<rootDir>/${baseUrl}`],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"esModuleInterop": true,
"experimentalDecorators": true,
"useDefineForClassFields": false,
"moduleResolution": "node",
"moduleResolution": "bundler",
"importHelpers": true,
"target": "ES2022",
"module": "ES2022",
Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => ({
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,14 +24,7 @@ export class Oauth2AuthService {
}

initAuthentication(): Observable<boolean> {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ private static JHipsterModuleAsserter assertAngularOAuthModule(ModuleFile angula
.containing("<jhi-login></jhi-login>")
.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();
}

Expand Down

0 comments on commit 40e2176

Please sign in to comment.