xqueue.py 1.37 KB
Newer Older
1 2 3 4 5 6 7 8 9
"""
Fixture to configure XQueue response.
"""

import requests
import json
from . import XQUEUE_STUB_URL


10 11 12 13 14 15 16
class XQueueResponseFixtureError(Exception):
    """
    Error occurred while configuring the stub XQueue.
    """
    pass


17
class XQueueResponseFixture(object):
18 19 20 21
    """
    Configure the XQueue stub's response to submissions.
    """

22
    def __init__(self, pattern, response_dict):
23 24
        """
        Configure XQueue stub to POST `response_dict` (a dictionary)
25 26
        back to the LMS when it receives a submission that contains the string
        `pattern`.
27 28 29 30 31

        Remember that there is one XQueue stub shared by all the tests;
        if possible, you should have tests use unique queue names
        to avoid conflict between tests running in parallel.
        """
32
        self._pattern = pattern
33 34 35 36 37 38 39 40 41
        self._response_dict = response_dict

    def install(self):
        """
        Configure the stub via HTTP.
        """
        url = XQUEUE_STUB_URL + "/set_config"

        # Configure the stub to respond to submissions to our queue
42
        payload = {self._pattern: json.dumps(self._response_dict)}
43 44 45
        response = requests.put(url, data=payload)

        if not response.ok:
46
            raise XQueueResponseFixtureError(
47
                "Could not configure XQueue stub for queue '{1}'.  Status code: {2}".format(
48
                    self._pattern, self._response_dict))