Example #1
0
def test_resolve_with_data():
    """
    Verifies calling resolve with some data serializes it
    correctly if necessary before sending it to the backend
    so that it returns a positive response
    """
    resolver = SimpleResolver(_API_MAP["1.1"]["api_sandbox_root"])

    simple_api_map = {"url": "/stock", "method": "post"}

    # Data posted to MKM, this will be serialized to XML
    # before being sent, otherwise the backend won't accept it
    data = {
        "article": [{
            "idProduct": 100569,
            "idLanguage": 1,
            "comments": "Inserted through the API",
            "count": 1,
            "price": 4,
            "condition": "EX",
            "isFoil": True,
            "isSigned": False,
            "isPlayset": False,
        }]
    }

    response = resolver.resolve(api_map=simple_api_map, data=data)

    assert response.status_code == 200
Example #2
0
def test_resolve_with_xml_data():
    """Verifies posting xml data to backend returns a positive response."""
    resolver = SimpleResolver(_API_MAP["1.1"]["api_sandbox_root"])

    simple_api_map = {"url": "/stock", "method": "post"}

    # Data posted to MKM, we post an XML because the API
    # doesn't accept anything else
    data = """<?xml version="1.0" encoding="UTF-8" ?>
                <request>
                    <article>
                        <idProduct>100569</idProduct>
                        <idLanguage>1</idLanguage>
                        <comments>Inserted through the API</comments>
                        <count>1</count>
                        <price>4</price>
                        <condition>EX</condition>
                        <isFoil>true</isFoil>
                        <isSigned>false</isSigned>
                        <isPlayset>false</isPlayset>
                    </article>
                </request>"""

    response = resolver.resolve(api_map=simple_api_map, data=data)

    assert response.status_code == 200
Example #3
0
class ResolversTest(unittest.TestCase):
    def setUp(self):
        self.live_resolver = SimpleResolver(sandbox_mode=False)
        self.empty_api_map = {}
        self.simple_api_map = {'url': '/account', 'method': 'get'}
        self.expected_url = '/account'
        self.simple_api_map_with_parameters = {'url': '/user/{user}', 'method': 'get'}
        self.expected_url_with_parameters = '/user/SimpleUser'
        self.expected_method = 'get'

    def test_if_bad_api_map_is_handled_correctly(self):

        self.assertRaises(Exception, self.live_resolver.setup)

        self.assertRaises(Exception, self.live_resolver.setup, self.empty_api_map)

    def test_if_setup_works_correctly(self):

        self.live_resolver.setup(self.simple_api_map)

        self.assertEqual(self.live_resolver.url, self.expected_url)
        self.assertEqual(self.live_resolver.method, self.expected_method)

        self.live_resolver.setup(self.simple_api_map_with_parameters, user='******')

        self.assertEqual(self.live_resolver.url, self.expected_url_with_parameters)
        self.assertEqual(self.live_resolver.method, self.expected_method)

    def test_if_bad_parameters_are_handled_correctly(self):

        self.assertRaises(exceptions.MissingParam, self.live_resolver.setup,
                          self.simple_api_map_with_parameters)

        self.assertRaises(exceptions.MissingParam, self.live_resolver.setup,
                          self.simple_api_map_with_parameters, bad_param='Worst parameter ever')
Example #4
0
def test_resolve_with_data():
    """
    Verifies calling resolve with some data serializes it
    correctly if necessary before sending it to the backend
    so that it returns a positive response
    """
    resolver = SimpleResolver(sandbox_mode=True)

    simple_api_map = {'url': '/stock', 'method': 'post'}

    # Data posted to MKM, this will be serialized to XML
    # before being sent, otherwise the backend won't accept it
    data = {
        'article': [{
            'idProduct': 100569,
            'idLanguage': 1,
            'comments': 'Inserted through the API',
            'count': 1,
            'price': 4,
            'condition': 'EX',
            'isFoil': True,
            'isSigned': False,
            'isPlayset': False
        }]
    }

    response = resolver.resolve(api_map=simple_api_map, data=data)

    assert response.status_code == 200
Example #5
0
def test_setup_escapes_additional_parameters():
    """Verifies setup escapes parameters containing spaces or special chars."""
    live_resolver = SimpleResolver(_API_MAP["1.1"]["api_root"])
    simple_api_map = {
        "url": "/products/{name}/{game}/{language}/{match}",
        "method": "get"
    }
    expected_url = "/products/Jace%2C%20the%20Mind%20Sculptor/1/1/False"

    live_resolver.setup(simple_api_map,
                        name="Jace, the Mind Sculptor",
                        game=1,
                        language=1,
                        match=False)

    assert live_resolver.url == expected_url
