]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/tests/utils.py
tests: Add multiple patch update test
[patchwork] / apps / patchwork / tests / utils.py
index a2bec15061a012c9ce8582ee93447da9b4af7fe4..5dc5d16e47dd46997750ee3fd3bf529ec80b5134 100644 (file)
@@ -19,7 +19,9 @@
 
 import os
 import codecs
-from patchwork.models import Project, Person
+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
@@ -47,6 +49,38 @@ 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()
+
+    return user
+
+def create_maintainer(project):
+    user = create_user()
+    profile = user.get_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)
@@ -65,17 +99,16 @@ def create_email(content, subject = None, sender = None, multipart = False,
         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')
-        if content_encoding is not None:
-            body.set_charset(content_encoding)
+        body = MIMEText(content, _subtype = 'plain',
+                        _charset = content_encoding)
         msg.attach(body)
     else:
-        msg = MIMEText(content)
-        if content_encoding is not None:
-            msg.set_charset(content_encoding)
+        msg = MIMEText(content, _charset = content_encoding)
 
     msg['Subject'] = subject
     msg['From'] = sender