You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by je...@apache.org on 2014/07/25 13:09:25 UTC

[15/16] git commit: [#7524] ticket:620 Use autits helper in tests

[#7524] ticket:620 Use autits helper in tests


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/9bcb40b2
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/9bcb40b2
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/9bcb40b2

Branch: refs/heads/je/42cc_7524
Commit: 9bcb40b221182c637b65a1d59a8b64bc9352955c
Parents: 4946492
Author: Igor Bondarenko <je...@gmail.com>
Authored: Fri Jul 25 12:39:38 2014 +0300
Committer: Igor Bondarenko <je...@gmail.com>
Committed: Fri Jul 25 12:39:38 2014 +0300

----------------------------------------------------------------------
 Allura/allura/tests/decorators.py            | 19 ++++++
 Allura/allura/tests/functional/test_admin.py | 19 +-----
 Allura/allura/tests/functional/test_auth.py  | 71 +++++++++--------------
 3 files changed, 49 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/9bcb40b2/Allura/allura/tests/decorators.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/decorators.py b/Allura/allura/tests/decorators.py
index 521c76c..3796de7 100644
--- a/Allura/allura/tests/decorators.py
+++ b/Allura/allura/tests/decorators.py
@@ -16,6 +16,7 @@
 #       under the License.
 
 import sys
+import re
 from functools import wraps
 import contextlib
 
@@ -150,3 +151,21 @@ class patch_middleware_config(object):
     def __exit__(self, exc_type, exc_val, exc_t):
         allura.config.middleware.make_app = self._make_app
         return self
+
+
+@contextlib.contextmanager
+def audits(*messages):
+    M.AuditLog.query.remove()
+    yield
+    for message in messages:
+        assert M.AuditLog.query.find(dict(
+            message=re.compile(message))).count(), 'Could not find "%s"' % message
+
+
+@contextlib.contextmanager
+def out_audits(*messages):
+    M.AuditLog.query.remove()
+    yield
+    for message in messages:
+        assert not M.AuditLog.query.find(dict(
+            message=re.compile(message))).count(), 'Found unexpected: "%s"' % message

http://git-wip-us.apache.org/repos/asf/allura/blob/9bcb40b2/Allura/allura/tests/functional/test_admin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/functional/test_admin.py b/Allura/allura/tests/functional/test_admin.py
index 09edd47..819a92a 100644
--- a/Allura/allura/tests/functional/test_admin.py
+++ b/Allura/allura/tests/functional/test_admin.py
@@ -34,6 +34,7 @@ import mock
 
 from allura.tests import TestController
 from allura.tests import decorators as td
+from allura.tests.decorators import audits, out_audits
 from alluratest.controller import TestRestApiBase, setup_trove_categories
 from allura import model as M
 from allura.app import SitemapEntry
@@ -47,24 +48,6 @@ from forgewiki.wiki_main import ForgeWikiApp
 log = logging.getLogger(__name__)
 
 
-@contextmanager
-def audits(*messages):
-    M.AuditLog.query.remove()
-    yield
-    for message in messages:
-        assert M.AuditLog.query.find(dict(
-            message=re.compile(message))).count(), 'Could not find "%s"' % message
-
-
-@contextmanager
-def out_audits(*messages):
-    M.AuditLog.query.remove()
-    yield
-    for message in messages:
-        assert not M.AuditLog.query.find(dict(
-            message=re.compile(message))).count(), 'Found unexpected: "%s"' % message
-
-
 class TestProjectAdmin(TestController):
 
     def get_available_tools(self):

http://git-wip-us.apache.org/repos/asf/allura/blob/9bcb40b2/Allura/allura/tests/functional/test_auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/functional/test_auth.py b/Allura/allura/tests/functional/test_auth.py
index 416fb2d..a8dafd4 100644
--- a/Allura/allura/tests/functional/test_auth.py
+++ b/Allura/allura/tests/functional/test_auth.py
@@ -103,50 +103,41 @@ class TestAuth(TestController):
                      'test-admin@users.localhost')
 
         # add test@example
-        r = self.app.post('/auth/preferences/update', params={
-            'preferences.display_name': 'Test Admin',
-            'new_addr.addr': 'test@example.com',
-            'new_addr.claim': 'Claim Address',
-            'primary_addr': 'test-admin@users.localhost',
-            'preferences.email_format': 'plain'},
-            extra_environ=dict(username='test-admin'))
+        with td.audits('New email address: test@example.com'):
+            r = self.app.post('/auth/preferences/update', params={
+                'preferences.display_name': 'Test Admin',
+                'new_addr.addr': 'test@example.com',
+                'new_addr.claim': 'Claim Address',
+                'primary_addr': 'test-admin@users.localhost',
+                'preferences.email_format': 'plain'},
+                extra_environ=dict(username='test-admin'))
         r = self.app.get('/auth/preferences/')
         assert 'test@example.com' in r
         user = M.User.query.get(username='test-admin')
         assert_equal(user.get_pref('email_address'), 'test-admin@users.localhost')
-        log = M.AuditLog.for_user(user).all()
-        assert_equal(len(log), 1)
-        assert_equal(log[0].message, 'New email address: test@example.com')
-        M.AuditLog.query.remove()
 
         # remove test-admin@users.localhost
-        r = self.app.post('/auth/preferences/update', params={
-            'preferences.display_name': 'Test Admin',
-            'addr-1.ord': '1',
-            'addr-1.delete': 'on',
-            'addr-2.ord': '2',
-            'new_addr.addr': '',
-            'primary_addr': 'test-admin@users.localhost',
-            'preferences.email_format': 'plain'},
-            extra_environ=dict(username='test-admin'))
+        with td.audits('Email address deleted: test-admin@users.localhost'):
+            r = self.app.post('/auth/preferences/update', params={
+                'preferences.display_name': 'Test Admin',
+                'addr-1.ord': '1',
+                'addr-1.delete': 'on',
+                'addr-2.ord': '2',
+                'new_addr.addr': '',
+                'primary_addr': 'test-admin@users.localhost',
+                'preferences.email_format': 'plain'},
+                extra_environ=dict(username='test-admin'))
         r = self.app.get('/auth/preferences/')
         assert 'test-admin@users.localhost' not in r
         # preferred address has not changed if email is not verified
         user = M.User.query.get(username='test-admin')
         assert_equal(user.get_pref('email_address'), None)
-        log = M.AuditLog.for_user(user).all()
-        assert_equal(len(log), 1)
-        assert_equal(log[0].message, 'Email address deleted: test-admin@users.localhost')
-        M.AuditLog.query.remove()
-
-        r = self.app.post('/auth/preferences/update', params={
-            'preferences.display_name': 'Admin',
-            'new_addr.addr': ''},
-            extra_environ=dict(username='test-admin'))
-        user = M.User.query.get(username='test-admin')
-        log = M.AuditLog.for_user(user).all()
-        assert_equal(len(log), 1)
-        assert_equal(log[0].message, 'Display Name changed Test Admin => Admin')
+
+        with td.audits('Display Name changed Test Admin => Admin'):
+            r = self.app.post('/auth/preferences/update', params={
+                'preferences.display_name': 'Admin',
+                'new_addr.addr': ''},
+                extra_environ=dict(username='test-admin'))
 
     @td.with_user_project('test-admin')
     def test_prefs_subscriptions(self):
@@ -732,7 +723,8 @@ class TestPasswordReset(TestController):
         email.confirmed = True
         ThreadLocalORMSession.flush_all()
         old_pw_hash = user.password
-        r = self.app.post('/auth/password_recovery_hash', {'email': email._id})
+        with td.audits('Password recovery link sent to: test-admin@users.localhost'):
+            r = self.app.post('/auth/password_recovery_hash', {'email': email._id})
         hash = user.get_tool_data('AuthPasswordReset', 'hash')
         hash_expiry = user.get_tool_data('AuthPasswordReset', 'hash_expiry')
         assert hash is not None
@@ -744,7 +736,9 @@ class TestPasswordReset(TestController):
         assert_in('New Password (again):', r)
         form = r.forms[0]
         form['pw'] = form['pw2'] = new_password = '154321'
-        r = form.submit()
+        with td.audits('Password changed \(through recovery process\)'):
+            # escape parentheses, so they would not be treated as regex group
+            r = form.submit()
         user = M.User.query.get(username='test-admin')
         assert_not_equal(old_pw_hash, user.password)
         provider = plugin.LocalAuthenticationProvider(None)
@@ -768,13 +762,6 @@ To reset your password on %s, please visit the following URL:
         hash_expiry = user.get_tool_data('AuthPasswordReset', 'hash_expiry')
         assert_equal(hash, '')
         assert_equal(hash_expiry, '')
-        log = M.AuditLog.for_user(user).all()
-        assert_equal(len(log), 2)
-        messages = set([l.message for l in log])
-        assert_equal(
-            messages,
-            set(['Password recovery link sent to: test-admin@users.localhost',
-                 'Password changed (through recovery process)']))
 
     @patch('allura.tasks.mail_tasks.sendmail')
     @patch('allura.lib.helpers.gen_message_id')