Example #6
0
def test_setup_escapes_additional_parameters():
    """Verifies setup escapes parameters containing spaces or special chars."""
    live_resolver = SimpleResolver(sandbox_mode=False)
    simple_api_map = {
        'url': '/products/{name}/{game}/{language}/{match}',
        'method': 'get'
    }
    expected_url = '/products/Jace%2C%20the%20Mind%20Sculptor/1/1/False'

    live_resolver.setup(simple_api_map,
                        name='Jace, the Mind Sculptor',
                        game=1,
                        language=1,
                        match=False)

    assert live_resolver.url == expected_url
Example #7
0
 def setUp(self):
     self.live_resolver = SimpleResolver(sandbox_mode=False)
     self.empty_api_map = {}
     self.simple_api_map = {'url': '/account', 'method': 'get'}
     self.expected_url = '/account'
     self.simple_api_map_with_parameters = {'url': '/user/{user}', 'method': 'get'}
     self.expected_url_with_parameters = '/user/SimpleUser'
     self.expected_method = 'get'
Example #8
0
def test_if_bad_api_map_is_handled_correctly():
    """Verifies setup throws an exception if api map used is empty."""

    live_resolver = SimpleResolver(_API_MAP["1.1"]["api_root"])
    empty_api_map = {}

    with pytest.raises(Exception):
        live_resolver.setup()

    with pytest.raises(Exception):
        live_resolver.setup(empty_api_map)
Example #9
0
def test_if_bad_parameters_are_handled_correctly():
    """Verifies exception is thrown when user passes an unexisting parameter."""
    live_resolver = SimpleResolver(_API_MAP["1.1"]["api_root"])
    simple_api_map_with_parameters = {"url": "/user/{user}", "method": "get"}

    with pytest.raises(exceptions.MissingParam):
        live_resolver.setup(simple_api_map_with_parameters)

    with pytest.raises(exceptions.MissingParam):
        live_resolver.setup(simple_api_map_with_parameters,
                            bad_param="Worst parameter ever")
Example #10
0
def test_if_bad_api_map_is_handled_correctly():
    """Verifies setup throws an exception if api map used is empty."""

    live_resolver = SimpleResolver(sandbox_mode=False)
    empty_api_map = {}

    with pytest.raises(Exception):
        live_resolver.setup()

    with pytest.raises(Exception):
        live_resolver.setup(empty_api_map)
Example #11
0
def test_if_bad_parameters_are_handled_correctly():
    """Verifies exception is thrown when user passes an unexisting parameter."""
    live_resolver = SimpleResolver(sandbox_mode=False)
    simple_api_map_with_parameters = {'url': '/user/{user}', 'method': 'get'}

    with pytest.raises(exceptions.MissingParam):
        live_resolver.setup(simple_api_map_with_parameters)

    with pytest.raises(exceptions.MissingParam):
        live_resolver.setup(simple_api_map_with_parameters,
                            bad_param='Worst parameter ever')
Example #12
0
def test_if_setup_works_correctly():
    """Verifies setup correctly formats the url."""

    live_resolver = SimpleResolver(_API_MAP["1.1"]["api_root"])
    simple_api_map = {"url": "/account", "method": "get"}
    expected_url = "/account"
    expected_url_with_parameters = "/user/SimpleUser"
    expected_method = "get"
    simple_api_map_with_parameters = {"url": "/user/{user}", "method": "get"}

    live_resolver.setup(simple_api_map)

    assert live_resolver.url == expected_url
    assert live_resolver.method == expected_method

    live_resolver.setup(simple_api_map_with_parameters, user="******")

    assert live_resolver.url == expected_url_with_parameters
    assert live_resolver.method == expected_method
Example #13
0
def test_if_setup_works_correctly():
    """Verifies setup correctly formats the url."""

    live_resolver = SimpleResolver(sandbox_mode=False)
    simple_api_map = {'url': '/account', 'method': 'get'}
    expected_url = '/account'
    expected_url_with_parameters = '/user/SimpleUser'
    expected_method = 'get'
    simple_api_map_with_parameters = {'url': '/user/{user}', 'method': 'get'}

    live_resolver.setup(simple_api_map)

    assert live_resolver.url == expected_url
    assert live_resolver.method == expected_method

    live_resolver.setup(simple_api_map_with_parameters, user='******')

    assert live_resolver.url == expected_url_with_parameters
    assert live_resolver.method == expected_method