Exemplo n.º 1
0
 def test_a_1_1_validate_model_id(self):
     # Model exists
     self.assertEqual(validate_model_id(User, 1), True)
     # Model does not exist
     self.assertEqual(validate_model_id(User, 10000000000), False)
     try:
         # model is not model
         validate_model_id(123, 10000000000)
         self.assertEqual(True, False)
     except Exception as e:
         self.assertEqual(
             str(e), "validate_model_id:expected the type " +
             "of SQLAlchemy, but found the type of <class 'int'> instead")
     print("Test a_1_1: validate_model_id success")
Exemplo n.º 2
0
 def test_a_1_2_validate_model_id_pydantic(self):
     # Model exists: nOo errors raised
     validate_model_id_pydantic(User, 1)
     try:
         # Model does not exist
         self.assertEqual(validate_model_id_pydantic(User, 10000000000),
                          False)
         self.assertEqual(True, False)
     except Exception as e:
         self.assertEqual(str(e),
                          "there is no User with this id: 10000000000")
     try:
         # model is not model
         validate_model_id(123, 10000000000)
         self.assertEqual(True, False)
     except Exception as e:
         self.assertEqual(
             str(e), "validate_model_id:expected the type " +
             "of SQLAlchemy, but found the type of <class 'int'> instead")
     print("Test a_1_2: validate_model_id success")
Exemplo n.º 3
0
    def delete_products(payload, product_id):
        #This endpoint will delete an existing product

        products_query = Product.query
        product_id_validation = validate_model_id(input_id=product_id,
                                                  model_query=products_query,
                                                  model_name_string="product")
        if product_id_validation["case"] == 1:
            #The product exists
            product = product_id_validation["result"]

        else:
            #No product with this id, can not convert to int,
            # or id is missing (Impossible)
            return my_error(
                status=product_id_validation["result"]["status"],
                description=product_id_validation["result"]["description"])

        #Now, we have "product", this is essential

        #Making sure that this user can delete this product
        if int(product.seller_id) != payload["uid"]:
            return my_error(
                status=403,
                description="you can not delete this product, because" +
                " you are not the one who created it")

        try:
            # Finally, deleting the product itself
            product.delete()
            return jsonify({
                "success": True,
                "result": "product deleted successfully"
            })
        except Exception as e:
            db.session.rollback()
            abort(500)
Exemplo n.º 4
0
    def delete_users(payload):
        #This endpoint will delete an existing user
        user_id = payload["uid"]
        users_query = User.query
        user_id_validation = validate_model_id(input_id=user_id,
                                               model_query=users_query,
                                               model_name_string="user")
        if user_id_validation["case"] == 1:
            #The user exists
            user = user_id_validation["result"]

        else:
            #No user with this id, can not convert to int,
            # or id is missing (Impossible)
            return my_error(
                status=user_id_validation["result"]["status"],
                description=user_id_validation["result"]["description"])

        #Now, we have "user", this is essential

        try:
            # Finally, deleting the user itself
            user.delete()
            r = jsonify({
                "success": True,
                "result": "user deleted successfully"
            })
            cookies = request.cookies
            for co in cookies:
                r.set_cookie(co, value="", expires=-50)
            return r
            #return jsonify({"success":True,
            #	"result":"user deleted successfully"})
        except Exception as e:
            raise (e)
            db.session.rollback()
            abort(500)
Exemplo n.º 5
0
    def delete_orders(payload, order_id):
        #This endpoint will delete an existing order

        orders_query = Order.query
        order_id_validation = validate_model_id(input_id=order_id,
                                                model_query=orders_query,
                                                model_name_string="order")
        if order_id_validation["case"] == 1:
            #The order exists
            order = order_id_validation["result"]
        else:
            #No order with this id, can not convert to int,
            # or id is missing (Impossible)
            return my_error(
                status=order_id_validation["result"]["status"],
                description=order_id_validation["result"]["description"])

        #Now, we have "order", this is essential

        #Now we validate if the this user can delete the order
        if int(order.user_id) != payload["uid"]:
            return my_error(
                status=403,
                description="you can not delete this order, because" +
                " you are not the one who created it")

        try:
            # Finally, deleting the order itself
            order.delete()
            return jsonify({
                "success": True,
                "result": "order deleted successfully"
            })
        except Exception as e:
            db.session.rollback()
            abort(500)
