]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/tests/patchparser.py
Fix django-1.6 incompatibilities
[patchwork] / apps / patchwork / tests / patchparser.py
index f6909ce9e458608300f060b09a3c9d316267ebb3..0496a69f231e21f5e0dda86c8b247560c5a7a8ea 100644 (file)
 # along with Patchwork; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-import unittest
 import os
 from email import message_from_string
-from patchwork.models import Project, Person, Patch, Comment
-from patchwork.tests.utils import read_patch, create_email, defaults
+from django.test import TestCase
+from patchwork.models import Project, Person, Patch, Comment, State, \
+         get_default_initial_patch_state
+from patchwork.tests.utils import read_patch, read_mail, create_email, \
+         defaults, create_user
 
 try:
     from email.mime.text import MIMEText
@@ -29,7 +31,7 @@ except ImportError:
     # Python 2.4 compatibility
     from email.MIMEText import MIMEText
 
-class PatchTest(unittest.TestCase):
+class PatchTest(TestCase):
     default_sender = defaults.sender
     default_subject = defaults.subject
     project = defaults.project
@@ -121,6 +123,11 @@ class ListFooterTest(InlinePatchTest):
         (self.patch, self.comment) = find_content(self.project, email)
 
 
+class DiffWordInCommentTest(InlinePatchTest):
+    test_comment = 'Lines can start with words beginning in "diff"\n' + \
+                   'difficult\nDifferent'
+
+
 class UpdateCommentTest(InlinePatchTest):
     """ Test for '---\nUpdate: v2' style comments to patches. """
     patch_filename = '0001-add-line.patch'
@@ -131,7 +138,7 @@ class UpdateSigCommentTest(SignatureCommentTest):
     patch_filename = '0001-add-line.patch'
     test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
 
-class SenderEncodingTest(unittest.TestCase):
+class SenderEncodingTest(TestCase):
     sender_name = u'example user'
     sender_email = 'user@example.com'
     from_header = 'example user <user@example.com>'
@@ -173,8 +180,31 @@ class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
     from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'
 
+class SubjectEncodingTest(PatchTest):
+    sender = 'example user <user@example.com>'
+    subject = 'test subject'
+    subject_header = 'test subject'
+
+    def setUp(self):
+        mail = 'From: %s\n' % self.sender + \
+               'Subject: %s\n\n' % self.subject_header + \
+               'test\n\n' + defaults.patch
+        self.projects = defaults.project
+        self.email = message_from_string(mail)
+
+    def testSubjectEncoding(self):
+        (patch, comment) = find_content(self.project, self.email)
+        self.assertEquals(patch.name, self.subject)
+
+class SubjectUTF8QPEncodingTest(SubjectEncodingTest):
+    subject = u'test s\xfcbject'
+    subject_header = '=?utf-8?q?test=20s=c3=bcbject?='
+
+class SubjectUTF8QPMultipleEncodingTest(SubjectEncodingTest):
+    subject = u'test s\xfcbject'
+    subject_header = 'test =?utf-8?q?s=c3=bcbject?='
 
-class SenderCorrelationTest(unittest.TestCase):
+class SenderCorrelationTest(TestCase):
     existing_sender = 'Existing Sender <existing@example.com>'
     non_existing_sender = 'Non-existing Sender <nonexisting@example.com>'
 
@@ -212,7 +242,7 @@ class SenderCorrelationTest(unittest.TestCase):
     def tearDown(self):
         self.person.delete()
 
-class MultipleProjectPatchTest(unittest.TestCase):
+class MultipleProjectPatchTest(TestCase):
     """ Test that patches sent to multiple patchwork projects are
         handled correctly """
 
@@ -276,7 +306,7 @@ class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
             # and the one we parsed in setUp()
             self.assertEquals(Comment.objects.filter(patch = patch).count(), 2)
 
