]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
utils: Add 'bundle_to_mbox' helper
authorStephen Finucane <stephen@that.guru>
Tue, 7 Feb 2017 11:29:59 +0000 (11:29 +0000)
committerStephen Finucane <stephen@that.guru>
Tue, 4 Apr 2017 15:24:03 +0000 (16:24 +0100)
This includes unit tests to validate correct behavior and prevent
regressions.

Signed-off-by: Stephen Finucane <stephen@that.guru>
Reviewed-by: Daniel Axtens <dja@axtens.net>
patchwork/tests/test_bundles.py
patchwork/views/bundle.py
patchwork/views/utils.py

index c18511057f882a44980f3d76848d86e9313e83c3..7f98d36c4be751d37bee80b94df00906f96ca430 100644 (file)
@@ -26,6 +26,7 @@ from django.conf import settings
 from django.core.urlresolvers import reverse
 from django.test import TestCase
 from django.utils.http import urlencode
+from django.utils import six
 from django.utils.six.moves import range
 from django.utils.six.moves import zip
 
@@ -42,6 +43,11 @@ def bundle_url(bundle):
         'username': bundle.owner.username, 'bundlename': bundle.name})
 
 
+def bundle_mbox_url(bundle):
+    return reverse('bundle-mbox', kwargs={
+        'username': bundle.owner.username, 'bundlename': bundle.name})
+
+
 class BundleListTest(TestCase):
 
     def setUp(self):
@@ -120,6 +126,21 @@ class BundleViewTest(BundleTestBase):
             pos = next_pos
 
 
+class BundleMboxTest(BundleTestBase):
+
+    def test_empty_bundle(self):
+        response = self.client.get(bundle_mbox_url(self.bundle))
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response.content, six.b(''))
+
+    def test_non_empty_bundle(self):
+        self.bundle.append_patch(self.patches[0])
+
+        response = self.client.get(bundle_mbox_url(self.bundle))
+        self.assertEqual(response.status_code, 200)
+        self.assertNotEqual(response.content, six.b(''))
+
+
 class BundleUpdateTest(BundleTestBase):
 
     def test_no_action(self):
index 3e8d034989cdeb411cbfd902c69aec16c5a50895..89acb3482ce33ef0dc0aa952dea6925ce8b549ba 100644 (file)
@@ -33,7 +33,7 @@ from patchwork.models import Bundle
 from patchwork.models import BundlePatch
 from patchwork.models import Project
 from patchwork.views import generic_list
-from patchwork.views.utils import patch_to_mbox
+from patchwork.views.utils import bundle_to_mbox
 
 if settings.ENABLE_REST_API:
     from rest_framework.authentication import BasicAuthentication  # noqa
@@ -149,8 +149,7 @@ def bundle_mbox(request, username, bundlename):
     response = HttpResponse(content_type='text/plain')
     response['Content-Disposition'] = \
         'attachment; filename=bundle-%d-%s.mbox' % (bundle.id, bundle.name)
-    response.write('\n'.join(
-        [patch_to_mbox(p) for p in bundle.ordered_patches()]))
+    response.write(bundle_to_mbox(bundle))
 
     return response
 
index c99864767e46bb26cbce5b72bdfe93ea0ea0837e..900480b58049f21b8e363bf2b3fddffd9a3c38a1 100644 (file)
@@ -104,3 +104,15 @@ def patch_to_mbox(patch):
         mail = mail.as_string(True)
 
     return mail
+
+
+def bundle_to_mbox(bundle):
+    """Get an mbox representation of a bundle.
+
+    Arguments:
+        patch: The Bundle object to convert.
+
+    Returns:
+        A string for the mbox file.
+    """
+    return '\n'.join([patch_to_mbox(p) for p in bundle.ordered_patches()])