Exemplo n.º 6
0
    def edit_orders(payload, order_id):
        #This endpoint will edit an exiting order
        try:
            body = request.get_json()
        except:
            return my_error(
                status=400,
                description="request body can not be parsed to json")
        try:
            amount = body.get("amount", None)
        except:
            return my_error(status=400, description="there is no request body")

        #There can not be 0 fields to change
        #There must be at least one input field
        if (amount == None):
            return my_error(status=400,
                            description="you must at least enter"
                            " one field to change")

        #Validating inputs one by one
        amount_validation = validate_must(input=amount,
                                          type="i",
                                          input_name_string="amount",
                                          minimum=0,
                                          maximum=1000000000)

        #Now we will validate all inputs as a group
        if amount_validation["case"] == True:
            # Success: they pass the conditions
            amount = amount_validation["result"]
        else:
            # Failure: Something went wrong
            return amount_validation["result"]
        #Now the inputs user_id and amount are validated

        orders_query = Order.query

        order_id_validation = validate_model_id(input_id=order_id,
                                                model_query=orders_query,
                                                model_name_string="order")
        if order_id_validation["case"] == 1:
            #The order exists
            order = order_id_validation["result"]

        else:
            #No order with this id, can not convert to int,
            # or id is missing (Impossible)
            return my_error(
                status=order_id_validation["result"]["status"],
                description=order_id_validation["result"]["description"])
        #Now, we have "order", this is essential

        #Now we validate if the this user can edit the order
        if int(order.user_id) != payload["uid"]:
            return my_error(
                status=403,
                description="you can not edit this order, because" +
                " you are not the one who created it")

        #Finally: applying changes
        order.amount = amount

        if amount == 0:
            try:
                order.update()
                return jsonify({
                    "success": True,
                    "result": "order" + " deleted successfully"
                })
            except Exception as e:
                db.session.rollback()
                abort(500)
        try:
            order.update()
            return jsonify({"success": True, "order": order.get_dict()})
        except Exception as e:
            db.session.rollback()
            abort(500)
Exemplo n.º 7
0
    def post_orders(payload):
        #This endpoint will add a new product
        try:
            body = request.get_json()
        except:
            return my_error(
                status=400,
                description="request body can not be parsed to json")
        try:
            #user_id = body.get("user_id",None)
            product_id = body.get("product_id", None)
            amount = body.get("amount", None)
        except:
            return my_error(status=400, description="there is no request body")

        #Validating inputs one by one
        #user_id_validation = validate_must(
        #	input=user_id,type="i",input_name_string="user_id",
        #	minimum=0,maximum=1000)
        amount_validation = validate_must(input=amount,
                                          type="i",
                                          input_name_string="amount",
                                          minimum=1,
                                          maximum=1000000000)

        #Validating inputs a group
        val_group = validate_must_group([amount_validation])

        #Now we will validate all inputs as a group
        if val_group["case"] == True:
            # Success: they pass the conditions
            amount = val_group["result"][0]
        else:
            # Failure: Something went wrong
            return val_group["result"]
        #Now the inputs user_id and amount are validated

        #Now we will validate product_id
        products_query = Product.query
        product_id_validation = validate_model_id(input_id=product_id,
                                                  model_query=products_query,
                                                  model_name_string="product")
        if product_id_validation["case"] == 1:
            #The product exists
            product = product_id_validation["result"]
        else:
            #No product with this id, can not convert to int,
            # or id is missing
            return my_error(
                status=product_id_validation["result"]["status"],
                description=product_id_validation["result"]["description"])

        product_id = product.id
        #Now, we have "product_id", this is essential

        user_id = payload["uid"]

        #Create the Order
        new_order = Order(user_id=user_id,
                          amount=amount,
                          product_id=product_id)
        #Insert the order in the database
        try:
            new_order.insert()
            return jsonify({"success": True, "order": new_order.get_dict()})
        except Exception as e:
            db.session.rollback()
            abort(500)
