Esempio n. 1
0
class TestShortenAPI(unittest.TestCase):
    def setUp(self):
        self.api = API()

    @patch_object(API, "_invoke_api", mocked_API_request_failure(200))
    def test_shorten_api_response_200(self):
        api_response = self.api.shorten("http://www.bernardofontes.net")
        self.assertEquals(api_response["status_code"], 200)
        self.assertTrue(api_response.has_key("url"))
        self.assertTrue(api_response["url"])

    @patch_object(API, "_invoke_api", mocked_API_request_failure(403))
    def test_shorten_api_response_403(self):
        api_response = self.api.shorten("http://www.bernardofontes.net")
        self.assertEquals(api_response["status_code"], 403)
        self.assertTrue(api_response.has_key("error_message"))
        self.assertTrue("Rate limit exceeded" in api_response["error_message"])

    @patch_object(API, "_invoke_api", mocked_API_request_failure(503))
    def test_shorten_api_response_503(self):
        api_response = self.api.shorten("http://www.bernardofontes.net")
        self.assertEquals(api_response["status_code"], 503)
        self.assertTrue(api_response.has_key("error_message"))
        self.assertTrue("Unknow error or temporary unavailability" in api_response["error_message"])

    @patch_object(API, "_invoke_api", mocked_API_request_failure(500))
    def test_shorten_api_response_500(self):
        api_response = self.api.shorten("http://www.bernardofontes.net")
        self.assertEquals(api_response["status_code"], 500)
        self.assertTrue(api_response.has_key("error_message"))
        self.assertTrue("Invalid request format" in api_response["error_message"])
Esempio n. 2
0
 def setUp(self):
     self.api = API()
     self.parameters = {
         "login": "******",
         "apiKey": "R_thisismycrazyapikey",
         "longUrl": "http://www.bernardofontes.net",
     }
Esempio n. 3
0
class TestBitlyAPIClientFunctions(unittest.TestCase):
    def setUp(self):
        self.api = API()
        self.parameters = {
            "login": "******",
            "apiKey": "R_thisismycrazyapikey",
            "longUrl": "http://www.bernardofontes.net",
        }

    def test_get_rest_method_parameters_return_expected_string(self):
        expected = "login=my_login&apiKey=R_thisismycrazyapikey&longUrl=http://www.bernardofontes.net"
        self.assertEquals(self.api._get_rest_method_parameters(self.parameters), expected)

    def test_get_api_method_parameters_url_works_for_shorten(self):
        expected = "http://api.bit.ly/v3/shorten?login=my_login&apiKey=R_thisismycrazyapikey&longUrl=http://www.bernardofontes.net"
        self.assertEquals(self.api._get_api_method_url("shorten", self.parameters), expected)
Esempio n. 4
0
 def setUp(self):
     self.api = API()
     self.parameters = {
         'login': '******',
         'apiKey': 'R_thisismycrazyapikey',
         'longUrl': 'http://www.bernardofontes.net',
     }
Esempio n. 5
0
class TestBitlyAPIClientFunctions(unittest.TestCase):

    def setUp(self):
        self.api = API()
        self.parameters = {
            'login': '******',
            'apiKey': 'R_thisismycrazyapikey',
            'longUrl': 'http://www.bernardofontes.net',
        }

    def test_get_rest_method_parameters_return_expected_string(self):
        expected = 'login=my_login&apiKey=R_thisismycrazyapikey&longUrl=http://www.bernardofontes.net'
        self.assertEquals(self.api._get_rest_method_parameters(self.parameters), expected)

    def test_get_rest_method_parameters_with_list_return_expected_string(self):
        self.parameters['listParam'] = ['value_1', 'value_2']
        expected = 'listParam=value_1&listParam=value_2&login=my_login&apiKey=R_thisismycrazyapikey&longUrl=http://www.bernardofontes.net'
        self.assertEquals(self.api._get_rest_method_parameters(self.parameters), expected)

    def test_get_api_method_parameters_url_works_for_shorten(self):
        expected = 'http://api.bit.ly/v3/shorten?login=my_login&apiKey=R_thisismycrazyapikey&longUrl=http://www.bernardofontes.net'
        self.assertEquals(self.api._get_api_method_url('shorten', self.parameters), expected)
