def test_pagure_missing_avatar(self, mock_call_api): mock_call_api.return_value = {} expected = ( "https://seccdn.libravatar.org/avatar/" "9c9f7784935381befc302fe3c814f9136e7a33953d0318761669b8643f4df55c") actual = PagureService()._avatar("ralph") self.assertEqual(actual.split("?")[0], expected)
def test_get_last_comment(self): pagure = PagureService() res = { "comments": [ { "comment": "first comment", "date_created": "1539776992", "user1": {"name": "username"}, }, { "comment": "last comment", "date_created": "1539777081", "user": {"name": "user2"}, }, ] } last_comment = pagure.get_last_comment(res) self.assertEqual( last_comment, LastComment( author="user2", body="last comment", created_at=datetime.datetime.utcfromtimestamp(1539777081), ), )
def test_request_review_incorrect_project_with_repo(self): pagure = PagureService() with self.assertRaises(Exception) as context: pagure.request_reviews( user_name=self.config["user_name"], repo_name=self.config["repo_name"], ) self.assertIn("Page not found", str(context.exception))
def test_avatar_without_url(self, mock_call_api, mock_hashlib, mock_urllib): """ Tests '_avatar' function where we don't have an avatar url """ # Set up mock return values and side effects mock_hash = MagicMock() mock_hash.hexdigest.return_value = 'mock_idx' mock_call_api.return_value = {} mock_urllib.parse.urlencode.return_value = "mock_query" mock_hashlib.sha256.return_value = mock_hash # Call function response = PagureService()._avatar(username='******') # Validate function calls and response mock_urllib.parse.urlencode.assert_called_with({'s': 64, 'd': 'retro'}) mock_call_api.assert_called_with( url='https://pagure.io/api/0/user/dummy_user', ssl_verify=True) mock_hashlib.sha256.assert_called_once_with( b'http://dummy_user.id.fedoraproject.org/') self.assertEqual( response, 'https://seccdn.libravatar.org/avatar/mock_idx?mock_query') mock_urllib.parse.urlparse.assert_not_called() mock_urllib.parse.parse_qs.assert_not_called() mock_urllib.parse.urlunparse.assert_not_called()
def test_avatar_with_url(self, mock_call_api, mock_hashlib, mock_urllib): """ Tests '_avatar' function where we have an avatar url """ # Set up mock return values and side effects mock_response = {'user': {'avatar_url': 'dummy_avatar_url'}} mock_url_parts = MagicMock() mock_url_parts.return_value = 'mock_url_parts' mock_url_parts.query = 'mock_url_parts_query' mock_url_query = MagicMock() mock_url_query.return_value = 'mock_url_query' mock_urllib.parse.urlparse.return_value = mock_url_parts mock_urllib.parse.parse_qs.return_value = mock_url_query mock_urllib.parse.urlencode.return_value = mock_url_query mock_urllib.parse.urlunparse.return_value = "dummy_avatar_url" mock_call_api.return_value = mock_response # Call function response = PagureService()._avatar(username='******') # Validate function calls and response self.assertEqual(response, 'dummy_avatar_url') mock_urllib.parse.urlparse.assert_called_with('dummy_avatar_url') mock_urllib.parse.parse_qs.assert_called_with('mock_url_parts_query') mock_call_api.assert_called_with( url='https://pagure.io/api/0/user/dummy_user', ssl_verify=True) mock_url_query.update.assert_called_with({'s': 64, 'd': 'retro'}) mock_urllib.parse.urlencode.assert_called_with(mock_url_query, doseq=True) mock_urllib.parse.urlunparse.assert_called_with(mock_url_parts[:4] + (mock_url_query, ) + mock_url_parts[5:]) mock_hashlib.assert_not_called()
def test_request_reviews_no_repo(self, mock_avatar, mock_PagureReview, mock_has_new_comments, mock_check_request_state, mock_datetime, mock_get_last_comment, mock_call_api): """ Tests 'request_reviews' function without repos and: * no last comment, * check_request_state returns True * _call_api raises a HTTPError, * no namespace """ # Set up mock return values and side effects mock_call_api.side_effect = requests.exceptions.HTTPError # Call function with self.assertRaises(Exception): PagureService().request_reviews(user_name='dummy_user') # Validate function calls and response mock_call_api.assert_called_with( url='https://pagure.io/api/0/dummy_user/pull-requests', ssl_verify=True) mock_get_last_comment.assert_not_called() mock_datetime.strptime.assert_not_called() mock_has_new_comments.assert_not_called() mock_check_request_state.assert_not_called() mock_avatar.assert_not_called() mock_PagureReview.assert_not_called()
def test_last_comment(self, mock_datetime, mock_LastComment): """ Tests 'get_last_comment' """ # Set up mock return values and side effects mock_datetime.utcfromtimestamp.return_value = 'mock_date' mock_LastComment.return_value = 'mock_return_value' # Call function response = PagureService().get_last_comment( res={ 'comments': [{ 'date_created': '1', 'comment': 'mock_comment', 'user': { 'name': 'mock_name' } }] }) # Validate function calls and response mock_datetime.utcfromtimestamp.assert_called_with(1) mock_LastComment.assert_called_with(author='mock_name', body='mock_comment', created_at='mock_date') self.assertEqual('mock_return_value', response)
def test_pagure_avatar(self): expected = ( "https://seccdn.libravatar.org/avatar/" "9c9f7784935381befc302fe3c814f9136e7a3" "3953d0318761669b8643f4df55c" ) actual = PagureService._avatar("ralph") self.assertEqual(actual.split("?")[0], expected)
def test_last_comment_no_comments(self, mock_datetime, mock_LastComment): """ Tests 'get_last_comment' with no comments """ response = PagureService().get_last_comment(res={'comments': {}}) mock_datetime.utcfromtimestamp.assert_not_called() mock_LastComment.assert_not_called() self.assertEqual(None, response)
def test_pagure_missing_avatar(self, mock_call_api): base_avatar_url = ( "https://seccdn.libravatar.org/avatar/" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbccccccccccccccc") pagure_avatar_url = base_avatar_url + "?s=16&d=retro" mock_call_api.return_value = { 'user': { 'avatar_url': pagure_avatar_url } } expected_query = "s=64&d=retro" expected = base_avatar_url + "?" + expected_query actual = PagureService()._avatar("ralph") self.assertEqual(actual.split("?")[0], base_avatar_url) actual_query = urllib.parse.parse_qs( urllib.parse.urlparse(actual).query) self.assertEqual(actual_query, urllib.parse.parse_qs(expected_query))
def test_request_reviews_with_repo(self, mock_avatar, mock_PagureReview, mock_has_new_comments, mock_check_request_state, mock_datetime, mock_get_last_comment, mock_call_api): """ Tests 'request_reviews' function with repos and: * no last comment, * check_request_state returns True * no errors, * no namespace """ # Set up mock return values and side effects mock_check_request_state.return_value = True mock_avatar.return_value = 'dummy_avatar' mock_get_last_comment.return_value = 'dummy_last_comment' mock_datetime.utcfromtimestamp.return_value = self.mock_utcfromtimestamp mock_datetime.strptime.return_value = 'mock_strptime_date' mock_PagureReview.return_value = '1' mock_call_api.return_value = mock_pagure.mock_api_call_return_value() # Call function response = PagureService().request_reviews(user_name='dummy_user', repo_name='dummy_repo') # Validate function calls and response mock_call_api.assert_called_with( url='https://pagure.io/api/0/dummy_user/dummy_repo/pull-requests', ssl_verify=True) mock_get_last_comment.assert_called_with( mock_call_api.return_value['requests'][0]) mock_datetime.strptime.assert_called_with('mock_date', '%Y-%m-%d %H:%M:%S.%f') mock_has_new_comments.assert_not_called() mock_check_request_state.assert_called_with('mock_strptime_date', None) mock_avatar.assert_called_with('dummy_user', ssl_verify=True) mock_PagureReview.assert_called_with( user='******', title='dummy_title', url='https://pagure.io/mock_repo_reference/pull-request/mock_id', time='mock_strptime_date', updated_time='mock_strptime_date', comments=3, image='dummy_avatar', last_comment='dummy_last_comment', project_name='mock_repo_reference', project_url='https://pagure.io/mock_repo_reference') self.assertEqual(response, ['1'])
def test_avatar_ValueError(self, mock_call_api, mock_hashlib, mock_urllib): """ Tests '_avatar' function where we get a HTTPError and raise a ValueError """ # Set up mock return values and side effects mock_call_api.side_effect = requests.exceptions.HTTPError # Call function with self.assertRaises(ValueError): PagureService()._avatar(username='******') # Validate function calls and response mock_call_api.assert_called_with( url='https://pagure.io/api/0/user/dummy_user', ssl_verify=True) mock_urllib.parse.urlencode.assert_not_called() mock_hashlib.sha256.assert_not_called() mock_urllib.parse.parse_qs.assert_not_called() mock_urllib.parse.urlunparse.assert_not_called()
def get_git_service(git): """ Returns git service as per requested. Args: git (str): String indicating git service requested. Returns: Returns desired git service """ if git == "github": return GithubService() elif git == "gitlab": return GitlabService() elif git == "pagure": return PagureService() elif git == "gerrit": return GerritService() else: raise ValueError('requested git service %s is not valid' % (git))
def test_request_reviews_with_repo_with_age( self, mock_avatar, mock_PagureReview, mock_has_new_comments, mock_check_request_state, mock_datetime, mock_get_last_comment, mock_call_api): """ Tests 'request_reviews' function with repos and: * no last comment, * check_request_state returns False * no errors, * no namespace """ # Set up mock return values and side effects mock_check_request_state.return_value = False mock_get_last_comment.return_value = 'dummy_last_comment' mock_datetime.utcfromtimestamp.return_value = self.mock_utcfromtimestamp_error mock_datetime.strptime.side_effect = [ ValueError, 'mock_date', 'mock_date' ] mock_PagureReview.return_value = '1' mock_call_api.return_value = mock_pagure.mock_api_call_return_value_age( ) # Call function response = PagureService().request_reviews(user_name='dummy_user', repo_name='dummy_repo', age=self.mock_age) # Validate function calls and response mock_call_api.assert_called_with( url='https://pagure.io/api/0/dummy_user/dummy_repo/pull-requests', ssl_verify=True) mock_get_last_comment.assert_called_with( mock_call_api.return_value['requests'][0]) mock_datetime.strptime.assert_any_call('2019-01-05 12:12:12', '%Y-%m-%d %H:%M:%S') mock_has_new_comments.assert_not_called() mock_check_request_state.assert_called_with('mock_date', self.mock_age) mock_avatar.assert_not_called() mock_PagureReview.assert_not_called() self.assertEqual(response, [])
def test_pagure_avatar(self): expected = ('https://seccdn.libravatar.org/avatar/' '9c9f7784935381befc302fe3c814f9136e7a3' '3953d0318761669b8643f4df55c') actual = PagureService._avatar('ralph') self.assertEqual(actual.split('?')[0], expected)