diff --git a/bookwyrm/tests/views/test_group.py b/bookwyrm/tests/views/test_group.py index 069f87d3..5bdd3a6c 100644 --- a/bookwyrm/tests/views/test_group.py +++ b/bookwyrm/tests/views/test_group.py @@ -26,6 +26,13 @@ class GroupViews(TestCase): local=True, localname="mouse", ) + self.rat = models.User.objects.create_user( + "rat@local.com", + "rat@rat.rat", + "password", + local=True, + localname="rat", + ) self.testgroup = models.Group.objects.create( name="Test Group", @@ -97,7 +104,6 @@ class GroupViews(TestCase): def test_group_edit(self, _): """test editing a "group" database entry""" - view = views.Group.as_view() request = self.factory.post( "", @@ -126,3 +132,59 @@ class GroupViews(TestCase): def test_invite_member(self, _): """invite a member to a group""" + request = self.factory.post( + "", + { + "group": self.testgroup.id, + "user": self.rat.localname, + }, + ) + request.user = self.local_user + result = views.invite_member(request) + self.assertEqual(result.status_code, 302) + + invite = models.GroupMemberInvitation.objects.get() + self.assertEqual(invite.user, self.rat) + self.assertEqual(invite.group, self.testgroup) + + def test_invite_member_twice(self, _): + """invite a member to a group again""" + request = self.factory.post( + "", + { + "group": self.testgroup.id, + "user": self.rat.localname, + }, + ) + request.user = self.local_user + result = views.invite_member(request) + self.assertEqual(result.status_code, 302) + result = views.invite_member(request) + self.assertEqual(result.status_code, 302) + + def test_remove_member_denied(self, _): + """remove member""" + request = self.factory.post( + "", + { + "group": self.testgroup.id, + "user": self.local_user.localname, + }, + ) + request.user = self.local_user + result = views.remove_member(request) + self.assertEqual(result.status_code, 400) + + def test_remove_member_non_member(self, _): + """remove member but wait, that's not a member""" + request = self.factory.post( + "", + { + "group": self.testgroup.id, + "user": self.rat.localname, + }, + ) + request.user = self.local_user + result = views.remove_member(request) + # nothing happens + self.assertEqual(result.status_code, 302) diff --git a/bookwyrm/views/group.py b/bookwyrm/views/group.py index dd207a80..7e610630 100644 --- a/bookwyrm/views/group.py +++ b/bookwyrm/views/group.py @@ -180,19 +180,13 @@ def delete_group(request, group_id): def invite_member(request): """invite a member to the group""" group = get_object_or_404(models.Group, id=request.POST.get("group")) - if not group: - return HttpResponseBadRequest() - user = get_user_from_username(request.user, request.POST["user"]) - if not user: - return HttpResponseBadRequest() if not group.user == request.user: return HttpResponseBadRequest() try: models.GroupMemberInvitation.objects.create(user=user, group=group) - except IntegrityError: pass @@ -203,17 +197,11 @@ def invite_member(request): @login_required def remove_member(request): """remove a member from the group""" - group = get_object_or_404(models.Group, id=request.POST.get("group")) - if not group: - return HttpResponseBadRequest() - user = get_user_from_username(request.user, request.POST["user"]) - if not user: - return HttpResponseBadRequest() # you can't be removed from your own group - if request.POST["user"] == group.user: + if user == group.user: return HttpResponseBadRequest() is_member = models.GroupMember.objects.filter(group=group, user=user).exists() @@ -233,11 +221,9 @@ def remove_member(request): pass if is_member: - try: models.List.remove_from_group(group.user, user) models.GroupMember.remove(group.user, user) - except IntegrityError: pass