def test_ok_if_order_in_allowed_status(self, allowed_status): """ Test that the order can be marked as paid if the order is in one of the allowed statuses. """ order = OrderWithAcceptedQuoteFactory(status=allowed_status) adviser = AdviserFactory() order.mark_as_paid( by=adviser, payments_data=[ { 'amount': 1, 'received_on': dateutil_parse('2017-01-01').date(), }, { 'amount': order.total_cost - 1, 'received_on': dateutil_parse('2017-01-02').date(), }, ], ) order.refresh_from_db() assert order.status == OrderStatus.paid assert order.paid_on == dateutil_parse('2017-01-02T00:00:00Z') assert list( order.payments.order_by('received_on').values_list( 'amount', 'received_on'), ) == [ (1, dateutil_parse('2017-01-01').date()), (order.total_cost - 1, dateutil_parse('2017-01-02').date()), ]
def test_notify_on_order_paid(self): """Test that a notification is triggered when an order is marked as paid.""" order = OrderWithAcceptedQuoteFactory(assignees=[]) OrderAssigneeFactory.create_batch(1, order=order) OrderSubscriberFactory.create_batch(2, order=order) notify.client.reset_mock() order.mark_as_paid( by=AdviserFactory(), payments_data=[ { 'amount': order.total_cost, 'received_on': dateutil_parse('2017-01-02').date(), }, ], ) # 1 = customer, 3 = assignees/subscribers assert len( notify.client.send_email_notification.call_args_list) == (3 + 1) templates_called = [ data[1]['template_id'] for data in notify.client.send_email_notification.call_args_list ] assert templates_called == [ Template.order_paid_for_customer.value, Template.order_paid_for_adviser.value, Template.order_paid_for_adviser.value, Template.order_paid_for_adviser.value, ]
def test_validation_error_if_amounts_less_then_total_cost(self): """ Test that if the sum of the amounts is < order.total_cose, the call fails. """ order = OrderWithAcceptedQuoteFactory() with pytest.raises(ValidationError): order.mark_as_paid( by=None, payments_data=[ { 'amount': order.total_cost - 1, 'received_on': dateutil_parse('2017-01-02').date(), }, ], )
def test_atomicity(self): """ Test that if there's a problem with saving the order, the payments are not saved either. """ order = OrderWithAcceptedQuoteFactory() with mock.patch.object(order, 'save') as mocked_save: mocked_save.side_effect = Exception() with pytest.raises(Exception): order.mark_as_paid( by=None, payments_data=[{ 'amount': order.total_cost, 'received_on': dateutil_parse('2017-01-02').date(), }], ) order.refresh_from_db() assert order.status == OrderStatus.quote_accepted assert not order.paid_on assert not Payment.objects.count()