From 3ee52c29889cf865fe278130f0ee16b6efab26ac Mon Sep 17 00:00:00 2001
From: Jana Cupova <root@localhost>
Date: Jun 01 2022 07:47:11 +0000
Subject: www: archivelist and rpmlist raise error when imageID is unknown


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

---

diff --git a/tests/test_www/test_archivelist.py b/tests/test_www/test_archivelist.py
new file mode 100644
index 0000000..501d231
--- /dev/null
+++ b/tests/test_www/test_archivelist.py
@@ -0,0 +1,68 @@
+import unittest
+import koji
+
+import mock
+from .loadwebindex import webidx
+
+
+class TestArchiveList(unittest.TestCase):
+    def setUp(self):
+        self.get_server = mock.patch.object(webidx, "_getServer").start()
+        self.assert_login = mock.patch.object(webidx, "_assertLogin").start()
+        self.server = mock.MagicMock()
+        self.environ = {
+            'koji.options': {
+                'SiteName': 'test',
+                'KojiFilesURL': 'https://server.local/files',
+            },
+            'koji.currentUser': None
+        }
+        self.buildroot_id = '1'
+        self.image_id = '2'
+
+    def tearDown(self):
+        mock.patch.stopall()
+
+    def test_archivelist_exception_unknown_buildroot_id(self):
+        """Test archivelist function raises exception when buildroot_id is unknown."""
+        self.get_server.return_value = self.server
+        self.server.getBuildroot.return_value = None
+
+        with self.assertRaises(koji.GenericError) as cm:
+            webidx.archivelist(self.environ, type='component', buildrootID=self.buildroot_id)
+        self.assertEqual(str(cm.exception), f'unknown buildroot ID: {self.buildroot_id}')
+
+    def test_archivelist_exception_unknown_archivetype(self):
+        """Test archivelist function raises exception when buildroot type is wrong type."""
+        self.get_server.return_value = self.server
+        self.server.getBuildroot.return_value = {'id': int(self.buildroot_id)}
+
+        with self.assertRaises(koji.GenericError) as cm:
+            webidx.archivelist(self.environ, type='non-exist', buildrootID=self.buildroot_id)
+        self.assertEqual(str(cm.exception), 'unrecognized type of archivelist')
+
+    def test_archivelist_exception_unknown_image_id(self):
+        """Test archivelist function raises exception when image_id is unknown."""
+        self.get_server.return_value = self.server
+        self.server.getArchive.return_value = None
+
+        with self.assertRaises(koji.GenericError) as cm:
+            webidx.archivelist(self.environ, type='image', imageID=self.image_id)
+        self.assertEqual(str(cm.exception), f'unknown image ID: {self.image_id}')
+
+    def test_archivelist_exception_unknown_image_type(self):
+        """Test archivelist function raises exception when image type is unknown."""
+        self.get_server.return_value = self.server
+        self.server.getArchive.return_value = {'id': int(self.image_id)}
+
+        with self.assertRaises(koji.GenericError) as cm:
+            webidx.archivelist(self.environ, type='non-exist', imageID=self.image_id)
+        self.assertEqual(str(cm.exception), 'unrecognized type of image archivelist')
+
+    def test_archivelist_exception_buildroot_and_image_none(self):
+        """Test archivelist function raises exception when buildroot and image is None."""
+        self.get_server.return_value = self.server
+
+        with self.assertRaises(koji.GenericError) as cm:
+            webidx.archivelist(self.environ, type='non-exist')
+        self.assertEqual(str(cm.exception), 'Both buildrootID and imageID are None')
diff --git a/tests/test_www/test_rpmlist.py b/tests/test_www/test_rpmlist.py
new file mode 100644
index 0000000..61e24e5
--- /dev/null
+++ b/tests/test_www/test_rpmlist.py
@@ -0,0 +1,68 @@
+import unittest
+import koji
+
+import mock
+from .loadwebindex import webidx
+
+
+class TestRpmList(unittest.TestCase):
+    def setUp(self):
+        self.get_server = mock.patch.object(webidx, "_getServer").start()
+        self.assert_login = mock.patch.object(webidx, "_assertLogin").start()
+        self.server = mock.MagicMock()
+        self.environ = {
+            'koji.options': {
+                'SiteName': 'test',
+                'KojiFilesURL': 'https://server.local/files',
+            },
+            'koji.currentUser': None
+        }
+        self.buildroot_id = '1'
+        self.image_id = '2'
+
+    def tearDown(self):
+        mock.patch.stopall()
+
+    def test_rpmlist_exception_unknown_buildroot_id(self):
+        """Test rpmlist function raises exception when buildroot_id is unknown."""
+        self.get_server.return_value = self.server
+        self.server.getBuildroot.return_value = None
+
+        with self.assertRaises(koji.GenericError) as cm:
+            webidx.rpmlist(self.environ, type='component', buildrootID=self.buildroot_id)
+        self.assertEqual(str(cm.exception), f'unknown buildroot ID: {self.buildroot_id}')
+
+    def test_rpmlist_exception_wrong_type(self):
+        """Test rpmlist function raises exception when buildroot type is wrong type."""
+        self.get_server.return_value = self.server
+        self.server.getBuildroot.return_value = {'id': int(self.buildroot_id)}
+
+        with self.assertRaises(koji.GenericError) as cm:
+            webidx.rpmlist(self.environ, type='non-exist', buildrootID=self.buildroot_id)
+        self.assertEqual(str(cm.exception), 'unrecognized type of rpmlist')
+
+    def test_rpmlist_exception_unknown_image_id(self):
+        """Test rpmlist function raises exception when image_id is unknown."""
+        self.get_server.return_value = self.server
+        self.server.getArchive.return_value = None
+
+        with self.assertRaises(koji.GenericError) as cm:
+            webidx.rpmlist(self.environ, type='image', imageID=self.image_id)
+        self.assertEqual(str(cm.exception), f'unknown image ID: {self.image_id}')
+
+    def test_rpmlist_exception_unknown_image_type(self):
+        """Test rpmlist function raises exception when image type is unknown."""
+        self.get_server.return_value = self.server
+        self.server.getArchive.return_value = {'id': int(self.image_id)}
+
+        with self.assertRaises(koji.GenericError) as cm:
+            webidx.rpmlist(self.environ, type='non-exist', imageID=self.image_id)
+        self.assertEqual(str(cm.exception), 'unrecognized type of image rpmlist')
+
+    def test_rpmlist_exception_buildroot_and_image_none(self):
+        """Test rpmlist function raises exception when buildroot and image is None."""
+        self.get_server.return_value = self.server
+
+        with self.assertRaises(koji.GenericError) as cm:
+            webidx.rpmlist(self.environ, type='non-exist')
+        self.assertEqual(str(cm.exception), 'Both buildrootID and imageID are None')
diff --git a/www/kojiweb/index.py b/www/kojiweb/index.py
index b2da61c..0f81097 100644
--- a/www/kojiweb/index.py
+++ b/www/kojiweb/index.py
@@ -1915,7 +1915,10 @@ def rpmlist(environ, type, buildrootID=None, imageID=None, start=None, order='nv
 
     elif imageID is not None:
         imageID = int(imageID)
-        values['image'] = server.getArchive(imageID)
+        image = server.getArchive(imageID)
+        values['image'] = image
+        if image is None:
+            raise koji.GenericError('unknown image ID: %i' % imageID)
         # If/When future image types are supported, add elifs here if needed.
         if type == 'image':
             kojiweb.util.paginateMethod(server, values, 'listRPMs',
@@ -1961,14 +1964,17 @@ def archivelist(environ, type, buildrootID=None, imageID=None, start=None, order
             raise koji.GenericError('unrecognized type of archivelist')
     elif imageID is not None:
         imageID = int(imageID)
-        values['image'] = server.getArchive(imageID)
+        image = server.getArchive(imageID)
+        values['image'] = image
+        if image is None:
+            raise koji.GenericError('unknown image ID: %i' % imageID)
         # If/When future image types are supported, add elifs here if needed.
         if type == 'image':
             kojiweb.util.paginateMethod(server, values, 'listArchives', kw={'imageID': imageID},
                                         start=start, dataName='archives', prefix='archive',
                                         order=order)
         else:
-            raise koji.GenericError('unrecognized type of archivelist')
+            raise koji.GenericError('unrecognized type of image archivelist')
     else:
         # It is an error if neither buildrootID and imageID are defined.
         raise koji.GenericError('Both buildrootID and imageID are None')

