Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2312 - Fix empêcher la suppression d'un valideur d'une structure d'accompagnement #2315

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
AgencyDto,
AgencyId,
AgencyRight,
InclusionConnectedUser,
OAuthGatewayProvider,
RemoveAgencyUserParams,
Expand Down Expand Up @@ -33,7 +33,7 @@ const getUserAndThrowIfNotFound = async (
const getUserAgencyRightsAndThrowIfUserHasNoAgencyRight = (
user: InclusionConnectedUser,
agencyId: AgencyId,
): AgencyDto => {
): AgencyRight => {
const userRight = user.agencyRights.find(
(agencyRight) => agencyRight.agency.id === agencyId,
);
Expand All @@ -44,7 +44,17 @@ const getUserAgencyRightsAndThrowIfUserHasNoAgencyRight = (
userId: user.id,
});

return userRight.agency;
return userRight;
};

const throwIfUserIsValidatorAndAgencyHasRefersTo = (
agencyRight: AgencyRight,
) => {
const agency = agencyRight.agency;

if (agency.refersToAgencyId && agencyRight.roles.includes("validator")) {
throw errors.agency.invalidUserRemovalWhenAgencyWithRefersTo(agency.id);
}
};

export const makeRemoveUserFromAgency = createTransactionalUseCase<
Expand All @@ -64,10 +74,13 @@ export const makeRemoveUserFromAgency = createTransactionalUseCase<
inputParams.userId,
provider,
);
const agency = getUserAgencyRightsAndThrowIfUserHasNoAgencyRight(
const agencyRight = getUserAgencyRightsAndThrowIfUserHasNoAgencyRight(
requestedUser,
inputParams.agencyId,
);
const agency = agencyRight.agency;

throwIfUserIsValidatorAndAgencyHasRefersTo(agencyRight);
await throwIfAgencyDontHaveOtherValidatorsReceivingNotifications(
uow,
agency,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,17 @@ describe("RemoveUserFromAgency", () => {
);
});

it("throws forbidden if user to delete has not rights on agency", async () => {
const inputParams: RemoveAgencyUserParams = {
agencyId: agency.id,
userId: notAdminUser.id,
};

expectPromiseToFailWithError(
removeUserFromAgency.execute(inputParams, backofficeAdminUser),
errors.user.expectedRightsOnAgency(inputParams),
);
});

it("throws forbidden if user to delete is the last validator receiving notifications", async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test en doublon

it("throws bad request if user attempts to delete validator when agency has refersTo", async () => {
const agencyWithRefersTo = new AgencyDtoBuilder()
.withId("agency-with-refers-to-id")
.withRefersToAgencyId(agency.id)
.withCounsellorEmails([notAdminUser.email])
.build();
const initialAgencyRights: AgencyRight[] = [
{
agency,
agency: agencyWithRefersTo,
roles: ["validator"],
isNotifiedByEmail: true,
isNotifiedByEmail: false,
},
];
const user: InclusionConnectedUser = {
Expand All @@ -122,15 +115,31 @@ describe("RemoveUserFromAgency", () => {
establishments: {},
},
};
agencyRepository.insert(agencyWithRefersTo);
userRepository.setInclusionConnectedUsers([user]);

const inputParams: RemoveAgencyUserParams = {
agencyId: agencyWithRefersTo.id,
userId: user.id,
};

expectPromiseToFailWithError(
removeUserFromAgency.execute(inputParams, backofficeAdminUser),
errors.agency.invalidUserRemovalWhenAgencyWithRefersTo(
agencyWithRefersTo.id,
),
);
});

it("throws forbidden if user to delete has not rights on agency", async () => {
const inputParams: RemoveAgencyUserParams = {
agencyId: agency.id,
userId: notAdminUser.id,
};

expectPromiseToFailWithError(
removeUserFromAgency.execute(inputParams, backofficeAdminUser),
errors.agency.notEnoughValidators(inputParams),
errors.user.expectedRightsOnAgency(inputParams),
);
});

Expand Down Expand Up @@ -173,7 +182,7 @@ describe("RemoveUserFromAgency", () => {
const initialAgencyRights: AgencyRight[] = [
{
agency: agencyWithRefersTo,
roles: ["validator"],
roles: ["counsellor"],
isNotifiedByEmail: true,
},
];
Expand Down
5 changes: 5 additions & 0 deletions front/src/app/components/agency/AgencyUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ export const AgencyUsers = ({ agency }: AgencyUsersProperties) => {
{
children: "Supprimer",
priority: "secondary",
disabled:
agency.refersToAgencyId !== null &&
agencyUser.agencyRights[agency.id].roles[0].includes(
"validator",
),
id: `${domElementIds.admin.agencyTab.editAgencyRemoveUserButton}-${agency.id}-${index}`,
onClick: () => {
dispatch(feedbackSlice.actions.clearFeedbacksTriggered());
Expand Down
5 changes: 5 additions & 0 deletions shared/src/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ export const errors = {
new BadRequestError(
`Le role "${role}" n'est pas autorisé pour l'agence "${agencyId}" car cette agence est une structure d'accompagnement.`,
),

invalidUserRemovalWhenAgencyWithRefersTo: (agencyId: AgencyId) =>
new BadRequestError(
`La suppression d'un valideur n'est pas autorisée pour l'agence "${agencyId}" car cette agence est une structure d'accompagnement.`,
),
},
user: {
unauthorized: () => new UnauthorizedError(),
Expand Down
Loading