Skip to content

Commit

Permalink
fix(mon-pix): move GAR authentication context in Session storage
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot committed Dec 10, 2024
1 parent 9f75bd8 commit ac2e9a1
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 19 deletions.
24 changes: 11 additions & 13 deletions mon-pix/app/services/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import SessionService from 'ember-simple-auth/services/session';
import get from 'lodash/get';
import ENV from 'mon-pix/config/environment';
import { FRENCH_FRANCE_LOCALE, FRENCH_INTERNATIONAL_LOCALE } from 'mon-pix/services/locale';
import { SessionStorageKey } from 'mon-pix/utils/session-storage-key.js';

const FRANCE_TLD = 'fr';

const externalUserTokenFromGarStorage = new SessionStorageKey('externalUserTokenFromGar');
const userIdForLearnerAssociationStorage = new SessionStorageKey('userIdForLearnerAssociation');

export default class CurrentSessionService extends SessionService {
@service currentUser;
@service currentDomain;
Expand Down Expand Up @@ -79,34 +83,28 @@ export default class CurrentSessionService extends SessionService {
}

get externalUserTokenFromGar() {
return this.data.externalUser;
return externalUserTokenFromGarStorage.get();
}

set externalUserTokenFromGar(token) {
this.data.externalUser = token;
externalUserTokenFromGarStorage.set(token);
}

get userIdForLearnerAssociation() {
return this.data.expectedUserId;
return userIdForLearnerAssociationStorage.get();
}

set userIdForLearnerAssociation(userId) {
this.data.expectedUserId = userId;
userIdForLearnerAssociationStorage.set(userId);
}

revokeGarExternalUserToken() {
if (this.externalUserTokenFromGar) {
delete this.data.externalUser;
}
externalUserTokenFromGarStorage.remove();
}

revokeGarAuthenticationContext() {
if (this.userIdForLearnerAssociation) {
delete this.data.expectedUserId;
}
if (this.externalUserTokenFromGar) {
delete this.data.externalUser;
}
externalUserTokenFromGarStorage.remove();
userIdForLearnerAssociationStorage.remove();
}

async _loadCurrentUserAndSetLocale(locale = null) {
Expand Down
115 changes: 109 additions & 6 deletions mon-pix/tests/unit/services/session-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Service from '@ember/service';
import { setupTest } from 'ember-qunit';
import { DEFAULT_LOCALE, FRENCH_FRANCE_LOCALE, FRENCH_INTERNATIONAL_LOCALE } from 'mon-pix/services/locale';
import { SessionStorageKey } from 'mon-pix/utils/session-storage-key.js';
import { module, test } from 'qunit';
import sinon from 'sinon';

Expand Down Expand Up @@ -54,28 +55,28 @@ module('Unit | Services | session', function (hooks) {
assert.ok(true);
});

test('should delete expectedUserId', async function (assert) {
test('should delete userIdForLearnerAssociation', async function (assert) {
// given
sessionService.currentDomain.getExtension.returns(FRANCE_TLD);
sessionService.data.expectedUserId = 1;
sessionService.userIdForLearnerAssociation = 1;

// when
await sessionService.authenticateUser('user', 'secret');

// then
assert.notOk(sessionService.data.expectedUserId);
assert.notOk(sessionService.userIdForLearnerAssociation);
});

test('should delete externalUser', async function (assert) {
test('should delete externalUserTokenFromGar', async function (assert) {
// given
sessionService.currentDomain.getExtension.returns(FRANCE_TLD);
sessionService.data.externalUser = 1;
sessionService.externalUserTokenFromGar = 1;

// when
await sessionService.authenticateUser('user', 'secret');

// then
assert.notOk(sessionService.data.externalUser);
assert.notOk(sessionService.externalUserTokenFromGar);
});
});

Expand Down Expand Up @@ -318,4 +319,106 @@ module('Unit | Services | session', function (hooks) {
assert.deepEqual(sessionService.attemptedTransition, { from: 'campaigns.campaign-landing-page' });
});
});

module('#isAuthenticatedByGar', function () {
test('returns true if the external user token from gar is set', function (assert) {
// given
sessionService.externalUserTokenFromGar = '134';

// when
const isAuthenticatedByGar = sessionService.isAuthenticatedByGar;

// then
assert.ok(isAuthenticatedByGar);
});

test('returns false if the external user token from gar not is set', function (assert) {
// given
sessionService.externalUserTokenFromGar = null;

// when
const isAuthenticatedByGar = sessionService.isAuthenticatedByGar;

// then
assert.notOk(isAuthenticatedByGar);
});
});

module('#externalUserTokenFromGar', function () {
test('gets the external user token from the session storage', function (assert) {
// given
const sessionStorage = new SessionStorageKey('externalUserTokenFromGar');
sessionStorage.set('XXX');

// when
const externalUserTokenFromGar = sessionService.externalUserTokenFromGar;

// then
assert.strictEqual(externalUserTokenFromGar, 'XXX');
});

test('sets the external user token to the session storage', function (assert) {
// given
sessionService.externalUserTokenFromGar = 'XXX';

// when
const sessionStorage = new SessionStorageKey('externalUserTokenFromGar');

// then
assert.strictEqual(sessionStorage.get(), 'XXX');
});
});

module('#userIdForLearnerAssociation', function () {
test('gets the user id for leaner association from the session storage', function (assert) {
// given
const sessionStorage = new SessionStorageKey('userIdForLearnerAssociation');
sessionStorage.set(123);

// when
const userIdForLearnerAssociation = sessionService.userIdForLearnerAssociation;

// then
assert.strictEqual(userIdForLearnerAssociation, 123);
});

test('sets the user id for leaner association to the session storage', function (assert) {
// given
sessionService.userIdForLearnerAssociation = 123;

// when
const sessionStorage = new SessionStorageKey('userIdForLearnerAssociation');

// then
assert.strictEqual(sessionStorage.get(), 123);
});
});

module('#revokeGarExternalUserToken', function () {
test('removes the external user token from the session storage', function (assert) {
// given
sessionService.externalUserTokenFromGar = 'XXX';

// when
sessionService.revokeGarExternalUserToken();

// then
assert.notOk(sessionService.externalUserTokenFromGar);
});
});

module('#revokeGarAuthenticationContext', function () {
test('removes the external user token from the session storage', function (assert) {
// given
sessionService.externalUserTokenFromGar = 'XXX';
sessionService.userIdForLearnerAssociation = 123;

// when
sessionService.revokeGarAuthenticationContext();

// then
assert.notOk(sessionService.externalUserTokenFromGar);
assert.notOk(sessionService.userIdForLearnerAssociation);
});
});
});

0 comments on commit ac2e9a1

Please sign in to comment.