test_shipping.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import os
  2. import pytest
  3. from asgi_csrf import asgi_csrf
  4. from fastapi.testclient import TestClient
  5. from app.main import app
  6. @pytest.fixture
  7. def app_csrf():
  8. """Return a CSRF token."""
  9. return asgi_csrf(app, signing_secret=os.getenv("CSRF_SECRET_KEY"))
  10. def test_shipping_cost(app_csrf):
  11. """
  12. This test checks that the shipping cost function works as expected.
  13. """
  14. client = TestClient(
  15. app,
  16. base_url="http://127.0.0.1:5014",
  17. )
  18. headers = {
  19. "Accept-Language": "en-US,en;q=0.5",
  20. }
  21. dataset = {
  22. 'Switzerland': [
  23. {
  24. 'country': 'Switzerland',
  25. 'weight': 0,
  26. },
  27. {
  28. 'country': 'Switzerland',
  29. 'weight': 5,
  30. },
  31. {
  32. 'country': 'Switzerland',
  33. 'weight': 120,
  34. },
  35. {
  36. 'country': 'Switzerland',
  37. 'weight': 251,
  38. },
  39. {
  40. 'country': 'Switzerland',
  41. 'weight': 1000,
  42. },
  43. {
  44. 'country': 'Switzerland',
  45. 'weight': 1600,
  46. },
  47. {
  48. 'country': 'Switzerland',
  49. 'weight': 2000,
  50. },
  51. {
  52. 'country': 'Switzerland',
  53. 'weight': 2001,
  54. },
  55. {
  56. 'country': 'Switzerland',
  57. 'weight': 5010,
  58. },
  59. ],
  60. 'Europe': [
  61. {
  62. 'country': 'Germany',
  63. 'weight': 0,
  64. },
  65. {
  66. 'country': 'France',
  67. 'weight': 5,
  68. },
  69. {
  70. 'country': 'Italy',
  71. 'weight': 120,
  72. },
  73. {
  74. 'country': 'Greece',
  75. 'weight': 251,
  76. },
  77. {
  78. 'country': 'Spain',
  79. 'weight': 1000,
  80. },
  81. {
  82. 'country': 'Ireland',
  83. 'weight': 1600,
  84. },
  85. {
  86. 'country': 'Portugal',
  87. 'weight': 2000,
  88. },
  89. {
  90. 'country': 'Netherlands',
  91. 'weight': 2001,
  92. },
  93. {
  94. 'country': 'Austria',
  95. 'weight': 5010,
  96. },
  97. ],
  98. 'International': [
  99. {
  100. 'country': 'Georgia',
  101. 'weight': 0,
  102. },
  103. {
  104. 'country': 'Japan',
  105. 'weight': 5,
  106. },
  107. {
  108. 'country': 'Mexico',
  109. 'weight': 120,
  110. },
  111. {
  112. 'country': 'Togo',
  113. 'weight': 251,
  114. },
  115. {
  116. 'country': 'Uzbekistan',
  117. 'weight': 1000,
  118. },
  119. {
  120. 'country': 'Viet Nam',
  121. 'weight': 1600,
  122. },
  123. {
  124. 'country': 'Singapore',
  125. 'weight': 2000,
  126. },
  127. {
  128. 'country': 'Iceland',
  129. 'weight': 2001,
  130. },
  131. {
  132. 'country': 'Colombia',
  133. 'weight': 5010,
  134. },
  135. ]
  136. }
  137. for key in dataset.keys():
  138. area = dataset[key]
  139. for shipping_entry in area:
  140. res = client.post(f"/checkout/shipping", headers=headers, data=shipping_entry)
  141. if area == 'Switzerland':
  142. if shipping_entry['weight'] == 0:
  143. assert res.status_code == 403
  144. elif shipping_entry['weight'] == 5:
  145. assert res.shipping == 7.2
  146. elif shipping_entry['weight'] == 120:
  147. assert res.shipping == 7.7
  148. elif shipping_entry['weight'] == 251:
  149. assert res.shipping == 16.5
  150. elif shipping_entry['weight'] == 1000:
  151. assert res.shipping == 16.5
  152. elif shipping_entry['weight'] == 1600:
  153. assert res.shipping == 16.5
  154. elif shipping_entry['weight'] == 2000:
  155. assert res.shipping == 16.5
  156. elif shipping_entry['weight'] == 2001:
  157. assert res.shipping == 19.5
  158. elif shipping_entry['weight'] == 5010:
  159. assert res.shipping == 19.5
  160. elif area == 'Europe':
  161. if shipping_entry['weight'] == 0:
  162. assert res.status_code == 422
  163. elif shipping_entry['weight'] == 5:
  164. assert res.shipping == 10.0
  165. elif shipping_entry['weight'] == 120:
  166. assert res.shipping == 15.0
  167. elif shipping_entry['weight'] == 251:
  168. assert res.shipping == 20.0
  169. elif shipping_entry['weight'] == 1000:
  170. assert res.shipping == 26.0
  171. elif shipping_entry['weight'] == 1600:
  172. assert res.shipping == 31.0
  173. elif shipping_entry['weight'] == 2000:
  174. assert res.shipping == 36.0
  175. elif shipping_entry['weight'] == 2001:
  176. assert res.shipping == 51.0
  177. elif shipping_entry['weight'] == 5010:
  178. assert res.shipping == 56.0
  179. elif area == 'International':
  180. if shipping_entry['weight'] == 5:
  181. assert res.shipping == 13.0
  182. elif shipping_entry['weight'] == 120:
  183. assert res.shipping == 18.0
  184. elif shipping_entry['weight'] == 251:
  185. assert res.shipping == 23.0
  186. elif shipping_entry['weight'] == 1000:
  187. assert res.shipping == 33.0
  188. elif shipping_entry['weight'] == 1600:
  189. assert res.shipping == 38.0
  190. elif shipping_entry['weight'] == 2000:
  191. assert res.shipping == 43.0
  192. elif shipping_entry['weight'] == 2001:
  193. assert res.shipping == 56.0
  194. elif shipping_entry['weight'] == 5010:
  195. assert res.shipping == 76.0