Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
hakifran committed May 30, 2024
1 parent b98b55d commit 08d56e9
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 71 deletions.
4 changes: 2 additions & 2 deletions hat/assets/js/apps/Iaso/constants/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,14 @@ export const planningPath = {
baseUrl: baseUrls.planning,
routerUrl: `${baseUrls.planning}/*`,
// FIXME use planning permissions when they exist
permissions: [Permission.PLANNINGS],
permissions: [Permission.PLANNING_READ, Permission.PLANNING_WRITE],
element: <Planning />,
};
export const assignmentsPath = {
baseUrl: baseUrls.assignments,
routerUrl: `${baseUrls.assignments}/*`,
// FIXME use planning permissions when they exist
permissions: [Permission.PLANNINGS],
permissions: [Permission.PLANNING_READ, Permission.PLANNING_WRITE],
element: <Assignments />,
};
export const teamsPath = {
Expand Down
124 changes: 75 additions & 49 deletions hat/assets/js/apps/Iaso/domains/plannings/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { CreateEditPlanning } from './CreateEditPlanning/CreateEditPlanning';
import DeleteDialog from '../../components/dialogs/DeleteDialogComponent';
import { PlanningApi } from './hooks/requests/useGetPlannings';
import MESSAGES from './messages';
import { DisplayIfUserHasPerm } from '../../components/DisplayIfUserHasPerm';
import { PLANNING_WRITE } from '../../utils/permissions';

const getAssignmentUrl = (planning: PlanningApi): string => {
return `/${baseUrls.assignments}/planningId/${planning.id}/team/${planning.team}`;
Expand Down Expand Up @@ -71,55 +73,79 @@ export const usePlanningColumns = (
tooltipMessage={MESSAGES.viewPlanning}
size="small"
/>
<CreateEditPlanning
type="edit"
id={settings.row.original.id}
name={settings.row.original?.name}
selectedTeam={settings.row.original?.team}
selectedOrgUnit={
settings.row.original?.org_unit
}
startDate={settings.row.original?.started_at}
endDate={settings.row.original?.ended_at}
forms={settings.row.original?.forms ?? []}
publishingStatus={settings.row.original?.status}
project={settings.row.original?.project}
description={settings.row.original?.description}
/>
<CreateEditPlanning
type="copy"
name={settings.row.original?.name}
selectedTeam={settings.row.original?.team}
selectedOrgUnit={
settings.row.original?.org_unit
}
startDate={settings.row.original?.started_at}
endDate={settings.row.original?.ended_at}
forms={settings.row.original?.forms ?? []}
publishingStatus={settings.row.original?.status}
project={settings.row.original?.project}
description={settings.row.original?.description}
/>
<DeleteDialog
titleMessage={{
...MESSAGES.deletePlanning,
values: {
planningName:
settings.row.original.name,
},
}}
message={{
...MESSAGES.deleteWarning,
values: {
name: settings.row.original.name,
},
}}
disabled={false}
onConfirm={() =>
deletePlanning(settings.row.original.id)
}
keyName="delete-planning"
/>
<DisplayIfUserHasPerm
permissions={[PLANNING_WRITE]}
>
<CreateEditPlanning
type="edit"
id={settings.row.original.id}
name={settings.row.original?.name}
selectedTeam={settings.row.original?.team}
selectedOrgUnit={
settings.row.original?.org_unit
}
startDate={
settings.row.original?.started_at
}
endDate={settings.row.original?.ended_at}
forms={settings.row.original?.forms ?? []}
publishingStatus={
settings.row.original?.status
}
project={settings.row.original?.project}
description={
settings.row.original?.description
}
/>
</DisplayIfUserHasPerm>
<DisplayIfUserHasPerm
permissions={[PLANNING_WRITE]}
>
<CreateEditPlanning
type="copy"
name={settings.row.original?.name}
selectedTeam={settings.row.original?.team}
selectedOrgUnit={
settings.row.original?.org_unit
}
startDate={
settings.row.original?.started_at
}
endDate={settings.row.original?.ended_at}
forms={settings.row.original?.forms ?? []}
publishingStatus={
settings.row.original?.status
}
project={settings.row.original?.project}
description={
settings.row.original?.description
}
/>
</DisplayIfUserHasPerm>
<DisplayIfUserHasPerm
permissions={[PLANNING_WRITE]}
>
<DeleteDialog
titleMessage={{
...MESSAGES.deletePlanning,
values: {
planningName:
settings.row.original.name,
},
}}
message={{
...MESSAGES.deleteWarning,
values: {
name: settings.row.original.name,
},
}}
disabled={false}
onConfirm={() =>
deletePlanning(settings.row.original.id)
}
keyName="delete-planning"
/>
</DisplayIfUserHasPerm>
</section>
);
},
Expand Down
11 changes: 8 additions & 3 deletions hat/assets/js/apps/Iaso/domains/plannings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { CreateEditPlanning } from './CreateEditPlanning/CreateEditPlanning';
import { useDeletePlanning } from './hooks/requests/useDeletePlanning';
import { useSingleTableParams } from '../../components/tables/SingleTable';
import { useParamsObject } from '../../routing/hooks/useParamsObject';
import { DisplayIfUserHasPerm } from '../../components/DisplayIfUserHasPerm';
import { PLANNING_WRITE } from '../../utils/permissions';

const useStyles = makeStyles(theme => ({
...commonStyles(theme),
Expand All @@ -38,9 +40,12 @@ export const Planning: FunctionComponent = () => {

<Box className={classes.containerFullHeightNoTabPadded}>
<PlanningFilters params={apiParams} />
<Grid container item justifyContent="flex-end">
<CreateEditPlanning type="create" />
</Grid>
<DisplayIfUserHasPerm permissions={[PLANNING_WRITE]}>
<Grid container item justifyContent="flex-end">
<CreateEditPlanning type="create" />
</Grid>
</DisplayIfUserHasPerm>

{/* @ts-ignore */}
<TableWithDeepLink
baseUrl={baseUrl}
Expand Down
6 changes: 4 additions & 2 deletions hat/assets/js/apps/Iaso/utils/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const ORG_UNIT_TYPES = 'iaso_org_unit_types';
const ORG_UNITS = 'iaso_org_units';
const PAGES = 'iaso_pages';
const PAGE_WRITE = 'iaso_page_write';
const PLANNINGS = 'iaso_planning';
const PLANNING_READ = 'iaso_planning_read';
const PLANNING_WRITE = 'iaso_planning_write';
const POLIO = 'iaso_polio';
const POLIO_CONFIG = 'iaso_polio_config';
const POLIO_SUPPLY_CHAIN_READ = 'iaso_polio_vaccine_supply_chain_read';
Expand Down Expand Up @@ -59,7 +60,8 @@ export {
PAGES,
PAGE_WRITE,
PAYMENTS,
PLANNINGS,
PLANNING_READ,
PLANNING_WRITE,
POLIO,
POLIO_CONFIG,
POLIO_NOTIFICATIONS,
Expand Down
10 changes: 5 additions & 5 deletions iaso/api/microplanning.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def filter_queryset(self, request, queryset, view):

class PlanningViewSet(AuditMixin, ModelViewSet):
remove_results_key_if_paginated = True
permission_classes = [ReadOnlyOrHasPermission(permission.PLANNING)] # type: ignore
permission_classes = [ReadOnlyOrHasPermission(permission.PLANNING_WRITE)] # type: ignore
serializer_class = PlanningSerializer
queryset = Planning.objects.all()
filter_backends = [
Expand Down Expand Up @@ -466,7 +466,7 @@ class AssignmentViewSet(AuditMixin, ModelViewSet):
sense outside of it's planning."""

remove_results_key_if_paginated = True
permission_classes = [IsAuthenticated, ReadOnlyOrHasPermission(permission.PLANNING)] # type: ignore
permission_classes = [IsAuthenticated, ReadOnlyOrHasPermission(permission.PLANNING_WRITE)] # type: ignore
serializer_class = AssignmentSerializer
queryset = Assignment.objects.all()
filter_backends = [
Expand Down Expand Up @@ -529,9 +529,9 @@ def get_assignments(self, planning: Planning):
for out in OrgUnitType.objects.filter(projects__account=user.iaso_profile.account):
out_set = set(out.form_set.values_list("id", flat=True))
intersection = out_set.intersection(planning_form_set)
forms_per_ou_type[
out.id
] = intersection # intersection of the two sets: the forms of the orgunit types and the forms of the planning
forms_per_ou_type[out.id] = (
intersection # intersection of the two sets: the forms of the orgunit types and the forms of the planning
)

for a in planning.assignment_set.filter(deleted_at__isnull=True).filter(user=user).prefetch_related("org_unit"):
# TODO: investigate type error on next line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def link_iaso_registry_write_and_read_to_users(apps, schema_editor):
permission = apps.get_model("auth", "Permission")
user = apps.get_model("auth", "User")
old_registry_permission = permission.objects.get(codename="iaso_registry")
old_registry_permission = permission.objects.filter(codename="iaso_registry").first()
users_linked_old_permission = user.objects.filter(user_permissions=old_registry_permission)
new_registry_permissions = permission.objects.filter(codename__in=["iaso_registry_write", "iaso_registry_read"])
for user in users_linked_old_permission:
Expand All @@ -19,7 +19,7 @@ def unlink_iaso_registry_write_and_read_to_users(apps, schema_editor):
user = apps.get_model("auth", "User")
old_registry_permissions = permission.objects.filter(codename__in=["iaso_registry_write", "iaso_registry_read"])
users_linked_old_permissions = user.objects.filter(user_permissions__in=old_registry_permissions)
new_registry_permission = permission.objects.get(codename="iaso_registry")
new_registry_permission = permission.objects.filter(codename="iaso_registry").first()
for user in users_linked_old_permissions:
user.user_permissions.remove(*old_registry_permissions)
user.user_permissions.add(new_registry_permission)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def link_iaso_planning_write_and_read_to_users(apps, schema_editor):
permission = apps.get_model("auth", "Permission")
user = apps.get_model("auth", "User")
old_planning_permission = permission.objects.get(codename="iaso_planning")
old_planning_permission = permission.objects.filter(codename="iaso_planning").first()
users_linked_old_permission = user.objects.filter(user_permissions=old_planning_permission)
new_planning_permissions = permission.objects.filter(codename__in=["iaso_planning_write", "iaso_planning_read"])
for user in users_linked_old_permission:
Expand All @@ -19,7 +19,7 @@ def unlink_iaso_planning_write_and_read_to_users(apps, schema_editor):
user = apps.get_model("auth", "User")
old_planning_permissions = permission.objects.filter(codename__in=["iaso_planning_write", "iaso_planning_read"])
users_linked_old_permissions = user.objects.filter(user_permissions__in=old_planning_permissions)
new_planning_permission = permission.objects.get(codename="iaso_planning")
new_planning_permission = permission.objects.filter(codename="iaso_planning").first()
for user in users_linked_old_permissions:
user.user_permissions.remove(*old_planning_permissions)
user.user_permissions.add(new_planning_permission)
Expand Down
12 changes: 6 additions & 6 deletions iaso/tests/test_microplanning.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ def test_patch_api(self):
team=self.team1,
)
user_with_perms = self.create_user_with_profile(
username="user_with_perms", account=self.account, permissions=["iaso_planning"]
username="user_with_perms", account=self.account, permissions=["iaso_planning_write"]
)
self.client.force_authenticate(user_with_perms)
data = {
Expand All @@ -596,7 +596,7 @@ def test_patch_api(self):

def test_create_api(self):
user_with_perms = self.create_user_with_profile(
username="user_with_perms", account=self.account, permissions=["iaso_planning"]
username="user_with_perms", account=self.account, permissions=["iaso_planning_write"]
)
self.client.force_authenticate(user_with_perms)
data = {
Expand Down Expand Up @@ -713,7 +713,7 @@ def test_query_filtering(self):

def test_create(self):
user_with_perms = self.create_user_with_profile(
username="user_with_perms", account=self.account, permissions=["iaso_planning"]
username="user_with_perms", account=self.account, permissions=["iaso_planning_write"]
)
self.client.force_authenticate(user_with_perms)
data = {
Expand All @@ -734,7 +734,7 @@ def test_create(self):

def test_bulk_create(self):
user_with_perms = self.create_user_with_profile(
username="user_with_perms", account=self.account, permissions=["iaso_planning"]
username="user_with_perms", account=self.account, permissions=["iaso_planning_write"]
)
assignments = Assignment.objects.filter(planning=self.planning)
self.assertEqual(assignments.count(), 1)
Expand Down Expand Up @@ -771,7 +771,7 @@ def test_bulk_no_access_planning(self):
other_account = Account.objects.create(name="other_account")

user = self.create_user_with_profile(
username="user_with_perms", account=other_account, permissions=["iaso_planning"]
username="user_with_perms", account=other_account, permissions=["iaso_planning_write"]
)
self.client.force_authenticate(user)
data = {
Expand All @@ -788,7 +788,7 @@ def test_restore_deleted_assignment(self):
"""restore deleted assignment if we try to create a new assignment with a previously assigned OU"""

user_with_perms = self.create_user_with_profile(
username="user_with_perms", account=self.account, permissions=["iaso_planning"]
username="user_with_perms", account=self.account, permissions=["iaso_planning_write"]
)
self.client.force_authenticate(user_with_perms)
data = {
Expand Down

0 comments on commit 08d56e9

Please sign in to comment.