Exemple #1
0
def post_account():
    post_params = request.form

    try:
        payload = account_schema.load(post_params)
    except ValidationError as ex:
        logger.info("Missing request parameter", exc_info=ex)
        return make_response(
            jsonify({
                "title": "Authentication error in Auth service",
                "detail": "Missing 'username' or 'password'"
            }),
            400,
        )

    try:
        with transactional_session() as session:
            user = User(username=payload.get("username"))
            user.set_hashed_password(payload.get("password"))
            session.add(user)
    except IntegrityError:
        logger.exception("Unable to create account with requested username")
        return make_response(
            jsonify({
                "title":
                "Auth service account create error",
                "detail":
                "Unable to create account with requested username",
            }),
            500,
        )
    except SQLAlchemyError:
        logger.exception("Unable to commit account to database")
        return make_response(
            jsonify({
                "title": "Auth service account create db error",
                "detail": "Unable to commit account to database"
            }),
            500,
        )

    logger.info("Successfully created account", user_id=user.id)
    return make_response(
        jsonify({
            "account": user.username,
            "created": "success"
        }), 201)
 def test_is_correct_password_true(self):
     user = User()
     user.set_hashed_password("password")
     self.assertTrue(user.is_correct_password("password"))
 def test_is_correct_password_false(self):
     user = User()
     user.set_hashed_password("password")
     self.assertFalse(user.is_correct_password("wrongpassword"))
 def test_set_hashed_password(self):
     user = User()
     user.set_hashed_password("password")
     self.assertTrue(bcrypt.verify("password", user.hashed_password))