def test_request_reviews__token(self): github = GitlabService() with self.assertRaises(GitlabConnectionError) as context: github.request_reviews(user_name=self.config['user_name'], token=self.config['token'], host=self.config['host']) self.assertTrue('NewConnectionError' in str(context.exception))
def test_get_last_comment(self, mock_mr): gitlab = GitlabService() now = datetime.datetime.now() mock_mr.notes.list.return_value = [ test_mock.FakeProjectMergeRequestNote( system=True, author="Gitlab", created_at=now.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), body="System comment", ), test_mock.FakeProjectMergeRequestNote( system=False, author="user", created_at=now.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), body="last comment by user", ), test_mock.FakeProjectMergeRequestNote( system=False, author="user2", created_at=(now - datetime.timedelta(minutes=1)).strftime( "%Y-%m-%dT%H:%M:%S.%fZ" ), body="user comment", ), ] last_comment = gitlab.get_last_comment(mock_mr) self.assertEqual( last_comment, LastComment( author="user", body="last comment by user", created_at=now ), )
def test_request_reviews_with_repo_success(self, mock_get_reviews, mock_gitlab): """ Tests 'request_reviews' function where we have repos and no errors """ # Set up mock return values and side effects mock_gitlab_instance = MagicMock(name='mock_gitlab_instance') mock_gitlab_instance.projects.get.return_value = 'dummy_project' mock_gitlab.return_value = mock_gitlab_instance mock_get_reviews.return_value = '1' # Call function response = GitlabService().request_reviews( user_name='dummy_user', token='dummy_token', host='dummy.com', ssl_verify=True, repo_name='dummy_repo' ) # Validate function calls and response mock_gitlab.assert_called_with('dummy.com', 'dummy_token', ssl_verify=True) mock_gitlab_instance.groups.get.assert_not_called() mock_gitlab_instance.auth.assert_called_once() mock_gitlab_instance.projects.get.assert_called_with('dummy_user/dummy_repo') mock_get_reviews.assert_called_with( uname='dummy_user', project='dummy_project', age=None, show_last_comment=None ) self.assertEqual(['1'], response)
def test_request_reviews_GitlabGetError_with_repo(self, mock_get_reviews, mock_gitlab): """ Tests 'request_reviews' function where we have repos and gl.projects.get throws an exception """ # Set up mock return values and side effects mock_gitlab_instance = MagicMock(name='mock_gitlab_instance') mock_gitlab_instance.projects.get.side_effect = GitlabGetError mock_gitlab.return_value = mock_gitlab_instance # Call function with self.assertRaises(Exception): GitlabService().request_reviews( user_name='dummy_user', token='dummy_token', host='dummy.com', ssl_verify=True, repo_name='dummy_repo' ) # Validate function calls and response mock_gitlab.assert_called_with('dummy.com', 'dummy_token', ssl_verify=True) mock_gitlab_instance.groups.get.assert_not_called() mock_gitlab_instance.auth.assert_called_once() mock_gitlab_instance.projects.get.assert_called_with('dummy_user/dummy_repo') mock_get_reviews.assert_not_called()
def test_request_reviews_with_repo(self, mock_auth, mock_gitlab, mock_get_reviews): mock_gitlab().projects.get.side_effect = test_mock.mock_projects_get_() res = GitlabService().request_reviews(user_name=self.config['user_name'], repo_name=self.config['repo_name'], token=self.config['token'], host=self.config['host']) self.assertEqual([self.config['msg']], res)
def test_request_reviews_token(self): with self.assertRaises(Exception) as context: GitlabService().request_reviews(user_name=self.config['user_name'], token=self.config['token'], host=self.config['host']) self.assertTrue('NewConnectionError' in str(context.exception), msg=context.exception)
def test_get_reviews_GitlabListError(self, mock_GitlabReview, mock_has_new_comments, mock_check_request_state, mock_get_last_comment): """ Test 'get_reviews' where getting merge requests throws GitlabListError """ # Set up mock return values and side effects self.mock_project.mergerequests.list.side_effect = GitlabListError() # Call function response = GitlabService().get_reviews( uname='dummy_user', project=self.mock_project, ) # Validate function calls and response self.mock_project.mergerequests.list.assert_called_with( project_id=1, state='opened' ) mock_GitlabReview.assert_not_called() mock_has_new_comments.assert_not_called() mock_check_request_state.assert_not_called() mock_get_last_comment.assert_not_called() self.assertEqual(response, [])
def test_request_reviews_no_group_no_repo(self, mock_get_reviews, mock_gitlab): """ Test 'request_reviews' function where gl.groups.get returns None """ # Set up mock return values and side effects mock_gitlab_instance = MagicMock(name='mock_gitlab_instance') mock_gitlab_instance.projects.get.return_value = 'dummy_project' mock_gitlab_instance.groups.get.return_value = None mock_gitlab.return_value = mock_gitlab_instance # Call function with self.assertRaises(Exception): GitlabService().request_reviews( user_name='dummy_user', token='dummy_token', host='dummy.com', ssl_verify=True ) # Validate function calls and response mock_gitlab.assert_called_with('dummy.com', 'dummy_token', ssl_verify=True) mock_gitlab_instance.auth.assert_called_once() mock_get_reviews.assert_not_called() mock_gitlab_instance.groups.get.assert_called_with('dummy_user') mock_gitlab_instance.projects.get.assert_not_called()
def test_request_reviews_groups_search(self, mock_auth, mock_gitlab): with self.assertRaises(Exception) as context: mock_gitlab().groups.get.return_value = [] GitlabService().request_reviews(user_name=self.config['user_name'], token=self.config['token'], host=self.config['host']) msg = 'Invalid user/group name: %s' % self.config['user_name'] self.assertTrue(msg in str(context.exception))
def test_request_reviews_with_repo(self, mock_projects_get_, mock_gitlab_get_reviews, mock_auth): res = GitlabService().request_reviews( user_name=self.config['user_name'], repo_name=self.config['repo_name'], token=self.config['token'], host=self.config['host']) self.assertEqual([self.config['msg']], res)
def test_request_reviews_projects_get(self, mock_projects_get, mock_auth): with self.assertRaises(Exception) as context: GitlabService().request_reviews(user_name=self.config['user_name'], repo_name=self.config['repo_name'], token=self.config['token'], host=self.config['host']) msg = 'Project %s not found for user %s' % (self.config['repo_name'], self.config['user_name']) self.assertTrue(msg in str(context.exception))
def test_request_reviews_with_repo(self, mock_auth, mock_gitlab, mock_get_reviews): mock_gitlab().projects.get.side_effect = test_mock.mock_projects_get_() res = GitlabService().request_reviews( user_name=self.config["user_name"], repo_name=self.config["repo_name"], token=self.config["token"], host=self.config["host"], ) self.assertEqual([self.config["msg"]], res)
def test_request_reviews_token(self): with self.assertRaises(Exception) as context: GitlabService().request_reviews( user_name=self.config["user_name"], token=self.config["token"], host=self.config["host"], ) self.assertTrue( "NewConnectionError" in str(context.exception), msg=context.exception, )
def test_get_reviews_ValueError(self, mock_GitlabReview, mock_has_new_comments, mock_check_request_state, mock_get_last_comment): """ Test 'get_reviews' where we have no age and no last comment and datetime.strptime throws a ValueError """ # Set up mock return values and side effects self.mock_mr.updated_at = '2010-10-04T03:41:22Z' self.mock_mr.created_at = '2010-10-04T03:41:22Z' expected_date = datetime.strptime( '2010-10-04T03:41:22Z', '%Y-%m-%dT%H:%M:%SZ') self.mock_project.mergerequests.list.return_value = [self.mock_mr] mock_get_last_comment.return_value = self.mock_last_comment mock_check_request_state.return_value = True mock_GitlabReview.return_value = "Successful Call!" mock_GitlabReview.logo = 'dummy_logo' # Call function response = GitlabService().get_reviews( uname='dummy_user', project=self.mock_project, ) # Validate function calls and response self.mock_project.mergerequests.list.assert_called_with( project_id=1, state='opened' ) mock_check_request_state.assert_called_with( expected_date, None, ) mock_get_last_comment.assert_called_with( self.mock_mr ) mock_GitlabReview.assert_called_with( user='******', title='dummy_title', url='dummy_url', time=expected_date, updated_time=expected_date, comments=1, image='dummy_logo', last_comment=self.mock_last_comment, project_name='dummy_project', project_url='dummy_url' ) mock_has_new_comments.assert_not_called() self.assertEqual(response, ["Successful Call!"])
def test_get_last_comment_with_note_system(self, mock_LastComment): """ Test 'get_last_comment' function with all notes in mr.notes.list() not having note.system """ # Set up mock return values and side effects mock_merge_requests = MagicMock() mock_merge_requests.notes.list().return_value = ['test'] # Call function response = GitlabService().get_last_comment(mock_merge_requests) # Validate function calls and response mock_LastComment.assert_not_called() self.assertEqual(None, response)
def test_request_reviews_projects_get(self, mock_auth, mock_gitlab): with self.assertRaises(Exception) as context: mock_gitlab().projects.get.side_effect = ( test_mock.mock_projects_get()) GitlabService().request_reviews( user_name=self.config["user_name"], repo_name=self.config["repo_name"], token=self.config["token"], host=self.config["host"], ) msg = "Project %s not found for user %s" % ( self.config["repo_name"], self.config["user_name"], ) self.assertTrue(msg in str(context.exception), msg=context.exception)
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_get_reviews_no_age_with_last_comment(self, mock_GitlabReview, mock_has_new_comments, mock_check_request_state, mock_get_last_comment): """ Test 'get_reviews' where we have no age and with last comment """ # Set up mock return values and side effects expected_date = datetime.strptime( self.mock_mr.updated_at, '%Y-%m-%dT%H:%M:%S.%fZ') self.mock_project.mergerequests.list.return_value = [self.mock_mr] mock_get_last_comment.return_value = self.mock_last_comment mock_check_request_state.return_value = True mock_GitlabReview.return_value = "Successful Call!" mock_GitlabReview.logo = 'dummy_logo' # Call function response = GitlabService().get_reviews( uname='dummy_user', project=self.mock_project, show_last_comment=True ) # Validate function calls and response self.mock_project.mergerequests.list.assert_called_with( project_id=1, state='opened' ) mock_check_request_state.assert_called_with( expected_date, None, ) mock_get_last_comment.assert_called_with( self.mock_mr ) mock_GitlabReview.assert_not_called() mock_has_new_comments.assert_called_with( 'dummy_created_at', True ) self.assertEqual(response, [])
def test_get_reviews_with_age_no_last_comment(self, mock_GitlabReview, mock_has_new_comments, mock_check_request_state, mock_get_last_comment): """ Test 'get_reviews' where we have a age and no last comment """ # Set up mock return values and side effects expected_date = datetime.strptime( self.mock_mr.updated_at, '%Y-%m-%dT%H:%M:%S.%fZ') self.mock_project.mergerequests.list.return_value = [self.mock_mr] mock_age = MagicMock() mock_age.state = 'mock_state ' mock_get_last_comment.return_value = 'last_comment' mock_check_request_state.return_value = False mock_GitlabReview.logo = 'dummy_logo' # Call function response = GitlabService().get_reviews( uname='dummy_user', project=self.mock_project, age=mock_age ) # Validate function calls and response self.mock_project.mergerequests.list.assert_called_with( project_id=1, state='opened' ) mock_check_request_state.assert_called_with( expected_date, mock_age, ) mock_get_last_comment.assert_called_with( self.mock_mr ) mock_GitlabReview.assert_not_called() mock_has_new_comments.assert_not_called() self.assertEqual(response, [])
def test_get_last_comment_no_note_system(self, mock_LastComment): """ Test 'get_last_comment' function with all notes in mr.notes.list() having note.system """ # Set up mock return values and side effects mock_merge_requests = MagicMock(name="mock_merge_requests") created_at_expected_response = datetime.strptime( self.mock_merge_request.created_at, '%Y-%m-%dT%H:%M:%S.%fZ') mock_LastComment.return_value = 'Successful Call!' mock_merge_requests.notes.list.return_value = [self.mock_merge_request] # Call function response = GitlabService().get_last_comment(mock_merge_requests) # Validate function calls and response mock_LastComment.assert_called_with( author='dummy_author', body='dummy_body', created_at=created_at_expected_response ) self.assertEqual('Successful Call!', response)
def test_request_reviews_SSLError_no_repo(self, mock_get_reviews, mock_gitlab): """ Test 'request_reviews' function where there is an SSL error and no repos """ # Set up mock return values and side effects mock_gitlab_group = MagicMock(name='mock_gitlab_group') mock_gitlab_group.id = 1 mock_gitlab_group_projects = MagicMock() mock_gitlab_group_projects.projects.list.return_value = [mock_gitlab_group] mock_gitlab_instance = MagicMock(name='mock_gitlab_instance') mock_gitlab_instance.groups.get.return_value = mock_gitlab_group_projects mock_gitlab_instance.projects.get.return_value = 'dummy_project' mock_gitlab_instance.auth.side_effect = SSLError mock_get_reviews.return_value = '1' mock_gitlab.return_value = mock_gitlab_instance # Call function response = GitlabService().request_reviews( user_name='dummy_user', token='dummy_token', host='dummy.com', ssl_verify=True ) # Validate function calls and response mock_gitlab.assert_called_with('dummy.com', 'dummy_token', ssl_verify=True) mock_gitlab_instance.auth.assert_called_once() mock_gitlab_instance.groups.get.assert_called_with('dummy_user') mock_gitlab_instance.projects.get.assert_called_with(1) mock_get_reviews.assert_called_with( uname='dummy_user', project='dummy_project', age=None) self.assertEqual(['1'], response)