| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- import os
- import pytest
- from asgi_csrf import asgi_csrf
- from fastapi.testclient import TestClient
- from lxml import html
- from starsessions import (
- CookieStore,
- SessionStore,
- )
- from app.main import app
- client = TestClient(app, base_url="http://127.0.0.1:5014")
- @pytest.fixture
- def app_csrf():
- """Return a CSRF token."""
- return asgi_csrf(app, signing_secret=os.getenv("CSRF_SECRET_KEY"))
- @pytest.fixture()
- def session_store() -> SessionStore:
- """WIP."""
- return CookieStore(secret_key=os.getenv("SESSION_SECRET_KEY"))
- def prepare_request(product_id: str) -> tuple[dict[str, str], str]:
- """Machinery to get back a tuple with headers and a valid CSRF token
- to then use to send a POST request.
- """
- headers = {
- "Accept-Language": "en-US,en;q=0.5",
- "Referer": f"http://localhost:5014/products/{product_id}",
- }
- response_get = client.get(f"/products/{product_id}", headers=headers)
- tree = html.fromstring(response_get.content)
- csrf_token = tree.xpath("//input[@name='csrftoken']")[0].get("value")
- return headers, csrf_token
- def test_add_simple_product(capsys, app_csrf, session_store):
- """Add simple product"""
- headers_no_options, csrf_token_no_options = prepare_request("g-axxi-i")
- data_good_no_options_add = {
- "page": "product",
- "operation": "add",
- "product_id": "price_1PTPnFKMYdpilqPdArvtszSN",
- "product_path": "products/g-axxi-i",
- "price": 30,
- "quantity": 2,
- "csrftoken": csrf_token_no_options,
- }
- response_good_no_options_add = client.post(
- "/checkout", data=data_good_no_options_add, headers=headers_no_options
- )
- with capsys.disabled():
- print(f"client => {client.cookies}")
- assert response_good_no_options_add.status_code == 200
- def test_remove_simple_product(capsys, app_csrf, session_store):
- """removing a product with no options (size, style), good """
- headers_no_options, csrf_token_no_options = prepare_request("g-axxi-i")
-
- data_good_no_options_remove = {
- "page": "product",
- "operation": "remove",
- "product_id": "price_1PTPnFKMYdpilqPdArvtszSN",
- "product_path": "products/g-axxi-i",
- "price": 30,
- "quantity": 1,
- "csrftoken": csrf_token_no_options,
- }
- response_good_no_options_remove = client.post(
- "/checkout", data=data_good_no_options_remove, headers=headers_no_options
- )
- assert response_good_no_options_remove.status_code == 200
- def test_wrong_request_simple_product(capsys, app_csrf, session_store):
- # Product operation with wrong query
- headers_no_options, csrf_token_no_options = prepare_request("g-axxi-i")
- data_bad_no_options_wrong_add = {
- "page": "product",
- "product_id": "price_1PTPnFKMYdpilqPdArvtszSN",
- "product_path": "products/g-axxi-i",
- "price": 30,
- "quantity": 2,
- "csrftoken": csrf_token_no_options,
- }
- response_bad_no_options_add = client.post(
- "/checkout", data=data_bad_no_options_wrong_add, headers=headers_no_options
- )
- assert response_bad_no_options_add.status_code == 422
- def test_add_product(capsys, app_csrf, session_store):
- """ Adding a product with size/style """
- headers_with_options, csrf_token_with_options = prepare_request("a-tshirt-01")
- data_good_with_options_add = {
- "page": "product",
- "operation": "add",
- "product_id": "prod_RZBk650b1Tx95N",
- "product_path": "products/a-tshirt-01",
- "price": 50,
- "quantity": 3,
- "size": "m",
- "style": "black",
- "csrftoken": csrf_token_with_options,
- }
- response_good_with_options_add = client.post(
- "/checkout", data=data_good_with_options_add, headers=headers_with_options
- )
- assert response_good_with_options_add.status_code == 200
- def test_remove_product(capsys, app_csrf, session_store):
- """ Removing product with size/style """
- headers_with_options, csrf_token_with_options = prepare_request("a-tshirt-01")
- data_good_with_options_remove = {
- "page": "product",
- "operation": "remove",
- "product_id": "stripe_product_tshirt-aus",
- "product_path": "products/a-tshirt-01",
- "price": 50,
- "quantity": 2,
- "size": "m",
- "style": "black",
- "csrftoken": csrf_token_with_options,
- }
- response_good_with_options_remove = client.post(
- "/checkout", data=data_good_with_options_remove, headers=headers_with_options
- )
- assert response_good_with_options_remove.status_code == 200
- def test_add_product_wrong_options(capsys, app_csrf, session_store):
- """ Trying to add product with wrong size/style """
- headers_with_options, csrf_token_with_options = prepare_request("a-tshirt-01")
- data_bad_with_options_wrong_add = {
- "page": "product",
- "operation": "add",
- "product_id": "prod_RZBk650b1Tx95N",
- "product_path": "products/a-tshirt-01",
- "price": 50,
- "quantity": 5,
- "size": "x",
- "style": "yellow",
- "csrftoken": csrf_token_with_options,
- }
- response_bad_with_options_add = client.post(
- "/checkout", data=data_bad_with_options_wrong_add, headers=headers_with_options
- )
- assert response_bad_with_options_add.status_code == 422
- def test_remove_product_wrong_options(capsys, app_csrf, session_store):
- """ Trying to remove product with wrong size/style """
- headers_with_options, csrf_token_with_options = prepare_request("a-tshirt-01")
- data_bad_with_options_wrong_remove = {
- "page": "product",
- "operation": "remove",
- "product_id": "prod_RZBk650b1Tx95N",
- "product_path": "products/a-tshirt-01",
- "price": 50,
- "quantity": 5,
- "size": "x",
- "style": "yellow",
- "csrftoken": csrf_token_with_options,
- }
- response_bad_with_options_remove = client.post(
- "/checkout",
- data=data_bad_with_options_wrong_remove,
- headers=headers_with_options,
- )
- assert response_bad_with_options_remove.status_code == 422
|