Skip to content

Commit 93de2e6

Browse files
committed
fix(api): fix isUserExistingByEmail case insensitive user account search
1 parent 01734ec commit 93de2e6

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

api/src/identity-access-management/infrastructure/repositories/user.repository.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ const checkIfEmailIsAvailable = async function (email) {
265265
};
266266

267267
const isUserExistingByEmail = async function (email) {
268-
const existingUser = await knex('users').where('email', email.toLowerCase()).first();
268+
const existingUser = await knex('users').whereRaw('LOWER("email") = ?', email.toLowerCase()).first();
269269
if (!existingUser) throw new UserNotFoundError();
270270
return true;
271271
};

api/tests/identity-access-management/integration/domain/usecases/create-reset-password-demand.usecase.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@ describe('Integration | Identity Access Management | Domain | UseCase | create-r
2222
expect(resetPasswordDemand).to.exist;
2323
});
2424

25+
context('when a user account exists but with an email differing by case', function () {
26+
it('creates a reset password demand', async function () {
27+
// given
28+
const accountEmail = '[email protected]';
29+
const passwordResetDemandEmail = '[email protected]';
30+
const userId = databaseBuilder.factory.buildUser({ email: accountEmail }).id;
31+
databaseBuilder.factory.buildAuthenticationMethod.withPixAsIdentityProviderAndHashedPassword({ userId });
32+
await databaseBuilder.commit();
33+
34+
// when
35+
await usecases.createResetPasswordDemand({
36+
email: passwordResetDemandEmail,
37+
locale,
38+
});
39+
40+
// then
41+
const resetPasswordDemand = await knex('reset-password-demands')
42+
.whereRaw('LOWER("email") = LOWER(?)', passwordResetDemandEmail)
43+
.first();
44+
expect(resetPasswordDemand).to.exist;
45+
});
46+
});
47+
2548
context('when user account does not exist with given email', function () {
2649
it('does not create a reset password demand', async function () {
2750
// given

api/tests/identity-access-management/integration/infrastructure/repositories/user.repository.test.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -1816,33 +1816,38 @@ describe('Integration | Identity Access Management | Infrastructure | Repository
18161816
});
18171817

18181818
describe('#isUserExistingByEmail', function () {
1819-
const email = '[email protected]';
1819+
const email = '[email protected]';
18201820

18211821
beforeEach(function () {
18221822
databaseBuilder.factory.buildUser({ email });
18231823
databaseBuilder.factory.buildUser();
18241824
return databaseBuilder.commit();
18251825
});
18261826

1827-
it('returns true when the user exists by email', async function () {
1827+
it('finds a user with the exact email', async function () {
18281828
const userExists = await userRepository.isUserExistingByEmail(email);
18291829
expect(userExists).to.be.true;
18301830
});
18311831

1832-
it('returns true when the user exists by email (case insensitive)', async function () {
1833-
// given
1834-
const uppercaseEmailAlreadyInDb = email.toUpperCase();
1832+
context('when a user exists but with an email differing by case (case insensitive search)', function () {
1833+
it('finds the user', async function () {
1834+
// given
1835+
const uppercaseEmailAlreadyInDb = email.toUpperCase();
18351836

1836-
// when
1837-
const userExists = await userRepository.isUserExistingByEmail(uppercaseEmailAlreadyInDb);
1837+
// when
1838+
const userExists = await userRepository.isUserExistingByEmail(uppercaseEmailAlreadyInDb);
18381839

1839-
// then
1840-
expect(userExists).to.be.true;
1840+
// then
1841+
expect(userExists).to.be.true;
1842+
});
18411843
});
18421844

1843-
it('throws an error when the user does not exist by email', async function () {
1844-
const err = await catchErr(userRepository.isUserExistingByEmail)('none');
1845-
expect(err).to.be.instanceOf(UserNotFoundError);
1845+
context('when no user account with a matching email exist', function () {
1846+
it('throws an error', async function () {
1847+
const searchedEmail = '[email protected]';
1848+
const err = await catchErr(userRepository.isUserExistingByEmail)(searchedEmail);
1849+
expect(err).to.be.instanceOf(UserNotFoundError);
1850+
});
18461851
});
18471852
});
18481853

0 commit comments

Comments
 (0)