def test_get_countries_returns_only_countries_allowed_by_shipping_rules( self): #Make sure there is another country, otherwise this is a bogus test country2 = factories.CountryFactory(code="NL") self.add_to_cartstall(self.prod_1, 4) countries = self.cart_stall.get_countries() self.assertEqual([self.country.id], [c.id for c in countries])
def test_get_returns_all_cart_stalls(self): country = factories.CountryFactory(code="ES", title="Spain") self.mock_get_countries.return_value = [country] view_url = reverse('cart_stalls') response = self.client.get(view_url) data = json.loads(response.content) self.assertEqual(len(data), 1) self.assertEqual(data[0], self.cart_stall.to_json())
def test_put_returns_cart_stall_summary(self): country = factories.CountryFactory(code="ES", title="Spain") self.mock_get_countries.return_value = [country] response = self.client.put(self.view_url, data=json.dumps({"country_id": country.id}), content_type="application/json") cs = CartStall.objects.get(id=self.cart_stall.id) self.assertEqual(json.loads(response.content), cs.to_json())
def test_get_country_returns_first_allowed_country_if_UK_invalid(self): profile = self.prod_1.shipping_profile rule = profile.shipping_rules.all()[0] country2 = factories.CountryFactory() rule.countries = [country2] rule.save() self.add_to_cartstall(self.prod_1, 3) self.assertEqual(self.cart_stall.get_country(), country2)
def test_throws_mismatching_country_if_address_speuclative_country_differ( self): country2 = factories.CountryFactory(code="DE") address = factories.ShippingAddressFactory(country=country2, user=self.user) self.cart_stall.speculative_country = self.country with self.assertRaises(purchase.models.MismatchingCountryError): self.cart_stall.set_address(address)
def test_throws_unavailable_shipping_country_if_invalid_shipping(self): country2 = factories.CountryFactory(code="DE") self.cart_stall.address.country = country2 self.cart_stall.address.save() self.add_to_cartstall(self.prod_1, 3) with self.assertRaises( purchase.models.UnavailableShippingCountryError): self.cart_stall.delivery_total()
def test_cart_stall_throws_error_on_set_invalid_shipping_address(self): country3 = factories.CountryFactory(code="FI") address = factories.ShippingAddressFactory(country=country3, user=self.user) self.add_to_cartstall(self.prod_1, 4) with self.assertRaises( purchase.models.UnavailableShippingCountryError): self.cart_stall.set_address(address)
def test_rules_from_other_countries_not_used(self): rule = factories.ShippingRuleFactory( profile=self.prod_1.shipping_profile, rule_price=100, rule_price_extra=1000) rule.countries.add(factories.CountryFactory(code="USA")) self.add_to_cartstall(self.prod_1, 3) self.assertEqual(self.cart_stall.delivery_total().amount, 20)
def test_put_updates_country(self): country = factories.CountryFactory(code="ES", title="Spain") self.mock_get_countries.return_value = [country] data = country.to_json() response = self.client.put(self.view_url, data=json.dumps({"country": data}), content_type="application/json") cs = CartStall.objects.get(id=self.cart_stall.id) self.assertEqual(cs.speculative_country, country)
def test_put_updates_note(self): data = {"note": "A test note"} country = factories.CountryFactory(code="ES", title="Spain") self.mock_get_countries.return_value = [country] response = self.client.put(self.view_url, data=json.dumps(data), content_type='application/json' ) cs = CartStall.objects.get(id=self.cart_stall.id) self.assertEqual(cs.note, "A test note")
def test_rest_of_world_used_if_not_none(self): profile = self.prod_1.shipping_profile profile.others_price = 50 profile.others_price_extra = 20 profile.save() country2 = factories.CountryFactory(code="DE") self.cart_stall.address.country = country2 self.cart_stall.address.save() self.add_to_cartstall(self.prod_1, 4) self.assertEqual(self.cart_stall.delivery_total().amount, 110)
def test_no_error_thrown_on_invalid_address_ships_to_rest_of_world(self): country3 = factories.CountryFactory(code="FI") product = self.create_product_with_profile(10, 3) self.profile.others_price = 10 self.profile.save() self.add_to_cartstall(self.prod_1, 4) address = factories.ShippingAddressFactory(country=country3, user=self.user) self.cart_stall.set_address(address) self.assertEqual(self.cart_stall.address.id, address.id)
def test_to_json(self): rule = factories.ShippingRuleFactory(rule_price=10, rule_price_extra=20, despatch_time=2, delivery_time=10, delivery_time_max=20) country1 = factories.CountryFactory(code="USA") country2 = factories.CountryFactory(code="NL") rule.countries.add(country1) rule.countries.add(country2) rule.save() expected = { "rule_price": 10.0, "rule_price_extra": 20.0, "dispatch_time": 2, "delivery_time": 10, "delivery_time_max": 20, } result = rule.to_json() countries = result.pop("countries") self.assertEqual(len(countries), 2) self.assertEqual(expected, result)
def test_speculative_country_used_if_exists(self): country2 = factories.CountryFactory(code="ES") rule2 = factories.ShippingRuleFactory( profile=self.prod_1.shipping_profile, rule_price=30, rule_price_extra=10) rule2.countries.add(country2) rule2.save() self.cart_stall.address = None self.cart_stall.speculative_country = country2 self.cart_stall.save() self.add_to_cartstall(self.prod_1, 1) delivery = self.cart_stall.delivery_total() self.assertEqual(delivery.amount, 30)
def test_shipping_address_with_invalid_country_returns_to_shipping(self): country2 = factories.CountryFactory(code="NL") data = { "name": "home", "line1": "A Place Somewhere", "line2": "Probably a village", "city": "Dorchester", "state": "Dorset", "postal_code": "DT1178T", "country": country2.id } product = factories.ProductFactory() self.cart.add(product) self.login() with mock.patch.object(purchase.models.CartStall, 'set_address') as mock_set_country: mock_set_country.side_effect = purchase.models.UnavailableShippingCountryError response = self.client.post(self.view_url, data=data) self.assertTemplateUsed('purchase/checkout_shipping.html') cart_stall = CartStall.objects.get(stall=product.stall, cart=self.cart) # Check that the address has not been updated self.assertIsNone(cart_stall.address) self.assertEqual(len(response.context['messages']), 1)
def test_get_default_country_returns_first_in_list_if_not_uk(self): country1 = factories.CountryFactory(code="USA") rule = factories.ShippingRuleFactory() rule.countries = [country1] rule.save() self.assertEqual(rule.profile.get_default_country(), country1)