]> git.ozlabs.org Git - patchwork/blob - patchwork/tests/utils.py
782ed369cca0254a80d20abd5a90b7a70c3f614d
[patchwork] / 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                       listid = 'test.example.com')
42
43     patch_author = 'Patch Author <patch-author@example.com>'
44     patch_author_person = Person(name = 'Patch Author',
45         email = 'patch-author@example.com')
46
47     comment_author = 'Comment Author <comment-author@example.com>'
48
49     sender = 'Test Author <test-author@example.com>'
50
51     subject = 'Test Subject'
52
53     patch_name = 'Test Patch'
54
55     patch = """--- /dev/null    2011-01-01 00:00:00.000000000 +0800
56 +++ a   2011-01-01 00:00:00.000000000 +0800
57 @@ -0,0 +1 @@
58 +a
59 """
60
61 error_strings = {
62     'email': 'Enter a valid email address.',
63 }
64
65 _user_idx = 1
66 def create_user():
67     global _user_idx
68     userid = 'test%d' % _user_idx
69     email = '%s@example.com' % userid
70     _user_idx += 1
71
72     user = User.objects.create_user(userid, email, userid)
73     user.save()
74
75     person = Person(email = email, name = userid, user = user)
76     person.save()
77
78     return user
79
80 def create_maintainer(project):
81     user = create_user()
82     profile = user.profile
83     profile.maintainer_projects.add(project)
84     profile.save()
85     return user
86
87 def find_in_context(context, key):
88     if isinstance(context, list):
89         for c in context:
90             v = find_in_context(c, key)
91             if v is not None:
92                 return v
93     else:
94         if key in context:
95             return context[key]
96     return None
97
98 def read_patch(filename, encoding = None):
99     file_path = os.path.join(_test_patch_dir, filename)
100     if encoding is not None:
101         f = codecs.open(file_path, encoding = encoding)
102     else:
103         f = file(file_path)
104
105     return f.read()
106
107 def read_mail(filename, project = None):
108     file_path = os.path.join(_test_mail_dir, filename)
109     mail = message_from_file(open(file_path))
110     if project is not None:
111         mail['List-Id'] = project.listid
112     return mail
113
114 def create_email(content, subject = None, sender = None, multipart = False,
115         project = None, content_encoding = None):
116     if subject is None:
117         subject = defaults.subject
118     if sender is None:
119         sender = defaults.sender
120     if project is None:
121         project = defaults.project
122     if content_encoding is None:
123         content_encoding = 'us-ascii'
124
125     if multipart:
126         msg = MIMEMultipart()
127         body = MIMEText(content, _subtype = 'plain',
128                         _charset = content_encoding)
129         msg.attach(body)
130     else:
131         msg = MIMEText(content, _charset = content_encoding)
132
133     msg['Subject'] = subject
134     msg['From'] = sender
135     msg['List-Id'] = project.listid
136
137
138     return msg