Repository: fedora-infra/bodhi

Issue:
Modify the Python bindings to work with the new multi-type REST API
We need to modify the Python bindings so they work with the new REST API that will results from #1325.


Discussion:

Resolving patch:
diff --git a/bodhi/client/bindings.py b/bodhi/client/bindings.py
--- a/bodhi/client/bindings.py
+++ b/bodhi/client/bindings.py
@@ -204,6 +204,7 @@ def request(self, update, request):
     def query(self, **kwargs):
         """ Query bodhi for a list of updates.
 
+        :kwarg content_type: A content type (rpm, module) to limit the query to.
         :kwarg releases: A list of releases that you wish to query updates for.
         :kwarg status: The update status (``pending``, ``testing``, ``stable``,
             ``obsolete``, ``unpushed``, ``processing``)
@@ -494,9 +495,9 @@ def update_str(self, update, minimal=False):
             val = ""
             date = update['date_pushed'] and update['date_pushed'].split()[0] \
                 or update['date_submitted'].split()[0]
-            val += ' %-43s  %-11s  %-8s  %10s ' % (update['builds'][0]['nvr'],
-                                                   update['type'],
-                                                   update['status'], date)
+            val += ' %-37s %-6s %-11s  %-8s  %10s' % (
+                update['builds'][0]['nvr'], update['content_type'], update['type'],
+                update['status'], date)
             for build in update['builds'][1:]:
                 val += '\n %s' % build['nvr']
             return val
@@ -504,48 +505,50 @@ def update_str(self, update, minimal=False):
             textwrap.wrap(update['title'].replace(',', ', '), width=80,
                           initial_indent=' ' * 5, subsequent_indent=' ' * 5)), '=' * 80)
         if update['alias']:
-            val += "  Update ID: %s\n" % update['alias']
-        val += """    Release: %s
-     Status: %s
-       Type: %s
-      Karma: %d
-  Autokarma: %s  [%s, %s]""" % (
-            update['release']['long_name'], update['status'], update['type'], update['karma'],
-            update['autokarma'], update['unstable_karma'], update['stable_karma'])
+            val += "   Update ID: %s\n" % update['alias']
+        val += """Content Type: %s
+     Release: %s
+      Status: %s
+        Type: %s
+       Karma: %d
+   Autokarma: %s  [%s, %s]""" % (
+            update['content_type'], update['release']['long_name'], update['status'],
+            update['type'], update['karma'], update['autokarma'], update['unstable_karma'],
+            update['stable_karma'])
         if update['request'] is not None:
-            val += "\n    Request: %s" % update['request']
+            val += "\n     Request: %s" % update['request']
         if len(update['bugs']):
             bugs = ''
             i = 0
             for bug in update['bugs']:
-                bugstr = '%s%s - %s\n' % (i and ' ' * 11 + ': ' or '',
+                bugstr = '%s%s - %s\n' % (i and ' ' * 12 + ': ' or '',
                                           bug['bug_id'], bug['title'])
                 bugs += '\n'.join(textwrap.wrap(bugstr, width=67,
                                                 subsequent_indent=' ' * 11 + ': ')) + '\n'
                 i += 1
             bugs = bugs[:-1]
-            val += "\n       Bugs: %s" % bugs
+            val += "\n        Bugs: %s" % bugs
         if update['notes']:
             notes = textwrap.wrap(update['notes'], width=67,
-                                  subsequent_indent=' ' * 11 + ': ')
-            val += "\n      Notes: %s" % '\n'.join(notes)
+                                  subsequent_indent=' ' * 12 + ': ')
+            val += "\n       Notes: %s" % '\n'.join(notes)
         val += """
-  Submitter: %s
-  Submitted: %s\n""" % (update['user']['name'], update['date_submitted'])
+   Submitter: %s
+   Submitted: %s\n""" % (update['user']['name'], update['date_submitted'])
         if len(update['comments']):
-            val += "   Comments: "
+            val += "    Comments: "
             comments = []
             for comment in update['comments']:
                 if comment['anonymous']:
                     anonymous = " (unauthenticated)"
                 else:
                     anonymous = ""
