|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 |
|
3 |
| -import sys |
4 | 3 | from email.header import Header
|
5 | 4 | from email.message import Message
|
6 |
| -from io import StringIO |
7 |
| -from tempfile import mkdtemp |
| 5 | +from mailbox import Maildir |
| 6 | + |
| 7 | +import pytest |
8 | 8 |
|
9 |
| -from django.test import TransactionTestCase |
10 | 9 | from django.core.management import call_command
|
11 | 10 | from django.core.management.base import CommandError
|
12 | 11 |
|
13 | 12 | from main.models import Donor
|
14 | 13 | from main.management.commands.donor_import import Command
|
15 | 14 |
|
16 | 15 |
|
17 |
| -class DonorImportTest(TransactionTestCase): |
18 |
| - |
19 |
| - def setUp(self): |
20 |
| - self.command = Command() |
21 |
| - |
22 |
| - def test_parse_subject(self): |
23 |
| - self.assertIsNone(self.command.parse_subject('garbage')) |
24 |
| - |
25 |
| - # Valid |
26 |
| - valid = u'Receipt [$25.00] By: John Doe [[email protected]]' |
27 |
| - output = self.command.parse_subject(valid) |
28 |
| - self.assertEqual(output, u'John Doe') |
29 |
| - |
30 |
| - def test_parse_name(self): |
31 |
| - self.assertEqual(self.command.sanitize_name(u'1244'), u'') |
32 |
| - self.assertEqual(self.command.sanitize_name(u'John Doe'), u'John Doe') |
33 |
| - self.assertEqual(self.command.sanitize_name(u' John Doe '), u'John Doe') |
34 |
| - self.assertEqual(self.command.sanitize_name(u'John Doe 23'), u'John Doe') |
35 |
| - |
36 |
| - def test_decode_subject(self): |
37 |
| - text = u'メイル' |
38 |
| - subject = Header(text, 'utf-8') |
39 |
| - self.assertEqual(self.command.decode_subject(subject), text) |
40 |
| - |
41 |
| - def test_invalid_args(self): |
42 |
| - stdin = sys.stdin |
43 |
| - with self.assertRaises(CommandError) as e: |
44 |
| - sys.stdin = StringIO('') |
45 |
| - call_command('donor_import') |
46 |
| - self.assertIn('Failed to read from STDIN', str(e.exception)) |
47 |
| - sys.stdin = stdin |
48 |
| - |
49 |
| - def test_invalid_path(self): |
50 |
| - with self.assertRaises(CommandError) as e: |
51 |
| - call_command('donor_import', '/tmp/non-existant') |
52 |
| - self.assertIn('argument input: can\'t open', str(e.exception)) |
53 |
| - |
54 |
| - def test_import(self): |
55 |
| - tmpmail = mkdtemp('archweb') + "/mail" |
56 |
| - |
57 |
| - msg = Message() |
58 |
| - msg['subject'] = 'John Doe' |
59 |
| - msg[ 'to'] = 'John Doe <[email protected]>' |
60 |
| - with open(tmpmail, 'wb') as fp: |
61 |
| - fp.write(msg.as_bytes()) |
62 |
| - |
63 |
| - # Invalid |
64 |
| - with self.assertRaises(SystemExit): |
65 |
| - call_command('donor_import', tmpmail) |
66 |
| - self.assertEqual(len(Donor.objects.all()), 0) |
67 |
| - |
68 |
| - # Valid |
69 |
| - msg = Message() |
70 |
| - msg[ 'subject'] = 'Receipt [$25.00] By: David Doe [[email protected]]' |
71 |
| - msg[ 'to'] = 'John Doe <[email protected]>' |
72 |
| - with open(tmpmail, 'wb') as fp: |
73 |
| - fp.write(msg.as_bytes()) |
74 |
| - |
75 |
| - call_command('donor_import', tmpmail) |
76 |
| - self.assertEqual(len(Donor.objects.all()), 1) |
77 |
| - |
78 |
| - # Re-running should result in no new donor |
79 |
| - call_command('donor_import', tmpmail) |
80 |
| - self.assertEqual(len(Donor.objects.all()), 1) |
| 16 | +command = Command() |
| 17 | + |
| 18 | + |
| 19 | +def gen_parse_subject(data): |
| 20 | + return command.parse_subject(data) |
| 21 | + |
| 22 | + |
| 23 | +def test_parse_subject(self): |
| 24 | + assert self.command.parse_subject('garbage') is None |
| 25 | + |
| 26 | + # Valid |
| 27 | + valid = 'Receipt [$25.00] By: John Doe [[email protected]]' |
| 28 | + output = command.parse_subject(valid) |
| 29 | + assert output == 'John Doe' |
| 30 | + |
| 31 | + |
| 32 | +def test_parse_name(): |
| 33 | + assert command.sanitize_name('1244') == '' |
| 34 | + assert command.sanitize_name('John Doe') == 'John Doe' |
| 35 | + assert command.sanitize_name(' John Doe ') == 'John Doe' |
| 36 | + assert command.sanitize_name('John Doe 23') == 'John Doe' |
| 37 | + |
| 38 | + |
| 39 | +def test_decode_subject(): |
| 40 | + text = u'メイル' |
| 41 | + subject = Header(text, 'utf-8') |
| 42 | + assert command.decode_subject(subject) == text |
| 43 | + |
| 44 | + |
| 45 | +def test_invalid_args(): |
| 46 | + with pytest.raises(CommandError) as e: |
| 47 | + call_command('donor_import') |
| 48 | + assert 'Error: the following arguments are required' in str(e.value) |
| 49 | + |
| 50 | + |
| 51 | +def test_invalid_path(): |
| 52 | + with pytest.raises(CommandError) as e: |
| 53 | + call_command('donor_import', '/tmp/non-existant') |
| 54 | + assert 'Failed to open maildir' in str(e.value) |
| 55 | + |
| 56 | + |
| 57 | +def test_maildir(db, tmp_path): |
| 58 | + tmpdir = tmp_path / 'archweb' |
| 59 | + tmpdir.mkdir() |
| 60 | + mdir = tmpdir / 'maildir' |
| 61 | + |
| 62 | + maildir = Maildir(mdir) |
| 63 | + msg = Message() |
| 64 | + msg['subject'] = 'John Doe' |
| 65 | + msg[ 'to'] = 'John Doe <[email protected]>' |
| 66 | + maildir.add(msg) |
| 67 | + |
| 68 | + # Invalid |
| 69 | + call_command('donor_import', mdir) |
| 70 | + assert len(Donor.objects.all()) == 0 |
| 71 | + |
| 72 | + # Valid |
| 73 | + msg = Message() |
| 74 | + msg[ 'subject'] = 'Receipt [$25.00] By: David Doe [[email protected]]' |
| 75 | + msg[ 'to'] = 'John Doe <[email protected]>' |
| 76 | + maildir.add(msg) |
| 77 | + call_command('donor_import', mdir) |
| 78 | + assert len(Donor.objects.all()) == 1 |
| 79 | + |
| 80 | + # Re-running should result in no new donor |
| 81 | + call_command('donor_import', mdir) |
| 82 | + assert len(Donor.objects.all()) == 1 |
0 commit comments