From f976ffbdc7336accdce06f2da4a08b80c64efd20 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Thu, 20 Jul 2017 15:57:42 -0400 Subject: [PATCH] Change endpoints/common to use a data interface --- endpoints/common.py | 23 +++++++++++++--------- endpoints/common_models_interface.py | 29 ++++++++++++++++++++++++++++ endpoints/common_models_pre_oci.py | 21 ++++++++++++++++++++ endpoints/test/test_common.py | 6 +++--- 4 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 endpoints/common_models_interface.py create mode 100644 endpoints/common_models_pre_oci.py diff --git a/endpoints/common.py b/endpoints/common.py index dbe7fabc4..26b45848f 100644 --- a/endpoints/common.py +++ b/endpoints/common.py @@ -14,6 +14,7 @@ from auth import scopes from auth.permissions import QuayDeferredPermissionUser from config import frontend_visible_config from external_libraries import get_external_javascript, get_external_css +from endpoints.common_models_pre_oci import pre_oci_model as model from util.secscan import PRIORITY_LEVELS from util.saas.useranalytics import build_error_callback from util.timedeltastring import convert_to_timedelta @@ -25,8 +26,12 @@ logger = logging.getLogger(__name__) def common_login(user_uuid, permanent_session=True): """ Performs login of the given user, with optional non-permanence on the session. """ + user = model.get_user(user_uuid) + if user is None: + return False + if login_user(LoginWrappedDBUser(user_uuid)): - logger.debug('Successfully signed in as user with uuid %s', user_uuid) + logger.debug('Successfully signed in as user %s with uuid %s', user.username, user_uuid) new_identity = QuayDeferredPermissionUser.for_id(user_uuid) identity_changed.send(app, identity=new_identity) session['login_time'] = datetime.datetime.now() @@ -37,14 +42,14 @@ def common_login(user_uuid, permanent_session=True): session.permanent_session_lifetime = convert_to_timedelta(session_timeout_str) # Inform our user analytics that we have a new "lead" - #create_lead_future = user_analytics.create_lead( - # db_user.email, - # db_user.username, - # db_user.given_name, - # db_user.family_name, - # db_user.company, - #) - #create_lead_future.add_done_callback(build_error_callback('Create lead failed')) + create_lead_future = user_analytics.create_lead( + user.email, + user.username, + user.given_name, + user.family_name, + user.company, + ) + create_lead_future.add_done_callback(build_error_callback('Create lead failed')) return True logger.debug('User could not be logged in, inactive?') diff --git a/endpoints/common_models_interface.py b/endpoints/common_models_interface.py new file mode 100644 index 000000000..4a9978eab --- /dev/null +++ b/endpoints/common_models_interface.py @@ -0,0 +1,29 @@ +from abc import ABCMeta, abstractmethod +from collections import namedtuple + +from six import add_metaclass + + +class User(namedtuple('User', ['uuid', 'username', 'email', 'given_name', 'family_name', 'company'])): + """ + User represents a user. + """ + + +@add_metaclass(ABCMeta) +class EndpointsCommonDataInterface(object): + """ + Interface that represents all data store interactions required by the common endpoints lib. + """ + + @abstractmethod + def get_user(self, user_uuid): + """ + Returns the User matching the given uuid, if any or None if none. + """ + + @abstractmethod + def get_namespace_uuid(self, namespace_name): + """ + Returns the uuid of the Namespace with the given name, if any. + """ diff --git a/endpoints/common_models_pre_oci.py b/endpoints/common_models_pre_oci.py new file mode 100644 index 000000000..833579d90 --- /dev/null +++ b/endpoints/common_models_pre_oci.py @@ -0,0 +1,21 @@ +from data import model +from endpoints.common_models_interface import User, EndpointsCommonDataInterface + + +class EndpointsCommonDataPreOCIModel(EndpointsCommonDataInterface): + def get_user(self, user_uuid): + user = model.user.get_user_by_uuid(user_uuid) + if user is None: + return None + + return User(uuid=user.uuid, username=user.username, email=user.email, + given_name=user.given_name, family_name=user.family_name, company=user.company) + + def get_namespace_uuid(self, namespace_name): + user = model.user.get_namespace_user(namespace_name) + if user is None: + return None + + return user.uuid + +pre_oci_model = EndpointsCommonDataPreOCIModel() diff --git a/endpoints/test/test_common.py b/endpoints/test/test_common.py index 39cc0c569..f13ec6e02 100644 --- a/endpoints/test/test_common.py +++ b/endpoints/test/test_common.py @@ -1,9 +1,9 @@ import pytest -from data import model from endpoints.common import common_login from test.fixtures import * +from endpoints.common_models_pre_oci import pre_oci_model as model @pytest.mark.parametrize('username, expect_success', [ # Valid users. @@ -20,6 +20,6 @@ from test.fixtures import * ('unverified', False), ]) def test_common_login(username, expect_success, app): - db_user = model.user.get_namespace_user(username) + uuid = model.get_namespace_uuid(username) with app.app_context(): - assert common_login(db_user.uuid) == expect_success + assert common_login(uuid) == expect_success