Esempio n. 6
0
class TestExpandAPI(unittest.TestCase):

    def setUp(self):
        self.api = API()

    @patch.object(API, '_invoke_api', mocked_expand_API_request_failure(200))
    def test_expand_api_response_200(self):
        api_response = self.api.expand(['http://bit.ly/hash', 'http://bit.ly/hash2'])
        status_code = api_response['status_code']
        self.assertEquals(status_code, 200)
        self.assertTrue(api_response.has_key('expand'))
        self.assertTrue(len(api_response['expand']) == 2)

    @patch.object(API, '_invoke_api', mocked_expand_API_request_failure(403))
    def test_expand_api_response_403(self):
        api_response = self.api.expand(['http://bit.ly/hash', 'http://bit.ly/hash2'])
        status_code = api_response['status_code']
        self.assertEquals(status_code, 403)
        self.assertTrue(api_response.has_key('error_message'))
        self.assertTrue('Rate limit exceeded' in api_response['error_message'])

    @patch.object(API, '_invoke_api', mocked_expand_API_request_failure(503))
    def test_expand_api_response_503(self):
        api_response = self.api.expand(['http://bit.ly/hash', 'http://bit.ly/hash2'])
        status_code = api_response['status_code']
        self.assertEquals(status_code, 503)
        self.assertTrue(api_response.has_key('error_message'))
        self.assertTrue('Unknow error or temporary unavailability' in api_response['error_message'])

    @patch.object(API, '_invoke_api', mocked_expand_API_request_failure(500))
    def test_expand_api_response_500(self):
        api_response = self.api.expand(['http://bit.ly/hash', 'http://bit.ly/hash2'])
        status_code = api_response['status_code']
        self.assertEquals(status_code, 500)
        self.assertTrue(api_response.has_key('error_message'))
        self.assertTrue('Invalid request format' in api_response['error_message'])
Esempio n. 7
0
class TestShortenAPI(unittest.TestCase):

    def setUp(self):
        self.api = API()

    @patch.object(API, '_invoke_api', mocked_shorten_API_response(200))
    def test_shorten_api_response_200(self):
        api_response = self.api.shorten('http://www.bernardofontes.net')
        status_code = api_response['status_code']
        self.assertEquals(status_code, 200)
        self.assertTrue(api_response.has_key('url'))
        self.assertTrue(api_response['url'])

    @patch.object(API, '_invoke_api', mocked_shorten_API_response(403))
    def test_shorten_api_response_403(self):
        api_response = self.api.shorten('http://www.bernardofontes.net')
        status_code = api_response['status_code']
        self.assertEquals(status_code, 403)
        self.assertTrue(api_response.has_key('error_message'))
        self.assertTrue('Rate limit exceeded' in api_response['error_message'])

    @patch.object(API, '_invoke_api', mocked_shorten_API_response(503))
    def test_shorten_api_response_503(self):
        api_response = self.api.shorten('http://www.bernardofontes.net')
        status_code = api_response['status_code']
        self.assertEquals(status_code, 503)
        self.assertTrue(api_response.has_key('error_message'))
        self.assertTrue('Unknow error or temporary unavailability' in api_response['error_message'])

    @patch.object(API, '_invoke_api', mocked_shorten_API_response(500))
    def test_shorten_api_response_500(self):
        api_response = self.api.shorten('http://www.bernardofontes.net')
        status_code = api_response['status_code']
        self.assertEquals(status_code, 500)
        self.assertTrue(api_response.has_key('error_message'))
        self.assertTrue('Invalid request format' in api_response['error_message'])
Esempio n. 8
0
 def setUp(self):
     self.api = API()