Beispiel #1
0
    def test_not_accept(self, decision, should_send_email):
        """
        If the decision is not ACCEPT then the order should be marked as failed
        """
        course_run, user = create_purchasable_course_run()
        order = create_unfulfilled_order(course_run.edx_course_key, user)

        data = {
            'req_reference_number': make_reference_id(order),
            'decision': decision,
        }
        with patch(
            'ecommerce.views.IsSignedByCyberSource.has_permission',
            return_value=True
        ), patch(
            'ecommerce.views.MailgunClient.send_individual_email',
        ) as send_email:
            resp = self.client.post(reverse('order-fulfillment'), data=data)
        assert resp.status_code == status.HTTP_200_OK
        assert len(resp.content) == 0
        order.refresh_from_db()
        assert Order.objects.count() == 1
        assert order.status == Order.FAILED

        if should_send_email:
            assert send_email.call_count == 1
            assert send_email.call_args[0] == (
                'Order fulfillment failed, decision={decision}'.format(decision='something else'),
                'Order fulfillment failed for order {order}'.format(order=order),
                '*****@*****.**',
            )
        else:
            assert send_email.call_count == 0
    def test_not_accept(self, decision, should_send_email):
        """
        If the decision is not ACCEPT then the order should be marked as failed
        """
        course_run, user = create_purchasable_course_run()
        order = create_unfulfilled_order(course_run.edx_course_key, user)

        data = {
            'req_reference_number': make_reference_id(order),
            'decision': decision,
        }
        with patch('ecommerce.views.IsSignedByCyberSource.has_permission',
                   return_value=True), patch(
                       'ecommerce.views.MailgunClient.send_individual_email',
                   ) as send_email:
            resp = self.client.post(reverse('order-fulfillment'), data=data)
        assert resp.status_code == status.HTTP_200_OK
        assert len(resp.content) == 0
        order.refresh_from_db()
        assert Order.objects.count() == 1
        assert order.status == Order.FAILED

        if should_send_email:
            assert send_email.call_count == 1
            assert send_email.call_args[0] == (
                'Order fulfillment failed, decision={decision}'.format(
                    decision='something else'),
                'Order fulfillment failed for order {order}'.format(
                    order=order),
                '*****@*****.**',
            )
        else:
            assert send_email.call_count == 0
Beispiel #3
0
    def test_failed_enroll(self):
        """
        If we fail to enroll in edX, the order status should be fulfilled but an error email should be sent
        """
        course_run, user = create_purchasable_course_run()
        order = create_unfulfilled_order(course_run.edx_course_key, user)

        data = {}
        for _ in range(5):
            data[FAKE.text()] = FAKE.text()

        data['req_reference_number'] = make_reference_id(order)
        data['decision'] = 'ACCEPT'

        with patch('ecommerce.views.IsSignedByCyberSource.has_permission', return_value=True), patch(
            'ecommerce.views.enroll_user_on_success', side_effect=KeyError
        ), patch(
            'ecommerce.views.MailgunClient.send_individual_email',
        ) as send_email:
            self.client.post(reverse('order-fulfillment'), data=data)

        assert Order.objects.count() == 1
        # An enrollment failure should not prevent the order from being fulfilled
        order = Order.objects.first()
        assert order.status == Order.FULFILLED

        assert send_email.call_count == 1
        assert send_email.call_args[0][0] == 'Error occurred when enrolling user during order fulfillment'
        assert send_email.call_args[0][1].startswith(
            'Error occurred when enrolling user during order fulfillment for {order}. '
            'Exception: '.format(
                order=order,
            )
        )
        assert send_email.call_args[0][2] == '*****@*****.**'
    def test_failed_enroll(self):
        """
        If we fail to enroll in edX, the order status should be fulfilled but an error email should be sent
        """
        course_run, user = create_purchasable_course_run()
        order = create_unfulfilled_order(course_run.edx_course_key, user)

        data = {}
        for _ in range(5):
            data[FAKE.text()] = FAKE.text()

        data['req_reference_number'] = make_reference_id(order)
        data['decision'] = 'ACCEPT'

        with patch(
                'ecommerce.views.IsSignedByCyberSource.has_permission',
                return_value=True), patch(
                    'ecommerce.views.enroll_user_on_success',
                    side_effect=KeyError), patch(
                        'ecommerce.views.MailgunClient.send_individual_email',
                    ) as send_email:
            self.client.post(reverse('order-fulfillment'), data=data)

        assert Order.objects.count() == 1
        # An enrollment failure should not prevent the order from being fulfilled
        order = Order.objects.first()
        assert order.status == Order.FULFILLED

        assert send_email.call_count == 1
        assert send_email.call_args[0][
            0] == 'Error occurred when enrolling user during order fulfillment'
        assert send_email.call_args[0][1].startswith(
            'Error occurred when enrolling user during order fulfillment for {order}. '
            'Exception: '.format(order=order, ))
        assert send_email.call_args[0][2] == '*****@*****.**'
    def test_ignore_duplicate_cancel(self):
        """
        If the decision is CANCEL and we already have a duplicate failed order, don't change anything.
        """
        course_run, user = create_purchasable_course_run()
        order = create_unfulfilled_order(course_run.edx_course_key, user)
        order.status = Order.FAILED
        order.save()

        data = {
            'req_reference_number': make_reference_id(order),
            'decision': 'CANCEL',
        }
        with patch('ecommerce.views.IsSignedByCyberSource.has_permission',
                   return_value=True):
            resp = self.client.post(reverse('order-fulfillment'), data=data)
        assert resp.status_code == status.HTTP_200_OK

        assert Order.objects.count() == 1
        assert Order.objects.get(id=order.id).status == Order.FAILED
