test_product.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import os
  2. import pytest
  3. from asgi_csrf import asgi_csrf
  4. from fastapi.testclient import TestClient
  5. from lxml import html
  6. from starsessions import (
  7. CookieStore,
  8. SessionStore,
  9. )
  10. from app.main import app
  11. client = TestClient(app, base_url="http://127.0.0.1:5014")
  12. @pytest.fixture
  13. def app_csrf():
  14. """Return a CSRF token."""
  15. return asgi_csrf(app, signing_secret=os.getenv("CSRF_SECRET_KEY"))
  16. @pytest.fixture()
  17. def session_store() -> SessionStore:
  18. """WIP."""
  19. return CookieStore(secret_key=os.getenv("SESSION_SECRET_KEY"))
  20. def prepare_request(product_id: str) -> tuple[dict[str, str], str]:
  21. """Machinery to get back a tuple with headers and a valid CSRF token
  22. to then use to send a POST request.
  23. """
  24. headers = {
  25. "Accept-Language": "en-US,en;q=0.5",
  26. "Referer": f"http://localhost:5014/products/{product_id}",
  27. }
  28. response_get = client.get(f"/products/{product_id}", headers=headers)
  29. tree = html.fromstring(response_get.content)
  30. csrf_token = tree.xpath("//input[@name='csrftoken']")[0].get("value")
  31. return headers, csrf_token
  32. def test_add_simple_product(capsys, app_csrf, session_store):
  33. """Add simple product"""
  34. headers_no_options, csrf_token_no_options = prepare_request("g-axxi-i")
  35. data_good_no_options_add = {
  36. "page": "product",
  37. "operation": "add",
  38. "product_id": "price_1PTPnFKMYdpilqPdArvtszSN",
  39. "product_path": "products/g-axxi-i",
  40. "price": 30,
  41. "quantity": 2,
  42. "csrftoken": csrf_token_no_options,
  43. }
  44. response_good_no_options_add = client.post(
  45. "/checkout", data=data_good_no_options_add, headers=headers_no_options
  46. )
  47. with capsys.disabled():
  48. print(f"client => {client.cookies}")
  49. assert response_good_no_options_add.status_code == 200
  50. def test_remove_simple_product(capsys, app_csrf, session_store):
  51. """removing a product with no options (size, style), good """
  52. headers_no_options, csrf_token_no_options = prepare_request("g-axxi-i")
  53. data_good_no_options_remove = {
  54. "page": "product",
  55. "operation": "remove",
  56. "product_id": "price_1PTPnFKMYdpilqPdArvtszSN",
  57. "product_path": "products/g-axxi-i",
  58. "price": 30,
  59. "quantity": 1,
  60. "csrftoken": csrf_token_no_options,
  61. }
  62. response_good_no_options_remove = client.post(
  63. "/checkout", data=data_good_no_options_remove, headers=headers_no_options
  64. )
  65. assert response_good_no_options_remove.status_code == 200
  66. def test_wrong_request_simple_product(capsys, app_csrf, session_store):
  67. # Product operation with wrong query
  68. headers_no_options, csrf_token_no_options = prepare_request("g-axxi-i")
  69. data_bad_no_options_wrong_add = {
  70. "page": "product",
  71. "product_id": "price_1PTPnFKMYdpilqPdArvtszSN",
  72. "product_path": "products/g-axxi-i",
  73. "price": 30,
  74. "quantity": 2,
  75. "csrftoken": csrf_token_no_options,
  76. }
  77. response_bad_no_options_add = client.post(
  78. "/checkout", data=data_bad_no_options_wrong_add, headers=headers_no_options
  79. )
  80. assert response_bad_no_options_add.status_code == 422
  81. def test_add_product(capsys, app_csrf, session_store):
  82. """ Adding a product with size/style """
  83. headers_with_options, csrf_token_with_options = prepare_request("a-tshirt-01")
  84. data_good_with_options_add = {
  85. "page": "product",
  86. "operation": "add",
  87. "product_id": "prod_RZBk650b1Tx95N",
  88. "product_path": "products/a-tshirt-01",
  89. "price": 50,
  90. "quantity": 3,
  91. "size": "m",
  92. "style": "black",
  93. "csrftoken": csrf_token_with_options,
  94. }
  95. response_good_with_options_add = client.post(
  96. "/checkout", data=data_good_with_options_add, headers=headers_with_options
  97. )
  98. assert response_good_with_options_add.status_code == 200
  99. def test_remove_product(capsys, app_csrf, session_store):
  100. """ Removing product with size/style """
  101. headers_with_options, csrf_token_with_options = prepare_request("a-tshirt-01")
  102. data_good_with_options_remove = {
  103. "page": "product",
  104. "operation": "remove",
  105. "product_id": "stripe_product_tshirt-aus",
  106. "product_path": "products/a-tshirt-01",
  107. "price": 50,
  108. "quantity": 2,
  109. "size": "m",
  110. "style": "black",
  111. "csrftoken": csrf_token_with_options,
  112. }
  113. response_good_with_options_remove = client.post(
  114. "/checkout", data=data_good_with_options_remove, headers=headers_with_options
  115. )
  116. assert response_good_with_options_remove.status_code == 200
  117. def test_add_product_wrong_options(capsys, app_csrf, session_store):
  118. """ Trying to add product with wrong size/style """
  119. headers_with_options, csrf_token_with_options = prepare_request("a-tshirt-01")
  120. data_bad_with_options_wrong_add = {
  121. "page": "product",
  122. "operation": "add",
  123. "product_id": "prod_RZBk650b1Tx95N",
  124. "product_path": "products/a-tshirt-01",
  125. "price": 50,
  126. "quantity": 5,
  127. "size": "x",
  128. "style": "yellow",
  129. "csrftoken": csrf_token_with_options,
  130. }
  131. response_bad_with_options_add = client.post(
  132. "/checkout", data=data_bad_with_options_wrong_add, headers=headers_with_options
  133. )
  134. assert response_bad_with_options_add.status_code == 422
  135. def test_remove_product_wrong_options(capsys, app_csrf, session_store):
  136. """ Trying to remove product with wrong size/style """
  137. headers_with_options, csrf_token_with_options = prepare_request("a-tshirt-01")
  138. data_bad_with_options_wrong_remove = {
  139. "page": "product",
  140. "operation": "remove",
  141. "product_id": "prod_RZBk650b1Tx95N",
  142. "product_path": "products/a-tshirt-01",
  143. "price": 50,
  144. "quantity": 5,
  145. "size": "x",
  146. "style": "yellow",
  147. "csrftoken": csrf_token_with_options,
  148. }
  149. response_bad_with_options_remove = client.post(
  150. "/checkout",
  151. data=data_bad_with_options_wrong_remove,
  152. headers=headers_with_options,
  153. )
  154. assert response_bad_with_options_remove.status_code == 422