Skip to content

Commit 26e8f32

Browse files
[BUGFIX] Le InMemoryTemporaryStorage ne suit pas la TemporaryStorage API et est utilisé à tort dans les tests d'intégration (PIX-12551)
#8954
2 parents 9a97cea + b2f47d6 commit 26e8f32

File tree

9 files changed

+48
-47
lines changed

9 files changed

+48
-47
lines changed

api/lib/infrastructure/temporary-storage/InMemoryTemporaryStorage.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,55 @@ class InMemoryTemporaryStorage extends TemporaryStorage {
1111
this._client = new NodeCache();
1212
}
1313

14-
save({ key, value, expirationDelaySeconds }) {
14+
async save({ key, value, expirationDelaySeconds }) {
1515
const storageKey = trim(key) || InMemoryTemporaryStorage.generateKey();
1616
this._client.set(storageKey, value, expirationDelaySeconds);
1717
return storageKey;
1818
}
1919

20-
update(key, value) {
20+
async update(key, value) {
2121
const storageKey = trim(key);
2222
const timeoutMs = this._client.getTtl(storageKey);
2323
const expirationDelaySeconds = timeoutMs === 0 ? 0 : (timeoutMs - Date.now()) / 1000;
2424
this._client.set(storageKey, value, expirationDelaySeconds);
2525
}
2626

27-
get(key) {
27+
async get(key) {
2828
return this._client.get(key);
2929
}
3030

31-
delete(key) {
31+
async delete(key) {
3232
return this._client.del(key);
3333
}
3434

3535
quit() {
3636
noop;
3737
}
3838

39-
expire({ key, expirationDelaySeconds }) {
39+
async expire({ key, expirationDelaySeconds }) {
4040
return this._client.ttl(key, expirationDelaySeconds);
4141
}
4242

43-
ttl(key) {
43+
async ttl(key) {
4444
return this._client.getTtl(key);
4545
}
4646

47-
lpush(key, value) {
47+
async lpush(key, value) {
4848
let list = this._client.get(key) || [];
4949
list = [value, ...list];
5050
this._client.set(key, list);
5151
return list.length;
5252
}
5353

54-
lrem(key, value) {
54+
async lrem(key, value) {
5555
const list = this._client.get(key) || [];
5656
const filtered = list.filter((item) => item !== value);
5757
const removed = list.filter((item) => item === value);
5858
this._client.set(key, filtered);
5959
return removed.length;
6060
}
6161

62-
lrange(key) {
62+
async lrange(key) {
6363
return this._client.get(key) || [];
6464
}
6565
}

api/lib/infrastructure/temporary-storage/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { config } from '../../config.js';
2-
const REDIS_URL = config.temporaryStorage.redisUrl;
2+
3+
const redisUrl = config.temporaryStorage.redisUrl;
34

45
import { InMemoryTemporaryStorage } from './InMemoryTemporaryStorage.js';
56
import { RedisTemporaryStorage } from './RedisTemporaryStorage.js';
67

78
function _createTemporaryStorage() {
8-
if (REDIS_URL) {
9-
return new RedisTemporaryStorage(REDIS_URL);
9+
if (redisUrl) {
10+
return new RedisTemporaryStorage(redisUrl);
1011
} else {
1112
return new InMemoryTemporaryStorage();
1213
}

api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
"test:api": "for testType in 'unit' 'integration' 'acceptance'; do npm run test:api:$testType || status=1 ; done ; exit $status",
160160
"test:api:path": "NODE_ENV=test mocha --exit --recursive --reporter=${MOCHA_REPORTER:-dot}",
161161
"test:api:scripts": "npm run test:api:path -- tests/integration/scripts",
162-
"test:api:unit": "TEST_DATABASE_URL=postgres://should.not.reach.db.in.unit.tests REDIS_URL= npm run test:api:path -- 'tests/**/unit/**/*test.js'",
162+
"test:api:unit": "TEST_DATABASE_URL=postgres://should.not.reach.db.in.unit.tests TEST_REDIS_URL= npm run test:api:path -- 'tests/**/unit/**/*test.js'",
163163
"test:api:integration": "npm run test:api:path -- 'tests/**/integration/**/*test.js'",
164164
"test:api:acceptance": "npm run test:api:path -- 'tests/**/acceptance/**/*test.js'",
165165
"test:api:debug": "NODE_ENV=test mocha --inspect-brk=9229 --recursive --exit --reporter dot tests",

api/src/identity-access-management/domain/services/fwb-oidc-authentication-service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class FwbOidcAuthenticationService extends OidcAuthenticationService {
1919
async getRedirectLogoutUrl({ userId, logoutUrlUUID }) {
2020
const redirectTarget = new URL(this.logoutUrl);
2121
const key = `${userId}:${logoutUrlUUID}`;
22-
const idToken = this.sessionTemporaryStorage.get(key);
22+
const idToken = await this.sessionTemporaryStorage.get(key);
2323

2424
const params = [{ key: 'id_token_hint', value: idToken }];
2525

api/src/identity-access-management/domain/services/pole-emploi-oidc-authentication-service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class PoleEmploiOidcAuthenticationService extends OidcAuthenticationServi
5151
async getRedirectLogoutUrl({ userId, logoutUrlUUID }) {
5252
const redirectTarget = new URL(this.logoutUrl);
5353
const key = `${userId}:${logoutUrlUUID}`;
54-
const idToken = this.sessionTemporaryStorage.get(key);
54+
const idToken = await this.sessionTemporaryStorage.get(key);
5555

5656
const params = [
5757
{ key: 'id_token_hint', value: idToken },

api/src/shared/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ const configuration = (function () {
393393

394394
config.temporaryKey.secret = 'the-password-must-be-at-least-32-characters-long';
395395

396-
config.temporaryStorage.redisUrl = null;
396+
config.temporaryStorage.redisUrl = process.env.TEST_REDIS_URL;
397397

398398
config.saml.accessTokenLifespanMs = 1000;
399399

@@ -448,6 +448,7 @@ const configuration = (function () {
448448
config.logging.enableLogStartingEventDispatch = false;
449449
config.logging.enableLogEndingEventDispatch = false;
450450

451+
// TODO: Rather use config.caching.redisUrl = process.env.TEST_REDIS_URL;
451452
config.caching.redisUrl = null;
452453
config.caching.redisCacheKeyLockTTL = 100;
453454
config.caching.redisCacheLockedWaitBeforeRetry = 1;
@@ -456,7 +457,6 @@ const configuration = (function () {
456457

457458
config.redis = {
458459
url: process.env.TEST_REDIS_URL,
459-
database: 1,
460460
};
461461

462462
config.import = {

api/tests/identity-access-management/integration/domain/services/fwb-oidc-authentication-service_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Integration | Identity Access Management | Domain | Service | fwb-oidc
3232

3333
// then
3434
const expectedResult = await defaultSessionTemporaryStorage.get(key);
35-
expect(expectedResult).to.be.undefined;
35+
expect(expectedResult).to.be.null;
3636

3737
expect(redirectTarget).to.equal(
3838
'https://logout-url.org/?id_token_hint=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',

api/tests/identity-access-management/integration/domain/services/pole-emploi-oidc-authentication-service_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('Integration | Identity Access Management | Domain | Services | pole-em
8787

8888
// then
8989
const expectedResult = await defaultSessionTemporaryStorage.get(key);
90-
expect(expectedResult).to.be.undefined;
90+
expect(expectedResult).to.be.null;
9191

9292
expect(redirectTarget).to.equal(
9393
'https://logout-url.fr/?id_token_hint=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c&redirect_uri=https%3A%2F%2Fafter-logout.fr',

api/tests/unit/infrastructure/temporary-storage/InMemoryTemporaryStorage_test.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ describe('Unit | Infrastructure | temporary-storage | InMemoryTemporaryStorage',
1919
clock.restore();
2020
});
2121

22-
it('should resolve with the generated key', function () {
22+
it('should resolve with the generated key', async function () {
2323
// when
24-
const key = inMemoryTemporaryStorage.save({ value: {}, expirationDelaySeconds: 1000 });
24+
const key = await inMemoryTemporaryStorage.save({ value: {}, expirationDelaySeconds: 1000 });
2525

2626
// then
27-
expect(key).to.exist;
27+
expect(key).to.be.a.string;
2828
});
2929

30-
it('should return a key from passed key parameter if valid', function () {
30+
it('should return a key from passed key parameter if valid', async function () {
3131
// given
3232
const keyParameter = 'KEY-PARAMETER';
3333

3434
// when
35-
const returnedKey = inMemoryTemporaryStorage.save({
35+
const returnedKey = await inMemoryTemporaryStorage.save({
3636
key: keyParameter,
3737
value: {},
3838
expirationDelaySeconds: 1000,
@@ -42,12 +42,12 @@ describe('Unit | Infrastructure | temporary-storage | InMemoryTemporaryStorage',
4242
expect(returnedKey).to.be.equal(keyParameter);
4343
});
4444

45-
it('should return a generated key if key parameter is not valid', function () {
45+
it('should return a generated key if key parameter is not valid', async function () {
4646
// given
4747
const keyParameter = ' ';
4848

4949
// when
50-
const returnedKey = inMemoryTemporaryStorage.save({
50+
const returnedKey = await inMemoryTemporaryStorage.save({
5151
key: keyParameter,
5252
value: {},
5353
expirationDelaySeconds: 1000,
@@ -57,87 +57,87 @@ describe('Unit | Infrastructure | temporary-storage | InMemoryTemporaryStorage',
5757
expect(returnedKey).not.be.equal(keyParameter);
5858
});
5959

60-
it('should save key value with a defined ttl in seconds', function () {
60+
it('should save key value with a defined ttl in seconds', async function () {
6161
// given
6262
const TWO_MINUTES_IN_SECONDS = 2 * 60;
6363
const TWO_MINUTES_IN_MILLISECONDS = 2 * 60 * 1000;
6464

6565
// when
66-
const key = inMemoryTemporaryStorage.save({
66+
const key = await inMemoryTemporaryStorage.save({
6767
value: { name: 'name' },
6868
expirationDelaySeconds: TWO_MINUTES_IN_SECONDS,
6969
});
7070

7171
// then
72-
const expirationKeyInTimestamp = inMemoryTemporaryStorage._client.getTtl(key);
72+
const expirationKeyInTimestamp = await inMemoryTemporaryStorage._client.getTtl(key);
7373
expect(expirationKeyInTimestamp).to.equal(TWO_MINUTES_IN_MILLISECONDS);
7474
});
7575
});
7676

7777
describe('#get', function () {
78-
it('should retrieve the value if it exists', function () {
78+
it('should retrieve the value if it exists', async function () {
7979
// given
8080
const value = { name: 'name' };
8181
const expirationDelaySeconds = 1000;
8282

83-
const key = inMemoryTemporaryStorage.save({ value, expirationDelaySeconds });
83+
const key = await inMemoryTemporaryStorage.save({ value, expirationDelaySeconds });
8484

8585
// when
86-
const result = inMemoryTemporaryStorage.get(key);
86+
const result = await inMemoryTemporaryStorage.get(key);
8787

8888
// then
8989
expect(result).to.deep.equal(value);
9090
});
9191
});
9292

9393
describe('#update', function () {
94-
it('should set a new value', function () {
94+
it('should set a new value', async function () {
9595
// given
96-
const key = inMemoryTemporaryStorage.save({
96+
const key = await inMemoryTemporaryStorage.save({
9797
value: { name: 'name' },
9898
});
9999

100100
// when
101-
inMemoryTemporaryStorage.update(key, { url: 'url' });
101+
await inMemoryTemporaryStorage.update(key, { url: 'url' });
102102

103103
// then
104-
const result = inMemoryTemporaryStorage.get(key);
104+
const result = await inMemoryTemporaryStorage.get(key);
105105
expect(result).to.deep.equal({ url: 'url' });
106106
});
107107

108108
it('should not change the time to live', async function () {
109109
// given
110-
const keyWithTtl = inMemoryTemporaryStorage.save({
110+
const keyWithTtl = await inMemoryTemporaryStorage.save({
111111
value: {},
112112
expirationDelaySeconds: 1,
113113
});
114-
const keyWithoutTtl = inMemoryTemporaryStorage.save({ value: {} });
114+
const keyWithoutTtl = await inMemoryTemporaryStorage.save({ value: {} });
115115

116116
// when
117117
await new Promise((resolve) => setTimeout(resolve, 500));
118-
inMemoryTemporaryStorage.update(keyWithTtl, {});
119-
inMemoryTemporaryStorage.update(keyWithoutTtl, {});
118+
await inMemoryTemporaryStorage.update(keyWithTtl, {});
119+
await inMemoryTemporaryStorage.update(keyWithoutTtl, {});
120120
await new Promise((resolve) => setTimeout(resolve, 600));
121121

122122
// then
123-
expect(inMemoryTemporaryStorage.get(keyWithTtl)).to.be.undefined;
124-
expect(inMemoryTemporaryStorage.get(keyWithoutTtl)).not.to.be.undefined;
123+
expect(await inMemoryTemporaryStorage.get(keyWithTtl)).to.be.undefined;
124+
expect(await inMemoryTemporaryStorage.get(keyWithoutTtl)).not.to.be.undefined;
125125
});
126126
});
127127

128128
describe('#delete', function () {
129-
it('should delete the value if it exists', function () {
129+
it('should delete the value if it exists', async function () {
130130
// given
131131
const value = { name: 'name' };
132132
const expirationDelaySeconds = 1000;
133133

134-
const key = inMemoryTemporaryStorage.save({ value, expirationDelaySeconds });
134+
const key = await inMemoryTemporaryStorage.save({ value, expirationDelaySeconds });
135135

136136
// when
137-
inMemoryTemporaryStorage.delete(key);
137+
await inMemoryTemporaryStorage.delete(key);
138138

139139
// then
140-
const savedKey = inMemoryTemporaryStorage.get(key);
140+
const savedKey = await inMemoryTemporaryStorage.get(key);
141141
expect(savedKey).to.be.undefined;
142142
});
143143
});
@@ -168,7 +168,7 @@ describe('Unit | Infrastructure | temporary-storage | InMemoryTemporaryStorage',
168168
const key = 'key:lpush';
169169
await inMemoryTemporaryStorage.lpush(key, 'value');
170170
await inMemoryTemporaryStorage.expire({ key, expirationDelaySeconds: 120 });
171-
const remainingExpirationSeconds = inMemoryTemporaryStorage.ttl(key);
171+
const remainingExpirationSeconds = await inMemoryTemporaryStorage.ttl(key);
172172

173173
// then
174174
expect(remainingExpirationSeconds).to.be.above(Date.now());

0 commit comments

Comments
 (0)