Exemplo n.º 1
0
def test_create_user_with_explicit_enabled_and_confirmed_flags(
        _not_logged_in_client):
    user_name = "test_register3"
    with session_scope() as db_session:
        try:
            user = User()
            user.user = user_name
            user.argon_password = "******"
            user.email = "*****@*****.**"
            user.enabled = True
            user.confirmed = True
            _assert_create_user(db_session, _not_logged_in_client, user)
        finally:
            # cleans the database
            _clean_test_users(db_session, user_name)
Exemplo n.º 2
0
def create_user():
    try:
        payload = _get_json_payload()
        if CONFIRMATION_URL not in payload:
            raise PhenopolisException("Please, provide a confirmation URL",
                                      400)
        confirmation_url = payload.pop(CONFIRMATION_URL)
        new_user = User(**payload)
        _check_user_valid(new_user)
        # encode password
        new_user.argon_password = argon2.hash(new_user.argon_password)
        # this is the default, but to avoid a misuse of the API that circumvents user registration it forces these
        # two flags to False
        new_user.confirmed = False
        new_user.enabled = False

        try:
            # persist users
            user_id = new_user.user
            with session_scope() as db_session:
                db_session.add(new_user)
                _add_config_from_admin(db_session, new_user)
                # sends confirmation email
                _send_confirmation_email(new_user,
                                         confirmation_url=confirmation_url)
            response = jsonify(success=True,
                               message="User was created",
                               id=user_id)
        except Exception as e:
            application.logger.exception(e)
            response = jsonify(success=False, message=str(e))
            response.status_code = 500
    except PhenopolisException as e:
        application.logger.error(str(e))
        response = jsonify(success=False, error=str(e))
        response.status_code = e.http_status
    return response