test_shipping.py 6.3 KB

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