#19 Tests
* refactors sharing user setting by introducing share_with(user) and share_with_list(user_list) for BaseObject for a more self-explanatory sharing
This commit is contained in:
parent
a5e816c67b
commit
6c35fc59af
@ -380,7 +380,7 @@ class NewEcoAccountForm(AbstractCompensationForm, CompensationResponsibleFormMix
|
|||||||
legal=legal
|
legal=legal
|
||||||
)
|
)
|
||||||
acc.fundings.set(fundings)
|
acc.fundings.set(fundings)
|
||||||
acc.users.add(user)
|
acc.share_with(user)
|
||||||
|
|
||||||
# Add the log entry to the main objects log list
|
# Add the log entry to the main objects log list
|
||||||
acc.log.add(action)
|
acc.log.add(action)
|
||||||
|
@ -235,12 +235,13 @@ class Compensation(AbstractCompensation):
|
|||||||
Checks whether a given user has access to this object
|
Checks whether a given user has access to this object
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
user ():
|
user (User): The user to be checked
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return self.intervention.users.filter(id=user.id)
|
# Compensations inherit their shared state from the interventions
|
||||||
|
return self.intervention.is_shared_with(user)
|
||||||
|
|
||||||
def get_LANIS_link(self) -> str:
|
def get_LANIS_link(self) -> str:
|
||||||
""" Generates a link for LANIS depending on the geometry
|
""" Generates a link for LANIS depending on the geometry
|
||||||
|
@ -88,7 +88,7 @@ class CompensationViewTestCase(BaseViewTestCase):
|
|||||||
client = Client()
|
client = Client()
|
||||||
client.login(username=self.superuser.username, password=self.superuser_pw)
|
client.login(username=self.superuser.username, password=self.superuser_pw)
|
||||||
self.superuser.groups.set([])
|
self.superuser.groups.set([])
|
||||||
self.intervention.users.set([self.superuser])
|
self.intervention.share_with_list([self.superuser])
|
||||||
|
|
||||||
# Since the user has no groups, it does not matter that data has been shared. There SHOULD not be any difference
|
# Since the user has no groups, it does not matter that data has been shared. There SHOULD not be any difference
|
||||||
# to a user without access, since the important permissions are missing
|
# to a user without access, since the important permissions are missing
|
||||||
@ -126,7 +126,7 @@ class CompensationViewTestCase(BaseViewTestCase):
|
|||||||
client.login(username=self.superuser.username, password=self.superuser_pw)
|
client.login(username=self.superuser.username, password=self.superuser_pw)
|
||||||
self.superuser.groups.set([])
|
self.superuser.groups.set([])
|
||||||
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
||||||
self.intervention.users.set([])
|
self.intervention.share_with_list([])
|
||||||
|
|
||||||
# Since the user has no groups, it does not matter that data is unshared. There SHOULD not be any difference
|
# Since the user has no groups, it does not matter that data is unshared. There SHOULD not be any difference
|
||||||
# to a user having shared access, since all important permissions are missing
|
# to a user having shared access, since all important permissions are missing
|
||||||
@ -166,7 +166,7 @@ class CompensationViewTestCase(BaseViewTestCase):
|
|||||||
group = self.groups.get(name=DEFAULT_GROUP)
|
group = self.groups.get(name=DEFAULT_GROUP)
|
||||||
self.superuser.groups.set([group])
|
self.superuser.groups.set([group])
|
||||||
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
||||||
self.intervention.users.set([self.superuser])
|
self.intervention.share_with_list([self.superuser])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
@ -200,7 +200,7 @@ class CompensationViewTestCase(BaseViewTestCase):
|
|||||||
group = self.groups.get(name=DEFAULT_GROUP)
|
group = self.groups.get(name=DEFAULT_GROUP)
|
||||||
self.superuser.groups.set([group])
|
self.superuser.groups.set([group])
|
||||||
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
||||||
self.intervention.users.set([])
|
self.intervention.share_with_list([])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
@ -271,7 +271,7 @@ class EcoAccountViewTestCase(CompensationViewTestCase):
|
|||||||
client = Client()
|
client = Client()
|
||||||
client.login(username=self.superuser.username, password=self.superuser_pw)
|
client.login(username=self.superuser.username, password=self.superuser_pw)
|
||||||
self.superuser.groups.set([])
|
self.superuser.groups.set([])
|
||||||
self.eco_account.users.set([self.superuser])
|
self.eco_account.share_with_list([self.superuser])
|
||||||
|
|
||||||
# Since the user has no groups, it does not matter that data has been shared. There SHOULD not be any difference
|
# Since the user has no groups, it does not matter that data has been shared. There SHOULD not be any difference
|
||||||
# to a user without access, since the important permissions are missing
|
# to a user without access, since the important permissions are missing
|
||||||
@ -308,7 +308,7 @@ class EcoAccountViewTestCase(CompensationViewTestCase):
|
|||||||
client = Client()
|
client = Client()
|
||||||
client.login(username=self.superuser.username, password=self.superuser_pw)
|
client.login(username=self.superuser.username, password=self.superuser_pw)
|
||||||
self.superuser.groups.set([])
|
self.superuser.groups.set([])
|
||||||
self.eco_account.users.set([])
|
self.eco_account.share_with_list([])
|
||||||
|
|
||||||
# Since the user has no groups, it does not matter that data is unshared. There SHOULD not be any difference
|
# Since the user has no groups, it does not matter that data is unshared. There SHOULD not be any difference
|
||||||
# to a user having shared access, since all important permissions are missing
|
# to a user having shared access, since all important permissions are missing
|
||||||
@ -348,7 +348,7 @@ class EcoAccountViewTestCase(CompensationViewTestCase):
|
|||||||
group = self.groups.get(name=DEFAULT_GROUP)
|
group = self.groups.get(name=DEFAULT_GROUP)
|
||||||
self.superuser.groups.set([group])
|
self.superuser.groups.set([group])
|
||||||
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
||||||
self.eco_account.users.set([self.superuser])
|
self.eco_account.share_with_list([self.superuser])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
@ -381,7 +381,7 @@ class EcoAccountViewTestCase(CompensationViewTestCase):
|
|||||||
client.login(username=self.superuser.username, password=self.superuser_pw)
|
client.login(username=self.superuser.username, password=self.superuser_pw)
|
||||||
group = self.groups.get(name=DEFAULT_GROUP)
|
group = self.groups.get(name=DEFAULT_GROUP)
|
||||||
self.superuser.groups.set([group])
|
self.superuser.groups.set([group])
|
||||||
self.eco_account.users.set([])
|
self.eco_account.share_with_list([])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
|
@ -21,7 +21,7 @@ class CompensationWorkflowTestCase(BaseWorkflowTestCase):
|
|||||||
super().setUpTestData()
|
super().setUpTestData()
|
||||||
|
|
||||||
# Give the user shared access to the dummy intervention -> inherits the access to the compensation
|
# Give the user shared access to the dummy intervention -> inherits the access to the compensation
|
||||||
cls.intervention.users.add(cls.superuser)
|
cls.intervention.share_with(cls.superuser)
|
||||||
|
|
||||||
# Make sure the intervention itself would be fine with valid data
|
# Make sure the intervention itself would be fine with valid data
|
||||||
cls.intervention = cls.fill_out_intervention(cls.intervention)
|
cls.intervention = cls.fill_out_intervention(cls.intervention)
|
||||||
@ -142,7 +142,7 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase):
|
|||||||
|
|
||||||
# Add user to conservation office group and give shared access to the account
|
# Add user to conservation office group and give shared access to the account
|
||||||
cls.superuser.groups.add(cls.groups.get(name=ETS_GROUP))
|
cls.superuser.groups.add(cls.groups.get(name=ETS_GROUP))
|
||||||
cls.eco_account.users.set([cls.superuser])
|
cls.eco_account.share_with_list([cls.superuser])
|
||||||
|
|
||||||
def test_deductability(self):
|
def test_deductability(self):
|
||||||
"""
|
"""
|
||||||
@ -154,7 +154,7 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
# Give user shared access to the dummy intervention, which will be needed here
|
# Give user shared access to the dummy intervention, which will be needed here
|
||||||
self.intervention.users.add(self.superuser)
|
self.intervention.share_with(self.superuser)
|
||||||
|
|
||||||
# Prepare data for deduction creation
|
# Prepare data for deduction creation
|
||||||
deduct_url = reverse("compensation:acc-new-deduction", args=(self.eco_account.id,))
|
deduct_url = reverse("compensation:acc-new-deduction", args=(self.eco_account.id,))
|
||||||
|
@ -616,7 +616,7 @@ def share_view(request: HttpRequest, id: str, token: str):
|
|||||||
request,
|
request,
|
||||||
_("{} has been shared with you").format(obj.identifier)
|
_("{} has been shared with you").format(obj.identifier)
|
||||||
)
|
)
|
||||||
obj.users.add(user)
|
obj.share_with(user)
|
||||||
return redirect("compensation:acc-detail", id=id)
|
return redirect("compensation:acc-detail", id=id)
|
||||||
else:
|
else:
|
||||||
messages.error(
|
messages.error(
|
||||||
|
@ -83,7 +83,7 @@ class NewEmaForm(AbstractCompensationForm, CompensationResponsibleFormMixin):
|
|||||||
acc.fundings.set(fundings)
|
acc.fundings.set(fundings)
|
||||||
|
|
||||||
# Add the creating user to the list of shared users
|
# Add the creating user to the list of shared users
|
||||||
acc.users.add(user)
|
acc.share_with(user)
|
||||||
|
|
||||||
# Add the log entry to the main objects log list
|
# Add the log entry to the main objects log list
|
||||||
acc.log.add(action)
|
acc.log.add(action)
|
||||||
|
@ -97,7 +97,7 @@ class EmaViewTestCase(CompensationViewTestCase):
|
|||||||
|
|
||||||
# Sharing does not have any effect in here, since the default group will prohibit further functionality access
|
# Sharing does not have any effect in here, since the default group will prohibit further functionality access
|
||||||
# to this user
|
# to this user
|
||||||
self.ema.users.set([self.superuser])
|
self.ema.share_with_list([self.superuser])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
@ -143,7 +143,7 @@ class EmaViewTestCase(CompensationViewTestCase):
|
|||||||
|
|
||||||
# Sharing does not have any effect in here, since the default group will prohibit further functionality access
|
# Sharing does not have any effect in here, since the default group will prohibit further functionality access
|
||||||
# to this user
|
# to this user
|
||||||
self.ema.users.set([])
|
self.ema.share_with_list([])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
@ -182,7 +182,7 @@ class EmaViewTestCase(CompensationViewTestCase):
|
|||||||
groups = self.groups.filter(Q(name=ETS_GROUP)|Q(name=DEFAULT_GROUP))
|
groups = self.groups.filter(Q(name=ETS_GROUP)|Q(name=DEFAULT_GROUP))
|
||||||
self.superuser.groups.set(groups)
|
self.superuser.groups.set(groups)
|
||||||
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
||||||
self.ema.users.set([self.superuser])
|
self.ema.share_with_list([self.superuser])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
@ -218,7 +218,7 @@ class EmaViewTestCase(CompensationViewTestCase):
|
|||||||
groups = self.groups.filter(Q(name=ETS_GROUP)|Q(name=DEFAULT_GROUP))
|
groups = self.groups.filter(Q(name=ETS_GROUP)|Q(name=DEFAULT_GROUP))
|
||||||
self.superuser.groups.set(groups)
|
self.superuser.groups.set(groups)
|
||||||
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
# Sharing is inherited by base intervention for compensation. Therefore configure the interventions share state
|
||||||
self.ema.users.set([])
|
self.ema.share_with_list([])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
|
@ -521,7 +521,7 @@ def share_view(request: HttpRequest, id: str, token: str):
|
|||||||
request,
|
request,
|
||||||
_("{} has been shared with you").format(obj.identifier)
|
_("{} has been shared with you").format(obj.identifier)
|
||||||
)
|
)
|
||||||
obj.users.add(user)
|
obj.share_with(user)
|
||||||
return redirect("ema:detail", id=id)
|
return redirect("ema:detail", id=id)
|
||||||
else:
|
else:
|
||||||
messages.error(
|
messages.error(
|
||||||
|
@ -255,7 +255,7 @@ class NewInterventionForm(BaseForm):
|
|||||||
intervention.log.add(action)
|
intervention.log.add(action)
|
||||||
|
|
||||||
# Add the performing user as the first user having access to the data
|
# Add the performing user as the first user having access to the data
|
||||||
intervention.users.add(user)
|
intervention.share_with(user)
|
||||||
return intervention
|
return intervention
|
||||||
|
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ class ShareInterventionModalForm(BaseModalForm):
|
|||||||
accessing_users = User.objects.filter(
|
accessing_users = User.objects.filter(
|
||||||
id__in=self.cleaned_data["users"]
|
id__in=self.cleaned_data["users"]
|
||||||
)
|
)
|
||||||
self.instance.users.set(accessing_users)
|
self.instance.share_with_list(accessing_users)
|
||||||
|
|
||||||
|
|
||||||
class NewRevocationModalForm(BaseModalForm):
|
class NewRevocationModalForm(BaseModalForm):
|
||||||
|
@ -323,7 +323,6 @@ class Intervention(BaseObject, ShareableObject, RecordableObject, CheckableObjec
|
|||||||
comp.log.add(log_entry)
|
comp.log.add(log_entry)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class InterventionDocument(AbstractDocument):
|
class InterventionDocument(AbstractDocument):
|
||||||
"""
|
"""
|
||||||
Specializes document upload for an intervention with certain path
|
Specializes document upload for an intervention with certain path
|
||||||
|
@ -116,7 +116,7 @@ class InterventionViewTestCase(BaseViewTestCase):
|
|||||||
# Add user to default group
|
# Add user to default group
|
||||||
default_group = Group.objects.get(name=DEFAULT_GROUP)
|
default_group = Group.objects.get(name=DEFAULT_GROUP)
|
||||||
self.superuser.groups.set([default_group])
|
self.superuser.groups.set([default_group])
|
||||||
self.intervention.users.set([self.superuser])
|
self.intervention.share_with_list([self.superuser])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
@ -156,7 +156,7 @@ class InterventionViewTestCase(BaseViewTestCase):
|
|||||||
# Add user to default group
|
# Add user to default group
|
||||||
default_group = Group.objects.get(name=DEFAULT_GROUP)
|
default_group = Group.objects.get(name=DEFAULT_GROUP)
|
||||||
self.superuser.groups.set([default_group])
|
self.superuser.groups.set([default_group])
|
||||||
self.intervention.users.set([])
|
self.intervention.share_with_list([])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
@ -196,7 +196,7 @@ class InterventionViewTestCase(BaseViewTestCase):
|
|||||||
# Add user to zb group
|
# Add user to zb group
|
||||||
zb_group = self.groups.get(name=ZB_GROUP)
|
zb_group = self.groups.get(name=ZB_GROUP)
|
||||||
self.superuser.groups.set([zb_group])
|
self.superuser.groups.set([zb_group])
|
||||||
self.intervention.users.set([self.superuser])
|
self.intervention.share_with_list([self.superuser])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
@ -236,7 +236,7 @@ class InterventionViewTestCase(BaseViewTestCase):
|
|||||||
# Add user to zb group
|
# Add user to zb group
|
||||||
zb_group = self.groups.get(name=ZB_GROUP)
|
zb_group = self.groups.get(name=ZB_GROUP)
|
||||||
self.superuser.groups.set([zb_group])
|
self.superuser.groups.set([zb_group])
|
||||||
self.intervention.users.set([])
|
self.intervention.share_with_list([])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
@ -276,7 +276,7 @@ class InterventionViewTestCase(BaseViewTestCase):
|
|||||||
# Add user to ets group
|
# Add user to ets group
|
||||||
ets_group = Group.objects.get(name=ETS_GROUP)
|
ets_group = Group.objects.get(name=ETS_GROUP)
|
||||||
self.superuser.groups.set([ets_group])
|
self.superuser.groups.set([ets_group])
|
||||||
self.intervention.users.set([self.superuser])
|
self.intervention.share_with_list([self.superuser])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
@ -316,7 +316,7 @@ class InterventionViewTestCase(BaseViewTestCase):
|
|||||||
# Add user to default group
|
# Add user to default group
|
||||||
ets_group = Group.objects.get(name=ETS_GROUP)
|
ets_group = Group.objects.get(name=ETS_GROUP)
|
||||||
self.superuser.groups.set([ets_group])
|
self.superuser.groups.set([ets_group])
|
||||||
self.intervention.users.set([])
|
self.intervention.share_with_list([])
|
||||||
|
|
||||||
success_urls = [
|
success_urls = [
|
||||||
self.index_url,
|
self.index_url,
|
||||||
|
@ -26,8 +26,7 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase):
|
|||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
super().setUpTestData()
|
super().setUpTestData()
|
||||||
|
|
||||||
# Give the user shared access to the dummy intervention
|
cls.intervention.share_with(cls.superuser)
|
||||||
cls.intervention.users.add(cls.superuser)
|
|
||||||
|
|
||||||
def test_new(self):
|
def test_new(self):
|
||||||
"""
|
"""
|
||||||
@ -312,7 +311,7 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase):
|
|||||||
|
|
||||||
# Now restore the deductable surface to a valid size back again but remove the user from the shared list
|
# Now restore the deductable surface to a valid size back again but remove the user from the shared list
|
||||||
self.eco_account.deductable_surface = test_surface + 100.00
|
self.eco_account.deductable_surface = test_surface + 100.00
|
||||||
self.eco_account.users.set([])
|
self.eco_account.share_with_list([])
|
||||||
self.eco_account.save()
|
self.eco_account.save()
|
||||||
|
|
||||||
# Now perform the (expected) failing request (again)
|
# Now perform the (expected) failing request (again)
|
||||||
@ -323,7 +322,7 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase):
|
|||||||
self.assertEqual(num_deductions_total, EcoAccountDeduction.objects.count())
|
self.assertEqual(num_deductions_total, EcoAccountDeduction.objects.count())
|
||||||
|
|
||||||
# Restore the sharing but remove the recording state
|
# Restore the sharing but remove the recording state
|
||||||
self.eco_account.users.set([self.superuser])
|
self.eco_account.share_with_list([self.superuser])
|
||||||
self.eco_account.recorded.delete()
|
self.eco_account.recorded.delete()
|
||||||
self.eco_account.refresh_from_db()
|
self.eco_account.refresh_from_db()
|
||||||
self.eco_account.save()
|
self.eco_account.save()
|
||||||
@ -354,7 +353,7 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase):
|
|||||||
action=UserAction.RECORDED
|
action=UserAction.RECORDED
|
||||||
)
|
)
|
||||||
self.eco_account.recorded = rec_action
|
self.eco_account.recorded = rec_action
|
||||||
self.eco_account.users.set([self.superuser])
|
self.eco_account.share_with_list([self.superuser])
|
||||||
self.eco_account.save()
|
self.eco_account.save()
|
||||||
|
|
||||||
# Run the request
|
# Run the request
|
||||||
|
@ -378,7 +378,7 @@ def share_view(request: HttpRequest, id: str, token: str):
|
|||||||
request,
|
request,
|
||||||
_("{} has been shared with you").format(intervention.identifier)
|
_("{} has been shared with you").format(intervention.identifier)
|
||||||
)
|
)
|
||||||
intervention.users.add(user)
|
intervention.share_with(user)
|
||||||
return redirect("intervention:detail", id=id)
|
return redirect("intervention:detail", id=id)
|
||||||
else:
|
else:
|
||||||
messages.error(
|
messages.error(
|
||||||
|
@ -155,6 +155,29 @@ class BaseObject(BaseResource):
|
|||||||
else:
|
else:
|
||||||
return User.objects.none()
|
return User.objects.none()
|
||||||
|
|
||||||
|
def share_with(self, user: User):
|
||||||
|
""" Adds user to list of shared access users
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user (User): The user to be added to the object
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
if not self.is_shared_with(user):
|
||||||
|
self.users.add(user)
|
||||||
|
|
||||||
|
def share_with_list(self, user_list: list):
|
||||||
|
""" Sets the list of shared access users
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user_list (list): The users to be added to the object
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
self.users.set(user_list)
|
||||||
|
|
||||||
def generate_new_identifier(self) -> str:
|
def generate_new_identifier(self) -> str:
|
||||||
""" Generates a new identifier for the intervention object
|
""" Generates a new identifier for the intervention object
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user