class TestMozilliansClientUserLookup(TestCase): def setUp(self): self.client = MozilliansClient('http://example.com/api/v2/', 'EXAMPLE_API_KEY') @raises(ResourceDoesNotExist) @responses.activate def test_does_not_exist(self): url = 'http://example.com/api/v2/users' body = '{"count": 0}' responses.add(responses.GET, url, body=body, status=200, content_type='application/json') self.client.lookup_user({'email': '*****@*****.**'}) @raises(MultipleResourcesReturned) @responses.activate def test_multiple_resources_returned(self): url = 'http://example.com/api/v2/users' body = '{"count": 2}' responses.add(responses.GET, url, body=body, status=200, content_type='application/json') self.client.lookup_user({'email': '*****@*****.**'}) @responses.activate def test_valid_response(self): resource_url = 'http://example.com/api/v2/users' profile_url = 'http://example.com/api/v2/users/1' body = '{"count": 1, "results": [{"_url": "http://example.com/api/v2/users/1"}]}' detailed_body = '{"key1": "value1", "key2": "value2"}' responses.add(responses.GET, resource_url, body=body, status=200, content_type='application/json') responses.add(responses.GET, profile_url, body=detailed_body, status=200, content_type='application/json') response = self.client.lookup_user({'email': '*****@*****.**'}) eq_(len(responses.calls), 2) query = parse_qs(urlparse(responses.calls[0].request.url).query) eq_(query['email'][0], '*****@*****.**') eq_(response, json.loads(detailed_body)) @responses.activate def test_vouched_status(self): url = 'http://example.com/api/v2/users' expected_result = '{"_url": "http://example.com/users/1", "is_vouched": true}' body = '{"count": 1, "results": [%s]}' % expected_result responses.add(responses.GET, url, body=body, status=200, content_type='application/json') user, status = self.client.is_vouched('*****@*****.**') eq_(len(responses.calls), 1) query = parse_qs(urlparse(responses.calls[0].request.url).query) eq_(query['email'][0], '*****@*****.**') eq_(user, json.loads(expected_result)) eq_(status, True)
class MozilliansAuthBackend(BrowserIDBackend): def __init__(self, *args, **kwargs): super(MozilliansAuthBackend, self).__init__(*args, **kwargs) api_url = settings.MOZILLIANS_API_URL api_key = settings.MOZILLIANS_API_KEY self.mozillians_client = MozilliansClient(api_url, api_key) def _normalize_email(self, email): email_name, domain = email.strip().rsplit('@', 1) email = '@'.join([email_name, domain.lower()]) return email def is_valid_email(self, email): try: user = self.mozillians_client.lookup_user({'email': email}, detailed=False) except: user = None authentication_policy = getattr(settings, 'MOZILLIANS_AUTHENTICATION_POLICY', lambda x: True) return user and authentication_policy(user) def create_user(self, email): user = super(MozilliansAuthBackend, self).create_user(self._normalize_email(email)) try: mozillian = self.mozillians_client.lookup_user({'email': email}) except: mozillian = None if user and mozillian: user.mozillians_url = mozillian['url'] user.mozillians_username = mozillian['username'] user.save() return user
def handle(self, *args, **options): api_url = settings.MOZILLIANS_API_URL api_key = settings.MOZILLIANS_API_KEY mozillians_client = MozilliansClient(api_url, api_key) for user in User.objects.all(): try: mozillian = mozillians_client.lookup_user({'email': user.email}, detailed=False) user.mozillians_username = mozillian['username'] user.save() self.stdout.write('Fetching username for {0}'.format(user.email)) except: self.stdout.write('Unable to fetch username for {0}'.format(user.email)) continue
class MozilliansAuthBackend(BrowserIDBackend): def __init__(self, *args, **kwargs): super(MozilliansAuthBackend, self).__init__(*args, **kwargs) api_url = settings.MOZILLIANS_API_URL api_key = settings.MOZILLIANS_API_KEY self.mozillians_client = MozilliansClient(api_url, api_key) def _normalize_email(self, email): email_name, domain = email.strip().rsplit('@', 1) email = '@'.join([email_name, domain.lower()]) return email def is_valid_email(self, email): try: user = self.mozillians_client.lookup_user({'email': email}, detailed=False) except: user = None authentication_policy = getattr(settings, 'MOZILLIANS_AUTHENTICATION_POLICY', lambda x: True) return user and authentication_policy(user) def create_user(self, email): user = super(MozilliansAuthBackend, self).create_user(self._normalize_email(email)) try: mozillian = self.mozillians_client.lookup_user({'email': email}) except: mozillian = None if user and mozillian: user.mozillians_url = mozillian['url'] user.mozillians_username = mozillian['username'] user.save() return user
def handle(self, *args, **options): api_url = settings.MOZILLIANS_API_URL api_key = settings.MOZILLIANS_API_KEY mozillians_client = MozilliansClient(api_url, api_key) for user in User.objects.all(): try: mozillian = mozillians_client.lookup_user( {'email': user.email}, detailed=False) user.mozillians_username = mozillian['username'] user.save() self.stdout.write('Fetching username for {0}'.format( user.email)) except: self.stdout.write('Unable to fetch username for {0}'.format( user.email)) continue