Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
SystersUser.leave_community() + test
Browse files Browse the repository at this point in the history
  • Loading branch information
ana-balica committed Feb 8, 2015
1 parent 069a130 commit 5c596b8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions systers_portal/community/constants.py
Expand Up @@ -9,8 +9,10 @@

# status
ALREADY_MEMBER = "already_member"
IS_ADMIN = "is_admin"
JOIN_REQUEST_EXISTS = "join_request_exists"
NO_PENDING_JOIN_REQUEST = "no_pending_join_request"
NOT_MEMBER = "not_member"
OK = "ok"

# messages displayed to the user
Expand Down
20 changes: 19 additions & 1 deletion systers_portal/users/models.py
Expand Up @@ -5,7 +5,8 @@
from django.dispatch import receiver
from django_countries.fields import CountryField

from community.constants import NO_PENDING_JOIN_REQUEST, OK
from community.constants import (NO_PENDING_JOIN_REQUEST, OK, NOT_MEMBER,
IS_ADMIN)
from community.utils import get_groups


Expand Down Expand Up @@ -117,6 +118,23 @@ def delete_all_join_requests(self, community):
join_request.delete()
return OK

def leave_community(self, community):
"""Leave a community. That involves losing all permissions towards
this community.
:param community: Community object
:return: string status: OK if left the community, NOT_MEMBER if the
user was not a member of the community in the first place
"""
if not self.is_member(community):
return NOT_MEMBER
if self == community.community_admin:
return IS_ADMIN
self.leave_groups(community.name)
community.remove_member(self)
community.save()
return OK


def user_unicode(self):
"""Unicode representation of Django User model
Expand Down
32 changes: 32 additions & 0 deletions systers_portal/users/tests/test_models.py
Expand Up @@ -127,6 +127,38 @@ def test_reject_all_join_requests(self):
self.assertFalse(bar_systers_user.is_member(community))
self.assertSequenceEqual(JoinRequest.objects.all(), [])

def test_leave_community(self):
"""Test leaving a community"""
community = Community.objects.create(name="Foo", slug="foo",
order=1,
community_admin=self.systers_user)
status = self.systers_user.leave_community(community)
self.assertEqual(status, "is_admin")

user = User.objects.create_user(username='bar', password='foobar')
bar_systers_user = SystersUser.objects.get(user=user)
status = bar_systers_user.leave_community(community)
self.assertEqual(status, "not_member")

community.add_member(bar_systers_user)
community.save()
self.assertTrue(bar_systers_user.is_member(community))
status = bar_systers_user.leave_community(community)
self.assertEqual(status, "ok")
self.assertFalse(bar_systers_user.is_member(community))

community.add_member(bar_systers_user)
community.save()
self.assertTrue(bar_systers_user.is_member(community))
content_manager_group = Group.objects.get(name="Foo: Content Manager")
bar_systers_user.join_group(content_manager_group)
self.assertSequenceEqual(bar_systers_user.user.groups.all(),
[content_manager_group])
status = bar_systers_user.leave_community(community)
self.assertEqual(status, "ok")
self.assertFalse(bar_systers_user.is_member(community))
self.assertSequenceEqual(bar_systers_user.user.groups.all(), [])


class UserTestCase(TestCase):
def setUp(self):
Expand Down

0 comments on commit 5c596b8

Please sign in to comment.