You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ma...@apache.org on 2019/12/12 23:58:12 UTC

[airavata-django-portal] branch airavata-3243 updated: AIRAVATA-3243 Don't send signal when user not added to group

This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch airavata-3243
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git


The following commit(s) were added to refs/heads/airavata-3243 by this push:
     new 7448824  AIRAVATA-3243 Don't send signal when user not added to group
7448824 is described below

commit 74488247bcb3246eec966638d8da960a502ad10a
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Thu Dec 12 18:57:44 2019 -0500

    AIRAVATA-3243 Don't send signal when user not added to group
---
 django_airavata/apps/api/tests.py | 83 +++++++++++++++++++++++++++++++++++++++
 django_airavata/apps/api/views.py | 19 ++++-----
 2 files changed, 93 insertions(+), 9 deletions(-)

diff --git a/django_airavata/apps/api/tests.py b/django_airavata/apps/api/tests.py
index bb52a9f..be75317 100644
--- a/django_airavata/apps/api/tests.py
+++ b/django_airavata/apps/api/tests.py
@@ -383,3 +383,86 @@ class IAMUserViewSetTests(TestCase):
         self.assertEqual(kwargs["user"], user_profile)
         self.assertSetEqual({"group2", "group3"},
                             {g.id for g in kwargs["groups"]})
+
+    @patch("django_airavata.apps.api.views.iam_admin_client")
+    def test_update_that_does_not_add_user_to_groups(
+            self, iam_admin_client):
+
+        username = "testuser1"
+        url = reverse(
+            'django_airavata_api:iam-user-profile-detail',
+            kwargs={'user_id': username})
+        data = {
+            "airavataInternalUserId": f"{username}@{GATEWAY_ID}",
+            "userId": username,
+            "gatewayId": GATEWAY_ID,
+            "email": "testuser1@example.com",
+            "firstName": "Test",
+            "lastName": "User1",
+            "airavataUserProfileExists": True,
+            "enabled": True,
+            "emailVerified": True,
+            "groups": [
+                {"id": "group1", "name": "Group 1"},
+            ]
+        }
+        request = self.factory.put(url, data)
+        force_authenticate(request, self.user)
+        request.is_gateway_admin = True
+
+        # Mock api clients
+        iam_user_profile = UserProfile(
+            airavataInternalUserId=f"testuser1@{GATEWAY_ID}",
+            userId="testuser1",
+            firstName="Test",
+            lastName="User1",
+            emails=["testuser1@example.com"]
+        )
+        iam_admin_client.get_user.return_value = iam_user_profile
+        group_manager_mock = MagicMock(name='group_manager')
+        user_profile_mock = MagicMock(name='user_profile')
+        request.profile_service = {
+            'group_manager': group_manager_mock,
+            'user_profile': user_profile_mock,
+        }
+        request.authz_token = "dummy"
+        user_profile_mock.doesUserExist.return_value = True
+        user_profile = UserProfile(
+            airavataInternalUserId=f"testuser1@{GATEWAY_ID}",
+            userId="testuser1",
+            firstName="Test",
+            lastName="User1",
+            emails=["testuser1@example.com"]
+        )
+        user_profile_mock.getUserProfileById.return_value = user_profile
+        group_manager_mock.getAllGroupsUserBelongs.return_value = [
+            GroupModel(id="group1")]
+
+        request.airavata_client = MagicMock(name="airavata_client")
+        request.airavata_client.getGatewayGroups.return_value = GatewayGroups(
+            gatewayId=GATEWAY_ID,
+            adminsGroupId="adminsGroupId",
+            readOnlyAdminsGroupId="readOnlyAdminsGroupId",
+            defaultGatewayUsersGroupId="defaultGatewayUsersGroupId"
+        )
+        request.session = {}
+
+        # Mock signal handler to verify 'user_added_to_group' signal is sent
+        user_added_to_group_handler = MagicMock(
+            name="user_added_to_group_handler")
+        signals.user_added_to_group.connect(
+            user_added_to_group_handler,
+            sender=views.IAMUserViewSet)
+        iam_user_update = views.IAMUserViewSet.as_view({'put': 'update'})
+        response = iam_user_update(request, user_id=username)
+        self.assertEquals(200, response.status_code)
+
+        user_profile_mock.doesUserExist.assert_called_once()
+        group_manager_mock.getAllGroupsUserBelongs.assert_called_once()
+
+        # Since user wasn't added to a group, these all should not have been
+        # called
+        user_profile_mock.getUserProfileById.assert_not_called()
+        group_manager_mock.getGroup.assert_not_called()
+        group_manager_mock.addUsersToGroup.assert_not_called()
+        user_added_to_group_handler.assert_not_called()
diff --git a/django_airavata/apps/api/views.py b/django_airavata/apps/api/views.py
index 02d6701..c168a4f 100644
--- a/django_airavata/apps/api/views.py
+++ b/django_airavata/apps/api/views.py
@@ -1613,21 +1613,22 @@ class IAMUserViewSet(mixins.RetrieveModelMixin,
         group_manager_client = self.request.profile_service['group_manager']
         user_profile_client = self.request.profile_service['user_profile']
         user_id = managed_user_profile['airavataInternalUserId']
-        user_profile = user_profile_client.getUserProfileById(
-            self.authz_token,
-            managed_user_profile['userId'],
-            settings.GATEWAY_ID)
         added_groups = []
         for group_id in managed_user_profile['_added_group_ids']:
             group = group_manager_client.getGroup(self.authz_token, group_id)
             group_manager_client.addUsersToGroup(
                 self.authz_token, [user_id], group_id)
             added_groups.append(group)
-        signals.user_added_to_group.send(
-            sender=self.__class__,
-            user=user_profile,
-            groups=added_groups,
-            request=self.request)
+        if len(added_groups) > 0:
+            user_profile = user_profile_client.getUserProfileById(
+                self.authz_token,
+                managed_user_profile['userId'],
+                settings.GATEWAY_ID)
+            signals.user_added_to_group.send(
+                sender=self.__class__,
+                user=user_profile,
+                groups=added_groups,
+                request=self.request)
         for group_id in managed_user_profile['_removed_group_ids']:
             group_manager_client.removeUsersFromGroup(
                 self.authz_token, [user_id], group_id)