Ejemplo n.º 1
0
def test_create_auth():
    """
    Verifies that the default Client is used
    when all the tokens are not empty strings.
    """
    api = Api()
    auth = api.create_auth('https://www.mkmapi.eu/ws/v1.1/output.json',
                           app_token='app_token',
                           app_secret='app_secret',
                           access_token='access_token',
                           access_token_secret='access_token_secret')

    assert isinstance(auth.client, Client)
    assert auth.client.client_key == 'app_token'
    assert auth.client.client_secret == 'app_secret'
    assert auth.client.resource_owner_key == 'access_token'
    assert auth.client.resource_owner_secret == 'access_token_secret'
Ejemplo n.º 2
0
def test_create_auth_with_empty_string_token():
    """
    Verifies that the custom MKMClient is used
    when access token and access token secret
    are empty strings.
    """
    api = Api()
    auth = api.create_auth('https://www.mkmapi.eu/ws/v1.1/output.json',
                           app_token='app_token',
                           app_secret='app_secret',
                           access_token='',
                           access_token_secret='')

    assert isinstance(auth.client, MKMClient)
    assert auth.client.client_key == 'app_token'
    assert auth.client.client_secret == 'app_secret'
    assert auth.client.resource_owner_key == ''
    assert auth.client.resource_owner_secret == ''
Ejemplo n.º 3
0
def test_create_auth():
    """
    Verifies that the default Client is used
    when all the tokens are not empty strings.
    """
    api = Api("https://api.cardmarket.com/ws/v1.1/output.json")
    auth = api.create_auth(
        "https://api.cardmarket.com/ws/v1.1/output.json",
        app_token="app_token",
        app_secret="app_secret",
        access_token="access_token",
        access_token_secret="access_token_secret",
    )

    assert isinstance(auth.client, Client)
    assert auth.client.client_key == "app_token"
    assert auth.client.client_secret == "app_secret"
    assert auth.client.resource_owner_key == "access_token"
    assert auth.client.resource_owner_secret == "access_token_secret"
Ejemplo n.º 4
0
def test_create_auth_with_empty_string_token():
    """
    Verifies that the custom MKMClient is used
    when access token and access token secret
    are empty strings.
    """
    api = Api("https://api.cardmarket.com/ws/v1.1/output.json")
    auth = api.create_auth(
        "https://api.cardmarket.com/ws/v1.1/output.json",
        app_token="app_token",
        app_secret="app_secret",
        access_token="",
        access_token_secret="",
    )

    assert isinstance(auth.client, MKMClient)
    assert auth.client.client_key == "app_token"
    assert auth.client.client_secret == "app_secret"
    assert auth.client.resource_owner_key == ""
    assert auth.client.resource_owner_secret == ""
Ejemplo n.º 5
0
class ApiTest(unittest.TestCase):

    def setUp(self):
        self.api = Api()
        self.response = mock.Mock()
        self.response.content = {}

    def test_create_auth(self):
        auth = self.api.create_auth('https://www.mkmapi.eu/ws/v1.1/output.json',
                                    app_token='app_token',
                                    app_secret='app_secret',
                                    access_token='access_token',
                                    access_token_secret='access_token_secret')

        self.assertIsInstance(auth.client, Client)
        self.assertEqual(auth.client.client_key, 'app_token')
        self.assertEqual(auth.client.client_secret, 'app_secret')
        self.assertEqual(auth.client.resource_owner_key, 'access_token')
        self.assertEqual(auth.client.resource_owner_secret, 'access_token_secret')

    def test_create_auth_with_empty_string_token(self):
        auth = self.api.create_auth('https://www.mkmapi.eu/ws/v1.1/output.json',
                                    app_token='app_token',
                                    app_secret='app_secret',
                                    access_token='',
                                    access_token_secret='')

        self.assertIsInstance(auth.client, MKMClient)
        self.assertEqual(auth.client.client_key, 'app_token')
        self.assertEqual(auth.client.client_secret, 'app_secret')
        self.assertEqual(auth.client.resource_owner_key, '')
        self.assertEqual(auth.client.resource_owner_secret, '')

    def test_missing_env_var_raise_exception_correctly(self):
        with mock.patch('mkmsdk.os') as os_mocked:
            os_mocked.environ = {}
            self.assertRaises(exceptions.MissingConfig, get_mkm_app_secret)

    def test_endpoint(self):
        expected_live_base_endpoint = 'https://www.mkmapi.eu/ws/v1.1/output.json'

        self.assertEqual(self.api.base_endpoint, expected_live_base_endpoint)

    def test_sandbox_mode(self):
        sandbox_api = Api(sandbox_mode=True)
        expected_sandbox_base_endpoint = 'https://sandbox.mkmapi.eu/ws/v1.1/output.json'

        self.assertEqual(sandbox_api.base_endpoint, expected_sandbox_base_endpoint)

    def test_redirection(self):
        self.response.status_code = 301
        self.assertRaises(exceptions.Redirection,
                          self.api.handle_response, self.response, self.response.content)

        self.response.status_code = 302
        self.assertRaises(exceptions.Redirection,
                          self.api.handle_response, self.response, self.response.content)

        self.response.status_code = 303
        self.assertRaises(exceptions.Redirection,
                          self.api.handle_response, self.response, self.response.content)

        self.response.status_code = 307
        self.assertRaises(exceptions.Redirection,
                          self.api.handle_response, self.response, self.response.content)

    def test_bad_request(self):
        self.response.status_code = 400
        self.assertRaises(exceptions.BadRequest,
                          self.api.handle_response, self.response, self.response.content)

    def test_unauthorized_access(self):
        self.response.status_code = 401
        self.assertRaises(exceptions.UnauthorizedAccess,
                          self.api.handle_response, self.response, self.response.content)

    def test_forbidden_access(self):
        self.response.status_code = 403
        self.assertRaises(exceptions.ForbiddenAccess,
                          self.api.handle_response, self.response, self.response.content)

    def test_resource_not_found(self):
        self.response.status_code = 404
        self.assertRaises(exceptions.ResourceNotFound,
                          self.api.handle_response, self.response, self.response.content)

    def test_method_not_allowed(self):
        self.response.status_code = 405
        self.assertRaises(exceptions.MethodNotAllowed,
                          self.api.handle_response, self.response, self.response.content)

    def test_resource_conflict(self):
        self.response.status_code = 409
        self.assertRaises(exceptions.ResourceConflict,
                          self.api.handle_response, self.response, self.response.content)

    def test_resource_gone(self):
        self.response.status_code = 410
        self.assertRaises(exceptions.ResourceGone,
                          self.api.handle_response, self.response, self.response.content)

    def test_resource_invalid(self):
        self.response.status_code = 422
        self.assertRaises(exceptions.ResourceInvalid,
                          self.api.handle_response, self.response, self.response.content)

    def test_client_error(self):
        self.response.status_code = 480
        self.assertRaises(exceptions.ClientError,
                          self.api.handle_response, self.response, self.response.content)

    def test_server_error(self):
        self.response.status_code = 545
        self.assertRaises(exceptions.ServerError,
                          self.api.handle_response, self.response, self.response.content)

    def test_unknown_error(self):
        self.response.status_code = 1001
        self.assertRaises(exceptions.ConnectionError,
                          self.api.handle_response, self.response, self.response.content)