Add unit tests to scm_request_processor plugin

Add tests for create_new_branch and validate_review_bug.

Signed-off-by: Michal Konečný <mkonecny@redhat.com>
This commit is contained in:
Michal Konečný 2022-03-18 17:38:22 +01:00
parent cc26011ee2
commit 03b815c112
2 changed files with 1215 additions and 37 deletions

File diff suppressed because it is too large Load diff

View file

@ -623,7 +623,7 @@ class SCMRequestProcessor(ToddlerBase):
if create_git_branch: if create_git_branch:
with TemporaryDirectory(dir=self.temp_dir) as tmp_dir: with TemporaryDirectory(dir=self.temp_dir) as tmp_dir:
repo = git.clone_repo(dist_git_url, tmp_dir) git_repo = git.clone_repo(dist_git_url, tmp_dir)
default_branch = self.dist_git.get_default_branch(namespace, repo) default_branch = self.dist_git.get_default_branch(namespace, repo)
if not default_branch: if not default_branch:
self.pagure_io.close_issue( self.pagure_io.close_issue(
@ -633,7 +633,7 @@ class SCMRequestProcessor(ToddlerBase):
reason="Invalid" reason="Invalid"
) )
return return
commit = repo.first_commit(default_branch) commit = git_repo.first_commit(default_branch)
self.dist_git.new_branch(namespace, repo, branch_name, from_commit=commit) self.dist_git.new_branch(namespace, repo, branch_name, from_commit=commit)
new_branch_comment = ('The branch was created in PDC and git. It ' new_branch_comment = ('The branch was created in PDC and git. It '
'may take up to 10 minutes before you have ' 'may take up to 10 minutes before you have '
@ -650,7 +650,7 @@ class SCMRequestProcessor(ToddlerBase):
reason="Processed" reason="Processed"
) )
if bug_id: if bug_id:
bugzilla_system.comment_on_bug(bug_id, new_repo_comment) bugzilla_system.comment_on_bug(bug_id, new_branch_comment)
def validate_review_bug(self, bug_id: str, pkg: str, branch: str, def validate_review_bug(self, bug_id: str, pkg: str, branch: str,
namespace: str = 'rpms', pagure_user: Optional[str] = None namespace: str = 'rpms', pagure_user: Optional[str] = None
@ -669,7 +669,7 @@ class SCMRequestProcessor(ToddlerBase):
pagure_user: a string of the requesting user's Pagure username pagure_user: a string of the requesting user's Pagure username
""" """
try: try:
bug = bugzilla.get_bug(bug_id) bug = bugzilla_system.get_bug(bug_id)
except Exception as error: except Exception as error:
raise ValidationError( raise ValidationError(
'The Bugzilla bug could not be verified. The following ' 'The Bugzilla bug could not be verified. The following '
@ -680,7 +680,7 @@ class SCMRequestProcessor(ToddlerBase):
# Check that the bug is valid # Check that the bug is valid
bz_proper_component = self.pagure_namespace_to_component.get(namespace) bz_proper_component = self.pagure_namespace_to_component.get(namespace)
bz_proper_products = self.pagure_namespace_to_product.get(namespace) bz_proper_products = self.pagure_namespace_to_product.get(namespace)
if namespace == 'rpms' and (branch != 'rawhide' or branch != 'main'): if namespace == 'rpms' and (branch != 'rawhide' and branch != 'main'):
if re.match(EPEL_REGEX, branch) and bug.product != 'Fedora EPEL': if re.match(EPEL_REGEX, branch) and bug.product != 'Fedora EPEL':
raise ValidationError( raise ValidationError(
'The Bugzilla bug is for "{0}" but the ' 'The Bugzilla bug is for "{0}" but the '
@ -699,14 +699,14 @@ class SCMRequestProcessor(ToddlerBase):
elif bug.assigned_to in ['', None, 'nobody@fedoraproject.org']: elif bug.assigned_to in ['', None, 'nobody@fedoraproject.org']:
raise ValidationError( raise ValidationError(
'The Bugzilla bug provided is not assigned to anyone') 'The Bugzilla bug provided is not assigned to anyone')
if pagure_user: fas_submitter = fedora_account.get_user_by_email(bug.creator)
fas_user = fedora_account.get_user_by_email(bug.creator) if not fas_submitter:
if not fas_user:
raise ValidationError( raise ValidationError(
'The Bugzilla review bug creator could not be found in ' 'The Bugzilla review bug creator could not be found in '
'FAS. Make sure your FAS email address is the same as in ' 'FAS. Make sure your FAS email address is the same as in '
'Bugzilla.') 'Bugzilla.')
if fas_user['username'] != pagure_user: if pagure_user:
if fas_submitter['username'] != pagure_user:
raise ValidationError('The Bugzilla review bug creator ' raise ValidationError('The Bugzilla review bug creator '
'didn\'t match the requester in Pagure.') 'didn\'t match the requester in Pagure.')
# Check if the review was approved and by whom # Check if the review was approved and by whom
@ -726,18 +726,10 @@ class SCMRequestProcessor(ToddlerBase):
'is approved by a user "{0}" that is ' 'is approved by a user "{0}" that is '
'not a packager or FAS check failed'.format( 'not a packager or FAS check failed'.format(
bug.assigned_to)) bug.assigned_to))
fas_submitter = self.get_fas_user_by_bz_email(bug.creator) if not fedora_account.user_member_of(fas_submitter, 'packager'):
if not fas_submitter:
raise ValidationError(
'The email address "{0}" of the Bugzilla submitter '
'is not tied to a user in FAS. Group membership '
'can\'t be validated.'.format(bug.creator))
if not fedora_acount.user_member_of(fas_submitter, 'packager'):
raise ValidationError('The Bugzilla reporter "{0}"' raise ValidationError('The Bugzilla reporter "{0}"'
'is not a packager'.format(bug.creator)) 'is not a packager'.format(bug.creator))
# Setter will be an empty string and emails will not be shown
# if the user is not logged in. This is why we check for
# authentication here.
if flag['setter'] == bug.creator: if flag['setter'] == bug.creator:
error = ('The Bugzilla bug\'s review is approved ' error = ('The Bugzilla bug\'s review is approved '
'by the person creating the bug. This is ' 'by the person creating the bug. This is '
@ -746,10 +738,10 @@ class SCMRequestProcessor(ToddlerBase):
assigned_to_emails = [bug.assigned_to] assigned_to_emails = [bug.assigned_to]
assigned_to_user = fedora_account.get_user_by_username(bug.assigned_to) if "emails" in fas_reviewer:
if assigned_to_user:
assigned_to_emails.append( assigned_to_emails.append(
assigned_to_user["emails"]) fas_reviewer["emails"])
if flag['setter'] not in assigned_to_emails: if flag['setter'] not in assigned_to_emails:
raise ValidationError('The review is not approved by ' raise ValidationError('The review is not approved by '
'the assignee of the Bugzilla ' 'the assignee of the Bugzilla '
@ -757,9 +749,8 @@ class SCMRequestProcessor(ToddlerBase):
update_dt = flag.get('modification_date') update_dt = flag.get('modification_date')
if update_dt: if update_dt:
dt = datetime.strptime( dt = arrow.get(update_dt, 'YYYY-MM-DDTHH-mm-ssZ')
update_dt.value, '%Y%m%dT%H:%M:%S') delta = arrow.utcnow().date() - dt.date()
delta = datetime.utcnow().date() - dt.date()
if delta.days > 60: if delta.days > 60:
raise ValidationError('The Bugzilla bug\'s review ' raise ValidationError('The Bugzilla bug\'s review '
'was approved over 60 days ago') 'was approved over 60 days ago')