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