From bfe9f1ca5ab8f563f116c9d85e0d721de214993b Mon Sep 17 00:00:00 2001 From: John Giannelos Date: Tue, 13 Jan 2015 20:06:20 +0200 Subject: [PATCH] [Fix bug 1120661] Respect group membership status in API results. --- mozillians/users/api.py | 16 ++++++++++------ mozillians/users/tests/test_api.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/mozillians/users/api.py b/mozillians/users/api.py index 62466b651..0dd3f59a9 100644 --- a/mozillians/users/api.py +++ b/mozillians/users/api.py @@ -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): @@ -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 diff --git a/mozillians/users/tests/test_api.py b/mozillians/users/tests/test_api.py index 4c959184e..c70b19009 100644 --- a/mozillians/users/tests/test_api.py +++ b/mozillians/users/tests/test_api.py @@ -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 @@ -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})