# This file is part of fedimg.
# Copyright (C) 2014 Red Hat, Inc.
#
# fedimg is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# fedimg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with fedimg; if not, see http://www.gnu.org/licenses,
# or write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Authors:  David Gay <dgay@redhat.com>
#

import mock
import requests
import unittest

import fedmsg

import fedimg.consumers


class TestKojiConsumer(unittest.TestCase):
    """ Fedimg should pick up on completed createImage Koji tasks and kick off
    the upload process if they produce an image we want to upload. """

    @classmethod
    def setUpClass(cls):
        import fedmsg.config
        import fedmsg.meta

        config = fedmsg.config.load_config([], None)
        fedmsg.meta.make_processors(**config)
        cls.fedmsg_config = config

    def setUp(self):
        class FakeHub(object):
            config = self.fedmsg_config

            def subscribe(*args, **kwargs):
                pass

        fedimg.consumers.KojiConsumer._initialized = True  # a lie
        self.consumer = fedimg.consumers.KojiConsumer(FakeHub())

    def tearDown(self):
        pass

    @mock.patch('fedimg.uploader.upload')
    def test_consume(self, upload):
        # I just pull the msg from the web for now
        # I will later put it in a text file or something I guess?
        koji_msg_id = '2014-e9065d79-8975-4b3d-897e-fcc807ba95dd'
        datagrepper_url = 'https://apps.fedoraproject.org/datagrepper/id?id={0}'.format(koji_msg_id)
        resp = requests.get(datagrepper_url)
        # Need to wrap the message in a body dict to emulate how an actual
        # consumed fedmsg would come through.
        msg = {'body': {'msg': resp.json()['msg']}}

        self.consumer.consume(msg)

if __name__ == '__main__':
    unittest.main()

@@ -1822,17 +1822,18 @@
  for now
+.
 %0A
-
         
@@ -1891,16 +1891,80 @@
  guess?%0A
+        # Below is an image task with two createImage children.%0A
         
@@ -2167,16 +2167,17 @@
 er_url)%0A
+%0A
         
@@ -2342,16 +2342,16 @@
 sg'%5D%7D%7D%0A%0A
-
         
@@ -2376,16 +2376,131 @@
 ume(msg)
+%0A        # the list seems to always be newest task first, so:%0A        upload.assert_called_with(%5B7577982, 7577981%5D)
 %0A%0Aif __n

improve test for consume function and actually add assertion