Пример #1
0
    def test_valid_single_line_item(self):
        order = Order()
        order.id = 40
        order.save()
        line_item = {
            "properties": [{
                "name": "email",
                "value": "*****@*****.**"
            }],
            "sku": "course-v1:org+course+run1"
        }

        enrollment_response = {
            'action': 'enroll',
            'courses': {
                'course-v1:org+course+run1': {
                    'action':
                    'enroll',
                    'results': [{
                        'identifier': '*****@*****.**',
                        'after': {
                            'enrollment': False,
                            'allowed': True,
                            'user': False,
                            'auto_enroll': True
                        },
                        'before': {
                            'enrollment': False,
                            'allowed': False,
                            'user': False,
                            'auto_enroll': False
                        }
                    }],
                    'auto_enroll':
                    True
                }
            },
            'email_students': True,
            'auto_enroll': True
        }

        with requests_mock.Mocker() as m:
            m.register_uri('POST', self.token_uri, json=self.token_response)
            m.register_uri('POST', self.enroll_uri, json=enrollment_response)

            order_item = process_line_item(order, line_item)

            # Read back the order item (can't just use
            # refresh_from_db(), because of the FSM-protected status
            # field)
            order_item = OrderItem.objects.get(pk=order_item.id)

            self.assertEqual(order_item.order, order)
            self.assertEqual(order_item.sku, 'course-v1:org+course+run1')
            self.assertEqual(order_item.email, '*****@*****.**')
            self.assertEqual(order_item.status, OrderItem.PROCESSED)
Пример #2
0
    def setUp(self):
        self.order_item = self.model()
        self.order = Order()
        self.order.id = 2
        self.order.email = "*****@*****.**"
        self.order.first_name = "Jane"
        self.order.last_name = "Doe"
        self.order.save()

        self.order_item = self.model()
        self.order_item.order = self.order
        self.order_item.sku = "42MYPRODUCT"
        self.order_item.email = "*****@*****.**"
Пример #3
0
 def test_invalid_email(self):
     order = Order()
     order.id = 43
     order.save()
     line_items = [{
         "properties": [{
             "name": "email",
             "value": "akjzcdfbgakugbfvkljzgh"
         }],
         "sku":
         "course-v1:org+course+run1"
     }]
     for line_item in line_items:
         with self.assertRaises(ValidationError):
             process_line_item(order, line_item)
Пример #4
0
 def test_invalid_sku(self):
     order = Order()
     order.id = 42
     order.save()
     line_items = [{
         "properties": [{
             "name": "email",
             "value": "*****@*****.**"
         }],
         "sku":
         "course-v1:org+nosuchcourse+run1"
     }]
     for line_item in line_items:
         with self.assertRaises(Http404):
             process_line_item(order, line_item)
Пример #5
0
    def test_valid_single_line_item(self):
        order = Order()
        order.id = 40
        order.save()
        line_items = [{
            "properties": [{
                "name": "email",
                "value": "*****@*****.**"
            }],
            "sku":
            "course-v1:org+course+run1"
        }]
        mock_get_course_by_id = Mock(return_value=self.course)
        mock_get_email_params = Mock(return_value=self.email_params)
        mock_enroll_email = Mock()
        for line_item in line_items:
            with patch.multiple(utils,
                                get_course_by_id=mock_get_course_by_id,
                                get_email_params=mock_get_email_params,
                                enroll_email=mock_enroll_email):
                order_item = process_line_item(order, line_item)

                # Did we mock-fetch the course with the correct locator?
                mock_get_course_by_id.assert_called_once_with(self.ck)

                # Did we mock-fetch the email params for the course
                # identified by that locator?
                mock_get_email_params.assert_called_once_with(self.course,
                                                              True,
                                                              secure=True)

                # Did we mock-invoke enroll_email with the correct parameters?
                mock_enroll_email.assert_called_once_with(
                    self.ck,
                    '*****@*****.**',  # noqa: E501
                    auto_enroll=True,
                    email_students=True,
                    email_params=self.email_params,  # noqa: E501
                    language=None)

                # Read back the order item
                order_item.refresh_from_db()
                self.assertEqual(order_item.order, order)
                self.assertEqual(order_item.sku, 'course-v1:org+course+run1')
                self.assertEqual(order_item.email, '*****@*****.**')
                self.assertEqual(order_item.status, OrderItem.PROCESSED)
Пример #6
0
    def test_invalid_sku(self):
        order = Order()
        order.id = 42
        order.save()
        line_items = [{
            "properties": [{
                "name": "email",
                "value": "*****@*****.**"
            }],
            "sku":
            "course-v1:org+nosuchcourse+run1"
        }]
        for line_item in line_items:
            with requests_mock.Mocker() as m:
                # In the bulk enrollment API, an invalid course ID
                # results in an HTTP 400
                m.register_uri('POST',
                               self.token_uri,
                               json=self.token_response)
                m.register_uri('POST', self.enroll_uri, status_code=400)

                with self.assertRaises(HTTPError):
                    process_line_item(order, line_item)
