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