-class ListIdHeaderTest(unittest.TestCase):
+class ListIdHeaderTest(TestCase):
     """ Test that we parse List-Id headers from mails correctly """
     def setUp(self):
         self.project = Project(linkname = 'test-project-1', name = 'Project 1',
@@ -322,3 +352,177 @@ class ListIdHeaderTest(unittest.TestCase):
 
     def tearDown(self):
         self.project.delete()
+
+class MBoxPatchTest(PatchTest):
+    def setUp(self):
+        self.mail = read_mail(self.mail_file, project = self.project)
+
+class GitPullTest(MBoxPatchTest):
+    mail_file = '0001-git-pull-request.mbox'
+
+    def testGitPullRequest(self):
+        (patch, comment) = find_content(self.project, self.mail)
+        self.assertTrue(patch is not None)
+        self.assertTrue(patch.pull_url is not None)
+        self.assertTrue(patch.content is None)
+        self.assertTrue(comment is not None)
+
+class GitPullWrappedTest(GitPullTest):
+    mail_file = '0002-git-pull-request-wrapped.mbox'
+
+class GitPullWithDiffTest(MBoxPatchTest):
+    mail_file = '0003-git-pull-request-with-diff.mbox'
+
+    def testGitPullWithDiff(self):
+        (patch, comment) = find_content(self.project, self.mail)
+        self.assertTrue(patch is not None)
+        self.assertEqual('git://git.kernel.org/pub/scm/linux/kernel/git/tip/' +
+             'linux-2.6-tip.git x86-fixes-for-linus', patch.pull_url)
+        self.assertTrue(
+            patch.content.startswith('diff --git a/arch/x86/include/asm/smp.h'),
+            patch.content)
+        self.assertTrue(comment is not None)
+
+class GitPullGitSSHUrlTest(GitPullTest):
+    mail_file = '0004-git-pull-request-git+ssh.mbox'
+
+class GitPullSSHUrlTest(GitPullTest):
+    mail_file = '0005-git-pull-request-ssh.mbox'
+
+class GitPullHTTPUrlTest(GitPullTest):
+    mail_file = '0006-git-pull-request-http.mbox'
+
+class GitRenameOnlyTest(MBoxPatchTest):
+    mail_file = '0008-git-rename.mbox'
+
+    def testGitRename(self):
+        (patch, comment) = find_content(self.project, self.mail)
+        self.assertTrue(patch is not None)
+        self.assertTrue(comment is not None)
+        self.assertEqual(patch.content.count("\nrename from "), 2)
+        self.assertEqual(patch.content.count("\nrename to "), 2)
+
+class GitRenameWithDiffTest(MBoxPatchTest):
+    mail_file = '0009-git-rename-with-diff.mbox'
+
+    def testGitRename(self):
+        (patch, comment) = find_content(self.project, self.mail)
+        self.assertTrue(patch is not None)
+        self.assertTrue(comment is not None)
+        self.assertEqual(patch.content.count("\nrename from "), 2)
+        self.assertEqual(patch.content.count("\nrename to "), 2)
+        self.assertEqual(patch.content.count('\n-a\n+b'), 1)
+
+class CVSFormatPatchTest(MBoxPatchTest):
+    mail_file = '0007-cvs-format-diff.mbox'
+
+    def testPatch(self):
+        (patch, comment) = find_content(self.project, self.mail)
+        self.assertTrue(patch is not None)
+        self.assertTrue(comment is not None)
+        self.assertTrue(patch.content.startswith('Index'))
+
+class DelegateRequestTest(TestCase):
+    patch_filename = '0001-add-line.patch'
+    msgid = '<1@example.com>'
+    invalid_delegate_email = "nobody"
+
+    def setUp(self):
+        self.patch = read_patch(self.patch_filename)
+        self.user = create_user()
+        self.p1 = Project(linkname = 'test-project-1', name = 'Project 1',
+                listid = '1.example.com', listemail='1@example.com')
+        self.p1.save()
+
+    def get_email(self):
+        email = create_email(self.patch)
+        del email['List-ID']
+        email['List-ID'] = '<' + self.p1.listid + '>'
+        email['Message-Id'] = self.msgid
+        return email
+
+    def _assertDelegate(self, delegate):
+        query = Patch.objects.filter(project=self.p1)
+        self.assertEquals(query.count(), 1)
+        self.assertEquals(query[0].delegate, delegate)
+
+    def testDelegate(self):
+        email = self.get_email()
+        email['X-Patchwork-Delegate'] = self.user.email
+        parse_mail(email)
+        self._assertDelegate(self.user)
+
+    def testNoDelegate(self):
+        email = self.get_email()
+        parse_mail(email)
+        self._assertDelegate(None)
+
+    def testInvalidDelegateFallsBackToNoDelegate(self):
+        email = self.get_email()
+        email['X-Patchwork-Delegate'] = self.invalid_delegate_email
+        parse_mail(email)
+        self._assertDelegate(None)
+
+    def tearDown(self):
+        self.p1.delete()
+        self.user.delete()
+
+class InitialPatchStateTest(TestCase):
+    patch_filename = '0001-add-line.patch'
+    msgid = '<1@example.com>'
+    invalid_state_name = "Nonexistent Test State"
+
+    def setUp(self):
+        self.patch = read_patch(self.patch_filename)
+        self.user = create_user()
+        self.p1 = Project(linkname = 'test-project-1', name = 'Project 1',
+                listid = '1.example.com', listemail='1@example.com')
+        self.p1.save()
+        self.default_state = get_default_initial_patch_state()
+        self.nondefault_state = State.objects.get(name="Accepted")
+
+    def get_email(self):
+        email = create_email(self.patch)
+        del email['List-ID']
+        email['List-ID'] = '<' + self.p1.listid + '>'
+        email['Message-Id'] = self.msgid
+        return email
+
+    def _assertState(self, state):
+        query = Patch.objects.filter(project=self.p1)
+        self.assertEquals(query.count(), 1)
+        self.assertEquals(query[0].state, state)
+
+    def testNonDefaultStateIsActuallyNotTheDefaultState(self):
+        self.assertNotEqual(self.default_state, self.nondefault_state)
+
+    def testExplicitNonDefaultStateRequest(self):
+        email = self.get_email()
+        email['X-Patchwork-State'] = self.nondefault_state.name
+        parse_mail(email)
+        self._assertState(self.nondefault_state)
+
+    def testExplicitDefaultStateRequest(self):
+        email = self.get_email()
+        email['X-Patchwork-State'] = self.default_state.name
+        parse_mail(email)
+        self._assertState(self.default_state)
+
+    def testImplicitDefaultStateRequest(self):
+        email = self.get_email()
+        parse_mail(email)
+        self._assertState(self.default_state)
+
+    def testInvalidTestStateDoesNotExist(self):
+        with self.assertRaises(State.DoesNotExist):
+            State.objects.get(name=self.invalid_state_name)
+
+    def testInvalidStateRequestFallsBackToDefaultState(self):
+        email = self.get_email()
+        email['X-Patchwork-State'] = self.invalid_state_name
+        parse_mail(email)
+        self._assertState(self.default_state)
+
+    def tearDown(self):
+        self.p1.delete()
+        self.user.delete()