Skip to content

Commit

Permalink
[BUGFIX] Perte des infos GAR quand la page est rafraichie (PIX-12534)
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-service-auto-merge authored Dec 12, 2024
2 parents f175857 + 67067dc commit d0a68eb
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 19 deletions.
10 changes: 10 additions & 0 deletions mon-pix/app/authenticators/gar.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isEmpty } from '@ember/utils';
import BaseAuthenticator from 'ember-simple-auth/authenticators/base';
import { decodeToken } from 'mon-pix/helpers/jwt';
import RSVP from 'rsvp';
Expand All @@ -13,4 +14,13 @@ export default class GarAuthenticator extends BaseAuthenticator {
source,
});
}

restore(data) {
return new RSVP.Promise((resolve, reject) => {
if (!isEmpty(data['access_token'])) {
resolve(data);
}
reject();
});
}
}
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 { SessionStorageEntry } from 'mon-pix/utils/session-storage-entry.js';

const FRANCE_TLD = 'fr';

const externalUserTokenFromGarStorage = new SessionStorageEntry('externalUserTokenFromGar');
const userIdForLearnerAssociationStorage = new SessionStorageEntry('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
18 changes: 18 additions & 0 deletions mon-pix/app/utils/session-storage-entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export class SessionStorageEntry {
constructor(key) {
this.key = key;
}

set(value) {
sessionStorage.setItem(this.key, JSON.stringify({ value }));
}

get() {
const result = JSON.parse(sessionStorage.getItem(this.key)) || {};
return result.value;
}

remove() {
sessionStorage.removeItem(this.key);
}
}
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 { SessionStorageEntry } from 'mon-pix/utils/session-storage-entry.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 SessionStorageEntry('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 SessionStorageEntry('externalUserTokenFromGar');

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

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

// when
const userIdForLearnerAssociation = sessionService.userIdForLearnerAssociation;

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

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

// when
const sessionStorage = new SessionStorageEntry('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);
});
});
});
76 changes: 76 additions & 0 deletions mon-pix/tests/unit/utils/session-storage-entry-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { SessionStorageEntry } from 'mon-pix/utils/session-storage-entry';
import { module, test } from 'qunit';

module('Unit | Utilities | SessionStorageEntry', function () {
test('sets and gets a string value', function (assert) {
// given
const entry = new SessionStorageEntry('testKey');
const value = 'testValue';

// when
entry.set(value);

// then
assert.strictEqual(entry.get(), value);
});

test('sets and gets a number value', function (assert) {
// given
const entry = new SessionStorageEntry('numberKey');
const value = 123;

// when
entry.set(value);

// then
assert.strictEqual(entry.get(), value);
});

test('sets and gets a boolean value', function (assert) {
// given
const entry = new SessionStorageEntry('booleanKey');
const value = true;

// when
entry.set(value);

// then
assert.strictEqual(entry.get(), value);
});

test('sets and gets a null value', function (assert) {
// given
const entry = new SessionStorageEntry('nullKey');
const value = null;

// when
entry.set(value);

// then
assert.strictEqual(entry.get(), value);
});

test('returns undefined for a non-existing key', function (assert) {
// given
const entry = new SessionStorageEntry('nonExistingKey');

// when
const result = entry.get();

// then
assert.strictEqual(result, undefined);
});

test('removes a key', function (assert) {
// given
const entry = new SessionStorageEntry('testKey');
const value = 'testValue';
entry.set(value);

// when
entry.remove();

// then
assert.strictEqual(entry.get(), undefined);
});
});

0 comments on commit d0a68eb

Please sign in to comment.