]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/utils.py
a2bec15061a012c9ce8582ee93447da9b4af7fe4
[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 import codecs
22 from patchwork.models import Project, Person
23 try:
24     from email.mime.text import MIMEText
25     from email.mime.multipart import MIMEMultipart
26 except ImportError:
27     # Python 2.4 compatibility
28     from email.MIMEText import MIMEText
29     from email.MIMEMultipart import MIMEMultipart
30
31 # helper functions for tests
32 _test_mail_dir  = 'patchwork/tests/mail'
33 _test_patch_dir = 'patchwork/tests/patches'
34
35 class defaults(object):
36     project = Project(linkname = 'test-project', name = 'Test Project')
37
38     patch_author = 'Patch Author <patch-author@example.com>'
39     patch_author_person = Person(name = 'Patch Author',
40                                  email = 'patch-author@example.com')
41
42     comment_author = 'Comment Author <comment-author@example.com>'
43
44     sender = 'Test Author <test-author@example.com>'
45
46     subject = 'Test Subject'
47
48     patch_name = 'Test Patch'
49
50
51 def read_patch(filename, encoding = None):
52     file_path = os.path.join(_test_patch_dir, filename)
53     if encoding is not None:
54         f = codecs.open(file_path, encoding = encoding)
55     else:
56         f = file(file_path)
57
58     return f.read()
59
60 def create_email(content, subject = None, sender = None, multipart = False,
61         project = None, content_encoding = None):
62     if subject is None:
63         subject = defaults.subject
64     if sender is None:
65         sender = defaults.sender
66     if project is None:
67         project = defaults.project
68
69     if multipart:
70         msg = MIMEMultipart()
71         body = MIMEText(content, _subtype = 'plain')
72         if content_encoding is not None:
73             body.set_charset(content_encoding)
74         msg.attach(body)
75     else:
76         msg = MIMEText(content)
77         if content_encoding is not None:
78             msg.set_charset(content_encoding)
79
80     msg['Subject'] = subject
81     msg['From'] = sender
82     msg['List-Id'] = project.linkname
83
84
85     return msg