def test_total_price_to_pay_with_more_credit_than_order_price(self): user = VokoUserFactory() BalanceFactory(user=user, type="CR", amount=100) order = OrderFactory(user=user) odp1 = OrderProductFactory(order=order, amount=1, product__base_price=10) self.assertEqual(order.total_price_to_pay_with_balances_taken_into_account(), 0)
def test_total_price_to_pay_with_large_debit(self): user = VokoUserFactory() BalanceFactory(user=user, type="DR", amount=100) order = OrderFactory(user=user) odp1 = OrderProductFactory(order=order, amount=1, product__base_price=10) self.assertEqual(order.total_price_to_pay_with_balances_taken_into_account(), order.total_price + Decimal("100"))
def test_user_order_number_with_multiple_orders_but_different_users(self): user1 = VokoUserFactory() user2 = VokoUserFactory() order1 = OrderFactory.create(paid=True, finalized=True, user=user1) order2 = OrderFactory.create(paid=True, finalized=True, user=user2) self.assertEqual(order1.user_order_number, 1) self.assertEqual(order2.user_order_number, 1)
def test_total_price_to_pay_with_no_balance(self): order = OrderFactory() OrderProductFactory(order=order) OrderProductFactory(order=order) self.assertEqual( order.total_price_to_pay_with_balances_taken_into_account(), order.total_price)
def test_total_price_to_pay_with_credit(self): user = VokoUserFactory() BalanceFactory(user=user, type="CR", amount=0.10) order = OrderFactory(user=user) odp1 = OrderProductFactory(order=order) odp2 = OrderProductFactory(order=order) self.assertEqual(order.total_price_to_pay_with_balances_taken_into_account(), order.total_price - Decimal("0.10"))
def setUp(self): super(TestPaymentWebhook, self).setUp() self.order = OrderFactory(user=self.user, finalized=True, paid=False) self.payment = PaymentFactory(order=self.order) self.url = reverse('finance.callback')
def test_member_fee_with_one_paid_order(self): user = VokoUserFactory() order1 = OrderFactory(paid=False, user=user) order2 = OrderFactory(paid=True, user=user) order3 = OrderFactory(paid=False, user=user) self.assertEqual(order1.member_fee, settings.MEMBER_FEE) self.assertEqual(order2.member_fee, settings.MEMBER_FEE) self.assertEqual(order3.member_fee, Decimal("0"))
def test_mail_failure_notification(self): order = OrderFactory() order.mail_failure_notification() self.get_template_by_id.assert_called_once_with(ORDER_FAILED_ID) self.render_mail_template.assert_called_once_with(self.get_template_by_id.return_value, user=order.user, order=order) self.mail_user.assert_called_once_with(order.user)
def test_total_price_to_pay_with_credit(self): user = VokoUserFactory() BalanceFactory(user=user, type="CR", amount=0.10) order = OrderFactory(user=user) OrderProductFactory(order=order) OrderProductFactory(order=order) self.assertEqual( order.total_price_to_pay_with_balances_taken_into_account(), order.total_price - Decimal("0.10"))
def test_member_fee_on_unpaid_orders(self): user = VokoUserFactory() order1 = OrderFactory(paid=False, user=user) order2 = OrderFactory(paid=False, user=user) order3 = OrderFactory(paid=False, user=user) self.assertEqual(order1.member_fee, settings.MEMBER_FEE) self.assertEqual(order2.member_fee, settings.MEMBER_FEE) self.assertEqual(order3.member_fee, settings.MEMBER_FEE)
def test_mail_confirmation(self): order = OrderFactory() order.mail_confirmation() self.get_template_by_id.assert_called_once_with(ORDER_CONFIRM_MAIL_ID) self.render_mail_template.assert_called_once_with(self.get_template_by_id.return_value, user=order.user, order=order) self.mail_user.assert_called_once_with(order.user)
def test_mail_confirmation(self): order = OrderFactory() order.mail_confirmation() self.get_template_by_id.assert_called_once_with( config.ORDER_CONFIRM_MAIL) self.render_mail_template.assert_called_once_with( self.get_template_by_id.return_value, user=order.user, order=order) self.mail_user.assert_called_once_with(order.user)
def setUp(self): super(TestConfirmTransaction, self).setUp() self.url = reverse('finance.confirmtransaction') self.order = OrderFactory(user=self.user, finalized=True, paid=False) self.payment = PaymentFactory(order=self.order) self.client.login()
def test_user_order_number_with_multiple_orders(self): user = VokoUserFactory() order1 = OrderFactory.create(paid=True, finalized=True, user=user) order2 = OrderFactory.create(paid=False, finalized=True, user=user) order3 = OrderFactory.create(paid=True, finalized=True, user=user) order4 = OrderFactory.create(paid=True, finalized=False, user=user) self.assertEqual(order1.user_order_number, 1) self.assertEqual(order2.user_order_number, None) self.assertEqual(order3.user_order_number, 2) self.assertEqual(order4.user_order_number, None)
def test_number_of_orders_with_paid_and_unpaid_orders(self): order_round = OrderRoundFactory() # 3 paid OrderFactory(order_round=order_round, paid=True) OrderFactory(order_round=order_round, paid=True) OrderFactory(order_round=order_round, paid=True) # 2 unpaid OrderFactory(order_round=order_round, paid=False) OrderFactory(order_round=order_round, paid=False, finalized=True) self.assertEqual(order_round.number_of_orders(), 3)
def test_create_debit(self): order = OrderFactory() odp1 = OrderProductFactory(order=order) self.assertIsNone(order.debit) order.create_debit() order = Order.objects.get() self.assertEqual(order.debit.user, order.user) self.assertEqual(order.debit.type, "DR") self.assertEqual(order.debit.amount, order.total_price) self.assertEqual(order.debit.notes, "Debit van %s voor bestelling #%d" % (order.total_price, order.id))
def test_suppliers_with_paid_and_unpaid_orders(self): order_round = OrderRoundFactory() supplier1 = SupplierFactory() supplier2 = SupplierFactory() paid_order = OrderFactory(paid=True, finalized=True, order_round=order_round) finalized_order = OrderFactory(paid=False, finalized=True, order_round=order_round) OrderProductFactory(product__supplier=supplier1, order=paid_order) OrderProductFactory(product__supplier=supplier2, order=finalized_order) self.assertCountEqual(order_round.suppliers(), [supplier1])
def test_create_debit(self): order = OrderFactory() OrderProductFactory(order=order) self.assertIsNone(order.debit) order.create_debit() order = Order.objects.get() self.assertEqual(order.debit.user, order.user) self.assertEqual(order.debit.type, "DR") self.assertEqual(order.debit.amount, order.total_price) self.assertEqual( order.debit.notes, "Debit van %s voor bestelling #%d" % (order.total_price, order.id))
def test_total_order_price_with_one_orderproduct(self): order = OrderFactory() odp1 = OrderProductFactory(order=order) self.assertEqual( order.total_price, (order.member_fee + order.order_round.transaction_costs) + odp1.total_retail_price)
def setUp(self): super(TestCreateTransaction, self).setUp() self.url = reverse('finance.createtransaction') self.order = OrderFactory(user=self.user, finalized=True, paid=False)
def setUp(self): super(TestConfirmTransaction, self).setUp() self.url = reverse("finance.confirmtransaction") self.order = OrderFactory(user=self.user, finalized=True, paid=False) self.payment = PaymentFactory(order=self.order) self.client.login()
def test_ordermanager_get_current_order_2(self): order = OrderFactory(paid=True) self.assertEqual(len(Order.objects.all()), 1) order.user.orders.get_current_order() self.get_or_create_order.assert_called_once_with(user=order.user) self.assertEqual(order.user.orders.get_current_order(), self.get_or_create_order.return_value)
def test_complete_after_payment_method(self): with patch("ordering.models.Order.mail_confirmation") as mock_mail: order = OrderFactory(paid=False, finalized=True) self.assertFalse(Balance.objects.exists()) # No debit created yet order.complete_after_payment() order = Order.objects.get() self.assertTrue(order.paid) debit = Balance.objects.get() self.assertEqual(debit.user, order.user) self.assertEqual(debit.type, 'DR') self.assertEqual(debit.amount, order.total_price) self.assertEqual(debit.notes, 'Debit van %.2f voor bestelling #%s' % (order.total_price, order.id)) mock_mail.assert_called_once_with()
def setUp(self): super(TestCreateTransaction, self).setUp() self.url = reverse("finance.createtransaction") self.order = OrderFactory(user=self.user, finalized=True, paid=False) s = self.client.session s["order_to_pay"] = self.order.id s.save()
def test_attribute_is_added_to_products_with_orderproduct(self): order = OrderFactory(order_round=self.round, user=self.user) product = ProductFactory(order_round=self.round) odp1 = OrderProductFactory(order=order, product=product) ret = self.client.get(self.url) self.assertEqual( list(ret.context['view'].products())[0].ordered_amount, odp1.amount)
def test_that_sold_out_product_is_removed(self): product = ProductFactory(order_round=self.round, maximum_total_order=10) self.assertEqual(10, product.amount_available) order1 = OrderFactory(order_round=self.round, finalized=True, paid=True) OrderProductFactory(order=order1, product=product, amount=10) self.assertEqual(10, product.amount_ordered) self.assertEqual(0, product.amount_available) order2 = OrderFactory(order_round=self.round) OrderProductFactory(order=order2, amount=1) update_totals_for_products_with_max_order_amounts(order2) self.assertEqual(1, len(product.orderproducts.all()))
def test_complete_after_payment_method(self): with patch("ordering.models.Order.mail_confirmation") as mock_mail: order = OrderFactory(paid=False, finalized=True) self.assertFalse(Balance.objects.exists()) # No debit created yet order.complete_after_payment() order = Order.objects.get() self.assertTrue(order.paid) debit = Balance.objects.get() self.assertEqual(debit.user, order.user) self.assertEqual(debit.type, 'DR') self.assertEqual(debit.amount, order.total_price) self.assertEqual( debit.notes, 'Debit van %.2f voor bestelling #%s' % (order.total_price, order.id)) mock_mail.assert_called_once_with()
def test_redirect_to_payment_page_when_current_order_is_finalized(self): self.patch("finance.views.Mollie") OrderFactory(order_round=self.round, user=self.user, finalized=True) ret = self.client.get(self.url, follow=True) self.assertRedirects(ret, reverse('finance.choosebank'), fetch_redirect_response=True) self.assertMsgInResponse( ret, "Je bent doorgestuurd naar de betaalpagina " "omdat je bestelling nog niet is betaald!")
def setUp(self): super(TestPaymentWebhook, self).setUp() self.order = OrderFactory(user=self.user, finalized=True, paid=False) self.payment = PaymentFactory(order=self.order) s = self.client.session s["order_to_pay"] = self.order.id s.save() self.url = reverse("finance.callback")
def test_stock_products_without_stock_are_excluded(self): order = OrderFactory(order_round=self.round, user=self.user) product = ProductFactory(order_round=self.round) sproduct = ProductFactory(order_round=None) OrderProductFactory(order=order, product=product) OrderProductFactory(order=order, product=sproduct) ret = self.client.get(self.url) self.assertNotIn(sproduct, list(ret.context['view'].products())) self.assertIn(product, list(ret.context['view'].products()))
def setUp(self): self.url = reverse('finance.cancelpayment') self.user = VokoUserFactory.create() self.user.set_password('secret') self.user.is_active = True self.user.save() self.client.login(username=self.user.email, password='******') self.order = OrderFactory(user=self.user, finalized=True, paid=False)
def test_sold_out_stock_product_is_removed(self): # 10 available product = ProductFactory(order_round=None) ProductStockFactory(product=product, amount=10) self.assertEqual(10, product.amount_available) order1 = OrderFactory(order_round=self.round, finalized=True, paid=True) OrderProductFactory(order=order1, product=product, amount=10) # 10 ordered, 0 remain self.assertEqual(10, product.amount_ordered) self.assertEqual(0, product.amount_available) # order 1 more order2 = OrderFactory(order_round=self.round) OrderProductFactory(order=order2, product=product, amount=1) self.assertEqual(2, len(product.orderproducts.all())) update_totals_for_products_with_max_order_amounts(order2) self.assertEqual(1, len(product.orderproducts.all()))
def test_supplier_total_order_sum_with_one_order(self): order_round = OrderRoundFactory() supplier1 = SupplierFactory() paid_order = OrderFactory(paid=True, finalized=True, order_round=order_round) supplier1_orderproduct = OrderProductFactory( product__supplier=supplier1, order=paid_order) self.assertCountEqual(order_round.suppliers(), [supplier1]) self.assertEqual( order_round.supplier_total_order_sum(supplier1), supplier1_orderproduct.product.base_price * supplier1_orderproduct.amount)
def test_has_orders_returns_true_on_paid_orders(self): order = OrderFactory(finalized=True, paid=True, order_round=self.order_round) OrderProductFactory(order=order, product__supplier=self.supplier) # This all works... self.assertEqual(OrderProduct.objects.all()[0].order, order) self.assertEqual(OrderProduct.objects.all()[0].order.order_round, self.order_round) self.assertEqual(OrderProduct.objects.all()[0].product.supplier, self.supplier) # Something is going on with get_current_order_round, # like it's mocked somewhere... self.assertTrue(self.supplier.has_orders_in_current_order_round())
def setUp(self): self.round = OrderRoundFactory() self.order = OrderFactory(order_round=self.round)
def setUp(self): self.round = OrderRoundFactory() self.url = reverse('view_products') self.login() self.order = OrderFactory(paid=False, finalized=False, user=self.user, order_round=self.round)
class TestCreateTransaction(FinanceTestCase): def setUp(self): super(TestCreateTransaction, self).setUp() self.url = reverse("finance.createtransaction") self.order = OrderFactory(user=self.user, finalized=True, paid=False) s = self.client.session s["order_to_pay"] = self.order.id s.save() def test_that_transaction_is_created(self): self.client.post(self.url, {"bank": "EXAMPLE_BANK"}) self.mollie_client.return_value.payments.create.assert_called_once_with( { "description": "VOKO Utrecht %d" % self.order.id, "webhookUrl": settings.BASE_URL + reverse("finance.callback"), "amount": float(self.order.total_price_to_pay_with_balances_taken_into_account()), "redirectUrl": settings.BASE_URL + "/finance/pay/transaction/confirm/?order=%d" % self.order.id, "metadata": {"order_id": self.order.id}, "method": "ideal", "issuer": "EXAMPLE_BANK", } ) def test_that_payment_object_is_created(self): assert Payment.objects.count() == 0 self.client.post(self.url, {"bank": "EXAMPLE_BANK"}) payment = Payment.objects.get() self.assertEqual(payment.amount, self.order.total_price_to_pay_with_balances_taken_into_account()) self.assertEqual(payment.order, self.order) self.assertEqual(payment.mollie_id, "transaction_id") self.assertEqual(payment.balance, None) def test_that_user_is_redirected_to_bank_url(self): ret = self.client.post(self.url, {"bank": "EXAMPLE_BANK"}) self.assertEqual(ret.status_code, 302) self.assertEqual(ret.url, "http://bank.url") def test_error_when_user_doesnt_own_order(self): s = self.client.session s["order_to_pay"] = OrderFactory().id s.save() with self.assertRaises(AssertionError): self.client.post(self.url, {"bank": "EXAMPLE_BANK"}) def test_error_when_order_not_finalized(self): self.order.finalized = False self.order.save() with self.assertRaises(AssertionError): self.client.post(self.url, {"bank": "EXAMPLE_BANK"}) def test_redirect_on_invalid_form(self): ret = self.client.post(self.url, {"bank": "foo"}) self.assertRedirects(ret, reverse("finance.choosebank")) def test_redirect_when_order_round_is_closed(self): month_ago = datetime.now(tz=UTC) - timedelta(days=30) order_round = OrderRoundFactory(closed_for_orders=month_ago) assert order_round.is_open is False self.order.order_round = order_round self.order.save() ret = self.client.post(self.url, {"bank": "EXAMPLE_BANK"}) self.assertRedirects(ret, reverse("finish_order", args=(self.order.id,)), fetch_redirect_response=False)
class TestPaymentWebhook(FinanceTestCase): def setUp(self): super(TestPaymentWebhook, self).setUp() self.order = OrderFactory(user=self.user, finalized=True, paid=False) self.payment = PaymentFactory(order=self.order) s = self.client.session s["order_to_pay"] = self.order.id s.save() self.url = reverse("finance.callback") def test_required_post_parameters_bad(self): ret = self.client.post(self.url, {}) self.assertEqual(ret.status_code, 404) def test_required_post_parameters_good(self): ret = self.client.post(self.url, {"id": self.payment.mollie_id}) self.assertEqual(ret.status_code, 200) def test_unsuccessful_payment(self): self.mollie_client.return_value.payments.get.return_value.isPaid.return_value = False ret = self.client.post(self.url, {"id": self.payment.mollie_id}) payment = Payment.objects.get(id=self.payment.id) self.assertFalse(payment.succeeded) self.assertFalse(self.mock_create_credit.called) self.assertFalse(payment.order.paid) self.assertFalse(payment.order.debit) self.assertFalse(self.mock_mail_confirmation.called) # self.assertFalse(self.mock_complete_after_payment.called) self.assertEqual(ret.status_code, 200) self.assertEqual(ret.content, "") def test_successful_payment(self): self.mollie_client.return_value.payments.get.return_value.isPaid.return_value = True ret = self.client.post(self.url, {"id": self.payment.mollie_id}) payment = Payment.objects.get(id=self.payment.id) self.assertTrue(payment.succeeded) self.assertTrue(self.mock_create_credit.called) self.assertTrue(payment.order.paid) self.assertTrue(payment.order.debit) self.assertTrue(self.mock_mail_confirmation.called) # self.mock_complete_after_payment.assert_called_once_with() self.assertEqual(ret.status_code, 200) self.assertEqual(ret.content, "") def test_order_is_completed_when_order_paid_is_false(self): assert self.order.paid is False self.mollie_client.return_value.payments.get.return_value.isPaid.return_value = True ret = self.client.post(self.url, {"id": self.payment.mollie_id}) # self.mock_complete_after_payment.assert_called_once_with() def test_order_is_not_completed_when_round_is_closed_and_notification_is_sent(self): month_ago = datetime.now(tz=UTC) - timedelta(days=30) order_round = OrderRoundFactory(closed_for_orders=month_ago) assert order_round.is_open is False self.order.order_round = order_round self.order.save() assert self.order.paid is False self.mollie_client.return_value.payments.get.return_value.isPaid.return_value = True ret = self.client.post(self.url, {"id": self.payment.mollie_id}) payment = Payment.objects.get() self.assertFalse(payment.order.paid) self.mock_create_credit.assert_called_once_with() # self.assertFalse(self.mock_complete_after_payment.called) self.mock_failure_notification.assert_called_once_with()
class TestCreateTransaction(FinanceTestCase): def setUp(self): super(TestCreateTransaction, self).setUp() self.url = reverse('finance.createtransaction') self.order = OrderFactory(user=self.user, finalized=True, paid=False) def test_that_transaction_is_created(self): self.client.post(self.url, {'bank': 'EXAMPLE_BANK'}) self.mollie_client.return_value.payments.create.assert_called_once_with( # noqa {'description': 'VOKO Utrecht %d' % self.order.id, 'webhookUrl': settings.BASE_URL + reverse('finance.callback'), 'amount': float(self.order.total_price_to_pay_with_balances_taken_into_account()), # noqa 'redirectUrl': (settings.BASE_URL + '/finance/pay/transaction/confirm/?order=%d' % self.order.id), 'metadata': {'order_id': self.order.id}, 'method': 'ideal', 'issuer': 'EXAMPLE_BANK'} ) def test_that_payment_object_is_created(self): assert Payment.objects.count() == 0 self.client.post(self.url, {'bank': "EXAMPLE_BANK"}) payment = Payment.objects.get() self.assertEqual( payment.amount, self.order.total_price_to_pay_with_balances_taken_into_account() ) self.assertEqual(payment.order, self.order) self.assertEqual(payment.mollie_id, "transaction_id") self.assertEqual(payment.balance, None) def test_that_user_is_redirected_to_bank_url(self): ret = self.client.post(self.url, {'bank': "EXAMPLE_BANK"}) self.assertEqual(ret.status_code, 302) self.assertEqual(ret.url, "http://bank.url") def test_redirect_when_order_not_finalized(self): # No order matches, so not found self.order.finalized = False self.order.save() ret = self.client.post(self.url, {'bank': "EXAMPLE_BANK"}) self.assertRedirects(ret, reverse('view_products')) def test_redirect_on_invalid_form(self): ret = self.client.post(self.url, {'bank': 'foo'}) self.assertRedirects(ret, reverse('finance.choosebank')) def test_redirect_when_order_round_is_closed(self): # No order matches, so not found month_ago = datetime.now(tz=UTC) - timedelta(days=30) order_round = OrderRoundFactory(closed_for_orders=month_ago) assert order_round.is_open is False self.order.order_round = order_round self.order.save() ret = self.client.post(self.url, {'bank': "EXAMPLE_BANK"}) self.assertRedirects(ret, reverse('view_products'))
def test_ordermanager_get_current_order_1(self): order = OrderFactory(paid=False) self.assertEqual(order.user.orders.get_current_order(), order)
def test_has_orders_returns_false_on_non_paid_orders(self): order = OrderFactory(finalized=True, paid=False, order_round=self.order_round) OrderProductFactory(order=order, product__supplier=self.supplier) self.assertFalse(self.supplier.has_orders_in_current_order_round())
def test_has_orders_ignores_stock_products(self): order = OrderFactory(finalized=True, paid=True) OrderProductFactory(order=order, product__supplier=self.supplier, product__order_round=None) self.assertFalse(self.supplier.has_orders_in_current_order_round())
def test_ordermanager_get_last_paid_order_2(self): order = OrderFactory(paid=True) self.assertEqual(order.user.orders.get_last_paid_order(), order)
def test_ordermanager_get_last_paid_order_1(self): order = OrderFactory(paid=False) self.assertIsNone(order.user.orders.get_last_paid_order())
def test_total_price_with_no_products(self): order = OrderFactory() self.assertEqual( order.total_price, order.member_fee + order.order_round.transaction_costs)
class TestProductsView(VokoTestCase): def setUp(self): self.round = OrderRoundFactory() self.url = reverse('view_products') self.login() self.order = OrderFactory(paid=False, finalized=False, user=self.user, order_round=self.round) def test_login_required(self): self.logout() ret = self.client.get(self.url) self.assertEqual(ret.status_code, 302) def test_product_round_override(self): round1 = self.round round2 = OrderRoundFactory() products1 = ProductFactory.create_batch(50, order_round=round1) products2 = ProductFactory.create_batch(50, order_round=round2) ret = self.client.get(self.url) self.assertItemsEqual(ret.context['object_list'], products1) ret = self.client.get(self.url + "?round=%d" % round2.id) self.assertItemsEqual(ret.context['object_list'], products2) def test_context_contains_current_order_round(self): current = self.round other_round = OrderRoundFactory() ret = self.client.get(self.url) self.assertEqual(ret.context['current_order_round'], current) ret = self.client.get(self.url + "?round=%d" % other_round.id) self.assertEqual(ret.context['current_order_round'], other_round) def test_context_contains_products_ordered_by_name(self): ProductFactory.create_batch(50, order_round=self.round) ret = self.client.get(self.url) self.assertItemsEqual(ret.context['view'].products(), Product.objects.all().order_by('name')) def test_attribute_is_added_to_products_for_which_an_orderproduct_exists(self): order = OrderFactory(order_round=self.round, user=self.user) product = ProductFactory(order_round=self.round) odp1 = OrderProductFactory(order=order, product=product) ret = self.client.get(self.url) self.assertEqual(list(ret.context['view'].products())[0].ordered_amount, odp1.amount) def test_stock_products_without_stock_are_excluded(self): order = OrderFactory(order_round=self.round, user=self.user) product = ProductFactory(order_round=self.round) sproduct = ProductFactory(order_round=None) odp1 = OrderProductFactory(order=order, product=product) odp2 = OrderProductFactory(order=order, product=sproduct) ret = self.client.get(self.url) self.assertNotIn(sproduct, list(ret.context['view'].products())) self.assertIn(product, list(ret.context['view'].products())) def test_context_contains_categories_alphabetically_sorted(self): cat1 = ProductCategoryFactory(name="Zeep") cat2 = ProductCategoryFactory(name="Kaas") cat3 = ProductCategoryFactory(name="Appels") ret = self.client.get(self.url) self.assertEqual(ret.context['view'].categories()[0], cat3) self.assertEqual(ret.context['view'].categories()[1], cat2) self.assertEqual(ret.context['view'].categories()[2], cat1) def test_context_contains_supplier_objects(self): suppliers = SupplierFactory.create_batch(10) ret = self.client.get(self.url) self.assertItemsEqual(ret.context['view'].suppliers(), suppliers) def test_redirect_to_payment_page_when_current_order_is_finalized(self): self.patch("finance.views.Mollie") OrderFactory(order_round=self.round, user=self.user, finalized=True) ret = self.client.get(self.url, follow=True) self.assertRedirects(ret, reverse('finance.choosebank'), fetch_redirect_response=True) self.assertMsgInResponse(ret, "Je bent doorgestuurd naar de betaalpagina " "omdat je bestelling nog niet is betaald!") def test_submit_without_data(self): ret = self.client.post(self.url) self.assertFalse(OrderProduct.objects.exists()) self.assertRedirects(ret, reverse("finish_order", args=(self.order.id,))) def test_submit_without_data_2(self): OrderProductFactory.create_batch(10, product__order_round=self.round, order__user=self.user) ret = self.client.post(self.url) self.assertEqual(len(OrderProduct.objects.all()), 10) self.assertRedirects(ret, reverse("finish_order", args=(self.order.id,))) def test_order_one_product(self): self.assertFalse(OrderProduct.objects.exists()) product = ProductFactory(order_round=self.round) ret = self.client.post(self.url, { "order-product-%d" % product.id: 3 }) odp = OrderProduct.objects.get() self.assertEqual(odp.product, product) self.assertEqual(odp.amount, 3) self.assertEqual(odp.order.order_round, self.round) self.assertEqual(odp.total_retail_price, product.retail_price * 3) self.assertEqual(odp.base_price, product.base_price) self.assertEqual(odp.retail_price, product.retail_price) self.assertRedirects(ret, reverse("finish_order", args=(self.order.id,))) def test_delete_all_products_of_user(self): user_odps = OrderProductFactory.create_batch(10, product__order_round=self.round, order=self.order) other_odps = OrderProductFactory.create_batch(10, product__order_round=self.round, order=self.order) data = {} for odp in user_odps: data["order-product-%d" % odp.product.id] = 0 ret = self.client.post(self.url, data) self.assertEqual(len(OrderProduct.objects.all()), 10) self.assertItemsEqual(OrderProduct.objects.all(), other_odps) self.assertRedirects(ret, reverse("finish_order", args=(self.order.id,))) def test_delete_some_products(self): user_odps = OrderProductFactory.create_batch(10, product__order_round=self.round, order=self.order) other_odps = OrderProductFactory.create_batch(10, product__order_round=self.round, order=self.order) data = {} for odp in user_odps[:5]: data["order-product-%d" % odp.product.id] = "" ret = self.client.post(self.url, data) self.assertEqual(len(OrderProduct.objects.all()), 15) self.assertItemsEqual(OrderProduct.objects.all(), other_odps + user_odps[5:]) self.assertRedirects(ret, reverse("finish_order", args=(self.order.id,))) def test_change_amounts(self): user_odps = OrderProductFactory.create_batch(10, product__order_round=self.round, order=self.order, amount=5) data = {} for odp in user_odps: data["order-product-%d" % odp.product.id] = "3" ret = self.client.post(self.url, data) for odp in OrderProduct.objects.all(): self.assertEqual(odp.amount, 3) self.assertRedirects(ret, reverse("finish_order", args=(self.order.id,))) def test_other_keys_in_post_data_are_ignored(self): odps = OrderProductFactory.create_batch(10, product__order_round=self.round, order=self.order) ret = self.client.post(self.url, {"order_product_999": 12, "foo": "bar"}) self.assertItemsEqual(OrderProduct.objects.all(), odps) self.assertRedirects(ret, reverse("finish_order", args=(self.order.id,))) def test_tamper_with_ids_1(self): ret = self.client.post(self.url, {"order-product-0": 1}, follow=True) self.assertFalse(OrderProduct.objects.exists()) self.assertRedirects(ret, reverse("view_products")) self.assertMsgInResponse(ret, "Er ging iets fout bij het opslaan. " "Probeer het opnieuw of neem contact met ons op.") def test_tamper_with_ids_2(self): ret = self.client.post(self.url, {"order-product-x": 1}, follow=True) self.assertFalse(OrderProduct.objects.exists()) self.assertRedirects(ret, reverse("view_products")) self.assertMsgInResponse(ret, "Er ging iets fout bij het opslaan. " "Probeer het opnieuw of neem contact met ons op.") def test_order_sold_out_product(self): sold_out_odp = OrderProductFactory(product__order_round=self.round, order=self.order, product__maximum_total_order=1, amount=1) self.order.finalized = True self.order.paid = True self.order.save() self.assertFalse(sold_out_odp.product.is_available) ret = self.client.post(self.url, {"order-product-%d" % sold_out_odp.product.id : 1}, follow=True) self.assertMsgInResponse(ret, "Het product '%s' van %s is uitverkocht!" % (sold_out_odp.product.name, sold_out_odp.product.supplier.name)) def test_order_more_than_max(self): product = ProductFactory(order_round=self.round, maximum_total_order=1) self.assertTrue(product.is_available) ret = self.client.post(self.url, {"order-product-%d" % product.id : 2}, follow=True) self.assertMsgInResponse(ret, "Van het product '%s' van %s is nog %s %s beschikbaar!" % (product.name, product.supplier.name, product.amount_available, product.unit_of_measurement.lower()))
class TestConfirmTransaction(FinanceTestCase): def setUp(self): super(TestConfirmTransaction, self).setUp() self.url = reverse("finance.confirmtransaction") self.order = OrderFactory(user=self.user, finalized=True, paid=False) self.payment = PaymentFactory(order=self.order) self.client.login() def test_required_get_parameters_bad(self): ret = self.client.get(self.url, {}) self.assertEqual(ret.status_code, 404) def test_required_get_parameters_good(self): ret = self.client.get(self.url, {"order": self.order.id}) self.assertEqual(ret.status_code, 200) def test_mollie_payment_is_obtained(self): self.client.get(self.url, {"order": self.order.id}) self.mollie_client.return_value.payments.get.assert_called_once_with(self.payment.mollie_id) def test_ispaid_is_called_on_mollie_payment(self): self.client.get(self.url, {"order": self.order.id}) self.mollie_client.return_value.payments.get.return_value.isPaid.assert_called_once_with() def test_context_when_payment_has_failed(self): self.mollie_client.return_value.payments.get.return_value.isPaid.return_value = False ret = self.client.get(self.url, {"order": self.order.id}) self.assertEqual(ret.context[0]["payment_succeeded"], False) def test_context_when_payment_has_succeeded(self): self.mollie_client.return_value.payments.get.return_value.isPaid.return_value = True ret = self.client.get(self.url, {"order": self.order.id}) self.assertEqual(ret.context[0]["payment_succeeded"], True) def test_payment_object_is_updated_on_success(self): self.assertFalse(self.payment.succeeded) self.mollie_client.return_value.payments.get.return_value.isPaid.return_value = True self.client.get(self.url, {"order": self.order.id}) # get new object reference payment = Payment.objects.get(id=self.payment.id) self.assertTrue(payment.succeeded) def test_payment_object_is_not_updated_on_failure(self): self.assertFalse(self.payment.succeeded) self.mollie_client.return_value.payments.get.return_value.isPaid.return_value = False self.client.get(self.url, {"order": self.order.id}) # get new object reference payment = Payment.objects.get(id=self.payment.id) self.assertFalse(payment.succeeded) def test_payment_and_order_status_after_successful_payment(self): self.mollie_client.return_value.payments.get.return_value.isPaid.return_value = True self.client.get(self.url, {"order": self.order.id}) payment = Payment.objects.get() self.assertTrue(payment.succeeded) self.assertTrue(payment.order.paid) self.mock_create_credit.assert_called_once_with() debit = Balance.objects.get() self.assertEqual(debit.user, payment.order.user) self.assertEqual(debit.type, "DR") self.assertEqual(debit.amount, payment.order.total_price) self.assertEqual( debit.notes, "Debit van %s voor bestelling #%s" % (payment.order.total_price, payment.order.id) ) self.mock_mail_confirmation.assert_called_once_with() def test_that_order_id_is_removed_from_session_on_successful_payment(self): self.mollie_client.return_value.payments.get.return_value.isPaid.return_value = True self.client.get(self.url, {"order": self.order.id}) s = self.client.session self.assertNotIn("order_to_pay", s) def test_nothing_is_changed_when_payment_already_confirmed(self): self.order.paid = True self.order.save() self.client.get(self.url, {"order": self.order.id}) ret = self.client.get(self.url, {"order": self.order.id}) self.assertEqual(ret.context[0]["payment_succeeded"], True) self.assertFalse(self.mollie_client.return_value.payments.get.return_value.isPaid.called) self.assertFalse(self.mock_create_credit.called) self.assertFalse(self.mock_mail_confirmation.called)
def test_has_products_with_one_orderproduct(self): order = OrderFactory() OrderProductFactory(order=order) self.assertTrue(order.has_products)
def test_member_fee_on_first_order(self): order = OrderFactory() self.assertEqual(order.member_fee, settings.MEMBER_FEE)