Exemplo n.º 8
0
    def edit_products(payload, product_id):
        #This endpoint will add a new product
        #This is the correct arrangement
        #payload then product id
        #the opposite will result in error
        #print("product_id: "+str(product_id),flush=True)
        #print("payload: "+str(payload),flush=True)
        try:
            body = request.get_json()
        except:
            return my_error(
                status=400,
                description="request body can not be parsed to json")
        try:
            name = body.get("name", None)
            price = body.get("price", None)
            in_stock = body.get("in_stock", None)
        except:
            return my_error(status=400, description="there is no request body")

        #There can not be 0 fields to change
        #There must be at least one input field
        if (name == None and price == None and in_stock == None):
            return my_error(status=400,
                            description="you must at least enter"
                            " one field to change")

        products_query = Product.query

        product_id_validation = validate_model_id(input_id=product_id,
                                                  model_query=products_query,
                                                  model_name_string="product")
        if product_id_validation["case"] == 1:
            #The product exists
            product = product_id_validation["result"]

        else:
            #No product with this id, can not convert to int,
            # or id is missing (Impossible)
            return my_error(
                status=product_id_validation["result"]["status"],
                description=product_id_validation["result"]["description"])

        #Now, we have "product", this is essential

        #there will be no None
        if name == None: name = product.name
        if price == None: price = product.price
        if in_stock == None: in_stock = product.in_stock
        #Now there is no None
        #There are default values
        #This step can not change it's place because
        #here we need default values

        name_validation = validate_must(input=name,
                                        type="s",
                                        input_name_string="name",
                                        minimum=3,
                                        maximum=150)
        price_validation = validate_must(input=price,
                                         type="f",
                                         input_name_string="price",
                                         minimum=0.1,
                                         maximum=1000000)
        in_stock_validation = validate_must(input=in_stock,
                                            type="b",
                                            input_name_string="in_stock")
        #seller_id_validation = validate_must(
        #	input=seller_id,type="i",input_name_string="seller_id",
        #	minimum=1,maximum=100000000000000000)
        #seller_id can not change

        val_group = validate_must_group(
            [name_validation, price_validation, in_stock_validation])

        #Now we will validate all inputs as a group
        if val_group["case"] == True:
            # Success: they pass the conditions
            name, price, in_stock, = val_group["result"]
        else:
            # Failure: Something went wrong
            return val_group["result"]

        #Making sure that this user can change this product
        if int(product.seller_id) != payload["uid"]:
            return my_error(
                status=403,
                description="you can not change this product, because" +
                " you are not the one who created it")

        #Finally: applying changes
        product.name = name
        product.price = price
        product.in_stock = in_stock

        try:
            product.update()
            return jsonify({"success": True, "product": product.simple()})
        except Exception as e:
            db.session.rollback()
            abort(500)
Exemplo n.º 9
0
    def post_products(payload):
        #This endpoint will add a new product
        #print(payload,flush=True)
        try:
            body = request.get_json()
        except:
            return my_error(
                status=400,
                description="request body can not be parsed to json")
        try:
            name = body.get("name", None)
            price = body.get("price", None)
            in_stock = body.get("in_stock", None)
            #seller_id = body.get("seller_id",None)
        except:
            return my_error(status=400, description="there is no request body")

        #Validating inputs one by one
        name_validation = validate_must(input=name,
                                        type="s",
                                        input_name_string="name",
                                        minimum=3,
                                        maximum=150)
        price_validation = validate_must(input=price,
                                         type="f",
                                         input_name_string="price",
                                         minimum=0.1,
                                         maximum=1000000)
        in_stock_validation = validate_must(input=in_stock,
                                            type="b",
                                            input_name_string="in_stock")
        #seller_id_validation = validate_must(
        #	input=seller_id,type="i",input_name_string="seller_id",
        #	minimum=1,maximum=100000000000000000)

        #Validating inputs a group
        val_group = validate_must_group(
            [name_validation, price_validation, in_stock_validation])

        #Now we will validate all inputs as a group
        if val_group["case"] == True:
            # Success: they pass the conditions
            name, price, in_stock = val_group["result"]
        else:
            # Failure: Something went wrong
            return val_group["result"]

        seller_id = payload["uid"]
        users_query = User.query
        user_id_validation = validate_model_id(input_id=seller_id,
                                               model_query=users_query,
                                               model_name_string="user")
        if user_id_validation["case"] == 1:
            #The user exists
            seller = user_id_validation["result"]
        else:
            #No user with this id, can not convert to int,
            # or id is missing
            return my_error(
                status=user_id_validation["result"]["status"],
                description=user_id_validation["result"]["description"])
        seller_id = seller.id

        #Create the product
        new_product = Product(name=name,
                              price=price,
                              seller_id=seller_id,
                              in_stock=in_stock)

        #Insert the product in the database
        try:
            new_product.insert()
            return jsonify({"success": True, "product": new_product.simple()})
        except Exception as e:
            db.session.rollback()
            abort(500)