Пример #1
0
    def post(cls):
        """
        Gets data from parser to find user in database and checks password.
        Checks and returns access token.
        """
        try:
            user_json = request.get_json()
            user_data = user_schema.load(user_json,
                                         partial=("name", "company_name",
                                                  "email"))

            user = UserModel.find_by_username(user_data.username)

            if not user:
                return {"message": gettext("user_not_found")}, 404

            if check_encrypted_password(user_data.password, user.password):
                access_token = create_access_token(identity=user.id,
                                                   fresh=True)
                return {"access_token": access_token}, 200

            return {"message": gettext("user_invalid_credentials")}, 401

        except:
            return {"message": gettext("user_error_logging_in")}, 500
Пример #2
0
 def delete(cls, user_id):
     try:
         user = UserModel.find_by_id(user_id)
         if user:
             user.delete_from_db()
             return {"message": gettext("user_deleted")}, 200
         return {"message": gettext("user_not_found")}, 404
     except:
         return {"message": gettext("user_error_deleting")}, 500
Пример #3
0
    def get(cls, user_id):

        try:
            user = UserModel.find_by_id(user_id)
            if user:
                return user_schema.dump(user), 200
            return {"message": gettext("user_not_found")}, 404
        except:
            return {"message": gettext("user_error_reading.")}, 500
Пример #4
0
    def post(cls):

        try:
            user = user_schema.load(request.get_json())

            if UserModel.find_by_username(user.username):
                return {"message": gettext("user_username_exists")}, 400

            user.password = encrypt_password(user.password)
            user.save_to_db()
            return {"message": gettext("user_registered")}, 201

        except:
            return {"message": gettext("user_error_creating")}, 500
Пример #5
0
    def put(cls, user_id):

        try:
            user_json = request.get_json()
            user = UserModel.find_by_id(user_id)

            if user:
                user.name = user_json["name"]
            else:
                return {"message": gettext("user_not_found")}, 404

            user.save_to_db()

            return user_schema.dump(user), 200

        except:
            return {"message": gettext("user_error_updating.")}, 500
    def test_register_user(self):
        with self.app() as client:
            with self.app_context():
                response = client.post(
                    "/register",
                    headers={"Content-Type": "application/json"},
                    data=json.dumps({
                        "name": "fanny",
                        "company_name": "certi",
                        "email": "*****@*****.**",
                        "username": "******",
                        "password": "******",
                    }),
                )

                self.assertEqual(response.status_code, 201)
                self.assertIsNotNone(UserModel.find_by_username("fcs"))
Пример #7
0
 def get(cls):
     try:
         users = user_list_schema.dump(UserModel.find_all())
         return {"users": users}, 200
     except:
         return {"message": gettext("user_not_found")}