def test_backend_validate_smaller_total(payment_3p_api_url, payment_api_url, iam_auth): """ Test /backend/validate with a smaller total """ card_number = "1234567890123456" total = 3000 # Create a payment token res_3p = requests.post(payment_3p_api_url + "/preauth", json={ "cardNumber": card_number, "amount": total }) payment_token = res_3p.json()["paymentToken"] # Validate the token res = requests.post(payment_api_url + "/backend/validate", auth=iam_auth(payment_api_url), json={ "paymentToken": payment_token, "total": total - 1000 }) assert res.status_code == 200 body = res.json() assert "ok" in body assert "message" not in body assert body["ok"] == True # Cleanup requests.post(payment_3p_api_url + "/cancelPayment", json={"paymentToken": payment_token})
def order_request(get_order, product, iam_auth, delivery_pricing_api_url, payment_3p_api_url): """ Order Request """ order = get_order() # Grab the correct delivery price from the backend res = requests.post("{}/backend/pricing".format(delivery_pricing_api_url), auth=iam_auth(delivery_pricing_api_url), json={ "products": [product], "address": order["address"] }) delivery_price = res.json()["pricing"] # Get a payment token total = delivery_price + sum( [p["price"] * p.get("quantity", 1) for p in order["products"]]) res = requests.post("{}/preauth".format(payment_3p_api_url), json={ "cardNumber": "1234567890123456", "amount": total }) payment_token = res.json()["paymentToken"] return { "products": [product], "address": order["address"], "deliveryPrice": delivery_price, "paymentToken": payment_token }
def delivery_price(delivery_api_url, iam_auth, order, products): res = requests.post(url=delivery_api_url + "/backend/pricing", auth=iam_auth(delivery_api_url), json={ "products": products, "address": order["address"] }) body = res.json() print(body) return body["pricing"]
def test_backend_validate(endpoint_url, iam_auth, product): """ Test POST /backend/validate """ res = requests.post( "{}/backend/validate".format(endpoint_url), auth=iam_auth(endpoint_url), json={"products": [product]} ) assert res.status_code == 200 body = res.json() assert "products" not in body
def test_backend_pricing(endpoint_url, iam_auth, order): """ Test POST /backend/pricing """ res = requests.post( "{}/backend/pricing".format(endpoint_url), auth=iam_auth(endpoint_url), json={ "products": order["products"], "address": order["address"] } ) assert res.status_code == 200 body = res.json() assert "pricing" in body
def test_backend_pricing_no_address(endpoint_url, iam_auth, order): """ Test POST /backend/pricing """ res = requests.post( "{}/backend/pricing".format(endpoint_url), auth=iam_auth(endpoint_url), json={ "products": order["products"] } ) assert res.status_code == 400 body = res.json() assert "message" in body assert isinstance(body["message"], str) assert "address" in body["message"]
def test_backend_validate_incorrect_pictures(endpoint_url, iam_auth, product): """ Test POST /backend/validate with an incorrect product """ wrong_product = copy.deepcopy(product) wrong_product["pictures"].append("INCORRECT_PICTURE") res = requests.post( "{}/backend/validate".format(endpoint_url), auth=iam_auth(endpoint_url), json={"products": [wrong_product]} ) assert res.status_code == 200 body = res.json() assert "products" not in body
def test_backend_validate_mixed(endpoint_url, iam_auth, product): """ Test /backend/validate with a mix of correct and incorrect product """ wrong_product = copy.deepcopy(product) wrong_product["price"] += 100 res = requests.post( "{}/backend/validate".format(endpoint_url), auth=iam_auth(endpoint_url), json={"products": [product, wrong_product]} ) assert res.status_code == 200 body = res.json() assert "products" in body assert len(body["products"]) == 1 compare_dict(body["products"][0], product)
def test_backend_validate_incorrect_package(endpoint_url, iam_auth, product): """ Test POST /backend/validate with an incorrect product """ wrong_product = copy.deepcopy(product) wrong_product["package"]["height"] += 100 res = requests.post( "{}/backend/validate".format(endpoint_url), auth=iam_auth(endpoint_url), json={"products": [wrong_product]} ) assert res.status_code == 200 body = res.json() assert "products" in body assert len(body["products"]) == 1 compare_dict(body["products"][0], product)
def order_request(get_order, product, iam_auth, delivery_pricing_api_url): """ Order Request """ order = get_order() # Grab the correct delivery price from the backend res = requests.post("{}/backend/pricing".format(delivery_pricing_api_url), auth=iam_auth(delivery_pricing_api_url), json={ "products": [product], "address": order["address"] }) delivery_price = res.json()["pricing"] return { "products": [product], "address": order["address"], "deliveryPrice": delivery_price, "paymentToken": order["paymentToken"] }
def test_backend_validate_non_existent(payment_3p_api_url, payment_api_url, iam_auth): """ Test /backend/validate with a non-existent token """ payment_token = str(uuid.uuid4()) total = 3000 # Validate the token res = requests.post(payment_api_url + "/backend/validate", auth=iam_auth(payment_api_url), json={ "paymentToken": payment_token, "total": total }) assert res.status_code == 200 body = res.json() assert "ok" in body assert "message" not in body assert body["ok"] == False