Change endpoints/common to use a data interface
This commit is contained in:
parent
aecec02b6c
commit
f976ffbdc7
4 changed files with 67 additions and 12 deletions
|
@ -14,6 +14,7 @@ from auth import scopes
|
||||||
from auth.permissions import QuayDeferredPermissionUser
|
from auth.permissions import QuayDeferredPermissionUser
|
||||||
from config import frontend_visible_config
|
from config import frontend_visible_config
|
||||||
from external_libraries import get_external_javascript, get_external_css
|
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.secscan import PRIORITY_LEVELS
|
||||||
from util.saas.useranalytics import build_error_callback
|
from util.saas.useranalytics import build_error_callback
|
||||||
from util.timedeltastring import convert_to_timedelta
|
from util.timedeltastring import convert_to_timedelta
|
||||||
|
@ -25,8 +26,12 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def common_login(user_uuid, permanent_session=True):
|
def common_login(user_uuid, permanent_session=True):
|
||||||
""" Performs login of the given user, with optional non-permanence on the session. """
|
""" 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)):
|
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)
|
new_identity = QuayDeferredPermissionUser.for_id(user_uuid)
|
||||||
identity_changed.send(app, identity=new_identity)
|
identity_changed.send(app, identity=new_identity)
|
||||||
session['login_time'] = datetime.datetime.now()
|
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)
|
session.permanent_session_lifetime = convert_to_timedelta(session_timeout_str)
|
||||||
|
|
||||||
# Inform our user analytics that we have a new "lead"
|
# Inform our user analytics that we have a new "lead"
|
||||||
#create_lead_future = user_analytics.create_lead(
|
create_lead_future = user_analytics.create_lead(
|
||||||
# db_user.email,
|
user.email,
|
||||||
# db_user.username,
|
user.username,
|
||||||
# db_user.given_name,
|
user.given_name,
|
||||||
# db_user.family_name,
|
user.family_name,
|
||||||
# db_user.company,
|
user.company,
|
||||||
#)
|
)
|
||||||
#create_lead_future.add_done_callback(build_error_callback('Create lead failed'))
|
create_lead_future.add_done_callback(build_error_callback('Create lead failed'))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
logger.debug('User could not be logged in, inactive?')
|
logger.debug('User could not be logged in, inactive?')
|
||||||
|
|
29
endpoints/common_models_interface.py
Normal file
29
endpoints/common_models_interface.py
Normal 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.
|
||||||
|
"""
|
21
endpoints/common_models_pre_oci.py
Normal file
21
endpoints/common_models_pre_oci.py
Normal 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()
|
|
@ -1,9 +1,9 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from data import model
|
|
||||||
from endpoints.common import common_login
|
from endpoints.common import common_login
|
||||||
|
|
||||||
from test.fixtures import *
|
from test.fixtures import *
|
||||||
|
from endpoints.common_models_pre_oci import pre_oci_model as model
|
||||||
|
|
||||||
@pytest.mark.parametrize('username, expect_success', [
|
@pytest.mark.parametrize('username, expect_success', [
|
||||||
# Valid users.
|
# Valid users.
|
||||||
|
@ -20,6 +20,6 @@ from test.fixtures import *
|
||||||
('unverified', False),
|
('unverified', False),
|
||||||
])
|
])
|
||||||
def test_common_login(username, expect_success, app):
|
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():
|
with app.app_context():
|
||||||
assert common_login(db_user.uuid) == expect_success
|
assert common_login(uuid) == expect_success
|
||||||
|
|
Reference in a new issue