Пример #7
0
class TestOrderItem(TestCase):

    model = OrderItem

    def setUp(self):
        self.order_item = self.model()
        self.order = Order()
        self.order.id = 2
        self.order.email = "*****@*****.**"
        self.order.first_name = "Jane"
        self.order.last_name = "Doe"
        self.order.save()

        self.order_item = self.model()
        self.order_item.order = self.order
        self.order_item.sku = "42MYPRODUCT"
        self.order_item.email = "*****@*****.**"
        # self.order_item.status must be populated by default

    def test_save(self):
        # Can we save a properly defined order item?
        self.order_item.save()

    def test_default_status(self):
        # Does the order get a default status on creation?
        self.assertEqual(self.order_item.status,
                         self.model.NEW)

    def test_empty_order_reference(self):
        # Does removing the parent order from an order item raise an
        # error?
        # Django 1.8 raises a ValueError on setting the value in
        # memory. Django 1.10, however, flags an IntegrityError on
        # attempting to save the object (i.e. write it to the
        # database).
        with self.assertRaises((ValueError, IntegrityError)):
            self.order_item.order = None
            self.order_item.save()

    def test_same_order_same_email_different_sku(self):
        # Can we create an order item with the same order reference
        # and email, but a different SKU?
        second_order_item = self.model()
        second_order_item.order = self.order_item.order
        second_order_item.sku = "54MYOTHERPRODUCT"
        second_order_item.email = self.order_item.email
        second_order_item.save()

    def test_same_order_different_email_same_sku(self):
        # Can we create an order item with the same order reference
        # and SKU, but a different email?
        second_order_item = self.model()
        second_order_item.order = self.order
        second_order_item.sku = self.order_item.sku
        second_order_item.email = "*****@*****.**"
        second_order_item.save()

    def test_same_order_same_email_same_sku(self):
        # Do we fail to create an order item with the same order
        # reference, SKU, and email?
        first_order_item = self.model()
        first_order_item.order = self.order_item.order
        first_order_item.sku = self.order_item.sku
        first_order_item.email = self.order_item.email
        first_order_item.save()

        second_order_item = self.model()
        second_order_item.order = first_order_item.order
        second_order_item.sku = first_order_item.sku
        second_order_item.email = first_order_item.email
        with self.assertRaises(DatabaseError):
            second_order_item.save()

    def test_start_processing(self):
        # Can we start processing the order item, and does this result
        # in the correct state?
        self.order_item.start_processing()
        self.assertEqual(self.order_item.status, self.model.PROCESSING)

    def test_finish_processing(self):
        # Can we finish processing the order item, and does this
        # result in the correct state?
        self.order_item.start_processing()
        self.order_item.finish_processing()
        self.assertEqual(self.order_item.status, self.model.PROCESSED)

    def test_fail(self):
        # Can we fail an order, and does this result in the correct
        # state?
        self.order_item.start_processing()
        self.order_item.fail()
        self.assertEqual(self.order_item.status, self.model.ERROR)

    def test_incorrect_transition(self):
        # Do we fail on a state transition that the FSM disallows?
        with self.assertRaises(TransitionNotAllowed):
            self.order_item.finish_processing()
Пример #8
0
class TestOrderItem(TestCase):

    model = OrderItem

    def setUp(self):
        self.order_item = self.model()
        self.order = Order()
        self.order.id = 2
        self.order.email = "*****@*****.**"
        self.order.first_name = "Jane"
        self.order.last_name = "Doe"
        self.order.save()

        self.order_item = self.model()
        self.order_item.order = self.order
        self.order_item.sku = "42MYPRODUCT"
        self.order_item.email = "*****@*****.**"
        # self.order_item.status must be populated by default

    def test_save(self):
        # Can we save a properly defined order item?
        self.order_item.save()

    def test_default_status(self):
        # Does the order get a default status on creation?
        self.assertEqual(self.order_item.status, self.model.UNPROCESSED)

    def test_empty_order_reference(self):
        # Does removing the parent order from an order item raise an
        # error?
        # Django 1.8 raises a ValueError on setting the value in
        # memory. Django 1.10, however, flags an IntegrityError on
        # attempting to save the object (i.e. write it to the
        # database).
        with self.assertRaises((ValueError, IntegrityError)):
            self.order_item.order = None
            self.order_item.save()

    def test_same_order_same_email_different_sku(self):
        # Can we create an order item with the same order reference
        # and email, but a different SKU?
        second_order_item = self.model()
        second_order_item.order = self.order_item.order
        second_order_item.sku = "54MYOTHERPRODUCT"
        second_order_item.email = self.order_item.email
        second_order_item.save()

    def test_same_order_different_email_same_sku(self):
        # Can we create an order item with the same order reference
        # and SKU, but a different email?
        second_order_item = self.model()
        second_order_item.order = self.order
        second_order_item.sku = self.order_item.sku
        second_order_item.email = "*****@*****.**"
        second_order_item.save()

    def test_same_order_same_email_same_sku(self):
        # Do we fail to create an order item with the same order
        # reference, SKU, and email?
        #
        # Note: the unique_together constraint is enforced at the
        # database level. The sqlite3 backend does not support it.
        second_order_item = self.model()
        second_order_item.order = self.order_item.order
        second_order_item.sku = self.order_item.sku
        second_order_item.email = self.order_item.email
        if connection.vendor in ['sqlite']:
            warnings.warn("Unable to test for unique_together "
                          "constraint enforcement with %s" % connection.vendor)
        else:
            with self.assertRaises(DatabaseError):
                second_order_item.save()