diff options
author | Jelle van der Waa <jelle@vdwaa.nl> | 2018-09-25 11:22:24 +0200 |
---|---|---|
committer | Jelle van der Waa <jelle@vdwaa.nl> | 2018-09-25 11:24:36 +0200 |
commit | 73759c31fb069e406f976ed425c7c47043e8b914 (patch) | |
tree | d6f7abd12bc1f10310e460f2b1adc90dbf25040f /main | |
parent | 338b462b72783436733f9b86cc20146ca40f784f (diff) | |
download | archweb-73759c31fb069e406f976ed425c7c47043e8b914.tar.gz archweb-73759c31fb069e406f976ed425c7c47043e8b914.zip |
main: Add maildir tests to donor_import
Include an integration test, to test donor_import with a real maildir
file with a valid and invalid donor.
Diffstat (limited to 'main')
-rw-r--r-- | main/tests/test_donor_import.py | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/main/tests/test_donor_import.py b/main/tests/test_donor_import.py index 5aad3dc6..ca964f4e 100644 --- a/main/tests/test_donor_import.py +++ b/main/tests/test_donor_import.py @@ -1,16 +1,20 @@ # -*- coding: utf-8 -*- from email.header import Header +from email.message import Message +from mailbox import Maildir +from tempfile import mkdtemp +from shutil import rmtree -from django.test import SimpleTestCase +from django.test import TransactionTestCase from django.core.management import call_command from django.core.management.base import CommandError - +from main.models import Donor from main.management.commands.donor_import import Command -class DonorImportTest(SimpleTestCase): +class DonorImportTest(TransactionTestCase): def setUp(self): self.command = Command() @@ -46,3 +50,31 @@ class DonorImportTest(SimpleTestCase): with self.assertRaises(CommandError) as e: call_command('donor_import', '/tmp/non-existant') self.assertIn('Failed to open maildir', str(e.exception)) + + def test_maildir(self): + tmpdir = mkdtemp('archweb') + mdir = tmpdir + '/maildir' + + maildir = Maildir(mdir) + msg = Message() + msg['subject'] = 'John Doe' + msg['to'] = 'John Doe <john@doe.com>' + maildir.add(msg) + + # Invalid + call_command('donor_import', mdir) + self.assertEqual(len(Donor.objects.all()), 0) + + # Valid + msg = Message() + msg['subject'] = 'Receipt [$25.00] By: David Doe [david@doe.com]' + msg['to'] = 'John Doe <david@doe.com>' + maildir.add(msg) + call_command('donor_import', mdir) + self.assertEqual(len(Donor.objects.all()), 1) + + # Re-running should result in no new donor + call_command('donor_import', mdir) + self.assertEqual(len(Donor.objects.all()), 1) + + rmtree(tmpdir) |