]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/utils.py
Add support for git-pull requests
[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 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 _user_idx = 1
54 def create_user():
55     global _user_idx
56     userid = 'test-%d' % _user_idx
57     email = '%s@example.com' % userid
58     _user_idx += 1
59
60     user = User.objects.create_user(userid, email, userid)
61     user.save()
62
63     profile = UserProfile(user = user)
64     profile.save()
65
66     return user
67
68 def create_maintainer(project):
69     user = create_user()
70     profile = user.get_profile()
71     profile.maintainer_projects.add(project)
72     profile.save()
73     return user
74
75 def find_in_context(context, key):
76     if isinstance(context, list):
77         for c in context:
78             v = find_in_context(c, key)
79             if v is not None:
80                 return v
81     else:
82         if key in context:
83             return context[key]
84     return None
85
86 def read_patch(filename, encoding = None):
87     file_path = os.path.join(_test_patch_dir, filename)
88     if encoding is not None:
89         f = codecs.open(file_path, encoding = encoding)
90     else:
91         f = file(file_path)
92
93     return f.read()
94
95 def read_mail(filename, project = None):
96     file_path = os.path.join(_test_mail_dir, filename)
97     mail = message_from_file(open(file_path))
98     if project is not None:
99         mail['List-Id'] = project.listid
100     return mail
101
102 def create_email(content, subject = None, sender = None, multipart = False,
103         project = None, content_encoding = None):
104     if subject is None:
105         subject = defaults.subject
106     if sender is None:
107         sender = defaults.sender
108     if project is None:
109         project = defaults.project
110     if content_encoding is None:
111         content_encoding = 'us-ascii'
112
113     if multipart:
114         msg = MIMEMultipart()
115         body = MIMEText(content, _subtype = 'plain',
116                         _charset = content_encoding)
117         msg.attach(body)
118     else:
119         msg = MIMEText(content, _charset = content_encoding)
120
121     msg['Subject'] = subject
122     msg['From'] = sender
123     msg['List-Id'] = project.linkname
124
125
126     return msg