2 new commits in galaxy-central: https://bitbucket.org/galaxy/galaxy-central/commits/ea4f983c227e/ Changeset: ea4f983c227e User: jmchilton Date: 2014-08-05 03:45:02 Summary: API tests for authenticate and some of users endpoints. Affected #: 4 files diff -r d4070ec86d404eec61f78a60f23536080b04ab1f -r ea4f983c227e4cac4b331699e8b7a87f9383df06 test/api/test_authenticate.py --- /dev/null +++ b/test/api/test_authenticate.py @@ -0,0 +1,29 @@ +import base64 + +from base.interactor import get_request +from base import api + + +TEST_USER_EMAIL = "auth_user_test@bx.psu.edu" +TEST_USER_PASSWORD = "testpassword1" + + +class AuthenticationApiTestCase( api.ApiTestCase ): + + def test_auth( self ): + self._setup_user( TEST_USER_EMAIL, TEST_USER_PASSWORD ) + baseauth_url = self._api_url( "authenticate/baseauth", use_key=False ) + unencoded_credentials = "%s:%s" % ( TEST_USER_EMAIL, TEST_USER_PASSWORD ) + authorization = base64.b64encode(unencoded_credentials) + headers = { + "Authorization": authorization, + } + auth_response = get_request( baseauth_url, headers=headers ) + self._assert_status_code_is( auth_response, 200 ) + auth_dict = auth_response.json() + self._assert_has_keys( auth_dict, "api_key" ) + + # Verify key... + random_api_url = self._api_url( "users", use_key=False ) + random_api_response = get_request( random_api_url, params=dict( key=auth_dict[ "api_key" ] ) ) + self._assert_status_code_is( random_api_response, 200 ) diff -r d4070ec86d404eec61f78a60f23536080b04ab1f -r ea4f983c227e4cac4b331699e8b7a87f9383df06 test/api/test_users.py --- /dev/null +++ b/test/api/test_users.py @@ -0,0 +1,24 @@ +from base import api + +TEST_USER_EMAIL = "user_for_users_index_test@bx.psu.edu" + + +class UsersApiTestCase( api.ApiTestCase ): + + def test_index( self ): + self._setup_user( TEST_USER_EMAIL ) + all_users_response = self._get( "users", admin=True ) + self._assert_status_code_is( all_users_response, 200 ) + all_users = all_users_response.json() + # New user is in list + assert len( [ u for u in all_users if u[ "email" ] == TEST_USER_EMAIL ] ) == 1 + # Request made from admin user, so should at least self and this + # new user. + assert len( all_users ) > 1 + + def test_index_only_self_for_nonadmins( self ): + self._setup_user( TEST_USER_EMAIL ) + with self._different_user( ): + all_users_response = self._get( "users" ) + # Non admin users can only see themselves + assert len( all_users_response.json() ) == 1 diff -r d4070ec86d404eec61f78a60f23536080b04ab1f -r ea4f983c227e4cac4b331699e8b7a87f9383df06 test/base/api.py --- a/test/base/api.py +++ b/test/base/api.py @@ -41,8 +41,8 @@ url = "%s?%s" % ( url, query ) return url - def _setup_user( self, email ): - self.galaxy_interactor.ensure_user_with_email(email) + def _setup_user( self, email, password=None ): + self.galaxy_interactor.ensure_user_with_email( email, password=password ) users = self._get( "users", admin=True ).json() user = [ user for user in users if user["email"] == email ][0] return user diff -r d4070ec86d404eec61f78a60f23536080b04ab1f -r ea4f983c227e4cac4b331699e8b7a87f9383df06 test/base/interactor.py --- a/test/base/interactor.py +++ b/test/base/interactor.py @@ -325,16 +325,17 @@ ) return self._post( "tools", files=files, data=data ) - def ensure_user_with_email( self, email ): + def ensure_user_with_email( self, email, password=None ): admin_key = self.master_api_key all_users = self._get( 'users', key=admin_key ).json() try: test_user = [ user for user in all_users if user["email"] == email ][0] except IndexError: username = re.sub('[^a-z-]', '--', email.lower()) + password = password or 'testpass' data = dict( email=email, - password='testuser', + password=password, username=username, ) test_user = self._post( 'users', data, key=admin_key ).json() https://bitbucket.org/galaxy/galaxy-central/commits/a714f168aa7d/ Changeset: a714f168aa7d User: jmchilton Date: 2014-08-05 03:45:02 Summary: Introduce ApiKeyManager to replace and expand upon CreatedApiKeysMixin. Affected #: 3 files diff -r ea4f983c227e4cac4b331699e8b7a87f9383df06 -r a714f168aa7ddb1dfd3142f49869992cd325456c lib/galaxy/managers/api_keys.py --- /dev/null +++ b/lib/galaxy/managers/api_keys.py @@ -0,0 +1,26 @@ + + +class ApiKeyManager( object ): + + def __init__( self, app ): + self.app = app + + def create_api_key( self, user ): + guid = self.app.security.get_new_guid() + new_key = self.app.model.APIKeys() + new_key.user_id = user.id + new_key.key = guid + sa_session = self.app.model.context + sa_session.add( new_key ) + sa_session.flush() + return guid + + def get_or_create_api_key( self, user ): + # Logic Galaxy has always used - but it would appear to have a race + # condition. Worth fixing? Would kind of need a message queue to fix + # in multiple process mode. + if user.api_keys: + key = user.api_keys[0].key + else: + key = self.create_api_key( user ) + return key diff -r ea4f983c227e4cac4b331699e8b7a87f9383df06 -r a714f168aa7ddb1dfd3142f49869992cd325456c lib/galaxy/web/base/controller.py --- a/lib/galaxy/web/base/controller.py +++ b/lib/galaxy/web/base/controller.py @@ -37,6 +37,7 @@ from galaxy.model import ExtendedMetadata, ExtendedMetadataIndex, LibraryDatasetDatasetAssociation, HistoryDatasetAssociation +from galaxy.managers import api_keys from galaxy.datatypes.metadata import FileParameter from galaxy.tools.parameters import RuntimeValue, visit_input_values from galaxy.tools.parameters.basic import DataToolParameter @@ -286,16 +287,12 @@ class CreatesApiKeysMixin: """ Mixing centralizing logic for creating API keys for user objects. + + Deprecated - please use api_keys.ApiKeyManager for new development. """ def create_api_key( self, trans, user ): - guid = trans.app.security.get_new_guid() - new_key = trans.app.model.APIKeys() - new_key.user_id = user.id - new_key.key = guid - trans.sa_session.add( new_key ) - trans.sa_session.flush() - return guid + return api_keys.ApiKeyManager( trans.app ).create_api_key( user ) class SharableItemSecurityMixin: diff -r ea4f983c227e4cac4b331699e8b7a87f9383df06 -r a714f168aa7ddb1dfd3142f49869992cd325456c lib/galaxy/webapps/galaxy/api/authenticate.py --- a/lib/galaxy/webapps/galaxy/api/authenticate.py +++ b/lib/galaxy/webapps/galaxy/api/authenticate.py @@ -15,14 +15,19 @@ from urllib import unquote from galaxy.web import _future_expose_api_anonymous as expose_api_anonymous +from galaxy.managers import api_keys from galaxy import exceptions -from galaxy.web.base.controller import BaseAPIController, CreatesApiKeysMixin +from galaxy.web.base.controller import BaseAPIController import logging log = logging.getLogger( __name__ ) -class AuthenticationController( BaseAPIController, CreatesApiKeysMixin ): +class AuthenticationController( BaseAPIController ): + + def __init__( self, app ): + super( AuthenticationController, self ).__init__( app ) + self.api_keys_manager = api_keys.ApiKeyManager( app ) @expose_api_anonymous def get_api_key( self, trans, **kwd ): @@ -47,10 +52,7 @@ user = user[0] is_valid_user = user.check_password( password ) if ( is_valid_user ): - if user.api_keys: - key = user.api_keys[0].key - else: - key = self.create_api_key( trans, user ) + key = self.api_keys_manager.get_or_create_api_key( user ) return dict( api_key=key ) else: raise exceptions.AuthenticationFailed() Repository URL: https://bitbucket.org/galaxy/galaxy-central/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email.