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

Commit

Permalink
Merge pull request #1147 from johngian/fix-1120661
Browse files Browse the repository at this point in the history
[Fix bug 1120661] Respect group membership status in API results.
  • Loading branch information
johngian committed Jan 14, 2015
2 parents 1a049d2 + bfe9f1c commit 468f47c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
16 changes: 10 additions & 6 deletions mozillians/users/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from mozillians.api.paginator import Paginator
from mozillians.api.resources import (ClientCacheResourceMixIn,
GraphiteMixIn)
from mozillians.users.models import UserProfile
from mozillians.users.models import GroupMembership, UserProfile


class UserResource(ClientCacheResourceMixIn, GraphiteMixIn, ModelResource):
Expand Down Expand Up @@ -100,11 +100,15 @@ def build_filters(self, filters=None):
**{'{0}__iexact'.format('ircname'):
getvalue('ircname')})

for group_filter in ['groups', 'skills']:
if group_filter in valid_filters:
database_filters[group_filter] = Q(
**{'{0}__name__in'.format(group_filter):
getvalue(group_filter).split(',')})
if 'groups' in valid_filters:
kwargs = {
'groups__name__in': getvalue('groups').split(','),
'groupmembership__status': GroupMembership.MEMBER
}
database_filters['groups'] = Q(**kwargs)

if 'skills' in valid_filters:
database_filters['skills'] = Q(skills__name__in=getvalue('skills').split(','))

return database_filters

Expand Down
28 changes: 28 additions & 0 deletions mozillians/users/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from mozillians.api.tests import APIAppFactory
from mozillians.common.tests import TestCase
from mozillians.geo.tests import CityFactory, CountryFactory, RegionFactory
from mozillians.groups.models import GroupMembership
from mozillians.groups.tests import GroupFactory, SkillFactory
from mozillians.users.models import ExternalAccount
from mozillians.users.tests import UserFactory
Expand Down Expand Up @@ -210,6 +211,33 @@ def test_search_groups(self):
eq_(len(data['objects']), 1)
eq_(data['objects'][0]['id'], user_1.userprofile.id)

def test_search_groups_mixed_membership(self):
client = Client()
group = GroupFactory.create()
user_1 = UserFactory.create()
user_2 = UserFactory.create()
group.add_member(user_1.userprofile)
group.add_member(user_2.userprofile, GroupMembership.PENDING)

url = urlparams(self.mozilla_resource_url, groups=group.name)
response = client.get(url, follow=True)
data = json.loads(response.content)
eq_(len(data['objects']), 1)
eq_(data['objects'][0]['id'], user_1.userprofile.id)

def test_search_groups_pending_membership(self):
client = Client()
group = GroupFactory.create()
user_1 = UserFactory.create()
user_2 = UserFactory.create()
group.add_member(user_1.userprofile, GroupMembership.PENDING)
group.add_member(user_2.userprofile, GroupMembership.PENDING)

url = urlparams(self.mozilla_resource_url, groups=group.name)
response = client.get(url, follow=True)
data = json.loads(response.content)
eq_(len(data['objects']), 0)

def test_search_combined_skills_country(self):
country = CountryFactory.create(code='fr')
user_1 = UserFactory.create(userprofile={'geo_country': country})
Expand Down

0 comments on commit 468f47c

Please sign in to comment.