Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_merges_empty(self):
"""Test when merges is empty API call"""
httpretty.register_uri(httpretty.GET,
GITLAB_URL_PROJECT,
body='',
status=200)
httpretty.register_uri(httpretty.GET,
GITLAB_MERGES_URL,
body="[]",
status=200
)
client = GitLabClient("fdroid", "fdroiddata", "aaa")
raw_merges = [merges for merges in client.merges()]
self.assertEqual(raw_merges[0], "[]")
self.assertEqual(
httpretty.last_request().headers["PRIVATE-TOKEN"], "aaa")
def test_issues_empty(self):
"""Test when issue is empty API call"""
httpretty.register_uri(httpretty.GET,
GITLAB_URL_PROJECT,
body='',
status=200)
httpretty.register_uri(httpretty.GET,
GITLAB_ISSUES_URL,
body="[]",
status=200
)
client = GitLabClient("fdroid", "fdroiddata", "aaa")
raw_issues = [issues for issues in client.issues()]
self.assertEqual(raw_issues[0], "[]")
self.assertEqual(
httpretty.last_request().headers["PRIVATE-TOKEN"], "aaa")
self.assertEqual(client.headers['PRIVATE-TOKEN'], "your-token")
self.assertEqual(client.retry_after_status, client.DEFAULT_RETRY_AFTER_STATUS_CODES)
client = GitLabClient("fdroid", "fdroiddata", "your-token", is_oauth_token=True,
sleep_for_rate=True, min_rate_to_sleep=100, max_retries=10, sleep_time=100)
self.assertEqual(client.sleep_for_rate, True)
self.assertEqual(client.base_url, GITLAB_API_URL)
self.assertEqual(client.min_rate_to_sleep, 100)
self.assertEqual(client.sleep_time, 100)
self.assertEqual(client.max_retries, 10)
self.assertEqual(client.is_oauth_token, True)
self.assertEqual(client.headers['Authorization'], "Bearer your-token")
self.assertEqual(client.retry_after_status, client.DEFAULT_RETRY_AFTER_STATUS_CODES)
client = GitLabClient("fdroid", "fdroiddata", "your-token", extra_retry_after_status=[404, 410])
self.assertEqual(client.sleep_for_rate, False)
self.assertEqual(client.base_url, GITLAB_API_URL)
self.assertEqual(client.min_rate_to_sleep, 10)
self.assertEqual(client.sleep_time, 1)
self.assertEqual(client.max_retries, 5)
self.assertEqual(client.is_oauth_token, False)
self.assertListEqual(client.retry_after_status, client.DEFAULT_RETRY_AFTER_STATUS_CODES + [404, 410])
def test_emojis(self):
"""Test emojis API call"""
setup_http_server(GITLAB_URL_PROJECT, GITLAB_ISSUES_URL, GITLAB_MERGES_URL,
rate_limit_headers={'RateLimit-Remaining': '20'})
client = GitLabClient("fdroid", "fdroiddata", "your-token")
raw_emojis = next(client.emojis("issues", 1))
emojis = json.loads(raw_emojis)
self.assertEqual(len(emojis), 2)
raw_emojis = next(client.emojis("merge_requests", 1))
emojis = json.loads(raw_emojis)
self.assertEqual(len(emojis), 2)
def test_calculate_time_to_reset(self):
"""Test whether the time to reset is zero if the sleep time is negative"""
httpretty.register_uri(httpretty.GET,
GITLAB_URL_PROJECT,
body='',
status=200,
forcing_headers={
'RateLimit-Remaining': '20',
'RateLimit-Reset': int(datetime_utcnow().replace(microsecond=0).timestamp())
})
client = GitLabClient("fdroid", "fdroiddata", "your-token")
time_to_reset = client.calculate_time_to_reset()
self.assertEqual(time_to_reset, 0)
httpretty.register_uri(httpretty.GET,
GITLAB_URL_PROJECT,
body='',
status=200,
forcing_headers={
'RateLimit-Remaining': '20',
'RateLimit-Reset': '15'
})
httpretty.register_uri(httpretty.GET,
GITLAB_ISSUES_URL,
body="",
status=500
)
client = GitLabClient("fdroid", "fdroiddata", "your-token", sleep_time=1)
with self.assertRaises(requests.exceptions.HTTPError):
_ = [issues for issues in client.issues()]
def test_merge_version(self):
"""Test merge version API call"""
setup_http_server(GITLAB_URL_PROJECT, GITLAB_ISSUES_URL, GITLAB_MERGES_URL,
rate_limit_headers={'RateLimit-Remaining': '20'})
client = GitLabClient("fdroid", "fdroiddata", "your-token")
raw_merge_version = client.merge_version(1, 1)
merge_version = json.loads(raw_merge_version)
self.assertEqual(len(merge_version['commits']), 1)
self.assertEqual(len(merge_version['diffs']), 1)
def test_initialization_entreprise(self):
"""Test initialization for GitLab entreprise server"""
setup_http_server(GITLAB_ENTERPRISE_URL_PROJECT,
GITLAB_ENTERPRISE_ISSUES_URL,
GITLAB_ENTERPRISE_MERGES_URL)
client = GitLabClient("am", "test", None, base_url=GITLAB_ENTERPRISE_URL)
self.assertEqual(client.owner, "am")
self.assertEqual(client.repository, "test")
self.assertEqual(client.token, None)
self.assertEqual(client.sleep_for_rate, False)
self.assertEqual(client.base_url, GITLAB_ENTERPRISE_API_URL)
self.assertEqual(client.min_rate_to_sleep, GitLabClient.MIN_RATE_LIMIT)
self.assertEqual(client.sleep_time, GitLabClient.DEFAULT_SLEEP_TIME)
self.assertEqual(client.max_retries, GitLabClient.MAX_RETRIES)
self.assertEqual(client.rate_limit, None)
self.assertEqual(client.rate_limit_reset_ts, None)
httpretty.register_uri(httpretty.GET,
GITLAB_URL_PROJECT,
body=project,
status=401)
with self.assertRaises(requests.exceptions.HTTPError):
GitLabClient("fdroid", "fdroiddata", "your-token")
project = read_file('data/gitlab/project')
httpretty.register_uri(httpretty.GET,
GITLAB_URL_PROJECT,
body=project,
status=400)
with self.assertLogs(logger, level='WARNING') as cm:
GitLabClient("fdroid", "fdroiddata", "your-token")
self.assertEqual(cm.output[0], 'WARNING:perceval.backends.core.gitlab:Rate limit not initialized: 400 '
'Client Error: Bad Request for '
def __get_issue_notes(self, issue_id):
"""Get issue notes"""
notes = []
group_notes = self.client.notes(GitLabClient.ISSUES, issue_id)
for raw_notes in group_notes:
for note in json.loads(raw_notes):
note_id = note['id']
note['award_emoji_data'] = \
self.__get_note_award_emoji(GitLabClient.ISSUES, issue_id, note_id)
notes.append(note)
return notes