X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Ftests%2Futils.py;h=782ed369cca0254a80d20abd5a90b7a70c3f614d;hb=f09e982f58384946111d4157fd2b7c2b31b78612;hp=c7be6ab40b5850b94582bf9376c0770d0cea4d04;hpb=969d359e9c2f3eb5ee4741208ef17639d5e1b180;p=patchwork diff --git a/apps/patchwork/tests/utils.py b/apps/patchwork/tests/utils.py index c7be6ab..782ed36 100644 --- a/apps/patchwork/tests/utils.py +++ b/apps/patchwork/tests/utils.py @@ -18,7 +18,12 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os -from patchwork.models import Project +import codecs +from patchwork.models import Project, Person +from django.contrib.auth.models import User +from django.forms.fields import EmailField + +from email import message_from_file try: from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart @@ -28,13 +33,16 @@ except ImportError: from email.MIMEMultipart import MIMEMultipart # helper functions for tests -_test_mail_dir = 'patchwork/tests/mail' -_test_patch_dir = 'patchwork/tests/patches' +_test_mail_dir = os.path.join(os.path.dirname(__file__), 'mail') +_test_patch_dir = os.path.join(os.path.dirname(__file__), 'patches') class defaults(object): - project = Project(linkname = 'test-project', name = 'Test Project') + project = Project(linkname = 'test-project', name = 'Test Project', + listid = 'test.example.com') patch_author = 'Patch Author ' + patch_author_person = Person(name = 'Patch Author', + email = 'patch-author@example.com') comment_author = 'Comment Author ' @@ -44,28 +52,87 @@ class defaults(object): patch_name = 'Test Patch' + patch = """--- /dev/null 2011-01-01 00:00:00.000000000 +0800 ++++ a 2011-01-01 00:00:00.000000000 +0800 +@@ -0,0 +1 @@ ++a +""" + +error_strings = { + 'email': 'Enter a valid email address.', +} + +_user_idx = 1 +def create_user(): + global _user_idx + userid = 'test%d' % _user_idx + email = '%s@example.com' % userid + _user_idx += 1 + + user = User.objects.create_user(userid, email, userid) + user.save() -def read_patch(filename): - return file(os.path.join(_test_patch_dir, filename)).read() + person = Person(email = email, name = userid, user = user) + person.save() + + return user + +def create_maintainer(project): + user = create_user() + profile = user.profile + profile.maintainer_projects.add(project) + profile.save() + return user + +def find_in_context(context, key): + if isinstance(context, list): + for c in context: + v = find_in_context(c, key) + if v is not None: + return v + else: + if key in context: + return context[key] + return None + +def read_patch(filename, encoding = None): + file_path = os.path.join(_test_patch_dir, filename) + if encoding is not None: + f = codecs.open(file_path, encoding = encoding) + else: + f = file(file_path) + + return f.read() + +def read_mail(filename, project = None): + file_path = os.path.join(_test_mail_dir, filename) + mail = message_from_file(open(file_path)) + if project is not None: + mail['List-Id'] = project.listid + return mail def create_email(content, subject = None, sender = None, multipart = False, - project = None): + project = None, content_encoding = None): if subject is None: subject = defaults.subject if sender is None: sender = defaults.sender if project is None: project = defaults.project + if content_encoding is None: + content_encoding = 'us-ascii' if multipart: msg = MIMEMultipart() - body = MIMEText(content, _subtype = 'plain') + body = MIMEText(content, _subtype = 'plain', + _charset = content_encoding) msg.attach(body) else: - msg = MIMEText(content) + msg = MIMEText(content, _charset = content_encoding) msg['Subject'] = subject msg['From'] = sender - msg['List-Id'] = project.linkname + msg['List-Id'] = project.listid + return msg