Beispiel #6
0
    def test_ignore_duplicate_cancel(self):
        """
        If the decision is CANCEL and we already have a duplicate failed order, don't change anything.
        """
        course_run, user = create_purchasable_course_run()
        order = create_unfulfilled_order(course_run.edx_course_key, user)
        order.status = Order.FAILED
        order.save()

        data = {
            'req_reference_number': make_reference_id(order),
            'decision': 'CANCEL',
        }
        with patch(
            'ecommerce.views.IsSignedByCyberSource.has_permission',
            return_value=True
        ):
            resp = self.client.post(reverse('order-fulfillment'), data=data)
        assert resp.status_code == status.HTTP_200_OK

        assert Order.objects.count() == 1
        assert Order.objects.get(id=order.id).status == Order.FAILED
    def test_error_on_duplicate_order(self, order_status, decision):
        """If there is a duplicate message (except for CANCEL), raise an exception"""
        course_run, user = create_purchasable_course_run()
        order = create_unfulfilled_order(course_run.edx_course_key, user)
        order.status = order_status
        order.save()

        data = {
            'req_reference_number': make_reference_id(order),
            'decision': decision,
        }
        with patch('ecommerce.views.IsSignedByCyberSource.has_permission',
                   return_value=True), self.assertRaises(
                       EcommerceException) as ex:
            self.client.post(reverse('order-fulfillment'), data=data)

        assert Order.objects.count() == 1
        assert Order.objects.get(id=order.id).status == order_status

        assert ex.exception.args[
            0] == "Order {id} is expected to have status 'created'".format(
                id=order.id, )
    def test_order_fulfilled(self):
        """
        Test the happy case
        """
        course_run, user = create_purchasable_course_run()
        order = create_unfulfilled_order(course_run.edx_course_key, user)
        data_before = order.to_dict()

        data = {}
        for _ in range(5):
            data[FAKE.text()] = FAKE.text()

        data['req_reference_number'] = make_reference_id(order)
        data['decision'] = 'ACCEPT'

        with patch('ecommerce.views.IsSignedByCyberSource.has_permission',
                   return_value=True), patch(
                       'ecommerce.views.enroll_user_on_success',
                       autospec=True,
                   ) as enroll_user, patch(
                       'ecommerce.views.MailgunClient.send_individual_email',
                   ) as send_email:
            resp = self.client.post(reverse('order-fulfillment'), data=data)

        assert len(resp.content) == 0
        assert resp.status_code == status.HTTP_200_OK
        order.refresh_from_db()
        assert order.status == Order.FULFILLED
        assert order.receipt_set.count() == 1
        assert order.receipt_set.first().data == data
        enroll_user.assert_called_with(order)

        assert send_email.call_count == 0

        assert OrderAudit.objects.count() == 2
        order_audit = OrderAudit.objects.last()
        assert order_audit.order == order
        assert order_audit.data_before == data_before
        assert order_audit.data_after == order.to_dict()
Beispiel #9
0
    def test_error_on_duplicate_order(self, order_status, decision):
        """If there is a duplicate message (except for CANCEL), raise an exception"""
        course_run, user = create_purchasable_course_run()
        order = create_unfulfilled_order(course_run.edx_course_key, user)
        order.status = order_status
        order.save()

        data = {
            'req_reference_number': make_reference_id(order),
            'decision': decision,
        }
        with patch(
            'ecommerce.views.IsSignedByCyberSource.has_permission',
            return_value=True
        ), self.assertRaises(EcommerceException) as ex:
            self.client.post(reverse('order-fulfillment'), data=data)

        assert Order.objects.count() == 1
        assert Order.objects.get(id=order.id).status == order_status

        assert ex.exception.args[0] == "Order {id} is expected to have status 'created'".format(
            id=order.id,
        )
Beispiel #10
0
    def test_order_fulfilled(self):
        """
        Test the happy case
        """
        course_run, user = create_purchasable_course_run()
        order = create_unfulfilled_order(course_run.edx_course_key, user)
        data_before = order.to_dict()

        data = {}
        for _ in range(5):
            data[FAKE.text()] = FAKE.text()

        data['req_reference_number'] = make_reference_id(order)
        data['decision'] = 'ACCEPT'

        with patch('ecommerce.views.IsSignedByCyberSource.has_permission', return_value=True), patch(
            'ecommerce.views.enroll_user_on_success', autospec=True,
        ) as enroll_user, patch(
            'ecommerce.views.MailgunClient.send_individual_email',
        ) as send_email:
            resp = self.client.post(reverse('order-fulfillment'), data=data)

        assert len(resp.content) == 0
        assert resp.status_code == status.HTTP_200_OK
        order.refresh_from_db()
        assert order.status == Order.FULFILLED
        assert order.receipt_set.count() == 1
        assert order.receipt_set.first().data == data
        enroll_user.assert_called_with(order)

        assert send_email.call_count == 0

        assert OrderAudit.objects.count() == 2
        order_audit = OrderAudit.objects.last()
        assert order_audit.order == order
        assert order_audit.data_before == data_before
        assert order_audit.data_after == order.to_dict()