From 4b0644598749de011324a96c080f17b9f2d00b73 Mon Sep 17 00:00:00 2001
From: Jan Kaluza <jkaluza@redhat.com>
Date: Dec 07 2017 14:00:26 +0000
Subject: Fix traceback in get_repo_urls if Event.compose_id is None. Fix traceback in BaseEvent.__repr__ caused by wrong argument names in ErrataAdvisoryRPMsSignedEvent


---

diff --git a/freshmaker/events.py b/freshmaker/events.py
index 64d6d2e..bc9a66d 100644
--- a/freshmaker/events.py
+++ b/freshmaker/events.py
@@ -267,12 +267,12 @@ class ErrataAdvisoryRPMsSignedEvent(BaseEvent):
     """
     Event when all RPMs in Errata advisory are signed.
     """
-    def __init__(self, msg_id, errata_name, errata_id, security_impact, state):
+    def __init__(self, msg_id, errata_name, errata_id, security_impact, errata_state):
         super(ErrataAdvisoryRPMsSignedEvent, self).__init__(msg_id)
         self.errata_name = errata_name
         self.errata_id = errata_id
         self.security_impact = security_impact
-        self.errata_state = state
+        self.errata_state = errata_state
 
     @property
     def search_key(self):
diff --git a/freshmaker/handlers/__init__.py b/freshmaker/handlers/__init__.py
index faa4ac4..942a5ed 100644
--- a/freshmaker/handlers/__init__.py
+++ b/freshmaker/handlers/__init__.py
@@ -381,6 +381,8 @@ class ContainerBuildHandler(BaseHandler):
         # Use compose ids to get the repofile URLs.
         repo_urls = []
         for compose_id in compose_ids:
+            if not compose_id:
+                continue
             compose = self.odcs_get_compose(compose_id)
             repo_urls.append(compose["result_repofile"])
 
diff --git a/tests/test_handler.py b/tests/test_handler.py
index a8e1a5f..bd67492 100644
--- a/tests/test_handler.py
+++ b/tests/test_handler.py
@@ -140,6 +140,79 @@ class TestContext(TestCase):
         self.assertRaises(ProgrammingError, handler.set_context, "something")
 
 
+class TestGetRepoURLs(TestCase):
+
+    def setUp(self):
+        db.session.remove()
+        db.drop_all()
+        db.create_all()
+        db.session.commit()
+
+        self.db_event = Event.get_or_create(
+            db.session, "msg1", "current_event", ErrataAdvisoryRPMsSignedEvent,
+            released=False)
+
+        self.build = ArtifactBuild.create(
+            db.session, self.db_event, "parent1-1-4", "image",
+            state=ArtifactBuildState.PLANNED, original_nvr="parent1-1-4")
+        db.session.commit()
+
+        def mocked_odcs_get_compose(compose_id):
+            return {
+                "id": compose_id,
+                "result_repofile": "http://localhost/%d.repo" % compose_id,
+            }
+
+        self.patch_odcs_get_compose = patch(
+            "freshmaker.handlers.ContainerBuildHandler.odcs_get_compose")
+        self.odcs_get_compose = self.patch_odcs_get_compose.start()
+        self.odcs_get_compose.side_effect = mocked_odcs_get_compose
+
+    def tearDown(self):
+        db.session.remove()
+        db.drop_all()
+        db.session.commit()
+        self.patch_odcs_get_compose.stop()
+
+    def test_get_repo_urls_no_composes(self):
+        handler = MyHandler()
+        repos = handler.get_repo_urls(self.db_event, self.build)
+        self.assertEqual(repos, [])
+
+    def test_get_repo_urls_only_main_compose(self):
+        self.db_event.compose_id = 1
+        db.session.commit()
+
+        handler = MyHandler()
+        repos = handler.get_repo_urls(self.db_event, self.build)
+        self.assertEqual(repos, ["http://localhost/1.repo"])
+
+    def test_get_repo_urls_only_pulp_compose(self):
+        build_args = json.dumps({
+            "odcs_pulp_compose_id": 15,
+        })
+        self.build.build_args = build_args
+        db.session.commit()
+
+        handler = MyHandler()
+        repos = handler.get_repo_urls(self.db_event, self.build)
+        self.assertEqual(repos, ["http://localhost/15.repo"])
+
+    def test_get_repo_urls_both_pulp_and_main_compose(self):
+        build_args = json.dumps({
+            "odcs_pulp_compose_id": 15,
+        })
+        self.db_event.compose_id = 1
+        self.build.build_args = build_args
+        db.session.commit()
+
+        handler = MyHandler()
+        repos = handler.get_repo_urls(self.db_event, self.build)
+        self.assertEqual(
+            repos,
+            ["http://localhost/1.repo", "http://localhost/15.repo"])
+
+
 class TestAllowBuildBasedOnWhitelist(TestCase):
     """Test BaseHandler.allow_build"""
 
diff --git a/tests/test_models.py b/tests/test_models.py
index 76657f9..9a38421 100644
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -142,7 +142,6 @@ class TestModels(unittest.TestCase):
                 self.assertEqual(build4.state, ArtifactBuildState.BUILD.value)
                 self.assertEqual(build4.state_reason, None)
 
-
     def test_get_unreleased(self):
         event1 = Event.create(db.session, "test_msg_id1", "test", events.TestingEvent)
         event1.state = EventState.COMPLETE

