Skip to content

Commit

Permalink
refactor(api): replace updateExpiredPassword by updatePassword
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot committed Jan 30, 2025
1 parent 2853633 commit 4aff7b8
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 97 deletions.
2 changes: 1 addition & 1 deletion api/lib/domain/usecases/update-expired-password.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const updateExpiredPassword = async function ({

const hashedPassword = await cryptoService.hashPassword(newPassword);

await authenticationMethodRepository.updateExpiredPassword({
await authenticationMethodRepository.updatePassword({
userId: foundUser.id,
hashedPassword,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,27 +153,6 @@ const updatePassword = async function ({ userId, hashedPassword, shouldChangePas
return _toDomain(authenticationMethodDTO);
};

const updateExpiredPassword = async function ({ userId, hashedPassword }) {
const knexConn = DomainTransaction.getConnection();
const authenticationComplement = new AuthenticationMethod.PixAuthenticationComplement({
password: hashedPassword,
shouldChangePassword: false,
});

const [authenticationMethodDTO] = await knexConn(AUTHENTICATION_METHODS_TABLE)
.where({
userId,
identityProvider: NON_OIDC_IDENTITY_PROVIDERS.PIX.code,
})
.update({ authenticationComplement, updatedAt: new Date() })
.returning(COLUMNS);

if (!authenticationMethodDTO) {
throw new AuthenticationMethodNotFoundError(`Authentication method PIX for User ID ${userId} not found.`);
}
return _toDomain(authenticationMethodDTO);
};

const updateExternalIdentifierByUserIdAndIdentityProvider = async function ({
externalIdentifier,
userId,
Expand Down Expand Up @@ -271,7 +250,6 @@ const anonymizeByUserIds = async function ({ userIds }) {
* @property {function} updateAuthenticationComplementByUserIdAndIdentityProvider
* @property {function} updateAuthenticationMethodUserId
* @property {function} updatePassword
* @property {function} updateExpiredPassword
* @property {function} updateExternalIdentifierByUserIdAndIdentityProvider
*/
export {
Expand All @@ -289,7 +267,6 @@ export {
update,
updateAuthenticationComplementByUserIdAndIdentityProvider,
updateAuthenticationMethodUserId,
updateExpiredPassword,
updateExternalIdentifierByUserIdAndIdentityProvider,
updatePassword,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,77 +612,6 @@ describe('Integration | Identity Access Management | Infrastructure | Repository
});
});

describe('#updateExpiredPassword', function () {
let userId;
let clock;

beforeEach(async function () {
clock = sinon.useFakeTimers({ now: new Date('2020-01-02'), toFake: ['Date'] });
userId = databaseBuilder.factory.buildUser({ shouldChangePassword: true }).id;
databaseBuilder.factory.buildAuthenticationMethod.withPixAsIdentityProviderAndHashedPassword({
id: 123,
userId,
hashedPassword,
shouldChangePassword: true,
});
await databaseBuilder.commit();
});

afterEach(function () {
clock.restore();
});

it('should update the password in database and set shouldChangePassword to false', async function () {
// when
await authenticationMethodRepository.updateExpiredPassword({
userId,
hashedPassword: newHashedPassword,
});

// then
const [authenticationComplement] = await knex('authentication-methods')
.pluck('authenticationComplement')
.where({ id: 123 });
expect(authenticationComplement.password).to.equal(newHashedPassword);
expect(authenticationComplement.shouldChangePassword).to.be.false;
});

it('should return the updated AuthenticationMethod', async function () {
// given
const expectedAuthenticationMethod =
domainBuilder.buildAuthenticationMethod.withPixAsIdentityProviderAndHashedPassword({
id: 123,
userId,
hashedPassword: newHashedPassword,
shouldChangePassword: false,
updatedAt: new Date(),
});

// when
const updatedAuthenticationMethod = await authenticationMethodRepository.updateExpiredPassword({
userId,
hashedPassword: newHashedPassword,
});

// then
expect(updatedAuthenticationMethod).to.deepEqualInstance(expectedAuthenticationMethod);
});

it('should throw AuthenticationMethodNotFoundError when user id is not found', async function () {
// given
const wrongUserId = 0;

// when
const error = await catchErr(authenticationMethodRepository.updateExpiredPassword)({
userId: wrongUserId,
hashedPassword,
});

// then
expect(error).to.be.instanceOf(AuthenticationMethodNotFoundError);
});
});

describe('#updateAuthenticationComplementByUserIdAndIdentityProvider', function () {
context('When authentication method exists', function () {
let authenticationMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Unit | UseCase | update-expired-password', function () {
extractUserId: sinon.stub(),
};
authenticationMethodRepository = {
updateExpiredPassword: sinon.stub(),
updatePassword: sinon.stub(),
findOneByUserIdAndIdentityProvider: sinon.stub(),
};

Expand Down Expand Up @@ -63,7 +63,7 @@ describe('Unit | UseCase | update-expired-password', function () {
userId: user.id,
identityProvider: NON_OIDC_IDENTITY_PROVIDERS.PIX.code,
});
expect(authenticationMethodRepository.updateExpiredPassword).to.have.been.calledOnceWith({
expect(authenticationMethodRepository.updatePassword).to.have.been.calledOnceWith({
userId: user.id,
hashedPassword,
});
Expand Down

0 comments on commit 4aff7b8

Please sign in to comment.