1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
4 # This file is part of the Patchwork package.
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.
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.
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
22 from patchwork.models import Project, Person, UserProfile
23 from django.contrib.auth.models import User
25 from email import message_from_file
27 from email.mime.text import MIMEText
28 from email.mime.multipart import MIMEMultipart
30 # Python 2.4 compatibility
31 from email.MIMEText import MIMEText
32 from email.MIMEMultipart import MIMEMultipart
34 # helper functions for tests
35 _test_mail_dir = 'patchwork/tests/mail'
36 _test_patch_dir = 'patchwork/tests/patches'
38 class defaults(object):
39 project = Project(linkname = 'test-project', name = 'Test Project')
41 patch_author = 'Patch Author <patch-author@example.com>'
42 patch_author_person = Person(name = 'Patch Author',
43 email = 'patch-author@example.com')
45 comment_author = 'Comment Author <comment-author@example.com>'
47 sender = 'Test Author <test-author@example.com>'
49 subject = 'Test Subject'
51 patch_name = 'Test Patch'
53 patch = """--- /dev/null 2011-01-01 00:00:00.000000000 +0800
54 +++ a 2011-01-01 00:00:00.000000000 +0800
62 userid = 'test-%d' % _user_idx
63 email = '%s@example.com' % userid
66 user = User.objects.create_user(userid, email, userid)
69 profile = UserProfile(user = user)
74 def create_maintainer(project):
76 profile = user.get_profile()
77 profile.maintainer_projects.add(project)
81 def find_in_context(context, key):
82 if isinstance(context, list):
84 v = find_in_context(c, key)
92 def read_patch(filename, encoding = None):
93 file_path = os.path.join(_test_patch_dir, filename)
94 if encoding is not None:
95 f = codecs.open(file_path, encoding = encoding)
101 def read_mail(filename, project = None):
102 file_path = os.path.join(_test_mail_dir, filename)
103 mail = message_from_file(open(file_path))
104 if project is not None:
105 mail['List-Id'] = project.listid
108 def create_email(content, subject = None, sender = None, multipart = False,
109 project = None, content_encoding = None):
111 subject = defaults.subject
113 sender = defaults.sender
115 project = defaults.project
116 if content_encoding is None:
117 content_encoding = 'us-ascii'
120 msg = MIMEMultipart()
121 body = MIMEText(content, _subtype = 'plain',
122 _charset = content_encoding)
125 msg = MIMEText(content, _charset = content_encoding)
127 msg['Subject'] = subject
129 msg['List-Id'] = project.linkname