Change endpoints/common to use a data interface

This commit is contained in:
Joseph Schorr 2017-07-20 15:57:42 -04:00
parent aecec02b6c
commit f976ffbdc7
4 changed files with 67 additions and 12 deletions

View file

@ -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?')

View file

@ -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.
"""

View file

@ -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()

View file

@ -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