diff --git a/apps/amo/authentication.py b/apps/amo/authentication.py index f3539277..8e741d7e 100644 --- a/apps/amo/authentication.py +++ b/apps/amo/authentication.py @@ -7,6 +7,7 @@ from django.conf import settings from amo.helpers import get_amo_cursor from person.models import Profile +from utils.amo import AMOOAuth DEFAULT_AMO_PASSWORD = 'saved in AMO' @@ -104,7 +105,7 @@ class AMOAuthentication: username = user_data['id'] self.user_data = user_data return username - + @staticmethod def auth_browserid_authenticate(email): """ @@ -112,25 +113,11 @@ class AMOAuthentication: browserid """ return AMOAuthentication.fetch_amo_user(email) - + @staticmethod def fetch_amo_user(email): - columns = ('id', 'email', 'username', 'password', - 'display_name', 'homepage') - auth_cursor = get_amo_cursor() - - SQL = ('SELECT %s FROM %s WHERE email=%%s') % ( - ','.join(columns), settings.AUTH_DATABASE['TABLE']) - auth_cursor.execute(SQL, email) - data = auth_cursor.fetchone() - if data == None: - return None - - user_data = {} - for i in range(len(data)): - user_data[columns[i]] = data[i] - - return user_data + amo = AMOOAuth() + return amo.get_user_by_email(email) or None def get_hexdigest(algorithm, salt, raw_password): diff --git a/apps/amo/tests/auth_tests.py b/apps/amo/tests/auth_tests.py index 24ab97cf..5bbfcf11 100644 --- a/apps/amo/tests/auth_tests.py +++ b/apps/amo/tests/auth_tests.py @@ -2,10 +2,21 @@ from django.conf import settings from django.test import TestCase from django.contrib.auth import authenticate +from mock import Mock from nose import SkipTest +from nose.tools import eq_ + +from amo.authentication import AMOAuthentication +from utils.amo import AMOOAuth + + +OLD_AMOOAUTH_SEND = AMOOAuth._send class AuthTest(TestCase): + def tearDown(self): + AMOOAuth._send = OLD_AMOOAUTH_SEND + def test_failing_login(self): # testing failed authentication on AMO # this test assumes FlightDeck has access to AMO database @@ -16,7 +27,8 @@ class AuthTest(TestCase): password='user') ) - def test_successful_login(self): + @staticmethod + def test_successful_login(): # if settings_local contains AMO user data check if login is # successful # assumes that FlightDeck has access to AMO database @@ -25,3 +37,20 @@ class AuthTest(TestCase): assert authenticate( username=settings.TEST_AMO_USERNAME, password=settings.TEST_AMO_PASSWORD) + + @staticmethod + def test_get_user(): + + AMOOAuth._send = Mock(return_value={ + "username": "some", + "display_name": "Some User", + "created": "2007-03-05 13:09:38", + "modified": "2012-01-31 12:38:50", + "id": 12345, + "location": "Portland, OR", + "homepage": "http://example.com", + "email": "some@example.com", + "occupation": "addon developer" + }) + eq_(AMOAuthentication.fetch_amo_user('some@example.com')['username'], + 'some') diff --git a/apps/amo/tests/upload_to_amo_tests.py b/apps/amo/tests/upload_to_amo_tests.py index 6e09a70a..f3a2c536 100644 --- a/apps/amo/tests/upload_to_amo_tests.py +++ b/apps/amo/tests/upload_to_amo_tests.py @@ -20,6 +20,8 @@ from utils.amo import AMOOAuth log = commonware.log.getLogger('f.test') +OLD_AMOOAUTH_SEND = AMOOAuth._send + class UploadTest(TestCase): fixtures = ['mozilla_user', 'users', 'core_sdk', 'packages'] ADDON_AMO_ID = 1 @@ -35,6 +37,8 @@ class UploadTest(TestCase): protocol=settings.AMOOAUTH_PROTOCOL, prefix=settings.AMOOAUTH_PREFIX) + def tearDown(self): + AMOOAuth._send = OLD_AMOOAUTH_SEND def test_create_new_amo_addon(self): AMOOAuth._send = Mock(return_value={ diff --git a/utils/amo.py b/utils/amo.py index dc2d64ed..d44de755 100644 --- a/utils/amo.py +++ b/utils/amo.py @@ -214,6 +214,9 @@ class AMOOAuth: def get_user(self): return self._send(self.url('user'), 'GET', {}) + def get_user_by_email(self, email): + return self._send(self.url('user'), 'GET', {'email': email}) + def create_addon(self, data): return self._send(self.url('addon'), 'POST', data)