def test_subscription_property(self): """ Test that property return write subscription """ subscription = model_factory(Subscription, save=True) duration = model_factory(Duration, product=subscription) self.assertEqual(duration.subscription, subscription)
def test_order_comfirm_method_cart(self): """ Test that method set cart as no active """ cart = model_factory(Cart, is_active=True, save=True) order = model_factory(Order, cart=cart, save=True) order.confirm() self.assertFalse(cart.is_active)
def test_min_variation(self): product = model_factory(Product, save=True) self.assertEqual(product.min_variation(), None) variations = model_factory(ProductVariation, product = [product, product], price = [10, 20], save=True) self.assertEqual(product.min_variation(), variations[0])
def test_authenticate(self): """ Test that backend can authenticate user """ code = 'test_code' expected_user = model_factory(User, save=True) model_factory(UserConfirmation, key=code, user=expected_user, save=True) backend = ConfirmationUserBackend() self.assertEqual(expected_user, backend.authenticate(code))
def test_get_discounts(self): """ Test has_discount method """ expected = model_factory(Discount, end_date=datetime.now(), start_date=datetime.now(), save=True) user = model_factory(User, save=True) expected.valid_users.add(user.contact) self.assertEqual([expected], list(user.contact.get_discounts_list()))
def test_unicode(self): """ Test that profile return valid username """ expeced_value = 'test_username' user = model_factory(User, username=expeced_value, save=True) profile = Contact.objects.get(user=user) self.assertEqual(expeced_value, str(profile)) profile = model_factory(Contact, save=True) self.assertEqual('Anonimus', str(profile))
def test_confirm_view(self): """ Test confirmation view """ confirm_key = "some_key" user = model_factory(User, save=True) model_factory(UserConfirmation, key=confirm_key, user=user, save=True) response = self.app.get(reverse("accounts:confirm"), params={"key": confirm_key}) self.assertEqual(self.app.session[SESSION_KEY], user.id) self.assertEqual(response.status, "200 OK")
def test_set_no_active_method(self): """ Test that method set cart as no active """ contact = model_factory(Contact, save=True) cart = model_factory(Cart, contact=contact, save=True) cart.set_no_active() self.assertFalse(cart.is_active)
def test_order_confirm_order_status(self): """ Test that method change order status """ cart = model_factory(Cart, is_active=True, save=True) order = model_factory(Order, cart=cart, save=True) order.confirm() self.assertEqual(order.status, 'Billed')
def test_unicode(self): product = model_factory(Product, name='test', save=True) variation = model_factory(ProductVariation, price=10, product=product, save=True) self.assertEqual(str(variation), 'test()')
def test_add_out_of_stock_product(self): """ Test that product can't be added if it out of stock """ cart = model_factory(Cart, save=True) product = model_factory(Product, save=True) product_variation = model_factory(ProductVariation, items_in_stock=0, product=product, save=True) self.assertRaises(AssertionError, cart.add_product, product_variation)
def test_get_existed_user(self): """ Test that cart can gets for anonymous user """ request = RequestFactory().get('/') user = model_factory(User, save=True) expected_cart = model_factory(Cart, contact=user.contact, is_active=True, save=True) request.user = user actual_cart = Cart.objects.get_or_create_from_request(request) self.assertEqual(expected_cart, actual_cart)
def test_has_discount(self): """ Test has_discount method """ discount = model_factory(Discount, end_date=datetime.now(), start_date=datetime.now(), save=True) user = model_factory(User, save=True) self.assertFalse(user.contact.has_discount()) discount.valid_users.add(user.contact) self.assertTrue(user.contact.has_discount())
def test_get_items_price(self): cart = Cart.objects.create_active() product = model_factory(Product, save=True) product_variation = model_factory(ProductVariation, product=product, save=True) cart_item = model_factory(CartItem, cart=cart, product=product_variation, save=True) with patch('salest.cart.models.CartItem.get_cart_item_price', Mock(return_value=10)): self.assertEqual(cart.get_items_price(), 10)
def test_create_with_request_user(self): """ Test that cart can be created from request and user logged in """ request = RequestFactory().get('/') user = model_factory(User, save=True) request.user = user expected_cart = model_factory(Cart) with patch('salest.cart.models.CartManager.create_active', Mock(return_value=expected_cart)) as active: Cart.objects.get_or_create_from_request(request) active.assert_called_once_with(contact=user.contact)
def test_confirm_method(self): """ Test that method add user """ user = model_factory(User, save=True) subscription = model_factory(Subscription, save=True) duration = model_factory(Duration, product=subscription, price=1, save=True) duration.confirm(user) self.assertTrue(user in duration.users.all())
def test_get_items(self): cart = Cart.objects.create_active() product = model_factory(Product, save=True) product_variation = model_factory(ProductVariation, product=product, save=True) cart_item = model_factory(CartItem, cart=cart, product=product_variation, quantity=10, save=True) self.assertEqual(list(cart.get_items()), [cart_item])
def test_authenticate_invalid(self): """ Test that backend skip authentication of invalid user confirmations codes """ expected_user = model_factory(User, save=True) model_factory(UserConfirmation, key='test_code', user=expected_user, save=True) backend = ConfirmationUserBackend() self.assertTrue(backend.authenticate('another code') is None)
def test_dummy_sucsess_get_context_data_confirm(self): """ Test that method call order confirm method """ cart = model_factory(Cart, is_active=True, save=True) order = model_factory(Order, cart=cart, save=True) request = RequestFactory().post('/', {'order_id': order.id}) request.session = {} with patch('salest.payments.models.Order.confirm') as confirm: dummy_success_view(request=request).get_context_data() confirm.assert_called_once_with()
def test_get_404(self): product = model_factory(Product, save=True) product_variation = model_factory(ProductVariation, product=product, save=True) cart = Cart.objects.create_active() user = model_factory(User, save=True) request = RequestFactory().get('/') request.user = user request.cart = cart view = CartItemAddView() self.assertRaises(Http404, view.get, request, 0)
def test_dummy_form_save_credit_number(self): """ Test that dummy payment form save method save order """ wizard = PrePaymentWizard() cart = model_factory(Cart, save=True) wizard.order = model_factory(Order, cart=cart) form = DummyPaymentForm() form.cleaned_data = FORM_CLEANED_DATA order = form.save(wizard) self.assertEqual(order.card_number, '1111')
def test_view(self): """ Test signup view process """ model_factory(EmailTemplate, template_key="invitation_email", save=True) resp = self.app.get(reverse("accounts:signup")) form = resp.form form["username"] = "******" form["password1"] = "password" form["password2"] = "password" form["email"] = get_fake_email() response = form.submit() self.assertEqual(response.status, "302 FOUND")
def test_dummy_sucsess_remove_cart_from_session(self): """ Test that success method call remove_cart_from_session """ cart = model_factory(Cart, is_active=True, save=True) order = model_factory(Order, cart=cart, save=True) request = RequestFactory().post('/', {'order_id': order.id}) request.session = {} with patch('salest.cart.models.CartManager.remove_cart_from_session')\ as remove: dummy_success_view(request=request).get_context_data() remove.assert_called_once_with(request)
def test_clear_cart(self): cart = Cart.objects.create_active() product = model_factory(Product, save=True) product_variation = model_factory(ProductVariation, product=product, save=True) discount = model_factory(Discount, save=True) cart.discount.add(discount) cart_item = model_factory(CartItem, cart=cart, product=product_variation, save=True) self.assertTrue(bool(cart.items.all())) cart.clear_cart() self.assertFalse(bool(cart.items.all()))
def test_get_quantity(self): cart = Cart.objects.create_active() product = model_factory(Product, save=True) product_variation = model_factory(ProductVariation, product=product, save=True) discount = model_factory(Discount, save=True) cart.discount.add(discount) cart_item = model_factory(CartItem, cart=cart, product=product_variation, quantity=10, save=True) self.assertEqual(cart.get_quantity(), 10)
def test_generate_duration_choice_method(self): """ Test that method generate currect choices """ subscription = model_factory(Subscription, save=True) duration = model_factory(Duration, product=subscription, price=12, save=True) expected_value = [(duration.id, "{0} days for ${1}.00".format( duration.duration.days, duration.price))] current_value = generate_duration_choice(subscription.id) self.assertEqual(expected_value, current_value)
def test_create_active_with_contact(self): """ Test that create_active creates active cart with contact and update other carts to be inactive """ contact = model_factory(Contact, save=True) old_cart = model_factory(Cart, contact=contact, is_active=True, save=True) new_cart = Cart.objects.create_active(contact=contact) self.assertTrue(new_cart.is_active) old_cart = Cart.objects.get(id=old_cart.id) self.assertFalse(old_cart.is_active)
def test_add_new_product(self): """ Test that cart item created if product not in cart yet """ cart = model_factory(Cart, save=True) product = model_factory(Product, save=True) product_variation = model_factory(ProductVariation, items_in_stock=1, product=product, save=True) cart.add_product(product_variation) actual_cart_item = cart.items.get() self.assertEqual(actual_cart_item.quantity, 1) self.assertEqual(actual_cart_item.product, product_variation)
def test_base_address_save(self): """ Test that form save address with shipping=True and address is saved """ wizard = PrePaymentWizard() wizard.contact = model_factory(Contact, save=True) address = model_factory(Address) form = BaseAddressForm() with patch('django.forms.ModelForm.save', Mock(return_value=address)): address = form.save(wizard=wizard) self.assertTrue(address.pk is not None) self.assertEqual(address.contact, wizard.contact)
def test_get(self): cart = Cart.objects.create_active() product = model_factory(Product, save=True) product_variation = model_factory(ProductVariation, product=product, save=True) user = model_factory(User, save=True) request = RequestFactory().get('/') request.user = user request.cart = cart view = CartItemAddView() with patch('salest.cart.models.Cart.add_product', Mock()) as test_cart: view.get(request, product_variation.id) test_cart.assert_called_once_with(product_variation)