]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/utils.py
5dc5d16e47dd46997750ee3fd3bf529ec80b5134
[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, UserProfile
23 from django.contrib.auth.models import User
24
25 try:
26     from email.mime.text import MIMEText
27     from email.mime.multipart import MIMEMultipart
28 except ImportError:
29     # Python 2.4 compatibility
30     from email.MIMEText import MIMEText
31     from email.MIMEMultipart import MIMEMultipart
32
33 # helper functions for tests
34 _test_mail_dir  = 'patchwork/tests/mail'
35 _test_patch_dir = 'patchwork/tests/patches'
36
37 class defaults(object):
38     project = Project(linkname = 'test-project', name = 'Test Project')
39
40     patch_author = 'Patch Author <patch-author@example.com>'
41     patch_author_person = Person(name = 'Patch Author',
42                                  email = 'patch-author@example.com')
43
44     comment_author = 'Comment Author <comment-author@example.com>'
45
46     sender = 'Test Author <test-author@example.com>'
47
48     subject = 'Test Subject'
49
50     patch_name = 'Test Patch'
51
52 _user_idx = 1
53 def create_user():
54     global _user_idx
55     userid = 'test-%d' % _user_idx
56     email = '%s@example.com' % userid
57     _user_idx += 1
58
59     user = User.objects.create_user(userid, email, userid)
60     user.save()
61
62     profile = UserProfile(user = user)
63     profile.save()
64
65     return user
66
67 def create_maintainer(project):
68     user = create_user()
69     profile = user.get_profile()
70     profile.maintainer_projects.add(project)
71     profile.save()
72     return user
73
74 def find_in_context(context, key):
75     if isinstance(context, list):
76         for c in context:
77             v = find_in_context(c, key)
78             if v is not None:
79                 return v
80     else:
81         if key in context:
82             return context[key]
83     return None
84
85 def read_patch(filename, encoding = None):
86     file_path = os.path.join(_test_patch_dir, filename)
87     if encoding is not None:
88         f = codecs.open(file_path, encoding = encoding)
89     else:
90         f = file(file_path)
91
92     return f.read()
93
94 def create_email(content, subject = None, sender = None, multipart = False,
95         project = None, content_encoding = None):
96     if subject is None:
97         subject = defaults.subject
98     if sender is None:
99         sender = defaults.sender
100     if project is None:
101         project = defaults.project
102     if content_encoding is None:
103         content_encoding = 'us-ascii'
104
105     if multipart:
106         msg = MIMEMultipart()
107         body = MIMEText(content, _subtype = 'plain',
108                         _charset = content_encoding)
109         msg.attach(body)
110     else:
111         msg = MIMEText(content, _charset = content_encoding)
112
113     msg['Subject'] = subject
114     msg['From'] = sender
115     msg['List-Id'] = project.linkname
116
117
118     return msg