-                comments.append("%s%s%s - %s (karma %s)" % (' ' * 13,
+                comments.append("%s%s%s - %s (karma %s)" % (' ' * 14,
                                 comment['user']['name'], anonymous,
                                 comment['timestamp'], comment['karma']))
                 if comment['text']:
-                    text = textwrap.wrap(comment['text'], initial_indent=' ' * 13,
-                                         subsequent_indent=' ' * 13, width=67)
+                    text = textwrap.wrap(comment['text'], initial_indent=' ' * 14,
+                                         subsequent_indent=' ' * 14, width=67)
                     comments.append('\n'.join(text))
             val += '\n'.join(comments).lstrip() + '\n'
         if update['alias']:


Test patch:
diff --git a/bodhi/tests/client/__init__.py b/bodhi/tests/client/__init__.py
--- a/bodhi/tests/client/__init__.py
+++ b/bodhi/tests/client/__init__.py
@@ -99,6 +99,7 @@
     u'updates': [{
         u'alias': u'FEDORA-2017-c95b33872d',
         u'autokarma': True,
+        u'content_type': u'rpm',
         u'bugs': [{
             u'bug_id': 1420605,
             u'feedback': [],
@@ -479,31 +480,33 @@
         u'long_name': u'Fedora EPEL 7', u'state': u'current', u'version': u'7',
         u'override_tag': u'epel7-override', u'branch': u'epel7', u'id_prefix': u'FEDORA-EPEL',
         u'pending_testing_tag': u'epel7-testing-pending', u'stable_tag': u'epel7',
-        u'candidate_tag': u'epel7-testing-candidate'}), u'date_stable': u'2016-10-21 13:23:01'})
+        u'candidate_tag': u'epel7-testing-candidate'}), u'date_stable': u'2016-10-21 13:23:01',
+    u'content_type': u'rpm'})
 
 
 # EXAMPLE_UPDATE_MUNCH is expected to generate this output in update_str
 EXPECTED_UPDATE_OUTPUT = u"""================================================================================
      bodhi-2.2.4-1.el7
 ================================================================================
-  Update ID: FEDORA-EPEL-2016-3081a94111
-    Release: Fedora EPEL 7
-     Status: stable
-       Type: bugfix
-      Karma: 0
-  Autokarma: True  [-3, 3]
-      Notes: Update to 2.2.4. Release notes available at https://github.com
-           : /fedora-infra/bodhi/releases/tag/2.2.4
-  Submitter: bowlofeggs
-  Submitted: 2016-10-05 18:10:22
-   Comments: bodhi - 2016-10-05 18:10:22 (karma 0)
-             This update has been submitted for testing by
-             bowlofeggs.
-             bodhi - 2016-10-05 18:10:27 (karma 0)
-             This update has obsoleted [bodhi-2.2.3-1.el7](https://
-             bodhi.fedoraproject.org/updates/FEDORA-
-             EPEL-2016-a0eb4cc41f), and has inherited its bugs and
-             notes.
+   Update ID: FEDORA-EPEL-2016-3081a94111
+Content Type: rpm
+     Release: Fedora EPEL 7
+      Status: stable
+        Type: bugfix
+       Karma: 0
+   Autokarma: True  [-3, 3]
+       Notes: Update to 2.2.4. Release notes available at https://github.com
+            : /fedora-infra/bodhi/releases/tag/2.2.4
+   Submitter: bowlofeggs
+   Submitted: 2016-10-05 18:10:22
+    Comments: bodhi - 2016-10-05 18:10:22 (karma 0)
+              This update has been submitted for testing by
+              bowlofeggs.
+              bodhi - 2016-10-05 18:10:27 (karma 0)
+              This update has obsoleted [bodhi-2.2.3-1.el7](https:/
+              /bodhi.fedoraproject.org/updates/FEDORA-
+              EPEL-2016-a0eb4cc41f), and has inherited its bugs and
+              notes.
 
   http://example.com/tests/updates/FEDORA-EPEL-2016-3081a94111
 """
diff --git a/bodhi/tests/client/test_bindings.py b/bodhi/tests/client/test_bindings.py
--- a/bodhi/tests/client/test_bindings.py
+++ b/bodhi/tests/client/test_bindings.py
@@ -783,6 +783,44 @@ def test_other_ServerError(self, send_request, __init__):
 
 class TestBodhiClient_update_str(unittest.TestCase):
     """This test contains tests for BodhiClient.update_str."""
+    @mock.patch.dict(
+        client_test_data.EXAMPLE_UPDATE_MUNCH,
+        {'bugs': [{'bug_id': 1234, 'title': 'it broke'}, {'bug_id': 1235, 'title': 'halp'}]})
+    def test_bugs(self):
+        """Ensure correct output when there are bugs on the update."""
+        client = bindings.BodhiClient()
+        client.base_url = 'http://example.com/tests/'
+
+        text = client.update_str(client_test_data.EXAMPLE_UPDATE_MUNCH)
+
+        self.assertEqual(
+            text,
+            client_test_data.EXPECTED_UPDATE_OUTPUT.replace(
+                '[-3, 3]', '[-3, 3]\n        Bugs: 1234 - it broke\n            : 1235 - halp'))
+
+    def test_minimal(self):
+        """Ensure correct output when minimal is True."""
+        client = bindings.BodhiClient()
+
+        text = client.update_str(client_test_data.EXAMPLE_UPDATE_MUNCH, minimal=True)
+
+        expected_output = (' bodhi-2.2.4-1.el7                     rpm    bugfix       stable    '
+                           '2016-10-21')
+        self.assertEqual(text, expected_output)
+
+    @mock.patch.dict(client_test_data.EXAMPLE_UPDATE_MUNCH, {'request': 'stable'})
+    def test_request_stable(self):
+        """Ensure correct output when the update is request stable."""
+        client = bindings.BodhiClient()
+        client.base_url = 'http://example.com/tests/'
+
+        text = client.update_str(client_test_data.EXAMPLE_UPDATE_MUNCH)
+
+        self.assertEqual(
+            text,
+            client_test_data.EXPECTED_UPDATE_OUTPUT.replace(
+                '[-3, 3]', '[-3, 3]\n     Request: stable'))
+
     def test_with_autokarma_set(self):
         """
         Ensure correct operation when autokarma is True, and stable/unstable karmas are set.