changeset 92:6e9ba2b64d1f

Test sending of activation and password reset emails.
author Daniele Nicolodi <daniele@grinta.net>
date Sun, 21 Aug 2011 18:17:27 +0200
parents 5c1c7f2d6469
children 8fd53ae8818a
files src/ltpdarepo/tests/test_users.py
diffstat 1 files changed, 74 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/ltpdarepo/tests/test_users.py	Sun Aug 21 18:17:27 2011 +0200
+++ b/src/ltpdarepo/tests/test_users.py	Sun Aug 21 18:17:27 2011 +0200
@@ -1,11 +1,12 @@
-import unittest
+import re
+import unittest2 as unittest
 import MySQLdb as mysql
 
-from flask import g
-
 from ltpdarepo.tests.utils import RequestContextTestCase
 from ltpdarepo.user import User, INVALIDPASSWORD
 
+USERNAME, PASSWORD = 'u1', 'u1'
+
 
 class TestCase(unittest.TestCase):
 
@@ -50,6 +51,7 @@
     def test_new_user_has_invalid_password(self):
         u2 = User(username='u2')
         u2.create()
+        from flask import g
         curs = g.db.cursor()
         curs.execute("""SELECT Password FROM mysql.user WHERE User=%s""", u2.username)
         self.assertEqual(curs.fetchone()[0], INVALIDPASSWORD)
@@ -71,3 +73,72 @@
         mysql.connect(host=self.app.config['HOSTNAME'],
                       user=u2.username, passwd='u2',
                       charset='utf8')
+
+
+class MailTestCase(unittest.TestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        from ltpdarepo.admin import wipe, install, useradd, grant
+        wipe()
+        install()
+        useradd('u1', 'u1', email='u1@example.net')
+        grant('u1', '%', ('admin', ))
+
+    def setUp(self):
+        from ltpdarepo import app
+        app.config.from_pyfile('config.py')
+        app.config['TESTING'] = True
+        self.app = app
+
+    def test_activation_email(self):
+        with self.app.test_client() as client:
+            # login
+            response = client.post('/login', data=dict(username=USERNAME, password=PASSWORD))
+            self.assertEqual(response.status_code, 302)
+            self.assertEqual(response.location, 'http://localhost/')
+
+            # user creation form
+            response = client.get('/manage/users/+')
+            self.assertEqual(response.status_code, 200)
+            # extract csrf token
+            regexp = r'<input id="csrf" name="csrf" type="hidden" value="(.*)" />'
+            token = re.search(regexp, response.data).group(1)
+
+            # create user
+            response = client.post('/manage/users/+', follow_redirects=True,
+                data=dict(username='u2', email='u2@example.net', csrf=token))
+            self.assertEqual(response.status_code, 200)
+            self.assertIn('User created. Activation token', response.data)
+
+            # check sent email
+            from flask import _request_ctx_stack
+            ctx = _request_ctx_stack.top
+            self.assertIn('Subject: LTPDA Repository account activation', ctx.sentmail[0].message)
+            self.assertIn('LTPDA Repository Administrator', ctx.sentmail[0].fromaddr)
+
+    def test_reset_email(self):
+        with self.app.test_client() as client:
+            # login
+            response = client.post('/login', data=dict(username=USERNAME, password=PASSWORD))
+            self.assertEqual(response.status_code, 302)
+            self.assertEqual(response.location, 'http://localhost/')
+
+            # password reset form
+            response = client.get('/manage/users/u1/reset')
+            self.assertEqual(response.status_code, 200)
+            # extract csrf token
+            regexp = r'<input id="csrf" name="csrf" type="hidden" value="(.*)" />'
+            token = re.search(regexp, response.data).group(1)
+
+            # create user
+            response = client.post('/manage/users/u1/reset', follow_redirects=True,
+                data=dict(ok='ok', csrf=token))
+            self.assertEqual(response.status_code, 200)
+            self.assertIn('Password reset token', response.data)
+
+            # check sent email
+            from flask import _request_ctx_stack
+            ctx = _request_ctx_stack.top
+            self.assertIn('Subject: LTPDA Repository password reset', ctx.sentmail[0].message)
+            self.assertIn('LTPDA Repository Administrator', ctx.sentmail[0].fromaddr)