def test_get_from_date_issues(self): """Test issues from date API call""" issues = read_file('data/pagure/pagure_repo_issue_from_2020_03_07') httpretty.register_uri(httpretty.GET, PAGURE_ISSUES_URL, body=issues, status=200, ) from_date = datetime.datetime(2020, 3, 7) client = PagureClient(namespace=None, repository='Project-example', token='aaa') raw_issues = [issues for issues in client.issues(from_date=from_date)] self.assertEqual(raw_issues[0], issues) # Check requests expected = { 'status': ['all'], 'per_page': ['100'], 'order': ['asc'], 'since': ['2020-03-07 00:00:00'] } self.assertDictEqual(httpretty.last_request().querystring, expected) self.assertEqual(httpretty.last_request().headers["Authorization"], "token aaa")
def test_http_wrong_status(self): """Test if an error is raised when the http status was not 200""" issue = "" httpretty.register_uri(httpretty.GET, PAGURE_ISSUES_URL, body=issue, status=501, ) client = PagureClient(namespace=None, repository="Project-example", token="aaa", sleep_time=1, max_retries=1) with self.assertRaises(requests.exceptions.HTTPError): _ = [issues for issues in client.issues()] # Check requests expected = { 'status': ['all'], 'per_page': ['100'], 'order': ['asc'] } self.assertDictEqual(httpretty.last_request().querystring, expected) self.assertEqual(httpretty.last_request().headers["Authorization"], "token aaa")
def test_get_empty_issues(self): """Test when issue is empty API call""" issue = read_file('data/pagure/pagure_empty_request') httpretty.register_uri( httpretty.GET, PAGURE_ISSUES_URL, body=issue, status=200, ) client = PagureClient(namespace=None, repository="Project-example", token="aaa") raw_issues = [issues for issues in client.issues()] self.assertEqual(raw_issues[0], issue) # Check requests expected = {'status': ['all'], 'per_page': ['100'], 'order': ['asc']} self.assertDictEqual(httpretty.last_request().querystring, expected) self.assertEqual(httpretty.last_request().headers["Authorization"], "token aaa")
def test_init(self): """Test initialization of client""" client = PagureClient(namespace=None, repository="Project-example", token="aaa") self.assertIsNone(client.namespace) self.assertEqual(client.repository, "Project-example") self.assertEqual(client.sleep_time, PagureClient.DEFAULT_SLEEP_TIME) self.assertEqual(client.max_retries, PagureClient.MAX_RETRIES) self.assertEqual(client.base_url, PAGURE_API_URL) self.assertTrue(client.ssl_verify) client = PagureClient(None, "Project-test-example", token='aaa', sleep_time=20, max_retries=2, max_items=1, archive=None, from_archive=False) self.assertIsNone(client.namespace) self.assertEqual(client.repository, "Project-test-example") self.assertEqual(client.token, 'aaa') self.assertEqual(client.sleep_time, 20) self.assertEqual(client.max_retries, 2) self.assertEqual(client.max_items, 1) self.assertIsNone(client.archive) self.assertFalse(client.from_archive) client = PagureClient(None, repository='Project-test-example', token=None) self.assertIsNone(client.token) # When the repository is within a namespace client = PagureClient(namespace='Test-group', repository="Project-namespace-example", token="aaa") self.assertEqual(client.namespace, 'Test-group') self.assertEqual(client.repository, "Project-namespace-example") self.assertEqual(client.sleep_time, PagureClient.DEFAULT_SLEEP_TIME) self.assertEqual(client.max_retries, PagureClient.MAX_RETRIES) self.assertEqual(client.base_url, PAGURE_API_URL) self.assertTrue(client.ssl_verify)
def test_get_page_issues(self): """Test issues pagination API call""" issue_1 = read_file('data/pagure/pagure_repo_issue_1') issue_2 = read_file('data/pagure/pagure_repo_only_issue_2') httpretty.register_uri( httpretty.GET, PAGURE_ISSUES_URL, body=issue_1, status=200, forcing_headers={ 'Link': '<' + PAGURE_ISSUES_URL + '/?&page=2>; rel="next", <' + PAGURE_ISSUES_URL + '/?&page=3>; rel="last"' }) httpretty.register_uri( httpretty.GET, PAGURE_ISSUES_URL + '/?&page=2', body=issue_2, status=200, ) client = PagureClient(namespace=None, repository="Project-example", token="aaa") issues = [issues for issues in client.issues()] self.assertEqual(len(issues), 2) self.assertEqual(issues[0], issue_1) self.assertEqual(issues[1], issue_2) # Check requests expected = { 'status': ['all'], 'page': ['2'], 'per_page': ['100'], 'order': ['asc'] } self.assertDictEqual(httpretty.last_request().querystring, expected) self.assertEqual(httpretty.last_request().headers["Authorization"], "token aaa")
def test_namespace_issues(self): """Test fetching issues from a repository within a namespace""" issue = read_file('data/pagure/pagure_namespace_issue_2') httpretty.register_uri(httpretty.GET, PAGURE_NAMESPACE_ISSUES_URL, body=issue, status=200) client = PagureClient(namespace='Test-group', repository='Project-namespace-example', token=None) raw_issues = [issues for issues in client.issues()] self.assertEqual(raw_issues[0], issue) # Check requests expected = {'status': ['all'], 'per_page': ['100'], 'order': ['asc']} self.assertDictEqual(httpretty.last_request().querystring, expected) self.assertIsNone(httpretty.last_request().headers["Authorization"])
def test_sanitize_for_archive(self): """Test whether the sanitize method works properly""" url = "http://example.com" headers = {PagureClient.HAUTHORIZATION: "token aaa"} c_headers = copy.deepcopy(headers) payload = {} san_u, san_h, san_p = PagureClient.sanitize_for_archive( url, c_headers, payload) headers.pop(PagureClient.HAUTHORIZATION) self.assertEqual(url, san_u) self.assertEqual(headers, san_h) self.assertEqual(payload, san_p)