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