def test_cis_groups_highest(self): user = UserFactory.create() group1 = GroupFactory.create(name='nda', is_access_group=True) group2 = GroupFactory.create(name='cis_whitelist', is_access_group=True) group3 = GroupFactory.create(name='open innovation + reps council', is_access_group=True) group4 = GroupFactory.create(name='group4') group1.add_member(user.userprofile) group2.add_member(user.userprofile) group3.add_member(user.userprofile) group4.add_member(user.userprofile, status='PENDING') IdpProfile.objects.create( profile=user.userprofile, auth0_user_id='github|[email protected]', primary=False, email='*****@*****.**' ) idp = IdpProfile.objects.create( profile=user.userprofile, auth0_user_id='ad|[email protected]', primary=True, email='*****@*****.**' ) eq_(set(user.userprofile.get_cis_groups(idp)), set(['mozilliansorg_nda', 'mozilliansorg_cis_whitelist', 'mozilliansorg_open-innovation-reps-council']))
def test_set_membership_group_matches_alias(self): group_1 = GroupFactory.create(name='foo') group_2 = GroupFactory.create(name='lo') GroupAliasFactory.create(alias=group_2, name='bar') user = UserFactory.create() user.userprofile.set_membership(Group, ['foo', 'bar']) eq_(set(user.userprofile.groups.all()), set([group_1, group_2]))
def test_update_basket_task_without_token(self, basket_mock, lookup_token_mock, request_mock): lookup_token_mock.return_value = "basket_token" user = UserFactory.create(userprofile={'is_vouched': True, 'country': 'gr', 'city': 'athens'}) group = GroupFactory.create( name='Web Development', steward=user.userprofile) GroupFactory.create(name='Marketing', steward=user.userprofile) group.members.add(user.userprofile) data = {'country': 'gr', 'city': 'athens', 'WEB_DEVELOPMENT': 'Y', 'MARKETING': 'N'} basket_mock.subscribe.return_value = {} update_basket_task(user.userprofile.id) basket_mock.subscribe.assert_called_with( user.email, 'newsletter', trigger_welcome='N') request_mock.assert_called_with( 'post', 'custom_update_phonebook', token='basket_token', data=data) ok_(UserProfile.objects.filter( basket_token='basket_token', id=user.userprofile.id).exists())
def test_export_view_post(self): user = UserFactory.create(is_superuser=True, is_staff=True) export_url = reverse('admin:users_userprofile_export') # NDA group required for admin response rendering GroupFactory.create(name=settings.NDA_GROUP) mock_bucket = MagicMock() mock_connection = MagicMock() mock_connection.get_bucket.return_value = mock_bucket with patch('mozillians.common.mixins.S3ExportMixin.get_export_filename') as mock_filename: with patch('mozillians.common.mixins.boto') as mock_boto: mock_boto.connect_s3.return_value = mock_connection mock_filename.return_value = 'example_filename.format' with self.login(user) as client: data = { 'file_format': '0' } client.post(export_url, data=data) kwargs = { 'calling_format': ANY, 'aws_access_key_id': 'foo', 'aws_secret_access_key': 'bar' } calling_format = mock_boto.connect_s3.call_args[1]['calling_format'] mock_boto.connect_s3.assert_called_with(**kwargs) ok_(isinstance(calling_format, boto.s3.connection.OrdinaryCallingFormat)) mock_connection.get_bucket.assert_called_with('s3-bucket') mock_bucket.new_key.assert_called_with('example_filename.format')
def test_update_basket_task(self, mock_basket): # When a user is created or added to a group, the appropriate # calls to update basket are made email = '*****@*****.**' token = 'footoken' mock_basket.lookup_user.return_value = { 'email': email, } mock_basket.subscribe.return_value = { 'token': token, } user = UserFactory.create(email=email) mock_basket.subscribe.reset_mock() # forget that subscribe was called group = GroupFactory.create(name='Web Development', functional_area=True) GroupFactory.create(name='Marketing', functional_area=True) data = {'country': 'gr', 'city': 'Athens', 'WEB_DEVELOPMENT': 'Y', 'MARKETING': 'N'} group.add_member(user.userprofile) # We just added a group, we should not need to subscribe anything ok_(not mock_basket.subscribe.called) # But we do need to update their phonebook record mock_basket.request.assert_called_with( 'post', 'custom_update_phonebook', token=token, data=data)
def test_set_membership_group_matches_alias(self): group_1 = GroupFactory.create(name="foo") group_2 = GroupFactory.create(name="lo") GroupAliasFactory.create(alias=group_2, name="bar") user = UserFactory.create() user.userprofile.set_membership(Group, ["foo", "bar"]) eq_(set(user.userprofile.groups.all()), set([group_1, group_2]))
def test_extract_document(self): user = UserFactory.create(userprofile={'allows_community_sites': False, 'allows_mozilla_sites': False, 'full_name': 'Nikos Koukos', 'bio': 'This is my bio'}) profile = user.userprofile group_1 = GroupFactory.create() group_2 = GroupFactory.create() skill_1 = SkillFactory.create() skill_2 = SkillFactory.create() LanguageFactory.create(code='fr', userprofile=profile) LanguageFactory.create(code='en', userprofile=profile) group_1.add_member(profile) group_2.add_member(profile) profile.skills.add(skill_1) profile.skills.add(skill_2) result = UserProfileMappingType.extract_document(profile.id) ok_(isinstance(result, dict)) eq_(result['id'], profile.id) eq_(result['is_vouched'], profile.is_vouched) eq_(result['region'], 'attika') eq_(result['city'], 'athens') eq_(result['allows_community_sites'], profile.allows_community_sites) eq_(result['allows_mozilla_sites'], profile.allows_mozilla_sites) eq_(set(result['country']), set(['gr', 'greece'])) eq_(result['fullname'], profile.full_name.lower()) eq_(result['name'], profile.full_name.lower()) eq_(result['bio'], profile.bio) eq_(result['has_photo'], False) eq_(result['groups'], [group_1.name, group_2.name]) eq_(result['skills'], [skill_1.name, skill_2.name]) eq_(set(result['languages']), set([u'en', u'fr', u'english', u'french', u'français']))
def test_valid_name(self): """Valid group with name that matches the old group regex doens't redirect.""" group = GroupFactory.create(name='111-foo') GroupFactory.create(name='foo') url = reverse('groups:show', kwargs={'url': group.url}) with self.login(self.user) as client: response = client.get(url, follow=True) eq_(response.status_code, 200) eq_(response.context['group'], group)
def test_autocomplete_assign(self): user = UserFactory.create() group_1 = GroupFactory.create() group_2 = GroupFactory.create(auto_complete=False) group_1.members.add(user.userprofile) tasks.assign_autocomplete_to_groups() group_1 = Group.objects.get(pk=group_1.pk) group_2 = Group.objects.get(pk=group_2.pk) eq_(group_1.auto_complete, True) eq_(group_2.auto_complete, False)
def setUp(self): self.user = UserFactory.create() self.group_1 = GroupFactory.create(name='abc') self.group_2 = GroupFactory.create(name='def') self.group_2.add_member(self.user.userprofile) self.query = Group.objects.filter(pk__in=[self.group_1.pk, self.group_2.pk]) self.template = 'groups/index.html' self.request = RequestFactory() self.request.GET = {} self.request.user = self.user
def test_merge_groups(self): master_group = GroupFactory.create() merge_group_1 = GroupFactory.create() merge_group_2 = GroupFactory.create() nested_group = GroupFactory.create() merge_group_1.merge_groups([nested_group]) master_group.merge_groups([merge_group_1, merge_group_2]) eq_(master_group.aliases.count(), 4) for group in [merge_group_1, merge_group_2, nested_group]: ok_(master_group.aliases.filter(name=group.name, url=group.url).exists()) ok_(not Group.objects.filter(pk=group.pk).exists())
def test_distinct_results(self): user = UserFactory.create() group_1 = GroupFactory.create() group_2 = GroupFactory.create() group_1.add_member(user.userprofile) group_2.add_member(user.userprofile) client = Client() url = urlparams(self.mozilla_resource_url, groups=",".join([group_1.name, group_2.name])) response = client.get(url, follow=True) data = json.loads(response.content) eq_(response.status_code, 200) eq_(len(data["objects"]), 1)
def test_get_annotated_groups_only_visible(self): """ Test that get_annotated_groups() only returns visible groups """ group_1 = GroupFactory.create(visible=True) group_2 = GroupFactory.create(visible=False) profile = UserFactory.create().userprofile group_1.add_member(profile) group_2.add_member(profile) user_groups = profile.get_annotated_groups() eq_([group_1], user_groups)
def setUp(self): self.user = UserFactory.create(userprofile={'is_vouched': True}) self.group_1 = GroupFactory.create() self.group_2 = GroupFactory.create() self.group_2.members.add(self.user.userprofile) self.query = (Group.objects .filter(pk__in=[self.group_1.pk, self.group_2.pk]) .annotate(num_members=Count('members'))) self.template = 'groups/index.html' self.request = RequestFactory() self.request.GET = {} self.request.user = self.user
def test_search_existing_group(self): user = UserFactory.create() group_1 = GroupFactory.create(visible=True) GroupFactory.create() url = urlparams(reverse("groups:search_groups"), term=group_1.name) with self.login(user) as client: response = client.get(url, follow=True, **{"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"}) eq_(response.status_code, 200) eq_(response.get("content-type"), "application/json") data = json.loads(response.content) eq_(len(data), 1, "Non autocomplete groups are included in search") eq_(data[0], group_1.name)
def test_index_functional_areas(self): user = UserFactory.create(userprofile={"is_vouched": True}) group_1 = GroupFactory.create(steward=user.userprofile) group_2 = GroupFactory.create() GroupFactory.create() group_1.members.add(user.userprofile) group_2.members.add(user.userprofile) with self.login(user) as client: response = client.get(self.url, follow=True) eq_(response.status_code, 200) self.assertTemplateUsed(response, "groups/index_areas.html") eq_(set(response.context["groups"].paginator.object_list), set([group_1]))
def test_search_existing_group(self): user = UserFactory.create(userprofile={'is_vouched': True}) group_1 = GroupFactory.create(auto_complete=True) GroupFactory.create() url = urlparams(reverse('groups:search_groups'), term=group_1.name) with self.login(user) as client: response = client.get(url, follow=True, **{'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}) eq_(response.status_code, 200) eq_(response.get('content-type'), 'application/json') data = json.loads(response.content) eq_(len(data), 1, 'Non autocomplete groups are included in search') eq_(data[0], group_1.name)
def test_search_groups(self): client = Client() group_1 = GroupFactory.create() group_2 = GroupFactory.create() user_1 = UserFactory.create(userprofile={'is_vouched': True}) user_1.userprofile.groups.add(group_1) user_2 = UserFactory.create(userprofile={'is_vouched': True}) user_2.userprofile.groups.add(group_2) url = urlparams(self.mozilla_resource_url, groups=group_1.name) response = client.get(url, follow=True) data = json.loads(response.content) eq_(len(data['objects']), 1) eq_(data['objects'][0]['id'], unicode(user_1.userprofile.id))
def test_search_groups(self): client = Client() group_1 = GroupFactory.create() group_2 = GroupFactory.create() user_1 = UserFactory.create() group_1.add_member(user_1.userprofile) user_2 = UserFactory.create() group_2.add_member(user_2.userprofile) url = urlparams(self.mozilla_resource_url, groups=group_1.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_index(self): user_1 = UserFactory.create(userprofile={"is_vouched": True}) user_2 = UserFactory.create() group_1 = GroupFactory.create() group_2 = GroupFactory.create() group_3 = GroupFactory.create() group_1.members.add(user_1.userprofile) group_2.members.add(user_1.userprofile) group_3.members.add(user_2.userprofile) with self.login(user_1) as client: response = client.get(self.url, follow=True) eq_(response.status_code, 200) self.assertTemplateUsed(response, "groups/index_groups.html") eq_(set(response.context["groups"].paginator.object_list), set([group_1, group_2]))
def test_index_functional_areas(self): user = UserFactory.create() group_1 = GroupFactory.create(curator=user.userprofile, functional_area=True) group_2 = GroupFactory.create() GroupFactory.create() group_1.add_member(user.userprofile) group_2.add_member(user.userprofile) with self.login(user) as client: response = client.get(self.url, follow=True) eq_(response.status_code, 200) self.assertTemplateUsed(response, 'groups/index_areas.html') eq_(set(response.context['groups'].paginator.object_list), set([group_1]))
def test_edit_invalidation_invalid_data(self): group = GroupFactory.create() data = {'invalidation_days': 1000} form = self.validate_group_edit_forms(forms.GroupTermsExpirationForm, group, data, None, False) eq_(form.errors, {'invalidation_days': [u'The maximum expiration date for a group ' 'cannot exceed two years.']})
def test_update_basket_task_without_token(self, basket_mock, lookup_token_mock, request_mock): lookup_token_mock.return_value = "basket_token" user = UserFactory.create(userprofile={"is_vouched": True, "country": "gr", "city": "athens"}) group = GroupFactory.create(name="Web Development", curator=user.userprofile) GroupFactory.create(name="Marketing", curator=user.userprofile) group.add_member(user.userprofile) data = {"country": "gr", "city": "athens", "WEB_DEVELOPMENT": "Y", "MARKETING": "N"} basket_mock.subscribe.return_value = {} update_basket_task(user.userprofile.id) basket_mock.subscribe.assert_called_with(user.email, "newsletter", trigger_welcome="N") request_mock.assert_called_with("post", "custom_update_phonebook", token="basket_token", data=data) ok_(UserProfile.objects.filter(basket_token="basket_token", id=user.userprofile.id).exists())
def setUp(self): voucher = UserFactory.create(userprofile={'is_vouched': True}) self.user = UserFactory.create( userprofile={'is_vouched': True, 'vouched_by': voucher.userprofile}) group = GroupFactory.create() self.user.userprofile.groups.add(group) skill = SkillFactory.create() self.user.userprofile.skills.add(skill) language = LanguageFactory.create() self.user.userprofile.languages.add(language) self.resource_url = reverse( 'api_dispatch_list', kwargs={'api_name': 'v1', 'resource_name': 'users'}) self.mozilla_app = APIAppFactory.create( owner=self.user, is_mozilla_app=True) self.mozilla_resource_url = urlparams( self.resource_url, app_name=self.mozilla_app.name, app_key=self.mozilla_app.key) self.community_app = APIAppFactory.create( owner=self.user, is_mozilla_app=False) self.community_resource_url = urlparams( self.resource_url, app_name=self.community_app.name, app_key=self.community_app.key)
def test_search_no_ajax(self): user = UserFactory.create() group = GroupFactory.create() url = urlparams(reverse('groups:search_groups'), term=group.name) with self.login(user) as client: response = client.get(url, follow=True) ok_(isinstance(response, HttpResponseBadRequest))
def test_old_group_url_redirects(self): group = GroupFactory.create() url = '/group/111-{0}/'.format(group.url) with self.login(self.user) as client: response = client.get(url, follow=True) eq_(response.status_code, 200) eq_(response.context['group'], group)
def test_search_distict_results(self): group_1 = GroupFactory.create(name='automation', visible=True) GroupAliasFactory.create(alias=group_1, name='automation development') GroupAliasFactory.create(alias=group_1, name='automation services') results = Group.search('automation') eq_(len(results), 1) eq_(results[0], group_1)
def test_sending_pending_email(self): # If a curated group has a pending membership, added since the reminder email # was last sent, send the curator an email. It should contain the count of # all pending memberships. curator = UserFactory.create() group = GroupFactory.create(curator=curator.userprofile) # Add a couple of pending memberships group.add_member(UserFactory.create().userprofile, GroupMembership.PENDING) group.add_member(UserFactory.create().userprofile, GroupMembership.PENDING) with patch('mozillians.groups.tasks.send_mail', autospec=True) as mock_send_mail: tasks.send_pending_membership_emails() ok_(mock_send_mail.called) # Should only have been called once eq_(1, len(mock_send_mail.call_args_list)) # The message body should mention that there are 2 pending memberships subject, body, from_addr, to_list = mock_send_mail.call_args[0] eq_('2 outstanding requests to join Mozillians group "%s"' % group.name, subject) ok_('There are 2 outstanding requests' in body) # Full path to group page is in the message ok_(group.get_absolute_url() in body) print("to_list=%s, curator.email=%s" % (to_list, curator.email)) ok_(curator.email in to_list) # Add another pending membership group.add_member(UserFactory.create().userprofile, GroupMembership.PENDING) # Should send email again with patch('mozillians.groups.tasks.send_mail', autospec=True) as mock_send_mail: tasks.send_pending_membership_emails() ok_(mock_send_mail.called)
def test_set_membership_system_group(self): # a "system" group is invisible and cannot be joined or left group_1 = GroupFactory.create(visible=False, members_can_leave=False, accepting_new_members="no") user = UserFactory.create() user.userprofile.set_membership(Group, [group_1.name, "bar"]) ok_(user.userprofile.groups.filter(name="bar").exists()) eq_(user.userprofile.groups.count(), 1)
def test_remove_empty_groups(self): user = UserFactory.create() group_1 = GroupFactory.create() GroupFactory.create() skill_1 = SkillFactory.create() SkillFactory.create() group_1.add_member(user.userprofile) skill_1.members.add(user.userprofile) tasks.remove_empty_groups() eq_(Group.objects.all().count(), 1) ok_(Group.objects.filter(id=group_1.id).exists()) eq_(Skill.objects.all().count(), 1) ok_(Skill.objects.filter(id=skill_1.id).exists())
def setUp(self): self.group = GroupFactory.create() self.user = UserFactory.create() self.factory = RequestFactory()
def test_can_join_by_request(self): group = GroupFactory.create(accepting_new_members='by_request') user = UserFactory.create() ok_(group.user_can_join(user.userprofile))
def test_is_nda_when_not_member(self): user = UserFactory.create() GroupFactory.create(name='foobar') ok_(not user.userprofile.is_nda)
def test_unvouched_cant_join(self): group = GroupFactory.create(accepting_new_members='yes') user = UserFactory.create(vouched=False) ok_(not group.user_can_join(user.userprofile))
def test_edit_terms(self): group = GroupFactory.create() data = {'terms': 'foobar'} self.validate_group_edit_forms(forms.GroupTermsExpirationForm, group, data)
def test_edit_terms_without_data(self): group = GroupFactory.create() data = {} self.validate_group_edit_forms(forms.GroupTermsExpirationForm, group, data)
def test_set_membership_system_group(self): group_1 = GroupFactory.create(system=True) user = UserFactory.create() user.userprofile.set_membership(Group, [group_1.name, 'bar']) ok_(user.userprofile.groups.filter(name='bar').exists()) eq_(user.userprofile.groups.count(), 1)
def test_edit_invalidation(self): group = GroupFactory.create() data = {'invalidation_days': 5} self.validate_group_edit_forms(forms.GroupTermsExpirationForm, group, data)
def setUp(self): self.group = GroupFactory.create() self.url = reverse('groups:show', kwargs={'url': self.group.url}) self.user_1 = UserFactory.create(userprofile={'is_vouched': True}) self.user_2 = UserFactory.create(userprofile={'is_vouched': True}) self.group.members.add(self.user_2.userprofile)
def test_edit_basic_form_without_data(self): group = GroupFactory.create() data = {} form = self.validate_group_edit_forms(forms.GroupBasicForm, group, data, None, False) eq_(form.errors, {'name': [u'This field is required.']})
def test_edit_terms_and_invalidation(self): group = GroupFactory.create() data = {'terms': 'foobar', 'invalidation_days': 40} self.validate_group_edit_forms(forms.GroupTermsExpirationForm, group, data)
def setUp(self): self.group = GroupFactory.create() self.user = UserFactory.create(userprofile={'is_vouched': True})
def test_pending_cant_join(self): group = GroupFactory.create(accepting_new_members='yes') user = UserFactory.create() group.add_member(user.userprofile, GroupMembership.PENDING) ok_(not group.user_can_join(user.userprofile))
def test_is_nda_when_member(self): user = UserFactory.create() group = GroupFactory.create(name='foobar') group.add_member(user.userprofile) ok_(user.userprofile.is_nda)
def setUp(self): self.group = GroupFactory.create() self.group.curators.add(UserFactory.create().userprofile) self.user = UserFactory.create()
def test_name_unique(self): group = GroupFactory.create() GroupAliasFactory.create(alias=group, name='bar') form = GroupForm({'name': 'bar'}) ok_(not form.is_valid()) ok_('name' in form.errors)
def test_is_nda_when_pending(self): user = UserFactory.create() group = GroupFactory.create(name='foobar') group.add_member(user.userprofile, status='PENDING') ok_(not user.userprofile.is_nda)