def setUp(self):
        """Setup class."""

        # Avoid circular dependencies
        from mozillians.common.authbackend import MozilliansAuthBackend

        self.backend = MozilliansAuthBackend()
 def test_random_source(self, authenticate_mock):
     backend = MozilliansAuthBackend()
     request_mock = Mock()
     request_mock.META = {
         'HTTP_REFERER': settings.SITE_URL + '/?source=foobar'
     }
     backend.request = request_mock
     backend.authenticate(request=request_mock)
     eq_(backend.referral_source, None)
 def test_get_involved_source(self, authenticate_mock):
     backend = MozilliansAuthBackend()
     request_mock = Mock()
     request_mock.META = {
         'HTTP_REFERER': settings.SITE_URL + '/?source=contribute'
     }
     backend.request = request_mock
     backend.authenticate(request=request_mock)
     eq_(backend.referral_source, 'contribute')
 def test_filter_users_by_secondary_email(self):
     backend = MozilliansAuthBackend()
     user = UserFactory.create(email='*****@*****.**')
     kwargs = {
         'type': ExternalAccount.TYPE_EMAIL,
         'identifier': '*****@*****.**'
     }
     user.userprofile.externalaccount_set.create(**kwargs)
     result = backend.filter_users_by_email('*****@*****.**')
     eq_(result[0], user)
    def test_create_user_integrity_error(self):
        backend = MozilliansAuthBackend()
        backend.User = Mock()
        error = IntegrityError()
        user = UserFactory.create()
        backend.User.objects.create_user.side_effect = error
        backend.User.objects.get.return_value = user

        eq_(backend.create_user('*****@*****.**'), user)

        backend.User.DoesNotExist = Exception
        backend.User.objects.get.side_effect = backend.User.DoesNotExist
        with self.assertRaises(IntegrityError) as e:
            backend.create_user('*****@*****.**')

        eq_(e.exception, error)