def test_connection_error__check_accounts(self, mocked_request, mocked_sleep): # mock request and raise the 'ConnectionError' mocked_request.side_effect = requests.ConnectionError config = { "access_token": "test_access_token", "user_agent": "test_user_agent", "accounts": "1, 2" } # initialize 'LinkedinClient' cl = client.LinkedinClient( access_token=config['access_token'], user_agent=config['user_agent'], timeout_from_config=config.get('request_timeout')) try: # function call cl.check_accounts(config) except requests.ConnectionError: pass # verify that we backoff for 5 times self.assertEquals(mocked_request.call_count, 5)
def test_valid_linkedIn_accounts(self, mocked_request, mocked_discover): ''' If accounts are valid LinkedIn Ads accounts then discover will be called ''' mocked_request.return_value = get_response(200, raise_error=True) config = {"accounts": "1111, 2222"} client = _client.LinkedinClient('') tap_linkedin_ads.do_discover(client, config) self.assertEquals(mocked_discover.call_count, 1)
def test_405_error_custom_message(self, mocked_request): mocked_request.return_value = get_response(405, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInMethodNotAllowedError as e: self.assertEquals( str(e), "HTTP-error-code: 405, Error: The provided HTTP method is not supported by the URL." )
def test_404_error_custom_message(self, mocked_request): mocked_request.return_value = get_response(404, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInNotFoundError as e: self.assertEquals( str(e), "HTTP-error-code: 404, Error: The resource you have specified cannot be found. Either the accounts provided are invalid or you do not have access to the Ad Account." )
def test_403_error_custom_message(self, mocked_request): mocked_request.return_value = get_response(403, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInForbiddenError as e: self.assertEquals( str(e), "HTTP-error-code: 403, Error: User does not have permission to access the resource." )
def test_401_error_custom_message(self, mocked_request): mocked_request.return_value = get_response(401, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInUnauthorizedError as e: self.assertEquals( str(e), "HTTP-error-code: 401, Error: Invalid authorization credentials." )
def test_400_error_custom_message(self, mocked_request): mocked_request.return_value = get_response(400, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInBadRequestError as e: self.assertEquals( str(e), "HTTP-error-code: 400, Error: The request is missing or has a bad parameter." )
def test_411_error_custom_message(self, mocked_request): mocked_request.return_value = get_response(411, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInLengthRequiredError as e: self.assertEquals( str(e), "HTTP-error-code: 411, Error: The server refuses to accept the request without a defined Content-Length header." )
def test_429_error_custom_message(self, mocked_request): mocked_request.return_value = get_response(429, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInRateLimitExceeededError as e: self.assertEquals( str(e), "HTTP-error-code: 429, Error: API rate limit exceeded, please retry after some time." )
def test_400_error_empty_json(self, mocked_access_token, mocked_request): mocked_request.return_value = get_response(400, raise_error=True) client = _client.LinkedinClient("access_token") try: client.request("GET") except _client.LinkedInBadRequestError as e: self.assertEquals( str(e), "HTTP-error-code: 400, Error: The request is missing or has a bad parameter." )
def test_504_error_custom_message(self, mocked_sleep, mocked_request): mocked_request.return_value = get_response(504, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInGatewayTimeoutError as e: self.assertEquals( str(e), "HTTP-error-code: 504, Error: A gateway timeout occurred. There is a problem at LinkedIn's end." ) self.assertEquals(mocked_request.call_count, 5)
def test_500_error_custom_message(self, mocked_sleep, mocked_request): mocked_request.return_value = get_response(500, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInInternalServiceError as e: self.assertEquals( str(e), "HTTP-error-code: 500, Error: An error has occurred at LinkedIn's end." ) self.assertEquals(mocked_request.call_count, 5)
def test_timeout_value_not_passed_in_config(self): config = { "access_token": "test_access_token", "user_agent": "test_user_agent" } # initialize 'LinkedinClient' cl = client.LinkedinClient( access_token=config['access_token'], user_agent=config['user_agent'], timeout_from_config=config.get('request_timeout')) # verify that timeout value is default as request timeout is not passed in config self.assertEquals(300, cl.request_timeout)
def test_timeout_string_value_passed_in_config(self): config = { "access_token": "test_access_token", "user_agent": "test_user_agent", "request_timeout": "100" } # initialize 'LinkedinClient' cl = client.LinkedinClient( access_token=config['access_token'], user_agent=config['user_agent'], timeout_from_config=config.get('request_timeout')) # verify that timeout value is same as the value passed in the config self.assertEquals(100.0, cl.request_timeout)
def test_404_error(self, mocked_access_token, mocked_request): json = {"message": "Not Found.", "status": 404, "code": "NOT_FOUND"} mocked_request.return_value = get_response(404, json=json, raise_error=True) client = _client.LinkedinClient("access_token") try: client.request("GET") except _client.LinkedInNotFoundError as e: self.assertEquals( str(e), "HTTP-error-code: 404, Error: The resource you have specified cannot be found. Either the accounts provided are invalid or you do not have access to the Ad Account." )
def test_429_error_response_message(self, mocked_request): response_json = { "message": "APT ratelimit exceeded, retry after some time.", "status": 429, "code": "RATELIMIT_EXCEEDED" } mocked_request.return_value = get_response(429, response_json, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInRateLimitExceeededError as e: self.assertEquals( str(e), "HTTP-error-code: 429, Error: {}".format( response_json.get('message')))
def test_411_error_response_message(self, mocked_request): response_json = { "message": "Please add a defined Content-Length header.", "status": 411, "code": "LENGTH_REQUIRED" } mocked_request.return_value = get_response(411, response_json, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInLengthRequiredError as e: self.assertEquals( str(e), "HTTP-error-code: 411, Error: {}".format( response_json.get('message')))
def test_405_error_response_message(self, mocked_request): response_json = { "message": "The URL doesn't support this HTTP method.", "status": 405, "code": "METHOD_NOT_ALLOWED" } mocked_request.return_value = get_response(405, response_json, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInMethodNotAllowedError as e: self.assertEquals( str(e), "HTTP-error-code: 405, Error: {}".format( response_json.get('message')))
def test_403_error_response_message(self, mocked_request): response_json = { "message": "You do not have permission to access this resource.", "status": 403, "code": "FORBIDDEN" } mocked_request.return_value = get_response(403, response_json, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInForbiddenError as e: self.assertEquals( str(e), "HTTP-error-code: 403, Error: {}".format( response_json.get('message')))
def test_400_error_response_message(self, mocked_request): response_json = { "message": "Invalid params for account.", "status": 400, "code": "BAD_REQUEST" } mocked_request.return_value = get_response(400, response_json, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInBadRequestError as e: self.assertEquals( str(e), "HTTP-error-code: 400, Error: {}".format( response_json.get('message')))
def test_401_error_response_message(self, mocked_request): response_json = { "message": "The authorization has expired, please re-authorize.", "status": 401, "code": "UNAUTHORIZED" } mocked_request.return_value = get_response(401, response_json, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInUnauthorizedError as e: self.assertEquals( str(e), "HTTP-error-code: 401, Error: {}".format( response_json.get('message')))
def test_500_error_response_message(self, mocked_sleep, mocked_request): response_json = { "message": "Internal error, please retry after some time.", "status": 500, "code": "INTERNAL_ERROR" } mocked_request.return_value = get_response(500, response_json, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInInternalServiceError as e: self.assertEquals( str(e), "HTTP-error-code: 500, Error: {}".format( response_json.get('message'))) self.assertEquals(mocked_request.call_count, 5)
def test_invalid_linkedIn_accounts(self, mocked_request, mocked_discover): ''' If accounts are invalid LinkedIn Ads accounts then Exception raised with explanatory message ''' mocked_request.return_value = get_response(404, raise_error=True) config = {"accounts": "1111, 2222"} client = _client.LinkedinClient('') try: tap_linkedin_ads.do_discover(client, config) except Exception as e: expected_invalid_accounts = ["1111", "2222"] self.assertEqual( str(e), "Invalid Linked Ads accounts provided during the configuration:{}" .format(expected_invalid_accounts)) self.assertEqual(mocked_discover.call_count, 0)
def test_504_error_response_message(self, mocked_sleep, mocked_request): response_json = { "message": "Gateway timed out, please retry after some time.", "status": 504, "code": "GATEWAY_TIMEOUT" } mocked_request.return_value = get_response(504, response_json, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInGatewayTimeoutError as e: self.assertEquals( str(e), "HTTP-error-code: 504, Error: {}".format( response_json.get('message'))) self.assertEquals(mocked_request.call_count, 5)
def test_400_error_simple_json(self, mocked_access_token, mocked_request): json = { "message": "Invalid params for account.", "status": 400, "code": "BAD_REQUEST" } mocked_request.return_value = get_response(400, json=json, raise_error=True) client = _client.LinkedinClient("access_token") try: client.request("GET") except _client.LinkedInBadRequestError as e: self.assertEquals( str(e), "HTTP-error-code: 400, Error: Invalid params for account.")
def test_400_error_detailed_json(self, mocked_access_token, mocked_request): json = { "errorDetailType": "com.linkedin.common.error.BadRequest", "message": "Multiple errors occurred during the input validation. Please see errorDetails for more information.", "errorDetails": { "inputErrors": [{ "description": "Invalid argument", "input": { "inputPath": { "fieldPath": "search/account" } }, "code": "ERROR :: /account/values/0 :: Invalid Urn Format. Key long is in invalid format. Urn urn:li:sponsoredAccount:aaa." }, { "description": "Invalid argument", "input": { "inputPath": { "fieldPath": "search/account/values/0" } }, "code": "Invalid value for field; wrong type or other syntax error" }] }, "status": 400 } mocked_request.return_value = get_response(400, json=json, raise_error=True) client = _client.LinkedinClient("access_token") try: client.request("GET") except _client.LinkedInBadRequestError as e: self.assertEquals( str(e), "HTTP-error-code: 400, Error: " + str(json.get("errorDetails")))
def test_401_error_expired_access_token(self, mocked_logger, mocked_request): response_json = { "message": "Expired access token , please re-authenticate.", "status": 401, "code": "UNAUTHORIZED" } mocked_request.return_value = get_response(401, response_json, raise_error=True) linkedIn_client = client.LinkedinClient("access_token") try: linkedIn_client.check_access_token() except client.LinkedInUnauthorizedError as e: mocked_logger.assert_called_with( "Your access_token has expired as per LinkedIn’s security policy. Please re-authenticate your connection to generate a new token and resume extraction." ) self.assertEquals( str(e), "HTTP-error-code: 401, Error: {}".format( response_json.get('message')))
def test_timeout_error__check_access_token(self, mocked_request, mocked_sleep): # mock request and raise the 'Timeout' error mocked_request.side_effect = requests.Timeout config = { "access_token": "test_access_token", "user_agent": "test_user_agent" } # initialize 'LinkedinClient' try: with client.LinkedinClient( access_token=config['access_token'], user_agent=config['user_agent'], timeout_from_config=config.get('request_timeout')) as cl: pass except requests.Timeout: pass # verify that we backoff for 5 times self.assertEquals(mocked_request.call_count, 5)