]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/utils.py
c7be6ab40b5850b94582bf9376c0770d0cea4d04
[patchwork] / apps / patchwork / tests / utils.py
1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
3 #
4 # This file is part of the Patchwork package.
5 #
6 # Patchwork is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # Patchwork is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Patchwork; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 import os
21 from patchwork.models import Project
22 try:
23     from email.mime.text import MIMEText
24     from email.mime.multipart import MIMEMultipart
25 except ImportError:
26     # Python 2.4 compatibility
27     from email.MIMEText import MIMEText
28     from email.MIMEMultipart import MIMEMultipart
29
30 # helper functions for tests
31 _test_mail_dir  = 'patchwork/tests/mail'
32 _test_patch_dir = 'patchwork/tests/patches'
33
34 class defaults(object):
35     project = Project(linkname = 'test-project', name = 'Test Project')
36
37     patch_author = 'Patch Author <patch-author@example.com>'
38
39     comment_author = 'Comment Author <comment-author@example.com>'
40
41     sender = 'Test Author <test-author@example.com>'
42
43     subject = 'Test Subject'
44
45     patch_name = 'Test Patch'
46
47
48 def read_patch(filename):
49     return file(os.path.join(_test_patch_dir, filename)).read()
50
51 def create_email(content, subject = None, sender = None, multipart = False,
52         project = None):
53     if subject is None:
54         subject = defaults.subject
55     if sender is None:
56         sender = defaults.sender
57     if project is None:
58         project = defaults.project
59
60     if multipart:
61         msg = MIMEMultipart()
62         body = MIMEText(content, _subtype = 'plain')
63         msg.attach(body)
64     else:
65         msg = MIMEText(content)
66
67     msg['Subject'] = subject
68     msg['From'] = sender
69     msg['List-Id'] = project.linkname
70
71     return msg