def common_login(user_uuid, permanent_session=True): """ Performs login of the given user, with optional non-permanence on the session. Returns a tuple with (success, headers to set on success). """ user = model.get_user(user_uuid) if user is None: return (False, None) if login_user(LoginWrappedDBUser(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() if permanent_session and features.PERMANENT_SESSIONS: session_timeout_str = app.config.get("SESSION_TIMEOUT", "31d") session.permanent = True session.permanent_session_lifetime = convert_to_timedelta( session_timeout_str) # Force a new CSRF token. headers = {} headers[QUAY_CSRF_UPDATED_HEADER_NAME] = generate_csrf_token( force=True) return (True, headers) logger.debug("User could not be logged in, inactive?") return (False, None)
def test_valid_user(app): # Login with a valid user. someuser = model.user.get_user("devtable") login_user(LoginWrappedDBUser(someuser.uuid, someuser)) result = validate_session_cookie() assert result.authed_user == someuser assert result.context.identity is not None assert result.has_nonrobot_user assert result.error_message is None
def test_disabled_user(app): # "Login" with a disabled user. someuser = model.user.get_user("disabled") login_user(LoginWrappedDBUser(someuser.uuid, someuser)) # Ensure we get an invalid session cookie format error. result = validate_session_cookie() assert result.authed_user is None assert result.context.identity is None assert not result.has_nonrobot_user assert result.error_message == "User account is disabled"
def test_invalidformatted_cookie(app): # "Login" with a non-UUID reference. someuser = model.user.get_user("devtable") login_user(LoginWrappedDBUser("somenonuuid", someuser)) # Ensure we get an invalid session cookie format error. result = validate_session_cookie() assert result.authed_user is None assert result.context.identity is None assert not result.has_nonrobot_user assert result.error_message == "Invalid session cookie format"
def test_process_auth_or_cookie_valid_user(app): def emptyfunc(): pass # Login as a valid user. someuser = model.user.get_user('devtable') login_user(LoginWrappedDBUser(someuser.uuid, someuser)) # Call the function. process_auth_or_cookie(emptyfunc)() # Ensure the authenticated user was updated. assert get_authenticated_user() == someuser
def test_require_session_login_valid_user(app): def emptyfunc(): pass # Login as a valid user. someuser = model.user.get_user('devtable') login_user(LoginWrappedDBUser(someuser.uuid, someuser)) # Call the function. require_session_login(emptyfunc)() # Ensure the authenticated user was updated. assert get_authenticated_user() == someuser
def test_require_session_login_invalid_user(app): def emptyfunc(): pass # "Login" as a disabled user. someuser = model.user.get_user('disabled') login_user(LoginWrappedDBUser(someuser.uuid, someuser)) # Call the function. with pytest.raises(HTTPException): require_session_login(emptyfunc)() # Ensure the authenticated user was not updated. assert get_authenticated_user() is None
def test_valid_organization(app): # "Login" with a valid organization. someorg = model.user.get_namespace_user("buynlarge") someorg.uuid = str(uuid.uuid4()) someorg.verified = True someorg.save() login_user(LoginWrappedDBUser(someorg.uuid, someorg)) result = validate_session_cookie() assert result.authed_user is None assert result.context.identity is None assert not result.has_nonrobot_user assert result.error_message == "Cannot login to organization"
def common_login(user_uuid, permanent_session=True): """ Performs login of the given user, with optional non-permanence on the session. Returns a tuple with (success, headers to set on success). """ user = model.get_user(user_uuid) if user is None: return (False, None) if login_user(LoginWrappedDBUser(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() if permanent_session and features.PERMANENT_SESSIONS: session_timeout_str = app.config.get('SESSION_TIMEOUT', '31d') session.permanent = 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( user.email, user.username, user.given_name, user.family_name, user.company, user.location, ) create_lead_future.add_done_callback( build_error_callback('Create lead failed')) # Force a new CSRF token. headers = {} headers[QUAY_CSRF_UPDATED_HEADER_NAME] = generate_csrf_token( force=True) return (True, headers) logger.debug('User could not be logged in, inactive?') return (False, None)