From ca5537e178a853aefda87ed457bc2fd3a1b30b40 Mon Sep 17 00:00:00 2001
From: Jana Cupova <root@localhost>
Date: Oct 04 2022 10:10:53 +0000
Subject: Increase CLI unit tests


Fixes: https://pagure.io/koji/issue/3527

---

diff --git a/tests/test_cli/test_download_build.py b/tests/test_cli/test_download_build.py
index 74ad2ff..cde3408 100644
--- a/tests/test_cli/test_download_build.py
+++ b/tests/test_cli/test_download_build.py
@@ -28,7 +28,10 @@ class TestDownloadBuild(utils.CliTestCase):
             'epoch': None,
             'nvr': 'bash-4.4.12-5.fc26',
             'build_id': 1,
+            'id': 123,
+            'name': 'bash',
         }
+        self.b_label = '%(name)s-%(version)s-%(release)s' % self.build_templ
 
         self.getrpminfo = {'arch': 'noarch',
                            'build_id': 1,
@@ -229,6 +232,42 @@ class TestDownloadBuild(utils.CliTestCase):
             exit_code=1
         )
 
+    @mock.patch('koji.buildLabel')
+    def test_download_build_without_all_rpms_with_arches(self, build_label):
+        build_id = '1'
+        arches = ['testarch', 'testarch2']
+        build_label.return_value = self.b_label
+        self.session.getRPM.return_value = self.getrpminfo
+        self.session.getBuild.return_value = self.build_templ
+        self.session.listRPMs.return_value = []
+        self.assert_system_exit(
+            anon_handle_download_build,
+            self.options,
+            self.session,
+            [build_id, '--arch', arches[0], '--arch', arches[1], '--key', self.sigkey],
+            stderr='No %s or %s packages available for %s\n'
+                   % (arches[0], arches[1], self.b_label),
+            activate_session=None,
+            exit_code=1
+        )
+
+    @mock.patch('koji.buildLabel')
+    def test_download_build_without_all_rpms(self, build_label):
+        build_id = '1'
+        build_label.return_value = self.b_label
+        self.session.getRPM.return_value = self.getrpminfo
+        self.session.getBuild.return_value = self.build_templ
+        self.session.listRPMs.return_value = []
+        self.assert_system_exit(
+            anon_handle_download_build,
+            self.options,
+            self.session,
+            [build_id, '--key', self.sigkey],
+            stderr='No packages available for %s\n' % self.b_label,
+            activate_session=None,
+            exit_code=1
+        )
+
     def test_handle_add_volume_help(self):
         self.assert_help(
             anon_handle_download_build,
diff --git a/tests/test_cli/test_list_signed.py b/tests/test_cli/test_list_signed.py
index d501c24..1f8b069 100644
--- a/tests/test_cli/test_list_signed.py
+++ b/tests/test_cli/test_list_signed.py
@@ -1,6 +1,7 @@
 from __future__ import absolute_import
 import mock
 from six.moves import StringIO
+import koji
 
 from koji_cli.commands import handle_list_signed
 from . import utils
@@ -19,6 +20,14 @@ class TestListSigned(utils.CliTestCase):
 %s: error: {message}
 """ % (self.progname, self.progname)
 
+    def __vm(self, result):
+        m = koji.VirtualCall('mcall_method', [], {})
+        if isinstance(result, dict) and result.get('faultCode'):
+            m._result = result
+        else:
+            m._result = (result,)
+        return m
+
     def test_list_signed_help(self):
         self.assert_help(
             handle_list_signed,
@@ -73,7 +82,7 @@ Options:
 
     @mock.patch('koji.pathinfo.build', return_value='fakebuildpath')
     @mock.patch('sys.stdout', new_callable=StringIO)
-    def test_list_signed_rpm_external_build_non_exist_path(self, stdout, pb):
+    def test_list_signed_rpm_with_build_non_exist_path(self, stdout, pb):
         binfo = {'build_id': 1, 'id': 1, 'name': 'test-build', 'release': '1', 'task_id': 8,
                  'version': '1', 'state': 1, 'completion_ts': 1614869140.368759,
                  'owner_name': 'kojiadmin', 'volume_name': 'DEFAULT',
@@ -99,7 +108,7 @@ Options:
     @mock.patch('os.path.exists', return_value=True)
     @mock.patch('koji.pathinfo.build', return_value='fakebuildpath')
     @mock.patch('sys.stdout', new_callable=StringIO)
-    def test_list_signed_rpm_external_build_valid(self, stdout, pb, os_path_exists):
+    def test_list_signed_rpm_with_build_valid(self, stdout, pb, os_path_exists):
         binfo = {'build_id': 1,
                  'id': 1,
                  'name': 'test-build',
@@ -113,7 +122,7 @@ Options:
                  'package_name': 'test-package'}
         list_rpms = [
             {'id': 123, 'name': 'test', 'version': '1.3', 'release': 1, 'arch': 'test-arch',
-             'external_repo_name': 'ext-repo', 'external_repo_id': 456, 'build_id': 1}]
+             'build_id': 1}]
         sigRpm = [{'rpm_id': 123, 'sigkey': 'qwertyuiop'}]
         self.session.getBuild.return_value = binfo
         self.session.listRPMs.return_value = list_rpms
@@ -128,3 +137,66 @@ Options:
         self.session.listRPMs.assert_called_once_with(buildID=binfo['id'])
         self.session.queryRPMSigs.assert_called_once_with(rpm_id=list_rpms[0]['id'])
         self.session.getRPM.assert_not_called()
+
+    @mock.patch('os.path.exists', return_value=True)
+    @mock.patch('koji.pathinfo.build', return_value='fakebuildpath')
+    @mock.patch('sys.stdout', new_callable=StringIO)
+    def test_list_signed_rpm_with_rpm_and_with_key_valid(self, stdout, pb, os_path_exists):
+        rinfo_1 = {'id': 123, 'name': 'test', 'version': '1.3', 'release': 1, 'arch': 'test-arch',
+                   'build_id': 2}
+        rinfo_2 = {'id': 124, 'name': 'test-2', 'version': '1.5', 'release': 1,
+                   'arch': 'test-arch', 'build_id': 3}
+        sigRpm = [{'rpm_id': 124, 'sigkey': 'qwertyuiop'}]
+        self.session.getRPM.side_effect = [rinfo_1, rinfo_2]
+        self.session.queryRPMSigs.return_value = sigRpm
+        handle_list_signed(self.options, self.session, ['--rpm=test-rpm', '--key=qwertyuiop'])
+        path = 'fakebuildpath/data/signed/%s/%s/%s-%s-%s.%s.rpm' \
+               % (sigRpm[0]['sigkey'], rinfo_2['arch'], rinfo_2['name'],
+                  rinfo_2['version'], rinfo_2['release'], rinfo_2['arch'])
+        self.assert_console_message(stdout, '%s\n' % path)
+        self.activate_session_mock.assert_called_once_with(self.session, self.options)
+        self.session.getBuild.assert_called_once_with(rinfo_2['build_id'])
+        self.session.listRPMs.assert_not_called()
+        self.session.queryRPMSigs.assert_called_once_with(sigkey='qwertyuiop',
+                                                          rpm_id=rinfo_1['id'])
+        self.session.getRPM.assert_has_calls([mock.call('test-rpm', strict=True),
+                                              mock.call(rinfo_2['id'])])
+
+    @mock.patch('os.path.exists', return_value=True)
+    @mock.patch('koji.pathinfo.build', return_value='fakebuildpath')
+    @mock.patch('sys.stdout', new_callable=StringIO)
+    def test_list_signed_rpm_tag_valid_and_with_key(self, stdout, pb, os_path_exists):
+        tagged_rpms = [{'id': 100,
+                        'build_id': 1,
+                        'name': 'rpmA',
+                        'version': '0.0.1',
+                        'release': '1.el6',
+                        'arch': 'noarch',
+                        'sigkey': 'sigkey',
+                        'extra': 'extra-value'},
+                       ]
+        tagged_builds = [{'id': 1,
+                          'name': 'packagename',
+                          'version': 'version',
+                          'release': '1.el6',
+                          'nvr': 'n-v-r',
+                          'tag_name': 'tag',
+                          'owner_name': 'owner',
+                          'extra': 'extra-value-2',
+                          'package_name': 'packagename'}]
+        self.session.listTaggedRPMS.return_value = [tagged_rpms, tagged_builds]
+        sigRpm = [{'rpm_id': 100, 'sigkey': 'qwertyuiop'}]
+        mcall = self.session.multicall.return_value.__enter__.return_value
+        mcall.queryRPMSigs.return_value = self.__vm(sigRpm)
+        handle_list_signed(self.options, self.session, ['--tag=test-tag'])
+        path = 'fakebuildpath/data/signed/%s/%s/%s-%s-%s.%s.rpm' \
+               % (sigRpm[0]['sigkey'], tagged_rpms[0]['arch'], tagged_rpms[0]['name'],
+                  tagged_rpms[0]['version'], tagged_rpms[0]['release'], tagged_rpms[0]['arch'])
+        self.assert_console_message(stdout, '%s\n' % path)
+        self.activate_session_mock.assert_called_once_with(self.session, self.options)
+        self.session.listTaggedRPMS.assert_called_once_with(
+            'test-tag', inherit=False, latest=False)
+        self.session.getBuild.assert_not_called()
+        self.session.listRPMs.assert_not_called()
+        self.session.queryRPMSigs.assert_not_called()
+        self.session.getRPM.assert_not_called()

