Esempio n. 1
0
    def test_notify_on_order_cancelled(self):
        """Test that a notification is triggered when an order is cancelled."""
        order = OrderFactory(assignees=[])
        OrderAssigneeFactory.create_batch(1, order=order, is_lead=True)
        OrderSubscriberFactory.create_batch(2, order=order)

        notify.client.reset_mock()

        order.cancel(by=AdviserFactory(),
                     reason=CancellationReason.objects.first())

        #  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_cancelled_for_customer.value,
            Template.order_cancelled_for_adviser.value,
            Template.order_cancelled_for_adviser.value,
            Template.order_cancelled_for_adviser.value,
        ]
    def test_fails_if_order_not_in_allowed_status(self, disallowed_status,
                                                  force):
        """
        Test that if the order is in a disallowed status, the order cannot be cancelled.
        """
        reason = CancellationReason.objects.order_by('?').first()
        order = OrderFactory(status=disallowed_status)

        with pytest.raises(APIConflictException):
            order.cancel(by=None, reason=reason, force=force)

        assert order.status == disallowed_status
    def test_ok_if_order_in_allowed_status(self, allowed_status, force):
        """
        Test that the order can be cancelled if it's in one of the allowed statuses.
        """
        reason = CancellationReason.objects.order_by('?').first()
        order = OrderFactory(status=allowed_status)
        adviser = AdviserFactory()

        with freeze_time('2018-07-12 13:00'):
            order.cancel(by=adviser, reason=reason, force=force)

        order.refresh_from_db()
        assert order.status == OrderStatus.cancelled
        assert order.cancelled_on == dateutil_parse('2018-07-12T13:00Z')
        assert order.cancellation_reason == reason
        assert order.cancelled_by == adviser
    def test_atomicity(self):
        """
        Test that if there's a problem with saving the order, nothing gets saved.
        """
        reason = CancellationReason.objects.order_by('?').first()
        order = OrderFactory(status=OrderStatus.draft)

        with mock.patch.object(order, 'save') as mocked_save:
            mocked_save.side_effect = Exception()

            with pytest.raises(Exception):
                order.cancel(by=None, reason=reason)

            order.refresh_from_db()
            assert order.status == OrderStatus.draft
            assert not order.cancellation_reason
            assert not order.cancelled_on
            assert not order.cancelled_by