Skip to content

Commit bc53da4

Browse files
committed
feat(api): save last logged at and identity provider in #reconcileOidcUser
1 parent 20cfbaa commit bc53da4

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

api/src/identity-access-management/domain/usecases/reconcile-oidc-user.usecase.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,34 @@ export const reconcileOidcUser = async function ({
6868
});
6969
}
7070

71+
await _updateUserLastConnection({
72+
userLoginRepository,
73+
userId,
74+
lastUserApplicationConnectionsRepository,
75+
requestedApplication,
76+
authenticationMethodRepository,
77+
oidcAuthenticationService,
78+
});
79+
80+
return { accessToken, logoutUrlUUID };
81+
};
82+
83+
async function _updateUserLastConnection({
84+
userLoginRepository,
85+
userId,
86+
lastUserApplicationConnectionsRepository,
87+
requestedApplication,
88+
authenticationMethodRepository,
89+
oidcAuthenticationService,
90+
}) {
7191
await userLoginRepository.updateLastLoggedAt({ userId });
7292
await lastUserApplicationConnectionsRepository.upsert({
7393
userId,
7494
application: requestedApplication.applicationName,
7595
lastLoggedAt: new Date(),
7696
});
77-
78-
return { accessToken, logoutUrlUUID };
79-
};
97+
return await authenticationMethodRepository.updateLastLoggedAtByIdentityProvider({
98+
userId,
99+
identityProvider: oidcAuthenticationService.identityProvider,
100+
});
101+
}

api/tests/identity-access-management/unit/domain/usecases/reconcile-oidc-user.usecase.test.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Unit | Identity Access Management | Domain | UseCase | reconcile-oidc-
2323

2424
beforeEach(function () {
2525
identityProvider = 'genericOidcProviderCode';
26-
authenticationMethodRepository = { create: sinon.stub() };
26+
authenticationMethodRepository = { create: sinon.stub(), updateLastLoggedAtByIdentityProvider: sinon.stub() };
2727
userLoginRepository = {
2828
updateLastLoggedAt: sinon.stub(),
2929
};
@@ -156,7 +156,7 @@ describe('Unit | Identity Access Management | Domain | UseCase | reconcile-oidc-
156156
);
157157
});
158158

159-
it('saves the last user application connection', async function () {
159+
it('saves the last user connection', async function () {
160160
// given
161161
const sessionContent = { idToken: 'idToken' };
162162
const externalIdentifier = 'external_id';
@@ -192,6 +192,11 @@ describe('Unit | Identity Access Management | Domain | UseCase | reconcile-oidc-
192192
application: 'app',
193193
lastLoggedAt: sinon.match.instanceOf(Date),
194194
});
195+
196+
expect(authenticationMethodRepository.updateLastLoggedAtByIdentityProvider).to.be.calledWithExactly({
197+
userId,
198+
identityProvider,
199+
});
195200
});
196201
});
197202

@@ -206,7 +211,7 @@ describe('Unit | Identity Access Management | Domain | UseCase | reconcile-oidc-
206211

207212
beforeEach(function () {
208213
identityProvider = POLE_EMPLOI.code;
209-
authenticationMethodRepository = { create: sinon.stub() };
214+
authenticationMethodRepository = { create: sinon.stub(), updateLastLoggedAtByIdentityProvider: sinon.stub() };
210215
userLoginRepository = {
211216
updateLastLoggedAt: sinon.stub(),
212217
};
@@ -303,13 +308,22 @@ describe('Unit | Identity Access Management | Domain | UseCase | reconcile-oidc-
303308
});
304309

305310
// then
306-
sinon.assert.calledOnce(oidcAuthenticationService.createAccessToken);
307-
sinon.assert.calledOnce(oidcAuthenticationService.saveIdToken);
308-
sinon.assert.calledOnceWithExactly(userLoginRepository.updateLastLoggedAt, { userId });
311+
expect(oidcAuthenticationService.createAccessToken).to.be.calledOnce;
312+
expect(oidcAuthenticationService.saveIdToken).to.be.calledOnce;
313+
expect(userLoginRepository.updateLastLoggedAt).to.have.been.calledWithExactly({ userId });
309314
expect(result).to.deep.equal({
310315
accessToken: 'accessToken',
311316
logoutUrlUUID: 'logoutUrlUUID',
312317
});
318+
expect(lastUserApplicationConnectionsRepository.upsert).to.be.calledWithExactly({
319+
userId,
320+
application: 'app',
321+
lastLoggedAt: sinon.match.instanceOf(Date),
322+
});
323+
expect(authenticationMethodRepository.updateLastLoggedAtByIdentityProvider).to.be.calledWithExactly({
324+
userId,
325+
identityProvider: oidcAuthenticationService.identityProvider,
326+
});
313327
});
314328
});
315329
});

0 commit comments

Comments
 (0)