Changeset View
Changeset View
Standalone View
Standalone View
looper/tests/test_customer.py
| from unittest.mock import patch | from unittest.mock import patch | ||||
| from django.contrib.auth import get_user_model | from django.contrib.auth import get_user_model | ||||
| from django.urls import reverse | from django.urls import reverse | ||||
| from django_countries.fields import Country | from django_countries.fields import Country | ||||
| from looper.exceptions import GatewayError | from looper.exceptions import GatewayError | ||||
| from looper.gateways import PaymentMethodInfo | from looper.gateways import PaymentMethodInfo | ||||
| from looper.models import Customer, Address, PaymentMethod, Gateway, GatewayCustomerId | from looper.models import Customer, Address, PaymentMethod, Gateway, GatewayCustomerId | ||||
| from looper.taxes import ProductType, TaxType | |||||
| from looper.tests import AbstractBaseTestCase | from looper.tests import AbstractBaseTestCase | ||||
| User = get_user_model() | User = get_user_model() | ||||
| class AbstractSingleUserTestCase(AbstractBaseTestCase): | class AbstractSingleUserTestCase(AbstractBaseTestCase): | ||||
| fixtures = ['testuser'] | fixtures = ['testuser'] | ||||
| Show All 26 Lines | class CustomerModelTestCase(AbstractSingleUserTestCase): | ||||
| def test_billing_address(self): | def test_billing_address(self): | ||||
| # Ensure the customer does not have an Address associated | # Ensure the customer does not have an Address associated | ||||
| with self.assertRaises(Address.DoesNotExist): | with self.assertRaises(Address.DoesNotExist): | ||||
| Address.objects.get(user=self.user) | Address.objects.get(user=self.user) | ||||
| # Getting the customer address should always return an Address object | # Getting the customer address should always return an Address object | ||||
| self.assertIsInstance(self.customer.billing_address, Address) | self.assertIsInstance(self.customer.billing_address, Address) | ||||
| def test_get_tax_no_billing_address(self): | |||||
| # Ensure the customer does not have an Address associated | |||||
| with self.assertRaises(Address.DoesNotExist): | |||||
| Address.objects.get(user=self.user) | |||||
| tax_type, tax_rate = self.customer.get_tax(ProductType.ELECTRONIC_SERVICE.value) | |||||
| self.assertEqual(tax_type, TaxType.NO_CHARGE) | |||||
| self.assertEqual(tax_rate, 0) | |||||
| def test_get_tax_no_vat_number_nl(self): | |||||
| self.customer.vat_number = None | |||||
| billing_address = self.customer.billing_address | |||||
| billing_address.country = 'NL' | |||||
| billing_address.save() | |||||
| tax_type, tax_rate = self.customer.get_tax(ProductType.ELECTRONIC_SERVICE.value) | |||||
| self.assertEqual(tax_type, TaxType.VAT_CHARGE) | |||||
| self.assertEqual(tax_rate, 21) | |||||
| def test_get_tax_vat_number_nl(self): | |||||
| self.customer.vat_number = 'NL818152011B01' | |||||
| billing_address = self.customer.billing_address | |||||
| billing_address.country = 'NL' | |||||
| billing_address.save() | |||||
| tax_type, tax_rate = self.customer.get_tax(ProductType.ELECTRONIC_SERVICE.value) | |||||
| self.assertEqual(tax_type, TaxType.VAT_CHARGE) | |||||
| self.assertEqual(tax_rate, 21) | |||||
| def test_get_tax_no_vat_number_de(self): | |||||
| self.customer.vat_number = None | |||||
| billing_address = self.customer.billing_address | |||||
| billing_address.country = 'DE' | |||||
| billing_address.save() | |||||
| tax_type, tax_rate = self.customer.get_tax(ProductType.ELECTRONIC_SERVICE.value) | |||||
| self.assertEqual(tax_type, TaxType.VAT_CHARGE) | |||||
| self.assertEqual(tax_rate, 19) | |||||
| def test_get_tax_vat_number_de(self): | |||||
| self.customer.vat_number = 'DE260543043' | |||||
| billing_address = self.customer.billing_address | |||||
| billing_address.country = 'DE' | |||||
| billing_address.save() | |||||
| tax_type, tax_rate = self.customer.get_tax(ProductType.ELECTRONIC_SERVICE.value) | |||||
| self.assertEqual(tax_type, TaxType.VAT_REVERSE_CHARGE) | |||||
| self.assertEqual(tax_rate, 19) | |||||
| def test_get_tax_no_vat_number_se(self): | |||||
| self.customer.vat_number = None | |||||
| billing_address = self.customer.billing_address | |||||
| billing_address.country = 'SE' | |||||
| billing_address.save() | |||||
| tax_type, tax_rate = self.customer.get_tax(ProductType.ELECTRONIC_SERVICE.value) | |||||
| self.assertEqual(tax_type, TaxType.VAT_CHARGE) | |||||
| self.assertEqual(tax_rate, 25) | |||||
| def test_get_tax_vat_number_se(self): | |||||
| self.customer.vat_number = 'SE 556070171501' | |||||
| billing_address = self.customer.billing_address | |||||
| billing_address.country = 'SE' | |||||
| billing_address.save() | |||||
| tax_type, tax_rate = self.customer.get_tax(ProductType.ELECTRONIC_SERVICE.value) | |||||
| self.assertEqual(tax_type, TaxType.VAT_REVERSE_CHARGE) | |||||
| self.assertEqual(tax_rate, 25) | |||||
| def test_get_tax_no_vat_number_us(self): | |||||
| self.customer.vat_number = None | |||||
| billing_address = self.customer.billing_address | |||||
| billing_address.country = 'US' | |||||
| billing_address.save() | |||||
| tax_type, tax_rate = self.customer.get_tax(ProductType.ELECTRONIC_SERVICE.value) | |||||
| self.assertEqual(tax_type, TaxType.NO_CHARGE) | |||||
| self.assertEqual(tax_rate, 0) | |||||
| def test_get_tax_vat_number_us(self): | |||||
| self.customer.vat_number = 'BOGUSVATNUMBER' | |||||
| billing_address = self.customer.billing_address | |||||
| billing_address.country = 'US' | |||||
| billing_address.save() | |||||
| tax_type, tax_rate = self.customer.get_tax(ProductType.ELECTRONIC_SERVICE.value) | |||||
| self.assertEqual(tax_type, TaxType.NO_CHARGE) | |||||
| self.assertEqual(tax_rate, 0) | |||||
| def test_payment_method_default_no_method(self): | def test_payment_method_default_no_method(self): | ||||
| # Ensure no payment method is associated to the Customer | # Ensure no payment method is associated to the Customer | ||||
| with self.assertRaises(PaymentMethod.DoesNotExist): | with self.assertRaises(PaymentMethod.DoesNotExist): | ||||
| PaymentMethod.objects.get(user=self.user) | PaymentMethod.objects.get(user=self.user) | ||||
| # A customer without payment method should return payment method None | # A customer without payment method should return payment method None | ||||
| self.assertIsNone(self.customer.payment_method_default) | self.assertIsNone(self.customer.payment_method_default) | ||||
| def test_payment_method_default_one_methods(self): | def test_payment_method_default_one_methods(self): | ||||
| ▲ Show 20 Lines • Show All 215 Lines • Show Last 20 Lines | |||||