Skip to content

Commit

Permalink
build: FORMS-1960 update keycloak-js (bcgov#1622)
Browse files Browse the repository at this point in the history
* build: FORMS-1960 update keycloak-js

Update the keycloak library so that we are using a more current version.

* fix: keycloak.createLoginUrl is now asynchronous

* test: fixed up the unit tests
  • Loading branch information
WalterMoar authored Feb 27, 2025
1 parent 0125e48 commit b626366
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 54 deletions.
37 changes: 5 additions & 32 deletions app/frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"fast-json-patch": "^3.1.1",
"font-awesome": "^4.7.0",
"formiojs": "4.17.4",
"keycloak-js": "^21.1.1",
"keycloak-js": "^26.2.0",
"leaflet": "^1.9.4",
"leaflet-draw": "^1.0.4",
"leaflet-geosearch": "^4.0.0",
Expand Down
9 changes: 5 additions & 4 deletions app/frontend/src/store/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export const useAuthStore = defineStore('auth', {
logoutUrl: undefined,
}),
getters: {
createLoginUrl: (state) => (options) =>
state.keycloak.createLoginUrl(options),
createLogoutUrl: (state) => (options) =>
state.keycloak.createLogoutUrl(options),
email: (state) =>
Expand Down Expand Up @@ -113,11 +111,14 @@ export const useAuthStore = defineStore('auth', {
},
},
actions: {
async createLoginUrl(options) {
return await this.keycloak.createLoginUrl(options);
},
updateKeycloak(keycloak, isAuthenticated) {
this.keycloak = keycloak;
this.authenticated = isAuthenticated;
},
login(idpHint) {
async login(idpHint) {
if (this.ready) {
if (!this.redirectUri) this.redirectUri = location.toString();

Expand All @@ -130,7 +131,7 @@ export const useAuthStore = defineStore('auth', {

if (options.idpHint) {
// Redirect to Keycloak if idpHint is available
window.location.replace(this.createLoginUrl(options));
window.location.replace(await this.createLoginUrl(options));
} else {
// Navigate to internal login page if no idpHint specified
const router = getRouter();
Expand Down
23 changes: 12 additions & 11 deletions app/frontend/tests/unit/store/modules/auth.actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('auth actions', () => {
formStore.$reset();
appStore.$reset();
mockStore.keycloak = {
createLoginUrl: vi.fn(() => 'about:blank'),
createLoginUrl: vi.fn().mockResolvedValue('about:blank'),
createLogoutUrl: vi.fn(() => 'about:blank'),
};
replaceSpy.mockReset();
Expand All @@ -37,49 +37,50 @@ describe('auth actions', () => {
appStore.config = { basePath: '/app' };
});

it('should do nothing if keycloak is not ready', () => {
it('should do nothing if keycloak is not ready', async () => {
mockStore.ready = false;
mockStore.login();

await mockStore.login();

expect(windowReplaceSpy).toHaveBeenCalledTimes(0);
});

it('should update redirectUri if not defined', () => {
it('should update redirectUri if not defined', async () => {
mockStore.ready = true;
mockStore.redirectUri = undefined;

mockStore.login('test');
await mockStore.login('test');

expect(windowReplaceSpy).toHaveBeenCalledTimes(1);
// Expecting location.toString() instead of 'about:blank'
expect(mockStore.redirectUri).toEqual(location.toString());
});

it('should not update redirectUri if already defined', () => {
it('should not update redirectUri if already defined', async () => {
mockStore.ready = true;
mockStore.redirectUri = 'value';

mockStore.login('test');
await mockStore.login('test');

expect(windowReplaceSpy).toHaveBeenCalledTimes(1);
expect(mockStore.redirectUri).toEqual('value');
});

it('should navigate with provided idpHint', () => {
it('should navigate with provided idpHint', async () => {
mockStore.ready = true;
mockStore.redirectUri = 'value';

mockStore.login('test');
await mockStore.login('test');

expect(windowReplaceSpy).toHaveBeenCalledTimes(1);
});

it('should navigate with pinia store idpHint', () => {
it('should navigate with pinia store idpHint', async () => {
mockStore.ready = true;
mockStore.redirectUri = undefined;
formStore.form = { idps: ['test'] };

mockStore.login();
await mockStore.login();

expect(replaceSpy).toHaveBeenCalledTimes(1);
expect(replaceSpy).toHaveBeenCalledWith({
Expand Down
6 changes: 0 additions & 6 deletions app/frontend/tests/unit/store/modules/auth.getters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ describe('auth getters', () => {
expect(store.authenticated).toBeTruthy();
});

it('createLoginUrl should return a string', () => {
expect(store.createLoginUrl).toBeTruthy();
expect(typeof store.createLoginUrl).toBe('function');
expect(store.createLoginUrl()).toMatch('loginUrl');
});

it('email should return a string', () => {
expect(store.email).toBeTruthy();
expect(store.email).toMatch('[email protected]');
Expand Down

0 comments on commit b626366

Please sign in to comment.