Skip to content

Commit 61be431

Browse files
committed
✨(backend) add management command cleaning onboarding document accesses
We change the strategy on how the new users have access to the onboarding documents. We should remove all created accesses we don't want to have anymore. There is no need to add then n the link_trace table, they are already present in the favorites and user have already access to it.
1 parent 24f753e commit 61be431

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- ✨(backend) add management command cleaning onboarding document accesses
12+
913
### Fixed
1014

1115
- 🐛(backend) create a link_trace record for onboarded documents
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Module for a management command removing accesses on onboarding documents with non privileged roles.
3+
"""
4+
5+
from django.conf import settings
6+
from django.core.management.base import BaseCommand
7+
8+
from core.models import PRIVILEGED_ROLES, DocumentAccess
9+
10+
11+
class Command(BaseCommand):
12+
"""Management command removing accesses on onboarding documents with non privileged roles."""
13+
14+
help = __doc__
15+
16+
def handle(self, *args, **options):
17+
"""Execute management command."""
18+
19+
if not settings.USER_ONBOARDING_DOCUMENTS:
20+
self.stdout.write("No onboarding documents set, nothing to do.")
21+
return
22+
23+
onboarding_document_ids = set(settings.USER_ONBOARDING_DOCUMENTS)
24+
25+
DocumentAccess.objects.filter(document_id__in=onboarding_document_ids).exclude(
26+
role__in=PRIVILEGED_ROLES
27+
).delete()
28+
29+
self.stdout.write("accesses deleted.")
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"""Module testing clean_onboarding_documents_accesses command."""
2+
3+
from django.core.management import call_command
4+
5+
import pytest
6+
7+
from core import factories, models
8+
9+
pytestmark = pytest.mark.django_db
10+
11+
12+
def test_clean_onboarding_documents_accesses(settings):
13+
"""test running the command remove the accesses for non priviliged roles."""
14+
15+
# Create onboarding documents
16+
onboarding_documents = factories.DocumentFactory.create_batch(
17+
2, link_reach=models.LinkReachChoices.PUBLIC
18+
)
19+
20+
settings.USER_ONBOARDING_DOCUMENTS = [
21+
str(document.id) for document in onboarding_documents
22+
]
23+
24+
# Create other documents
25+
non_onboarding_documents = factories.DocumentFactory.create_batch(
26+
3, link_reach=models.LinkReachChoices.PUBLIC
27+
)
28+
29+
all_documents = onboarding_documents + non_onboarding_documents
30+
31+
# for every documents create 2 priviliged role, an owner and an admin
32+
for document in all_documents:
33+
factories.UserDocumentAccessFactory(
34+
document=document, role=models.RoleChoices.OWNER
35+
)
36+
factories.UserDocumentAccessFactory(
37+
document=document, role=models.RoleChoices.ADMIN
38+
)
39+
40+
# for every documents, create non privileged roles
41+
for document in all_documents:
42+
factories.UserDocumentAccessFactory.create_batch(
43+
10, document=document, role=models.RoleChoices.READER
44+
)
45+
factories.UserDocumentAccessFactory.create_batch(
46+
10, document=document, role=models.RoleChoices.EDITOR
47+
)
48+
factories.UserDocumentAccessFactory.create_batch(
49+
10, document=document, role=models.RoleChoices.COMMENTER
50+
)
51+
52+
# All documents should have 32 accesses, so 160 accesses created
53+
assert models.DocumentAccess.objects.count() == 160
54+
assert (
55+
models.DocumentAccess.objects.filter(document__in=onboarding_documents)
56+
.exclude(role__in=models.PRIVILEGED_ROLES)
57+
.count()
58+
== 60
59+
)
60+
assert (
61+
models.DocumentAccess.objects.filter(
62+
document__in=onboarding_documents, role__in=models.PRIVILEGED_ROLES
63+
).count()
64+
== 4
65+
)
66+
assert (
67+
models.DocumentAccess.objects.filter(document__in=non_onboarding_documents)
68+
.exclude(role__in=models.PRIVILEGED_ROLES)
69+
.count()
70+
== 90
71+
)
72+
assert (
73+
models.DocumentAccess.objects.filter(
74+
document__in=non_onboarding_documents, role__in=models.PRIVILEGED_ROLES
75+
).count()
76+
== 6
77+
)
78+
79+
# Run the command
80+
call_command("clean_onboarding_documents_accesses")
81+
82+
# 60 accesses should have been removed, 30 for each onboarding docs
83+
assert models.DocumentAccess.objects.count() == 100
84+
85+
# Non privileged roles should have been deleted on the onboarding documents
86+
assert (
87+
models.DocumentAccess.objects.filter(document__in=onboarding_documents)
88+
.exclude(role__in=models.PRIVILEGED_ROLES)
89+
.count()
90+
== 0
91+
)
92+
93+
# Priviliged roles should have been kept
94+
assert (
95+
models.DocumentAccess.objects.filter(
96+
document__in=onboarding_documents, role__in=models.PRIVILEGED_ROLES
97+
).count()
98+
== 4
99+
)
100+
101+
# On other documents, all accesses should remain
102+
assert (
103+
models.DocumentAccess.objects.filter(document__in=non_onboarding_documents)
104+
.exclude(role__in=models.PRIVILEGED_ROLES)
105+
.count()
106+
== 90
107+
)
108+
109+
# Priviliged roles should have been kept
110+
assert (
111+
models.DocumentAccess.objects.filter(
112+
document__in=non_onboarding_documents, role__in=models.PRIVILEGED_ROLES
113+
).count()
114+
== 6
115+
)
116+
117+
118+
def test_clean_onboarding_documents_accesses_no_onboarding_documents(settings):
119+
"""test running the command without onboarding documents should stop it."""
120+
settings.USER_ONBOARDING_DOCUMENTS = None
121+
122+
factories.UserDocumentAccessFactory.create_batch(10)
123+
124+
assert models.DocumentAccess.objects.count() == 10
125+
126+
# Run the command
127+
call_command("clean_onboarding_documents_accesses")
128+
129+
assert models.DocumentAccess.objects.count() == 10

0 commit comments

Comments
 (0)