コード例 #1
0
class WebhookTestCase(TestCase):
    """Abstract base class for webhook tests"""
    PAYLOAD_FILENAME = None
    COURSE_ID_STRING = 'course-v1:org+course+run1'

    def setUp(self):
        self.setup_payload()
        self.setup_requests()

    def setup_payload(self):
        # Grab an example payload and make it available to test
        # methods as a raw string and as a JSON dictionary.
        payload_file = os.path.join(os.path.dirname(__file__),
                                    self.PAYLOAD_FILENAME)
        self.raw_payload = open(payload_file, 'rb').read()
        self.json_payload = json.load(open(payload_file, 'r'))

    def setup_webhook_data(self):
        self.webhook_data = JSONWebhookData(headers={},
                                            body=b'',
                                            content=self.json_payload)
        self.webhook_data.save()

    def setup_course(self):
        # TODO: Set up a mock course
        course = Mock()
        course.id = self.COURSE_ID_STRING

    def setup_requests(self):
        self.token_uri = '%s/oauth2/access_token' % settings.WEBHOOK_RECEIVER_LMS_BASE_URL  # noqa: E501
        self.enroll_uri = '%s/api/bulk_enroll/v1/bulk_enroll/' % settings.WEBHOOK_RECEIVER_LMS_BASE_URL  # noqa: E501

        self.token_response = {'access_token': 'foobar', 'expires_in': 3600}
コード例 #2
0
    def test_invalid_sku(self):
        fixup_payload = self.raw_payload.decode('utf-8').replace(
            "course-v1:org+course+run1",  # noqa: E501
            "course-v1:org+nosuchcourse+run1")  # noqa: E501
        fixup_json_payload = json.loads(fixup_payload)
        fixup_webhook_data = JSONWebhookData(headers={},
                                             body=b'',
                                             content=fixup_json_payload)
        fixup_webhook_data.save()
        order, created = record_order(fixup_webhook_data)

        result = None
        with requests_mock.Mocker() as m:
            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):
                result = process.delay(fixup_json_payload)
                result.get(5)

        self.assertEqual(result.state, 'FAILURE')

        # Even with the exception raised, it's the task failure
        # handler's job to set the status to ERROR. Given the async
        # nature of the task, though, the object reference doesn't
        # learn of the update until we read back the order. This can't
        # just use refresh_from_db(), because of the FSM-protected
        # status field.
        order = Order.objects.get(pk=order.id)
        self.assertEqual(order.status, Order.ERROR)
コード例 #3
0
    def test_invalid_sku(self):
        fixup_payload = self.raw_payload.decode('utf-8').replace(
            "course-v1:org+course+run1",  # noqa: E501
            "course-v1:org+nosuchcourse+run1")  # noqa: E501
        fixup_json_payload = json.loads(fixup_payload)
        fixup_webhook_data = JSONWebhookData(headers={},
                                             body=b'',
                                             content=fixup_json_payload)
        fixup_webhook_data.save()

        order, created = record_order(fixup_webhook_data)

        with requests_mock.Mocker() as m:
            m.register_uri('POST', self.token_uri, json=self.token_response)
            m.register_uri('POST', self.enroll_uri, status_code=404)
            # Non-existent course should raise a 404
            with self.assertRaises(HTTPError):
                process_order(order, fixup_json_payload)

        # At this stage, the order is still PROCESSING -- it's the
        # task failure handler's job to set the status to ERROR
        self.assertEqual(order.status, Order.PROCESSING)