#101 Team sharing tests
* adds tests for team sharing * extends the API for team sharing support * adds shared_teams property shortcut for ShareableObjectMixin * adds full support for team-based sharing to all views and functions * simplifies ShareModalForm * adds/updates translations
This commit is contained in:
@@ -85,6 +85,26 @@ class APIV1GetTestCase(BaseAPIV1TestCase):
|
||||
except KeyError as e:
|
||||
self.fail(e)
|
||||
|
||||
def test_get_shared(self):
|
||||
""" Tests api GET on shared info of the intervention
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.intervention.share_with_user(self.superuser)
|
||||
self.intervention.share_with_team(self.team)
|
||||
url = reverse("api:v1:intervention-share", args=(str(self.intervention.id),))
|
||||
response = self._run_get_request(url)
|
||||
content = json.loads(response.content)
|
||||
self.assertIn("users", content)
|
||||
self.assertIn(self.superuser.username, content["users"])
|
||||
self.assertEqual(1, len(content["users"]))
|
||||
self.assertIn("teams", content)
|
||||
self.assertEqual(1, len(content["teams"]))
|
||||
for team in content["teams"]:
|
||||
self.assertEqual(team["id"], str(self.team.id))
|
||||
self.assertEqual(team["name"], self.team.name)
|
||||
|
||||
def test_get_compensation(self):
|
||||
""" Tests api GET
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"users": [
|
||||
"CHANGE_ME"
|
||||
],
|
||||
"teams": [
|
||||
"CHANGE_ME"
|
||||
]
|
||||
}
|
||||
@@ -184,3 +184,24 @@ class APIV1UpdateTestCase(BaseAPIV1TestCase):
|
||||
self.assertEqual(put_body["intervention"], str(self.deduction.intervention.id))
|
||||
self.assertEqual(put_body["eco_account"], str(self.deduction.account.id))
|
||||
self.assertEqual(put_body["surface"], self.deduction.surface)
|
||||
|
||||
def test_update_share_intervention(self):
|
||||
self.intervention.share_with_user(self.superuser)
|
||||
url = reverse("api:v1:intervention-share", args=(str(self.intervention.id),))
|
||||
json_file_path = "api/tests/v1/update/intervention_share_update_put_body.json"
|
||||
with open(json_file_path) as json_file:
|
||||
put_body = json.load(fp=json_file)
|
||||
put_body["users"] = [self.user.username]
|
||||
put_body["teams"] = [self.team.name]
|
||||
|
||||
self.assertFalse(self.intervention.is_shared_with(self.user))
|
||||
self.assertEqual(0, self.intervention.shared_teams.count())
|
||||
|
||||
response = self._run_update_request(url, put_body)
|
||||
self.assertEqual(response.status_code, 200, msg=response.content)
|
||||
self.intervention.refresh_from_db()
|
||||
|
||||
self.assertEqual(1, self.intervention.shared_teams.count())
|
||||
self.assertEqual(2, self.intervention.shared_users.count())
|
||||
self.assertEqual(self.team.name, self.intervention.shared_teams.first().name)
|
||||
self.assertTrue(self.intervention.is_shared_with(self.user))
|
||||
|
||||
@@ -19,7 +19,7 @@ from ema.models import Ema
|
||||
from intervention.models import Intervention
|
||||
from konova.utils.message_templates import DATA_UNSHARED
|
||||
from konova.utils.user_checks import is_default_group_only
|
||||
from user.models import User
|
||||
from user.models import User, Team
|
||||
|
||||
|
||||
class AbstractAPIView(View):
|
||||
@@ -198,13 +198,21 @@ class AbstractModelShareAPIView(AbstractAPIView):
|
||||
"""
|
||||
try:
|
||||
users = self._get_shared_users_of_object(id)
|
||||
teams = self._get_shared_teams_of_object(id)
|
||||
except Exception as e:
|
||||
return self._return_error_response(e)
|
||||
|
||||
data = {
|
||||
"users": [
|
||||
user.username for user in users
|
||||
]
|
||||
],
|
||||
"teams": [
|
||||
{
|
||||
"id": team.id,
|
||||
"name": team.name,
|
||||
}
|
||||
for team in teams
|
||||
],
|
||||
}
|
||||
|
||||
return JsonResponse(data)
|
||||
@@ -258,6 +266,22 @@ class AbstractModelShareAPIView(AbstractAPIView):
|
||||
users = obj.shared_users
|
||||
return users
|
||||
|
||||
def _get_shared_teams_of_object(self, id) -> QuerySet:
|
||||
""" Check permissions and get the teams
|
||||
|
||||
Args:
|
||||
id (str): The object's id
|
||||
|
||||
Returns:
|
||||
users (QuerySet)
|
||||
"""
|
||||
obj = self.model.objects.get(
|
||||
id=id
|
||||
)
|
||||
self._check_user_has_shared_access(obj)
|
||||
teams = obj.shared_teams
|
||||
return teams
|
||||
|
||||
def _process_put_body(self, body: bytes, id: str):
|
||||
""" Reads the body data, performs validity checks and sets the new users
|
||||
|
||||
@@ -271,19 +295,26 @@ class AbstractModelShareAPIView(AbstractAPIView):
|
||||
obj = self.model.objects.get(id=id)
|
||||
self._check_user_has_shared_access(obj)
|
||||
|
||||
new_users = json.loads(body.decode("utf-8"))
|
||||
new_users = new_users.get("users", [])
|
||||
content = json.loads(body.decode("utf-8"))
|
||||
new_users = content.get("users", [])
|
||||
if len(new_users) == 0:
|
||||
raise ValueError("Shared user list must not be empty!")
|
||||
new_teams = content.get("teams", [])
|
||||
|
||||
# Eliminate duplicates
|
||||
new_users = list(dict.fromkeys(new_users))
|
||||
new_teams = list(dict.fromkeys(new_teams))
|
||||
|
||||
# Make sure each of these names exist as a user
|
||||
new_users_objs = []
|
||||
for user in new_users:
|
||||
new_users_objs.append(User.objects.get(username=user))
|
||||
|
||||
# Make sure each of these names exist as a user
|
||||
new_teams_objs = []
|
||||
for team_name in new_teams:
|
||||
new_teams_objs.append(Team.objects.get(name=team_name))
|
||||
|
||||
if is_default_group_only(self.user):
|
||||
# Default only users are not allowed to remove other users from having access. They can only add new ones!
|
||||
new_users_to_be_added = User.objects.filter(
|
||||
@@ -292,7 +323,16 @@ class AbstractModelShareAPIView(AbstractAPIView):
|
||||
id__in=obj.shared_users
|
||||
)
|
||||
new_users_objs = obj.shared_users.union(new_users_to_be_added)
|
||||
|
||||
new_teams_to_be_added = Team.objects.filter(
|
||||
name__in=new_teams
|
||||
).exclude(
|
||||
id__in=obj.shared_teams
|
||||
)
|
||||
new_teams_objs = obj.shared_teams.union(new_teams_to_be_added)
|
||||
|
||||
obj.share_with_user_list(new_users_objs)
|
||||
obj.share_with_team_list(new_teams_objs)
|
||||
return True
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user