| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- import os
- import pytest
- from asgi_csrf import asgi_csrf
- from fastapi.testclient import TestClient
- from app.main import app
- @pytest.fixture
- def app_csrf():
- """Return a CSRF token."""
- return asgi_csrf(app, signing_secret=os.getenv("CSRF_SECRET_KEY"))
- def test_shipping_cost(app_csrf):
- """
- This test checks that the shipping cost function works as expected.
- """
- client = TestClient(
- app,
- base_url="http://127.0.0.1:5014",
- )
- headers = {
- "Accept-Language": "en-US,en;q=0.5",
- }
- dataset = {
- 'Switzerland': [
- {
- 'country': 'Switzerland',
- 'weight': 0,
- },
- {
- 'country': 'Switzerland',
- 'weight': 5,
- },
- {
- 'country': 'Switzerland',
- 'weight': 120,
- },
- {
- 'country': 'Switzerland',
- 'weight': 251,
- },
- {
- 'country': 'Switzerland',
- 'weight': 1000,
- },
- {
- 'country': 'Switzerland',
- 'weight': 1600,
- },
- {
- 'country': 'Switzerland',
- 'weight': 2000,
- },
- {
- 'country': 'Switzerland',
- 'weight': 2001,
- },
- {
- 'country': 'Switzerland',
- 'weight': 5010,
- },
- ],
- 'Europe': [
- {
- 'country': 'Germany',
- 'weight': 0,
- },
- {
- 'country': 'France',
- 'weight': 5,
- },
- {
- 'country': 'Italy',
- 'weight': 120,
- },
- {
- 'country': 'Greece',
- 'weight': 251,
- },
- {
- 'country': 'Spain',
- 'weight': 1000,
- },
- {
- 'country': 'Ireland',
- 'weight': 1600,
- },
- {
- 'country': 'Portugal',
- 'weight': 2000,
- },
- {
- 'country': 'Netherlands',
- 'weight': 2001,
- },
- {
- 'country': 'Austria',
- 'weight': 5010,
- },
- ],
- 'International': [
- {
- 'country': 'Georgia',
- 'weight': 0,
- },
- {
- 'country': 'Japan',
- 'weight': 5,
- },
- {
- 'country': 'Mexico',
- 'weight': 120,
- },
- {
- 'country': 'Togo',
- 'weight': 251,
- },
- {
- 'country': 'Uzbekistan',
- 'weight': 1000,
- },
- {
- 'country': 'Viet Nam',
- 'weight': 1600,
- },
- {
- 'country': 'Singapore',
- 'weight': 2000,
- },
- {
- 'country': 'Iceland',
- 'weight': 2001,
- },
- {
- 'country': 'Colombia',
- 'weight': 5010,
- },
- ]
- }
- for key in dataset.keys():
- area = dataset[key]
- for shipping_entry in area:
- res = client.post(f"/checkout/shipping", headers=headers, data=shipping_entry)
- if area == 'Switzerland':
- if shipping_entry['weight'] == 0:
- assert res.status_code == 403
-
- elif shipping_entry['weight'] == 5:
- assert res.shipping == 7.2
- elif shipping_entry['weight'] == 120:
- assert res.shipping == 7.7
- elif shipping_entry['weight'] == 251:
- assert res.shipping == 16.5
- elif shipping_entry['weight'] == 1000:
- assert res.shipping == 16.5
- elif shipping_entry['weight'] == 1600:
- assert res.shipping == 16.5
- elif shipping_entry['weight'] == 2000:
- assert res.shipping == 16.5
- elif shipping_entry['weight'] == 2001:
- assert res.shipping == 19.5
- elif shipping_entry['weight'] == 5010:
- assert res.shipping == 19.5
- elif area == 'Europe':
- if shipping_entry['weight'] == 0:
- assert res.status_code == 422
- elif shipping_entry['weight'] == 5:
- assert res.shipping == 10.0
- elif shipping_entry['weight'] == 120:
- assert res.shipping == 15.0
- elif shipping_entry['weight'] == 251:
- assert res.shipping == 20.0
- elif shipping_entry['weight'] == 1000:
- assert res.shipping == 26.0
- elif shipping_entry['weight'] == 1600:
- assert res.shipping == 31.0
- elif shipping_entry['weight'] == 2000:
- assert res.shipping == 36.0
- elif shipping_entry['weight'] == 2001:
- assert res.shipping == 51.0
- elif shipping_entry['weight'] == 5010:
- assert res.shipping == 56.0
-
- elif area == 'International':
- if shipping_entry['weight'] == 5:
- assert res.shipping == 13.0
- elif shipping_entry['weight'] == 120:
- assert res.shipping == 18.0
- elif shipping_entry['weight'] == 251:
- assert res.shipping == 23.0
- elif shipping_entry['weight'] == 1000:
- assert res.shipping == 33.0
- elif shipping_entry['weight'] == 1600:
- assert res.shipping == 38.0
- elif shipping_entry['weight'] == 2000:
- assert res.shipping == 43.0
- elif shipping_entry['weight'] == 2001:
- assert res.shipping == 56.0
- elif shipping_entry['weight'] == 5010:
- assert res.shipping == 76.0
-
|