def test_fetch_events_enterprise(self): """Test if it fetches events from a GitHub Enterprise server""" events = read_file('data/github/github_events_page_2') issue = read_file('data/github/github_issue_2') rate_limit = read_file('data/github/rate_limit') httpretty.register_uri(httpretty.GET, GITHUB_ENTREPRISE_RATE_LIMIT, body=rate_limit, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.GET, GITHUB_ENTERPRISE_ISSUES_URL, body=issue, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.POST, GITHUB_ENTERPRISE_API_GRAPHQL_URL, body=events, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) github = GitHubQL("zhquan_example", "repo", ["aaa"], base_url=GITHUB_ENTERPRISE_URL) events = [events for events in github.fetch(from_date=None, to_date=None, category=CATEGORY_EVENT)] self.assertEqual(len(events), 2) event = events[0] self.assertEqual(event['origin'], 'https://example.com/zhquan_example/repo') self.assertEqual(event['uuid'], '7ae18a45805b971b46dba2874f6deb28e1fb3db1') self.assertEqual(event['updated_on'], 1586265768.0) self.assertEqual(event['category'], CATEGORY_EVENT) self.assertEqual(event['tag'], 'https://example.com/zhquan_example/repo') self.assertEqual(event['data']['actor']['login'], 'valeriocos') self.assertEqual(event['data']['createdAt'], '2020-04-07T13:22:48Z') self.assertEqual(event['data']['eventType'], 'MovedColumnsInProjectEvent') self.assertIn('issue', event['data']) event = events[1] self.assertEqual(event['origin'], 'https://example.com/zhquan_example/repo') self.assertEqual(event['uuid'], 'd0d1489be06622577843c91b70cb5543f48be918') self.assertEqual(event['updated_on'], 1586265783.0) self.assertEqual(event['category'], CATEGORY_EVENT) self.assertEqual(event['tag'], 'https://example.com/zhquan_example/repo') self.assertEqual(event['data']['actor']['login'], 'valeriocos') self.assertEqual(event['data']['createdAt'], '2020-04-07T13:23:03Z') self.assertEqual(event['data']['eventType'], 'CrossReferencedEvent') self.assertIn('issue', event['data'])
def test_fetch_events(self): """Test whether a list of events is returned""" events = read_file('data/github/github_events_page_2') issue = read_file('data/github/github_issue_2') rate_limit = read_file('data/github/rate_limit') httpretty.register_uri(httpretty.GET, GITHUB_RATE_LIMIT, body=rate_limit, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.GET, GITHUB_ISSUES_URL, body=issue, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.POST, GITHUB_API_GRAPHQL_URL, body=events, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) github = GitHubQL("zhquan_example", "repo", ["aaa"]) events = [events for events in github.fetch(from_date=None, to_date=None, category=CATEGORY_EVENT)] self.assertEqual(len(events), 2) event = events[0] self.assertEqual(event['origin'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['uuid'], 'b46499fd01d2958d836241770063adff953b280e') self.assertEqual(event['updated_on'], 1586265768.0) self.assertEqual(event['category'], CATEGORY_EVENT) self.assertEqual(event['tag'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['data']['actor']['login'], 'valeriocos') self.assertEqual(event['data']['createdAt'], '2020-04-07T13:22:48Z') self.assertEqual(event['data']['eventType'], 'MovedColumnsInProjectEvent') self.assertIn('issue', event['data']) event = events[1] self.assertEqual(event['origin'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['uuid'], 'd05238b1254cf69deac49248ad8cc855482a6737') self.assertEqual(event['updated_on'], 1586265783.0) self.assertEqual(event['category'], CATEGORY_EVENT) self.assertEqual(event['tag'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['data']['actor']['login'], 'valeriocos') self.assertEqual(event['data']['createdAt'], '2020-04-07T13:23:03Z') self.assertEqual(event['data']['eventType'], 'CrossReferencedEvent') self.assertIn('issue', event['data'])
def setUp(self): super().setUp() self.backend_write_archive = GitHubQL("zhquan_example", "repo", ["aaa"], archive=self.archive) self.backend_read_archive = GitHubQL("zhquan_example", "repo", ["aaa"], archive=self.archive)
def test_fetch_merged_event(self): """Test the MergedEvent is fetched properly""" events = read_file('data/github/github_events_page_3') issue = read_file('data/github/github_issue_1') rate_limit = read_file('data/github/rate_limit') httpretty.register_uri(httpretty.GET, GITHUB_RATE_LIMIT, body=rate_limit, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.GET, GITHUB_ISSUES_URL, body=issue, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.POST, GITHUB_API_GRAPHQL_URL, body=events, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) github = GitHubQL("zhquan_example", "repo", ["aaa"]) events = [ events for events in github.fetch( from_date=None, to_date=None, category=CATEGORY_EVENT) ] self.assertEqual(len(events), 1) event = events[0] self.assertEqual(event['origin'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['uuid'], '5ad76253ec2e63e9d4431a8550386303012fd6ca') self.assertEqual(event['updated_on'], 1602094090.0) self.assertEqual(event['category'], CATEGORY_EVENT) self.assertEqual(event['tag'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['data']['actor']['login'], 'zhquan') self.assertEqual(event['data']['createdAt'], '2020-10-07T18:08:10Z') self.assertEqual(event['data']['eventType'], 'MergedEvent') self.assertIn('issue', event['data'])
def test_search_fields_event(self): """Test whether the search_fields is properly set""" events = read_file('data/github/github_events_page_2') issue = read_file('data/github/github_issue_2') rate_limit = read_file('data/github/rate_limit') httpretty.register_uri(httpretty.GET, GITHUB_RATE_LIMIT, body=rate_limit, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.GET, GITHUB_ISSUES_URL, body=issue, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.POST, GITHUB_API_GRAPHQL_URL, body=events, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) github = GitHubQL("zhquan_example", "repo", ["aaa"]) events = [ events for events in github.fetch( from_date=None, to_date=None, category=CATEGORY_EVENT) ] self.assertEqual(len(events), 2) event = events[0] self.assertEqual(github.metadata_id(event['data']), event['search_fields']['item_id']) self.assertEqual(event['search_fields']['owner'], 'zhquan_example') self.assertEqual(event['search_fields']['repo'], 'repo') event = events[1] self.assertEqual(github.metadata_id(event['data']), event['search_fields']['item_id']) self.assertEqual(event['search_fields']['owner'], 'zhquan_example') self.assertEqual(event['search_fields']['repo'], 'repo')
def test_initialization(self): """Test whether attributes are initialized""" rate_limit = read_file('data/github/rate_limit') httpretty.register_uri(httpretty.GET, GITHUB_RATE_LIMIT, body=rate_limit, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) github = GitHubQL('zhquan_example', 'repo', ['aaa'], tag='test') self.assertEqual(github.owner, 'zhquan_example') self.assertEqual(github.repository, 'repo') self.assertEqual(github.origin, 'https://github.com/zhquan_example/repo') self.assertEqual(github.tag, 'test') self.assertEqual(github.max_items, MAX_CATEGORY_ITEMS_PER_PAGE) self.assertFalse(github.exclude_user_data) self.assertEqual(github.categories, [CATEGORY_EVENT]) self.assertTrue(github.ssl_verify) # When tag is empty or None it will be set to the value in origin github = GitHubQL('zhquan_example', 'repo', ['aaa'], ssl_verify=False) self.assertEqual(github.owner, 'zhquan_example') self.assertEqual(github.repository, 'repo') self.assertEqual(github.origin, 'https://github.com/zhquan_example/repo') self.assertEqual(github.tag, 'https://github.com/zhquan_example/repo') self.assertFalse(github.ssl_verify) github = GitHubQL('zhquan_example', 'repo', ['aaa'], tag='') self.assertEqual(github.owner, 'zhquan_example') self.assertEqual(github.repository, 'repo') self.assertEqual(github.origin, 'https://github.com/zhquan_example/repo') self.assertEqual(github.tag, 'https://github.com/zhquan_example/repo')
def test_fetch_events_pagination(self): """Test whether a list of paginated events is returned""" requests = [] events_page_1 = read_file('data/github/github_events_page_1') events_page_2 = read_file('data/github/github_events_page_2') bodies_json = [events_page_1, events_page_2] def request_callback(method, uri, headers): body = bodies_json.pop(0) requests.append(httpretty.last_request()) return 200, headers, body issue = read_file('data/github/github_issue_2') rate_limit = read_file('data/github/rate_limit') httpretty.register_uri(httpretty.GET, GITHUB_RATE_LIMIT, body=rate_limit, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.GET, GITHUB_ISSUES_URL, body=issue, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.POST, GITHUB_API_GRAPHQL_URL, responses=[httpretty.Response(body=request_callback) for _ in range(2)], status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) github = GitHubQL("zhquan_example", "repo", ["aaa"]) events = [events for events in github.fetch(from_date=None, to_date=None, category=CATEGORY_EVENT)] self.assertEqual(len(events), 4) event = events[0] self.assertEqual(event['origin'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['uuid'], '116d709c3225b31f094218148d3fcceaf6737b37') self.assertEqual(event['updated_on'], 1586258472.0) self.assertEqual(event['category'], CATEGORY_EVENT) self.assertEqual(event['tag'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['data']['actor']['login'], 'valeriocos') self.assertEqual(event['data']['createdAt'], '2020-04-07T11:21:12Z') self.assertEqual(event['data']['eventType'], 'LabeledEvent') self.assertIn('issue', event['data']) event = events[1] self.assertEqual(event['origin'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['uuid'], 'a5e67dcb8ec7b722cc088c2e5f8bad0b3e285329') self.assertEqual(event['updated_on'], 1586258479.0) self.assertEqual(event['category'], CATEGORY_EVENT) self.assertEqual(event['tag'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['data']['actor']['login'], 'valeriocos') self.assertEqual(event['data']['createdAt'], '2020-04-07T11:21:19Z') self.assertEqual(event['data']['eventType'], 'LabeledEvent') self.assertIn('issue', event['data']) event = events[2] self.assertEqual(event['origin'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['uuid'], 'b46499fd01d2958d836241770063adff953b280e') self.assertEqual(event['updated_on'], 1586265768.0) self.assertEqual(event['category'], CATEGORY_EVENT) self.assertEqual(event['tag'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['data']['actor']['login'], 'valeriocos') self.assertEqual(event['data']['createdAt'], '2020-04-07T13:22:48Z') self.assertEqual(event['data']['eventType'], 'MovedColumnsInProjectEvent') self.assertIn('issue', event['data']) event = events[3] self.assertEqual(event['origin'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['uuid'], 'd05238b1254cf69deac49248ad8cc855482a6737') self.assertEqual(event['updated_on'], 1586265783.0) self.assertEqual(event['category'], CATEGORY_EVENT) self.assertEqual(event['tag'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['data']['actor']['login'], 'valeriocos') self.assertEqual(event['data']['createdAt'], '2020-04-07T13:23:03Z') self.assertEqual(event['data']['eventType'], 'CrossReferencedEvent') self.assertIn('issue', event['data'])
def test_fetch_events_github_app(self): """Test whether a list of events is returned using GitHub App""" events = read_file('data/github/github_events_page_2') issue = read_file('data/github/github_issue_2') rate_limit = read_file('data/github/rate_limit') installation = [ { "account": { "login": "******" }, "id": "1" } ] httpretty.register_uri(httpretty.GET, GITHUB_RATE_LIMIT, body=rate_limit, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.GET, GITHUB_ISSUES_URL, body=issue, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.POST, GITHUB_API_GRAPHQL_URL, body=events, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) httpretty.register_uri(httpretty.GET, GITHUB_APP_INSTALLATION_URL, body=json.dumps(installation), status=200) httpretty.register_uri(httpretty.POST, GITHUB_APP_ACCESS_TOKEN_URL, body='{"token": "v1.aaa"}', status=200) httpretty.register_uri(httpretty.GET, GITHUB_APP_AUTH_URL, body='', status=200) github = GitHubQL("zhquan_example", "repo", github_app_id='1', github_app_pk_filepath='data/github/private.pem') events = [events for events in github.fetch(from_date=None, to_date=None, category=CATEGORY_EVENT)] self.assertEqual(len(events), 2) event = events[0] self.assertEqual(event['origin'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['uuid'], 'b46499fd01d2958d836241770063adff953b280e') self.assertEqual(event['updated_on'], 1586265768.0) self.assertEqual(event['category'], CATEGORY_EVENT) self.assertEqual(event['tag'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['data']['actor']['login'], 'valeriocos') self.assertEqual(event['data']['createdAt'], '2020-04-07T13:22:48Z') self.assertEqual(event['data']['eventType'], 'MovedColumnsInProjectEvent') self.assertIn('issue', event['data']) event = events[1] self.assertEqual(event['origin'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['uuid'], 'd05238b1254cf69deac49248ad8cc855482a6737') self.assertEqual(event['updated_on'], 1586265783.0) self.assertEqual(event['category'], CATEGORY_EVENT) self.assertEqual(event['tag'], 'https://github.com/zhquan_example/repo') self.assertEqual(event['data']['actor']['login'], 'valeriocos') self.assertEqual(event['data']['createdAt'], '2020-04-07T13:23:03Z') self.assertEqual(event['data']['eventType'], 'CrossReferencedEvent') self.assertIn('issue', event['data']) self.assertEqual(httpretty.last_request().headers["Authorization"], "token v1.aaa")
def test_has_archiving(self): """Test if it returns True when has_archiving is called""" self.assertEqual(GitHubQL.has_archiving(), True)
def test_has_resuming(self): """Test if it returns False when has_resuming is called""" self.assertEqual(GitHubQL.has_resuming(), False)