Skip to content

Commit

Permalink
refactor(api): move PasswordResetDemandNotFoundError to src
Browse files Browse the repository at this point in the history
Co-authored-by: Emmanuelle Bonnemay <emmanuelle.bonnemay@pix.fr>
  • Loading branch information
mariannebost and EmmanuelleBonnemay committed Jul 15, 2024
1 parent dabff64 commit 0645b2d
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 56 deletions.
3 changes: 0 additions & 3 deletions api/lib/application/error-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,6 @@ function _mapToHttpError(error) {
if (error instanceof DomainErrors.UserNotFoundError) {
return new HttpErrors.NotFoundError(error.message);
}
if (error instanceof DomainErrors.PasswordResetDemandNotFoundError) {
return new HttpErrors.NotFoundError(error.message);
}
if (error instanceof DomainErrors.AlreadyRegisteredEmailAndUsernameError) {
return new HttpErrors.BadRequestError(error.message);
}
Expand Down
16 changes: 1 addition & 15 deletions api/lib/domain/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,20 +671,6 @@ class FileValidationError extends DomainError {
}
}

class PasswordResetDemandNotFoundError extends DomainError {
constructor(message = "La demande de réinitialisation de mot de passe n'existe pas.") {
super(message);
}

getErrorMessage() {
return {
data: {
temporaryKey: ['Cette demande de réinitialisation n’existe pas.'],
},
};
}
}

// FIXME: used ?
class SessionWithIdAndInformationOnMassImportError extends DomainError {
constructor(message = 'Merci de ne pas renseigner les informations de session') {
Expand All @@ -709,6 +695,7 @@ class UserAlreadyLinkedToCandidateInSessionError extends DomainError {
super(message);
}
}

class UserNotAuthorizedToUpdateEmailError extends DomainError {
constructor(message = 'User is not authorized to update email') {
super(message);
Expand Down Expand Up @@ -1063,7 +1050,6 @@ export {
OrganizationNotFoundError,
OrganizationTagNotFound,
OrganizationWithoutEmailError,
PasswordResetDemandNotFoundError,
SendingEmailError,
SendingEmailToInvalidDomainError,
SendingEmailToInvalidEmailAddressError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MissingOrInvalidCredentialsError,
MissingUserAccountError,
PasswordNotMatching,
PasswordResetDemandNotFoundError,
UserCantBeCreatedError,
UserShouldChangePasswordError,
} from '../domain/errors.js';
Expand All @@ -32,6 +33,10 @@ const authenticationDomainErrorMappingConfiguration = [
name: PasswordNotMatching.name,
httpErrorFn: (error) => new HttpErrors.UnauthorizedError(error.message),
},
{
name: PasswordResetDemandNotFoundError.name,
httpErrorFn: (error) => new HttpErrors.NotFoundError(error.message),
},
{
name: UserCantBeCreatedError.name,
httpErrorFn: (error) => new HttpErrors.UnauthorizedError(error.message),
Expand Down
15 changes: 15 additions & 0 deletions api/src/identity-access-management/domain/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ class PasswordNotMatching extends DomainError {
}
}

class PasswordResetDemandNotFoundError extends DomainError {
constructor(message = "La demande de réinitialisation de mot de passe n'existe pas.") {
super(message);
}

getErrorMessage() {
return {
data: {
temporaryKey: ['Cette demande de réinitialisation n’existe pas.'],
},
};
}
}

class UserCantBeCreatedError extends DomainError {
constructor(message = "L'utilisateur ne peut pas être créé") {
super(message);
Expand All @@ -51,6 +65,7 @@ export {
MissingOrInvalidCredentialsError,
MissingUserAccountError,
PasswordNotMatching,
PasswordResetDemandNotFoundError,
UserCantBeCreatedError,
UserShouldChangePasswordError,
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { knex } from '../../../../db/knex-database-connection.js';
import { PasswordResetDemandNotFoundError } from '../../../../lib/domain/errors.js';
import { ResetPasswordDemand } from '../../../../lib/infrastructure/orm-models/ResetPasswordDemand.js';
import { PasswordResetDemandNotFoundError } from '../../domain/errors.js';
import { ResetPasswordDemand as ResetPasswordDemandModel } from '../../domain/models/ResetPasswordDemand.js';

const RESET_PASSWORD_DEMANDS_TABLE_NAME = 'reset-password-demands';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PasswordResetDemandNotFoundError, UserNotFoundError } from '../../../../../lib/domain/errors.js';
import { UserNotFoundError } from '../../../../../lib/domain/errors.js';
import { passwordController } from '../../../../../src/identity-access-management/application/password/password.controller.js';
import { identityAccessManagementRoutes } from '../../../../../src/identity-access-management/application/routes.js';
import { PasswordResetDemandNotFoundError } from '../../../../../src/identity-access-management/domain/errors.js';
import { usecases } from '../../../../../src/identity-access-management/domain/usecases/index.js';
import { InvalidTemporaryKeyError } from '../../../../../src/shared/domain/errors.js';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PasswordResetDemandNotFoundError, UserNotFoundError } from '../../../../../lib/domain/errors.js';
import { UserNotFoundError } from '../../../../../lib/domain/errors.js';
import { PasswordResetDemandNotFoundError } from '../../../../../src/identity-access-management/domain/errors.js';
import { User } from '../../../../../src/identity-access-management/domain/models/User.js';
import { resetPasswordService } from '../../../../../src/identity-access-management/domain/services/reset-password.service.js';
import { getUserByResetPasswordDemand } from '../../../../../src/identity-access-management/domain/usecases/get-user-by-reset-password-demand.usecase.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PasswordResetDemandNotFoundError } from '../../../../../lib/domain/errors.js';
import { PasswordResetDemandNotFoundError } from '../../../../../src/identity-access-management/domain/errors.js';
import { ResetPasswordDemand } from '../../../../../src/identity-access-management/domain/models/ResetPasswordDemand.js';
import { resetPasswordDemandRepository } from '../../../../../src/identity-access-management/infrastructure/repositories/reset-password-demand.repository.js';
import { catchErr, databaseBuilder, expect, knex } from '../../../../test-helper.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
MissingOrInvalidCredentialsError,
MissingUserAccountError,
PasswordNotMatching,
PasswordResetDemandNotFoundError,
UserCantBeCreatedError,
UserShouldChangePasswordError,
} from '../../../../src/identity-access-management/domain/errors.js';
Expand Down Expand Up @@ -99,6 +100,23 @@ describe('Unit | Identity Access Management | Application | HttpErrorMapperConfi
});
});

