Repository: hydroshare/hydroshare

Issue:
Group invitations not working
**Description of the bug**
I tried to invite dcowan (and then I tested this with demo) to the HydroShare developers group.  After issuing invitations I saw the message "invitation issued" but invitations never appeared on the invitation pending list.

Steps to reproduce the bug:
1. Go to a group where you are the owner and can invite people.
2. In the members tab click on Invite people
3. Type the username of the person you want to invite and click send invitation
4. See the message "An invitation has been sent"
![image](https://user-images.githubusercontent.com/3456833/189507367-05076bf1-591a-466d-bfaa-3fda6fc37bad.png)
5. Refresh the group page.  No pending invitations appear
![image](https://user-images.githubusercontent.com/3456833/189507396-ad46d712-4140-4c19-b14d-a2a83776457b.png)

**Expected behavior**
- The invited group member should appear on the list of pending invitations.
- The invited group member should receive an email with a link to accept the invitation




Discussion:
![image](https://user-images.githubusercontent.com/17934193/189933060-d1c4fcd9-e5c3-4fcf-a3ce-ae30ef17cebc.png)
Seemed to work when I _requested_ membership to "Developers"

Also, the workflow was successful when I created a new group and invited someone (they were emailed and added to the pending list).

I reproduced this issue when the "Require explanation when requesting membership" box is enabled/checked for the group. So this was introduced sometime after 1.55. I will fix it.

Resolving patch:
diff --git a/hs_access_control/models/user.py b/hs_access_control/models/user.py
--- a/hs_access_control/models/user.py
+++ b/hs_access_control/models/user.py
@@ -114,14 +114,14 @@ def create_group_membership_request(self, this_group, this_user=None, explanatio
         elif this_group.gaccess.members.filter(id=this_user.id).exists():
             raise PermissionDenied("User is already a member of this group")
 
-        if this_group.gaccess.requires_explanation:
-            if not explanation:
-                raise PermissionDenied("This group requires an explanation for requests")
-            elif len(explanation) > 300:
-                raise PermissionDenied("Explanation too long. Shorten to 300 characters")
-
         # user (self) requesting to join a group
         if this_user is None:
+            if this_group.gaccess.requires_explanation:
+                if not explanation:
+                    raise PermissionDenied("This group requires an explanation for requests")
+                elif len(explanation) > 300:
+                    raise PermissionDenied("Explanation too long. Shorten to 300 characters")
+
             # check if the user already has made a request to join this_group
             if GroupMembershipRequest.objects.filter(request_from=self.user,
                                                      group_to_join=this_group,


Test patch:
diff --git a/hs_access_control/tests/test_group_membership_request.py b/hs_access_control/tests/test_group_membership_request.py
--- a/hs_access_control/tests/test_group_membership_request.py
+++ b/hs_access_control/tests/test_group_membership_request.py
@@ -446,6 +446,54 @@ def test_user_sending_request_with_explanation(self):
         self.assertEqual(
             self.modeling_group.gaccess.group_membership_requests.count(), 0)
 
+    def test_user_sending_invitation_without_explanation(self):
+        # require explanation on the group
+        self.modeling_group.gaccess.requires_explanation = True
+        self.modeling_group.gaccess.save()
+
+        # lisa should should not be one of the members
+        self.assertNotIn(
+            self.lisa_group_member,
+            self.modeling_group.gaccess.members)
+
+        # user lisa should have no pending request to join group
+        self.assertEqual(
+            self.lisa_group_member.uaccess.group_membership_requests.count(), 0)
+
+        # modeling group should have no pending membership requests
+        self.assertEqual(
+            self.modeling_group.gaccess.group_membership_requests.count(), 0)
+
+        # there should be 4 members in the group
+        self.assertEqual(self.modeling_group.gaccess.members.count(), 4)
+
+        # let john (group owner) invite lisa to join group without explanation
+        membership_request = self.john_group_owner.uaccess.create_group_membership_request(
+            self.modeling_group, self.lisa_group_member)
+
+        # modeling group should have 1 pending membership request
+        self.assertEqual(
+            self.modeling_group.gaccess.group_membership_requests.count(), 1)
+
+        # user lisa should have 1 request to join group
+        self.assertEqual(
+            self.lisa_group_member.uaccess.group_membership_requests.count(), 1)
+
+        # let lisa accept john's invitation
+        self.lisa_group_member.uaccess.act_on_group_membership_request(
+            membership_request, accept_request=True)
+
+        # user lisa should have no pending request to join group
+        self.assertEqual(
+            self.lisa_group_member.uaccess.group_membership_requests.count(), 0)
+
+        # modeling group should have no pending membership requests
+        self.assertEqual(
+            self.modeling_group.gaccess.group_membership_requests.count(), 0)
+
+        # there should be 5 members in the group
+        self.assertEqual(self.modeling_group.gaccess.members.count(), 5)
+
     def test_user_sending_request_auto_approval(self):
         # here we are testing auto approval of group membership request