def test_thread_request_error(self): expected = 'Mocked exception' remote_mock = mock.Mock() remote_mock.get_issue_comments = self.mock_remote_get_issue_comments self.mocked_request_parse_request_exception_return = expected remote_mock.parse_request_exception = self.mock_remote_parse_request_exception self.controller.deps.remote = remote_mock requester_mock = mock.Mock() requester_mock.get_request.side_effect = UnsuccessfulHttpRequestException( 400, expected) self.requester_mock = requester_mock original_requester = self.controller.deps.requester self.controller.deps.remote.requester = requester_mock temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): self.controller.thread( '1') # We need a number to pass the first condition. pass actual = temp_stdout.getvalue().strip().splitlines()[1] self.controller.deps.remote.requester = original_requester self.assertEqual(expected, actual)
def test_get_issues_description_not_found_issue(self): requester_mock = mock.Mock() requester_mock.get_request.side_effect = UnsuccessfulHttpRequestException(404, {}) github = Github(requester_mock) issues = ['1', '2', '3'] expected = [], issues actual = github.get_issues_description('username', 'repo', issues) self.assertEqual(expected, actual)
def test_get_issues_description_other_error(self): requester_mock = mock.Mock() requester_mock.get_request.side_effect = UnsuccessfulHttpRequestException(500, {}) github = Github(requester_mock) issues = ['1'] expected = [], [] actual = github.get_issues_description('username', 'repo', issues) self.assertEqual(expected, actual)
def test_parse_request_exception_other_exception(self): exception_code = 403 exception_headers = { 'X-RateLimit-Remaining': 1 } input_exception = UnsuccessfulHttpRequestException(exception_code, exception_headers) requester_mock = mock.Mock() github = Github(requester_mock) expected = 'An error occurred in the request.' actual = github.parse_request_exception(input_exception) self.assertEqual(expected, actual)
def test_parse_request_exception_api_limit(self): exception_code = 403 exception_headers = { 'X-RateLimit-Remaining': '0' } input_exception = UnsuccessfulHttpRequestException(exception_code, exception_headers) requester_mock = mock.Mock() github = Github(requester_mock) expected = 'API limit hit.' actual = github.parse_request_exception(input_exception) self.assertEqual(expected, actual)
def test_parse_request_exception_404(self): exception_code = 404 exception_headers = { 'X-RateLimit-Remaining': 100 } input_exception = UnsuccessfulHttpRequestException(exception_code, exception_headers) not_found_issues = ['1', '2', '3'] requester_mock = mock.Mock() github = Github(requester_mock) expected = "The following issue(s) couldn't be found: {0}".\ format(', '.join(not_found_issues)) actual = github.parse_request_exception(input_exception, not_found_issues) self.assertEqual(expected, actual)
def get_request(self, request): """ Executes a GET request. :param request: the GET request to execute. :return: response JSON object; False if the HTTP status code distinct to 200. """ try: response = requests.get(request, timeout=self.TIMEOUT) except RequestException: raise if response.status_code != 200: raise UnsuccessfulHttpRequestException(response.status_code, response.headers) response_object = json.loads(response.text) response.close() return response_object