context('when mapping "PasswordResetDemandNotFoundError"', function () {
it('returns a NotFoundError Http Error', function () {
//given
const httpErrorMapper = authenticationDomainErrorMappingConfiguration.find(
(httpErrorMapper) => httpErrorMapper.name === PasswordResetDemandNotFoundError.name,
);
const message = 'Test message error';

//when
const error = httpErrorMapper.httpErrorFn(new PasswordResetDemandNotFoundError(message));

//then
expect(error).to.be.instanceOf(HttpErrors.NotFoundError);
expect(error.message).to.equal(message);
});
});

context('when mapping "UserCantBeCreatedError"', function () {
it('returns an UnauthorizedError Http Error', function () {
//given
Expand Down
20 changes: 20 additions & 0 deletions api/tests/identity-access-management/unit/domain/errors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { PasswordResetDemandNotFoundError } from '../../../../src/identity-access-management/domain/errors.js';
import { expect } from '../../../test-helper.js';

describe('Unit | Identity Access Management | Domain | Errors', function () {
describe('#PasswordResetDemandNotFoundError', function () {
it('has a getErrorMessage method', function () {
// given
const expectedErrorMessage = {
data: {
temporaryKey: ['Cette demande de réinitialisation n’existe pas.'],
},
};

// then
const error = new PasswordResetDemandNotFoundError();
expect(error.getErrorMessage).to.be.a('function');
expect(error.getErrorMessage()).to.eql(expectedErrorMessage);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PasswordResetDemandNotFoundError, UserNotFoundError } from '../../../../../lib/domain/errors.js';
import { UserNotFoundError } from '../../../../../lib/domain/errors.js';
import { PasswordResetDemandNotFoundError } from '../../../../../src/identity-access-management/domain/errors.js';
import { User } from '../../../../../src/identity-access-management/domain/models/User.js';
import { getUserByResetPasswordDemand } from '../../../../../src/identity-access-management/domain/usecases/get-user-by-reset-password-demand.usecase.js';
import { InvalidTemporaryKeyError } from '../../../../../src/shared/domain/errors.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PasswordResetDemandNotFoundError } from '../../../../../lib/domain/errors.js';
import { PasswordResetDemandNotFoundError } from '../../../../../src/identity-access-management/domain/errors.js';
import { User } from '../../../../../src/identity-access-management/domain/models/User.js';
import { updateUserPassword } from '../../../../../src/identity-access-management/domain/usecases/update-user-password.usecase.js';
import { UserNotAuthorizedToUpdatePasswordError } from '../../../../../src/shared/domain/errors.js';
Expand Down
12 changes: 0 additions & 12 deletions api/tests/integration/application/error-manager_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,6 @@ describe('Integration | API | Controller Error', function () {
expect(response.statusCode).to.equal(NOT_FOUND_ERROR);
expect(responseDetail(response)).to.equal('Ce compte est introuvable.');
});

it('responds Not Found when a PasswordResetDemandNotFoundError error occurs', async function () {
routeHandler.throws(
new DomainErrors.PasswordResetDemandNotFoundError(
"La demande de réinitialisation de mot de passe n'existe pas.",
),
);
const response = await server.requestObject(request);

expect(response.statusCode).to.equal(NOT_FOUND_ERROR);
expect(responseDetail(response)).to.equal("La demande de réinitialisation de mot de passe n'existe pas.");
});
});

context('409 Conflict', function () {
Expand Down
20 changes: 0 additions & 20 deletions api/tests/unit/domain/errors_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,6 @@ describe('Unit | Domain | Errors', function () {
});
});

describe('#PasswordResetDemandNotFoundError', function () {
it('should export a PasswordResetDemandNotFoundError', function () {
expect(errors.PasswordResetDemandNotFoundError).to.exist;
});

it('should have a getErrorMessage method', function () {
// given
const expectedErrorMessage = {
data: {
temporaryKey: ['Cette demande de réinitialisation n’existe pas.'],
},
};

// then
const error = new errors.PasswordResetDemandNotFoundError();
expect(error.getErrorMessage).to.be.a('function');
expect(error.getErrorMessage()).to.eql(expectedErrorMessage);
});
});

it('should export a TargetProfileInvalidError', function () {
expect(errors.TargetProfileInvalidError).to.exist;
});
Expand Down

0 comments on commit 0645b2d

Please sign in to comment.