]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/tests/utils.py
[tests] Add initial bundle tests
[patchwork] / apps / patchwork / tests / utils.py
index c7be6ab40b5850b94582bf9376c0770d0cea4d04..7166ed27ccdc0fdb7bdb11fce584fab3e2f0c05d 100644 (file)
 # 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, UserProfile
+from django.contrib.auth.models import User
+
 try:
     from email.mime.text import MIMEText
     from email.mime.multipart import MIMEMultipart
@@ -35,6 +38,8 @@ class defaults(object):
     project = Project(linkname = 'test-project', name = 'Test Project')
 
     patch_author = 'Patch Author <patch-author@example.com>'
+    patch_author_person = Person(name = 'Patch Author',
+                                email = 'patch-author@example.com')
 
     comment_author = 'Comment Author <comment-author@example.com>'
 
@@ -44,12 +49,43 @@ class defaults(object):
 
     patch_name = 'Test Patch'
 
+_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()
+
+    profile = UserProfile(user = user)
+    profile.save()
 
-def read_patch(filename):
-    return file(os.path.join(_test_patch_dir, filename)).read()
+    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 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:
@@ -60,12 +96,17 @@ def create_email(content, subject = None, sender = None, multipart = False,
     if multipart:
         msg = MIMEMultipart()
         body = MIMEText(content, _subtype = 'plain')
+        if content_encoding is not None:
+            body.set_charset(content_encoding)
         msg.attach(body)
     else:
         msg = MIMEText(content)
+        if content_encoding is not None:
+            msg.set_charset(content_encoding)
 
     msg['Subject'] = subject
     msg['From'] = sender
     msg['List-Id'] = project.linkname
 
+
     return msg