From d9a961fe67c4dd658a96fd89dc2f4ff7493bd090 Mon Sep 17 00:00:00 2001
From: Robert Kuska <rkuska@redhat.com>
Date: Thu, 27 Jun 2013 10:08:07 +0200
Subject: [PATCH] Replace with statements with decorator

---
 coprs_frontend/tests/coprs_test_case.py            |  45 ++
 .../test_views/test_coprs_ns/test_coprs_builds.py  |  99 ++--
 .../test_views/test_coprs_ns/test_coprs_general.py | 557 +++++++++------------
 3 files changed, 325 insertions(+), 376 deletions(-)

diff --git a/coprs_frontend/tests/coprs_test_case.py b/coprs_frontend/tests/coprs_test_case.py
index 6659bbf..f7e0c4e 100644
--- a/coprs_frontend/tests/coprs_test_case.py
+++ b/coprs_frontend/tests/coprs_test_case.py
@@ -1,8 +1,10 @@
 import base64
 import os
 import time
+from functools import wraps
 
 import pytest
+import decorator
 
 import coprs
 
@@ -122,3 +124,46 @@ class CoprsTestCase(object):
                                 result=helpers.BackendResultEnum('success'),
                                 created_on=int(time.time()))
         self.db.session.add_all([self.a1, self.a2, self.a3])
+
+
+
+
+class TransactionDecorator(object):
+
+    """
+    This is decorator as a class.
+
+    Its purpose is to replace repetative lines of 'with' statements
+    in test's functions. Everytime you find your self writing test function 
+    which uses following 'with's construct:
+
+    with self.tc as test_client:
+        with c.session_transaction() as session:
+            session['openid'] = self.u.openid_name 
+
+    where 'u' stands for any user from 'f_users' fixture, use this to decorate
+    your test function:
+
+    @TransactionDecorator('u')
+    def test_function_without_with_statements(self, f_users):
+        # write code as you were in with 'self.tc as test_client' indent
+        # you can also access object 'test_client' through 'self.test_client'
+
+    where decorator parameter ''u'' stands for string representation of any 
+    user from 'f_users' fixture from which you wish to store 'openid_name'.
+    Please note that you **must** include 'f_users' fixture in decorated 
+    function parameters.
+
+    """
+
+    def __init__(self, user):
+        self.user = user
+
+    def __call__(self, fn):
+        @wraps(fn)
+        def wrapper(fn, fn_self, *args):
+            with fn_self.tc as fn_self.test_client:
+                with fn_self.test_client.session_transaction() as session:
+                    session['openid'] = getattr(fn_self, self.user).openid_name
+                return fn(fn_self, *args)
+        return decorator.decorator(wrapper, fn)
diff --git a/coprs_frontend/tests/test_views/test_coprs_ns/test_coprs_builds.py b/coprs_frontend/tests/test_views/test_coprs_ns/test_coprs_builds.py
index 574ca57..9d38fa5 100644
--- a/coprs_frontend/tests/test_views/test_coprs_ns/test_coprs_builds.py
+++ b/coprs_frontend/tests/test_views/test_coprs_ns/test_coprs_builds.py
@@ -1,6 +1,6 @@
 import flask
 
-from tests.coprs_test_case import CoprsTestCase
+from tests.coprs_test_case import CoprsTestCase, TransactionDecorator
 
 class TestCoprShowBuilds(CoprsTestCase):
     def test_copr_show_builds(self, f_users, f_coprs, f_builds, f_db):
@@ -8,71 +8,56 @@ class TestCoprShowBuilds(CoprsTestCase):
         assert r.data.count('<tr class="build-') == 2
 
 class TestCoprAddBuild(CoprsTestCase):
+    @TransactionDecorator('u1')
     def test_copr_owner_can_add_build(self, f_users, f_coprs, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add_all([self.u1, self.c1])
-            r = c.post('/coprs/detail/{0}/{1}/new_build/'.format(self.u1.name, self.c1.name),
-                      data = {'pkgs': 'http://testing'},
-                      follow_redirects = True)
-            assert self.models.Build.query.first().pkgs == 'http://testing'
+        self.db.session.add_all([self.u1, self.c1])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/new_build/'.format(self.u1.name, self.c1.name),
+                  data = {'pkgs': 'http://testing'},
+                  follow_redirects = True)
+        assert self.models.Build.query.first().pkgs == 'http://testing'
 
+    @TransactionDecorator('u1')
     def test_copr_allowed_user_can_add_build(self, f_users, f_coprs, f_copr_permissions, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add_all([self.u2, self.c2])
-            r = c.post('/coprs/detail/{0}/{1}/new_build/'.format(self.u2.name, self.c2.name),
-                      data = {'pkgs': 'http://testing'},
-                      follow_redirects = True)
-            assert self.models.Build.query.first().pkgs == 'http://testing'
-
+        self.db.session.add_all([self.u2, self.c2])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/new_build/'.format(self.u2.name, self.c2.name),
+                  data = {'pkgs': 'http://testing'},
+                  follow_redirects = True)
+        assert self.models.Build.query.first().pkgs == 'http://testing'
+
+    
+    @TransactionDecorator('u1')
     def test_copr_not_yet_allowed_user_cant_add_build(self, f_users, f_coprs, f_copr_permissions, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add_all([self.u2, self.c3])
-            r = c.post('/coprs/detail/{0}/{1}/new_build/'.format(self.u2.name, self.c3.name),
-                      data = {'pkgs': 'http://testing'},
-                      follow_redirects = True)
-            assert not self.models.Build.query.first()
+        self.db.session.add_all([self.u2, self.c3])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/new_build/'.format(self.u2.name, self.c3.name),
+                  data = {'pkgs': 'http://testing'},
+                  follow_redirects = True)
+        assert not self.models.Build.query.first()
 
+    @TransactionDecorator('u3')
     def test_copr_user_without_permission_cant_add_build(self, f_users, f_coprs, f_copr_permissions, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u3.openid_name
-
-            self.db.session.add_all([self.u1, self.c1])
-            r = c.post('/coprs/detail/{0}/{1}/new_build/'.format(self.u1.name, self.c1.name),
-                      data = {'pkgs': 'http://testing'},
-                      follow_redirects = True)
-            assert not self.models.Build.query.first()
+        self.db.session.add_all([self.u1, self.c1])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/new_build/'.format(self.u1.name, self.c1.name),
+                  data = {'pkgs': 'http://testing'},
+                  follow_redirects = True)
+        assert not self.models.Build.query.first()
 
 class TestCoprCancelBuild(CoprsTestCase):
+
+    @TransactionDecorator('u1')
     def test_copr_build_submitter_can_cancel_build(self, f_users, f_coprs, f_builds, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
+        self.db.session.add_all([self.u1, self.c1, self.b1])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/cancel_build/{2}/'.format(self.u1.name, self.c1.name, self.b1.id),
+                  data = {},
+                  follow_redirects = True)
+        self.db.session.add(self.b1)
+        assert self.b1.canceled == True
 
-            self.db.session.add_all([self.u1, self.c1, self.b1])
-            r = c.post('/coprs/detail/{0}/{1}/cancel_build/{2}/'.format(self.u1.name, self.c1.name, self.b1.id),
-                      data = {},
-                      follow_redirects = True)
-            self.db.session.add(self.b1)
-            assert self.b1.canceled == True
 
+    @TransactionDecorator('u2')
     def test_copr_build_non_submitter_cannot_cancel_build(self, f_users, f_coprs, f_builds, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u2.openid_name
-
-            self.db.session.add_all([self.u1, self.c1, self.b1])
-            r = c.post('/coprs/detail/{0}/{1}/cancel_build/{2}/'.format(self.u1.name, self.c1.name, self.b1.id),
-                      data = {},
-                      follow_redirects = True)
-            self.db.session.add(self.b1)
-            assert self.b1.canceled == False
+        self.db.session.add_all([self.u1, self.c1, self.b1])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/cancel_build/{2}/'.format(self.u1.name, self.c1.name, self.b1.id),
+                  data = {},
+                  follow_redirects = True)
+        self.db.session.add(self.b1)
+        assert self.b1.canceled == False
diff --git a/coprs_frontend/tests/test_views/test_coprs_ns/test_coprs_general.py b/coprs_frontend/tests/test_views/test_coprs_ns/test_coprs_general.py
index 906ece1..00551dd 100644
--- a/coprs_frontend/tests/test_views/test_coprs_ns/test_coprs_general.py
+++ b/coprs_frontend/tests/test_views/test_coprs_ns/test_coprs_general.py
@@ -5,7 +5,7 @@ from flexmock import flexmock
 
 from coprs.signals import copr_created
 
-from tests.coprs_test_case import CoprsTestCase
+from tests.coprs_test_case import CoprsTestCase, TransactionDecorator
 
 class TestCoprsShow(CoprsTestCase):
     def test_show_no_entries(self):
@@ -16,140 +16,109 @@ class TestCoprsShow(CoprsTestCase):
         assert r.data.count('<div class="copr">') == 3
 
 class TestCoprsOwned(CoprsTestCase):
+    @TransactionDecorator('u3')
     def test_owned_none(self, f_users, f_coprs, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u3.openid_name
-
-            self.db.session.add(self.u3)
-            r = c.get('/coprs/owned/{0}/'.format(self.u3.name))
-            assert 'No coprs...' in r.data
+        self.db.session.add(self.u3)
+        r = self.test_client.get('/coprs/owned/{0}/'.format(self.u3.name))
+        assert 'No coprs...' in r.data
 
+    @TransactionDecorator('u1')
     def test_owned_one(self, f_users, f_coprs, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add(self.u1)
-            r = c.get('/coprs/owned/{0}/'.format(self.u1.name))
-            assert r.data.count('<div class="copr">') == 1
+        self.db.session.add(self.u1)
+        r = self.test_client.get('/coprs/owned/{0}/'.format(self.u1.name))
+        assert r.data.count('<div class="copr">') == 1
 
 class TestCoprsAllowed(CoprsTestCase):
-    def test_allowed_none(self, f_users, f_coprs, f_copr_permissions, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u3.openid_name
 
-            self.db.session.add(self.u3)
-            r = c.get('/coprs/allowed/{0}/'.format(self.u3.name))
-            assert 'No coprs...' in r.data
+    @TransactionDecorator('u3')
+    def test_allowed_none(self, f_users, f_coprs, f_copr_permissions, f_db):
+        self.db.session.add(self.u3)
+        r = self.test_client.get('/coprs/allowed/{0}/'.format(self.u3.name))
+        assert 'No coprs...' in r.data
 
+    @TransactionDecorator('u2')
     def test_allowed_one(self, f_users, f_coprs, f_copr_permissions, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u2.openid_name
-
-            self.db.session.add(self.u1)
-            r = c.get('/coprs/allowed/{0}/'.format(self.u1.name))
-            assert r.data.count('<div class="copr">') == 1
+        self.db.session.add(self.u1)
+        r = self.test_client.get('/coprs/allowed/{0}/'.format(self.u1.name))
+        assert r.data.count('<div class="copr">') == 1
 
+    @TransactionDecorator('u1')
     def test_allowed_one_but_asked_for_one_more(self, f_users, f_coprs, f_copr_permissions, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add(self.u1)
-            r = c.get('/coprs/allowed/{0}/'.format(self.u1.name))
-            assert r.data.count('<div class="copr">') == 1
+        self.db.session.add(self.u1)
+        r = self.test_client.get('/coprs/allowed/{0}/'.format(self.u1.name))
+        assert r.data.count('<div class="copr">') == 1
 
 class TestCoprNew(CoprsTestCase):
     success_string = 'New copr was successfully created'
 
+    @TransactionDecorator('u1')
     def test_copr_new_normal(self, f_users, f_mock_chroots, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
+        r = self.test_client.post('/coprs/new/', data = {'name': 'foo', 'fedora-rawhide-i386': 'y', 'arches': ['i386']}, follow_redirects = True)
+        assert self.models.Copr.query.filter(self.models.Copr.name == 'foo').first()
+        assert self.success_string in r.data
 
-            r = c.post('/coprs/new/', data = {'name': 'foo', 'fedora-rawhide-i386': 'y', 'arches': ['i386']}, follow_redirects = True)
-            assert self.models.Copr.query.filter(self.models.Copr.name == 'foo').first()
-            assert self.success_string in r.data
-
-            # make sure no initial build was submitted
-            assert self.models.Build.query.first() == None
+        # make sure no initial build was submitted
+        assert self.models.Build.query.first() == None
 
+    @TransactionDecorator('u1')
     def test_copr_new_emits_signal(self, f_users, f_mock_chroots, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-            # TODO: this should probably be mocked...
-            signals_received = []
-            def test_receiver(sender, **kwargs):
-                signals_received.append(kwargs['copr'])
-            copr_created.connect(test_receiver)
-            r = c.post('/coprs/new/', data={'name': 'foo', 'fedora-rawhide-i386': 'y', 'arches': ['i386']}, follow_redirects=True)
-            assert len(signals_received) == 1
-            assert signals_received[0].name == 'foo'
+        # TODO: this should probably be mocked...
+        signals_received = []
+        def test_receiver(sender, **kwargs):
+            signals_received.append(kwargs['copr'])
+        copr_created.connect(test_receiver)
+        r = self.test_client.post('/coprs/new/', data={'name': 'foo', 'fedora-rawhide-i386': 'y', 'arches': ['i386']}, follow_redirects=True)
+        assert len(signals_received) == 1
+        assert signals_received[0].name == 'foo'
 
 
+    @TransactionDecorator('u3')
     def test_copr_new_exists_for_another_user(self, f_users, f_coprs, f_mock_chroots, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u3.openid_name
-
-            self.db.session.add(self.c1)
-            foocoprs = len(self.models.Copr.query.filter(self.models.Copr.name == self.c1.name).all())
-            assert foocoprs > 0
+        self.db.session.add(self.c1)
+        foocoprs = len(self.models.Copr.query.filter(self.models.Copr.name == self.c1.name).all())
+        assert foocoprs > 0
 
-            r = c.post('/coprs/new/', data = {'name': self.c1.name, 'fedora-rawhide-i386': 'y'}, follow_redirects = True)
-            self.db.session.add(self.c1)
-            assert len(self.models.Copr.query.filter(self.models.Copr.name == self.c1.name).all()) == foocoprs + 1
-            assert self.success_string in r.data
+        r = self.test_client.post('/coprs/new/', data = {'name': self.c1.name, 'fedora-rawhide-i386': 'y'}, follow_redirects = True)
+        self.db.session.add(self.c1)
+        assert len(self.models.Copr.query.filter(self.models.Copr.name == self.c1.name).all()) == foocoprs + 1
+        assert self.success_string in r.data
 
+    @TransactionDecorator('u1')
     def test_copr_new_exists_for_this_user(self, f_users, f_coprs, f_mock_chroots, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add(self.c1)
-            foocoprs = len(self.models.Copr.query.filter(self.models.Copr.name == self.c1.name).all())
-            assert foocoprs > 0
+        self.db.session.add(self.c1)
+        foocoprs = len(self.models.Copr.query.filter(self.models.Copr.name == self.c1.name).all())
+        assert foocoprs > 0
 
-            r = c.post('/coprs/new/', data = {'name': self.c1.name, 'fedora-rawhide-i386': 'y'}, follow_redirects = True)
-            self.db.session.add(self.c1)
-            assert len(self.models.Copr.query.filter(self.models.Copr.name == self.c1.name).all()) == foocoprs
-            assert "You already have copr named" in r.data
+        r = self.test_client.post('/coprs/new/', data = {'name': self.c1.name, 'fedora-rawhide-i386': 'y'}, follow_redirects = True)
+        self.db.session.add(self.c1)
+        assert len(self.models.Copr.query.filter(self.models.Copr.name == self.c1.name).all()) == foocoprs
+        assert "You already have copr named" in r.data
 
+    @TransactionDecorator('u1')
     def test_copr_new_with_initial_pkgs(self, f_users, f_mock_chroots, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
+        r = self.test_client.post('/coprs/new/', data = {'name': 'foo', 'fedora-rawhide-i386': 'y', 'initial_pkgs': ['http://f', 'http://b']}, follow_redirects = True)
+        copr = self.models.Copr.query.filter(self.models.Copr.name == 'foo').first()
+        assert copr
+        assert self.success_string in r.data
 
-            r = c.post('/coprs/new/', data = {'name': 'foo', 'fedora-rawhide-i386': 'y', 'initial_pkgs': ['http://f', 'http://b']}, follow_redirects = True)
-            copr = self.models.Copr.query.filter(self.models.Copr.name == 'foo').first()
-            assert copr
-            assert self.success_string in r.data
-
-            assert self.models.Build.query.first().copr == copr
-            assert copr.build_count == 1
-            assert 'Initial packages were successfully submitted' in r.data
+        assert self.models.Build.query.first().copr == copr
+        assert copr.build_count == 1
+        assert 'Initial packages were successfully submitted' in r.data
 
+    @TransactionDecorator('u1')
     def test_copr_new_is_allowed_even_if_deleted_has_same_name(self, f_users, f_coprs, f_mock_chroots, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add(self.c1)
-            self.c1.deleted = True
-            self.c1.owner = self.u1
-            self.db.session.commit()
+        self.db.session.add(self.c1)
+        self.c1.deleted = True
+        self.c1.owner = self.u1
+        self.db.session.commit()
 
-            self.db.session.add(self.c1)
-            r = c.post('/coprs/new/', data = {'name': self.c1.name, 'fedora-rawhide-i386': 'y', 'arches': ['i386']}, follow_redirects = True)
-            self.db.session.add_all([self.c1, self.u1])
-            assert len(self.models.Copr.query.filter(self.models.Copr.name==self.c1.name).\
-                                              filter(self.models.Copr.owner==self.u1).\
-                                              all()) == 2
-            assert self.success_string in r.data
+        self.db.session.add(self.c1)
+        r = self.test_client.post('/coprs/new/', data = {'name': self.c1.name, 'fedora-rawhide-i386': 'y', 'arches': ['i386']}, follow_redirects = True)
+        self.db.session.add_all([self.c1, self.u1])
+        assert len(self.models.Copr.query.filter(self.models.Copr.name==self.c1.name).\
+                                          filter(self.models.Copr.owner==self.u1).\
+                                          all()) == 2
+        assert self.success_string in r.data
 
 
 class TestCoprDetail(CoprsTestCase):
@@ -176,236 +145,194 @@ class TestCoprDetail(CoprsTestCase):
         assert '<td>{0}'.format(self.u3.name) in r.data
         assert '<td>{0}'.format(self.u1.name) in r.data
 
+    @TransactionDecorator('u1')
     def test_copr_detail_allows_asking_for_permissions(self, f_users, f_coprs, f_copr_permissions, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add_all([self.u2, self.c2])
-            r = c.get('/coprs/detail/{0}/{1}/permissions/'.format(self.u2.name, self.c2.name))
-            # u1 is approved builder, check for that
-            assert '/permissions_applier_change/' in r.data
+        self.db.session.add_all([self.u2, self.c2])
+        r = self.test_client.get('/coprs/detail/{0}/{1}/permissions/'.format(self.u2.name, self.c2.name))
+        # u1 is approved builder, check for that
+        assert '/permissions_applier_change/' in r.data
 
+    @TransactionDecorator('u2')
     def test_copr_detail_doesnt_allow_owner_to_ask_for_permissions(self, f_users, f_coprs, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u2.openid_name
-
-            self.db.session.add_all([self.u2, self.c2])
-            r = c.get('/coprs/detail/{0}/{1}/permissions/'.format(self.u2.name, self.c2.name))
-            assert '/permissions_applier_change/' not in r.data
+        self.db.session.add_all([self.u2, self.c2])
+        r = self.test_client.get('/coprs/detail/{0}/{1}/permissions/'.format(self.u2.name, self.c2.name))
+        assert '/permissions_applier_change/' not in r.data
 
+    @TransactionDecorator('u2')
     def test_detail_has_correct_permissions_form(self, f_users, f_coprs, f_copr_permissions, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u2.openid_name
-
-            self.db.session.add_all([self.u2, self.c3])
-            r = c.get('/coprs/detail/{0}/{1}/permissions/'.format(self.u2.name, self.c3.name))
+        self.db.session.add_all([self.u2, self.c3])
+        r = self.test_client.get('/coprs/detail/{0}/{1}/permissions/'.format(self.u2.name, self.c3.name))
 
-            assert r.data.count('nothing') == 2
-            assert '<select id="copr_builder_1" name="copr_builder_1">' in r.data
-            assert '<select id="copr_admin_1" name="copr_admin_1">' in r.data
+        assert r.data.count('nothing') == 2
+        assert '<select id="copr_builder_1" name="copr_builder_1">' in r.data
+        assert '<select id="copr_admin_1" name="copr_admin_1">' in r.data
 
     def test_copr_detail_doesnt_show_cancel_build_for_anonymous(self, f_users, f_coprs, f_builds, f_db):
         r = self.tc.get('/coprs/detail/{0}/{1}/'.format(self.u2.name, self.c2.name))
         assert '/cancel_build/' not in r.data
 
+    @TransactionDecorator('u1')
     def test_copr_detail_doesnt_allow_non_submitter_to_cancel_build(self, f_users, f_coprs, f_builds, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add_all([self.u2, self.c2])
-            r = c.get('/coprs/detail/{0}/{1}/builds/'.format(self.u2.name, self.c2.name))
-            assert '/cancel_build/' not in r.data
+        self.db.session.add_all([self.u2, self.c2])
+        r = self.test_client.get('/coprs/detail/{0}/{1}/builds/'.format(self.u2.name, self.c2.name))
+        assert '/cancel_build/' not in r.data
 
+    @TransactionDecorator('u2')
     def test_copr_detail_allows_submitter_to_cancel_build(self, f_users, f_coprs, f_builds, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u2.openid_name
-
-            self.db.session.add_all([self.u2, self.c2])
-            r = c.get('/coprs/detail/{0}/{1}/builds/'.format(self.u2.name, self.c2.name))
-            assert '/cancel_build/' in r.data
+        self.db.session.add_all([self.u2, self.c2])
+        r = self.test_client.get('/coprs/detail/{0}/{1}/builds/'.format(self.u2.name, self.c2.name))
+        assert '/cancel_build/' in r.data
 
 
 class TestCoprEdit(CoprsTestCase):
-    def test_edit_prefills_id(self, f_users, f_coprs, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
 
-            self.db.session.add_all([self.u1, self.c1])
-            r = c.get('/coprs/detail/{0}/{1}/edit/'.format(self.u1.name, self.c1.name))
-            # TODO: use some kind of html parsing library to look for the hidden input, this ties us
-            # to the precise format of the tag
-            assert '<input hidden id="id" name="id" type="hidden" value="{0}">'.format(self.c1.id) in r.data
+    @TransactionDecorator('u1')
+    def test_edit_prefills_id(self, f_users, f_coprs, f_db):
+        self.db.session.add_all([self.u1, self.c1])
+        r = self.test_client.get('/coprs/detail/{0}/{1}/edit/'.format(self.u1.name, self.c1.name))
+        # TODO: use some kind of html parsing library to look for the hidden input, this ties us
+        # to the precise format of the tag
+        assert '<input hidden id="id" name="id" type="hidden" value="{0}">'.format(self.c1.id) in r.data
 
 
 class TestCoprUpdate(CoprsTestCase):
-    def test_update_no_changes(self, f_users, f_coprs, f_mock_chroots, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
 
-            self.db.session.add_all([self.u1, self.c1])
-            r = c.post('/coprs/detail/{0}/{1}/update/'.format(self.u1.name, self.c1.name),
-                       data = {'name': self.c1.name, 'fedora-18-x86_64': 'y', 'id': self.c1.id},
-                       follow_redirects = True)
-            assert 'Copr was updated successfully' in r.data
+    @TransactionDecorator('u1')
+    def test_update_no_changes(self, f_users, f_coprs, f_mock_chroots, f_db):
+        self.db.session.add_all([self.u1, self.c1])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/update/'.format(self.u1.name, self.c1.name),
+                   data = {'name': self.c1.name, 'fedora-18-x86_64': 'y', 'id': self.c1.id},
+                   follow_redirects = True)
+        assert 'Copr was updated successfully' in r.data
 
+    @TransactionDecorator('u1')
     def test_copr_admin_can_update(self, f_users, f_coprs, f_copr_permissions, f_mock_chroots, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add_all([self.u2, self.c3])
-            r = c.post('/coprs/detail/{0}/{1}/update/'.format(self.u2.name, self.c3.name),
-                       data = {'name': self.c3.name, 'fedora-rawhide-i386': 'y', 'id': self.c3.id},
-                       follow_redirects = True)
-            assert 'Copr was updated successfully' in r.data
+        self.db.session.add_all([self.u2, self.c3])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/update/'.format(self.u2.name, self.c3.name),
+                   data = {'name': self.c3.name, 'fedora-rawhide-i386': 'y', 'id': self.c3.id},
+                   follow_redirects = True)
+        assert 'Copr was updated successfully' in r.data
 
+    @TransactionDecorator('u1')
     def test_update_multiple_chroots(self, f_users, f_coprs, f_copr_permissions, f_mock_chroots, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add_all([self.u1, self.c1, self.mc1, self.mc2, self.mc3])
-            r = c.post('/coprs/detail/{0}/{1}/update/'.format(self.u1.name, self.c1.name),
-                       data = {'name': self.c1.name, self.mc2.chroot_name: 'y', self.mc3.chroot_name: 'y', 'id': self.c1.id},
-                       follow_redirects = True)
-            assert 'Copr was updated successfully' in r.data
-            self.db.session.add_all([self.c1, self.mc1, self.mc2, self.mc3])
-            mock_chroots = self.models.MockChroot.query.join(self.models.CoprChroot).\
-                                                        filter(self.models.CoprChroot.copr_id==\
-                                                               self.c1.id).all()
-            mock_chroots_names = map(lambda x: x.chroot_name, mock_chroots)
-            assert self.mc2.chroot_name in mock_chroots_names
-            assert self.mc3.chroot_name in mock_chroots_names
-            assert self.mc1.chroot_name not in mock_chroots_names
-
+        self.db.session.add_all([self.u1, self.c1, self.mc1, self.mc2, self.mc3])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/update/'.format(self.u1.name, self.c1.name),
+                   data = {'name': self.c1.name, self.mc2.chroot_name: 'y', self.mc3.chroot_name: 'y', 'id': self.c1.id},
+                   follow_redirects = True)
+        assert 'Copr was updated successfully' in r.data
+        self.db.session.add_all([self.c1, self.mc1, self.mc2, self.mc3])
+        mock_chroots = self.models.MockChroot.query.join(self.models.CoprChroot).\
+                                                    filter(self.models.CoprChroot.copr_id==\
+                                                           self.c1.id).all()
+        mock_chroots_names = map(lambda x: x.chroot_name, mock_chroots)
+        assert self.mc2.chroot_name in mock_chroots_names
+        assert self.mc3.chroot_name in mock_chroots_names
+        assert self.mc1.chroot_name not in mock_chroots_names
+
+    @TransactionDecorator('u2')
     def test_update_deletes_multiple_chroots(self, f_users, f_coprs, f_copr_permissions, f_mock_chroots, f_db):
         # https://fedorahosted.org/copr/ticket/42
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u2.openid_name
-
-            self.db.session.add_all([self.u2, self.c2, self.mc1])
-            # add one more mock_chroot, so that we can remove two
-            cc = self.models.CoprChroot()
-            cc.mock_chroot = self.mc1
-            self.c2.copr_chroots.append(cc)
-
-            r = c.post('/coprs/detail/{0}/{1}/update/'.format(self.u2.name, self.c2.name),
-                       data = {'name': self.c2.name, self.mc1.chroot_name: 'y', 'id': self.c2.id},
-                       follow_redirects = True)
-            assert 'Copr was updated successfully' in r.data
-            self.db.session.add_all([self.c2, self.mc1])
-            mock_chroots = self.models.MockChroot.query.join(self.models.CoprChroot).\
-                                                        filter(self.models.CoprChroot.copr_id==\
-                                                               self.c2.id).all()
-            assert len(mock_chroots) == 1
+        self.db.session.add_all([self.u2, self.c2, self.mc1])
+        # add one more mock_chroot, so that we can remove two
+        cc = self.models.CoprChroot()
+        cc.mock_chroot = self.mc1
+        self.c2.copr_chroots.append(cc)
+
+        r = self.test_client.post('/coprs/detail/{0}/{1}/update/'.format(self.u2.name, self.c2.name),
+                   data = {'name': self.c2.name, self.mc1.chroot_name: 'y', 'id': self.c2.id},
+                   follow_redirects = True)
+        assert 'Copr was updated successfully' in r.data
+        self.db.session.add_all([self.c2, self.mc1])
+        mock_chroots = self.models.MockChroot.query.join(self.models.CoprChroot).\
+                                                    filter(self.models.CoprChroot.copr_id==\
+                                                           self.c2.id).all()
+        assert len(mock_chroots) == 1
 
 class TestCoprApplyForPermissions(CoprsTestCase):
+
+    @TransactionDecorator('u2')
     def test_apply(self, f_users, f_coprs, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u2.openid_name
-
-            self.db.session.add_all([self.u1, self.u2, self.c1])
-            r = c.post('/coprs/detail/{0}/{1}/permissions_applier_change/'.format(self.u1.name, self.c1.name),
-                       data = {'copr_builder': '1'},
-                       follow_redirects = True)
-            assert 'Successfuly updated' in r.data
-
-            self.db.session.add_all([self.u1, self.u2, self.c1])
-            new_perm = self.models.CoprPermission.query.filter(self.models.CoprPermission.user_id == self.u2.id).\
-                                                        filter(self.models.CoprPermission.copr_id == self.c1.id).\
-                                                        first()
-            assert new_perm.copr_builder == 1
-            assert new_perm.copr_admin == 0
+        self.db.session.add_all([self.u1, self.u2, self.c1])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/permissions_applier_change/'.format(self.u1.name, self.c1.name),
+                   data = {'copr_builder': '1'},
+                   follow_redirects = True)
+        assert 'Successfuly updated' in r.data
 
+        self.db.session.add_all([self.u1, self.u2, self.c1])
+        new_perm = self.models.CoprPermission.query.filter(self.models.CoprPermission.user_id == self.u2.id).\
+                                                    filter(self.models.CoprPermission.copr_id == self.c1.id).\
+                                                    first()
+        assert new_perm.copr_builder == 1
+        assert new_perm.copr_admin == 0
+
+
+    @TransactionDecorator('u1')
     def test_apply_doesnt_lower_other_values_from_admin_to_request(self, f_users, f_coprs, f_copr_permissions, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add_all([self.u1, self.u2, self.cp1, self.c2])
-            r = c.post('/coprs/detail/{0}/{1}/permissions_applier_change/'.format(self.u2.name, self.c2.name),
-                       data = {'copr_builder': 1, 'copr_admin': '1'},
-                       follow_redirects = True)
-            assert 'Successfuly updated' in r.data
-
-            self.db.session.add_all([self.u1, self.c2])
-            new_perm = self.models.CoprPermission.query.filter(self.models.CoprPermission.user_id == self.u1.id).\
-                                                        filter(self.models.CoprPermission.copr_id == self.c2.id).\
-                                                        first()
-            assert new_perm.copr_builder == 2
-            assert new_perm.copr_admin == 1
+        self.db.session.add_all([self.u1, self.u2, self.cp1, self.c2])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/permissions_applier_change/'.format(self.u2.name, self.c2.name),
+                   data = {'copr_builder': 1, 'copr_admin': '1'},
+                   follow_redirects = True)
+        assert 'Successfuly updated' in r.data
+
+        self.db.session.add_all([self.u1, self.c2])
+        new_perm = self.models.CoprPermission.query.filter(self.models.CoprPermission.user_id == self.u1.id).\
+                                                    filter(self.models.CoprPermission.copr_id == self.c2.id).\
+                                                    first()
+        assert new_perm.copr_builder == 2
+        assert new_perm.copr_admin == 1
 
 class TestCoprUpdatePermissions(CoprsTestCase):
-    def test_cancel_permission(self, f_users, f_coprs, f_copr_permissions, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u2.openid_name
 
-            self.db.session.add_all([self.u2, self.c2])
-            r = c.post('/coprs/detail/{0}/{1}/update_permissions/'.format(self.u2.name, self.c2.name),
-                       data = {'copr_builder_1': '0'},
-                       follow_redirects = True)
+    @TransactionDecorator('u2')
+    def test_cancel_permission(self, f_users, f_coprs, f_copr_permissions, f_db):
+        self.db.session.add_all([self.u2, self.c2])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/update_permissions/'.format(self.u2.name, self.c2.name),
+                   data = {'copr_builder_1': '0'},
+                   follow_redirects = True)
 
-            # very volatile, but will fail fast if something changes
-            check_string = '<select id="copr_builder_1" name="copr_builder_1"><option value="0">nothing</option><option value="1">'
-            check_string += 'request</option><option selected value="2">approved</option></select>'
-            assert check_string not in r.data
+        # very volatile, but will fail fast if something changes
+        check_string = '<select id="copr_builder_1" name="copr_builder_1"><option value="0">nothing</option><option value="1">'
+        check_string += 'request</option><option selected value="2">approved</option></select>'
+        assert check_string not in r.data
 
+    @TransactionDecorator('u2')
     def test_update_more_permissions(self, f_users, f_coprs, f_copr_permissions, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u2.openid_name
-
-            self.db.session.add_all([self.u2, self.c3])
-            r = c.post('/coprs/detail/{0}/{1}/update_permissions/'.format(self.u2.name, self.c3.name),
-                       data = {'copr_builder_1': '2', 'copr_admin_1': '1', 'copr_admin_3': '2'},
-                       follow_redirects = True)
-            self.db.session.add_all([self.c3, self.u1, self.u3])
-            u1_c3_perms = self.models.CoprPermission.query.filter(self.models.CoprPermission.copr_id == self.c3.id).\
-                                                           filter(self.models.CoprPermission.user_id == self.u1.id).\
-                                                           first()
-            assert u1_c3_perms.copr_builder == self.helpers.PermissionEnum('approved')
-            assert u1_c3_perms.copr_admin == self.helpers.PermissionEnum('request')
-
-            u3_c3_perms = self.models.CoprPermission.query.filter(self.models.CoprPermission.copr_id == self.c3.id).\
-                                                           filter(self.models.CoprPermission.user_id == self.u3.id).\
-                                                           first()
-            assert u3_c3_perms.copr_builder == self.helpers.PermissionEnum('nothing')
-            assert u3_c3_perms.copr_admin == self.helpers.PermissionEnum('approved')
-
+        self.db.session.add_all([self.u2, self.c3])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/update_permissions/'.format(self.u2.name, self.c3.name),
+                   data = {'copr_builder_1': '2', 'copr_admin_1': '1', 'copr_admin_3': '2'},
+                   follow_redirects = True)
+        self.db.session.add_all([self.c3, self.u1, self.u3])
+        u1_c3_perms = self.models.CoprPermission.query.filter(self.models.CoprPermission.copr_id == self.c3.id).\
+                                                       filter(self.models.CoprPermission.user_id == self.u1.id).\
+                                                       first()
+        assert u1_c3_perms.copr_builder == self.helpers.PermissionEnum('approved')
+        assert u1_c3_perms.copr_admin == self.helpers.PermissionEnum('request')
+
+        u3_c3_perms = self.models.CoprPermission.query.filter(self.models.CoprPermission.copr_id == self.c3.id).\
+                                                       filter(self.models.CoprPermission.user_id == self.u3.id).\
+                                                       first()
+        assert u3_c3_perms.copr_builder == self.helpers.PermissionEnum('nothing')
+        assert u3_c3_perms.copr_admin == self.helpers.PermissionEnum('approved')
+
+    @TransactionDecorator('u1')
     def test_copr_admin_can_update_permissions(self, f_users, f_coprs, f_copr_permissions, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add_all([self.u2, self.c3])
-            r = c.post('/coprs/detail/{0}/{1}/update_permissions/'.format(self.u2.name, self.c3.name),
-                       data = {'copr_builder_1': '2', 'copr_admin_3': '2'},
-                       follow_redirects = True)
-            assert 'Copr permissions were updated' in r.data
+        self.db.session.add_all([self.u2, self.c3])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/update_permissions/'.format(self.u2.name, self.c3.name),
+                   data = {'copr_builder_1': '2', 'copr_admin_3': '2'},
+                   follow_redirects = True)
+        assert 'Copr permissions were updated' in r.data
 
+    @TransactionDecorator('u1')
     def test_copr_admin_can_give_up_his_permissions(self, f_users, f_coprs, f_copr_permissions, f_db):
         # if admin is giving up his permission and there are more permissions for
         # this copr, then if the admin is altered first, he won't be permitted
         # to alter the other permissions and the whole update would fail
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
         self.db.session.add_all([self.u2, self.c3, self.cp2, self.cp3])
         # mock out the order of CoprPermission objects, so that we are sure
         # the admin is the first one and therefore this fails if
         # the view doesn't reorder the permissions
         flexmock(self.models.Copr, copr_permissions=[self.cp3, self.cp2])
-        r = c.post('/coprs/detail/{0}/{1}/update_permissions/'.format(self.u2.name, self.c3.name),
+        r = self.test_client.post('/coprs/detail/{0}/{1}/update_permissions/'.format(self.u2.name, self.c3.name),
                    data = {'copr_admin_1': '1', 'copr_admin_3': '1'},
                    follow_redirects = True)
         self.db.session.add_all([self.u1, self.c3])
@@ -417,46 +344,38 @@ class TestCoprUpdatePermissions(CoprsTestCase):
 
 
 class TestCoprDelete(CoprsTestCase):
+
+    @TransactionDecorator('u1')
     def test_delete(self, f_users, f_coprs, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add_all([self.u1, self.c1])
-            r = c.post('/coprs/detail/{0}/{1}/delete/'.format(self.u1.name, self.c1.name),
-                       data = {'verify': 'yes'},
-                       follow_redirects = True)
-            assert 'Copr was deleted successfully' in r.data
-            self.db.session.add(self.c1)
-            assert self.models.Action.query.first().id == self.c1.id
-            assert self.models.Copr.query.filter(self.models.Copr.id==self.c1.id).first().deleted
+        self.db.session.add_all([self.u1, self.c1])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/delete/'.format(self.u1.name, self.c1.name),
+                   data = {'verify': 'yes'},
+                   follow_redirects = True)
+        assert 'Copr was deleted successfully' in r.data
+        self.db.session.add(self.c1)
+        assert self.models.Action.query.first().id == self.c1.id
+        assert self.models.Copr.query.filter(self.models.Copr.id==self.c1.id).first().deleted
 
+    @TransactionDecorator('u1')
     def test_copr_delete_does_not_delete_if_verify_filled_wrongly(self, f_users, f_coprs, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u1.openid_name
-
-            self.db.session.add_all([self.u1, self.c1])
-            r = c.post('/coprs/detail/{0}/{1}/delete/'.format(self.u1.name, self.c1.name),
-                       data = {'verify': 'no'},
-                       follow_redirects = True)
-            assert 'Copr was deleted successfully' not in r.data
-            assert not self.models.Action.query.first()
-            assert self.models.Copr.query.filter(self.models.Copr.id==self.c1.id).first()
+        self.db.session.add_all([self.u1, self.c1])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/delete/'.format(self.u1.name, self.c1.name),
+                   data = {'verify': 'no'},
+                   follow_redirects = True)
+        assert 'Copr was deleted successfully' not in r.data
+        assert not self.models.Action.query.first()
+        assert self.models.Copr.query.filter(self.models.Copr.id==self.c1.id).first()
 
+    @TransactionDecorator('u2')
     def test_non_owner_cant_delete(self, f_users, f_coprs, f_db):
-        with self.tc as c:
-            with c.session_transaction() as s:
-                s['openid'] = self.u2.openid_name
-
-            self.db.session.add_all([self.u1, self.u2, self.c1])
-            r = c.post('/coprs/detail/{0}/{1}/delete/'.format(self.u1.name, self.c1.name),
-                       data = {'verify': 'yes'},
-                       follow_redirects = True)
-            self.db.session.add_all([self.c1])
-            assert 'Copr was deleted successfully' not in r.data
-            assert not self.models.Action.query.first()
-            assert self.models.Copr.query.filter(self.models.Copr.id==self.c1.id).first()
+        self.db.session.add_all([self.u1, self.u2, self.c1])
+        r = self.test_client.post('/coprs/detail/{0}/{1}/delete/'.format(self.u1.name, self.c1.name),
+                   data = {'verify': 'yes'},
+                   follow_redirects = True)
+        self.db.session.add_all([self.c1])
+        assert 'Copr was deleted successfully' not in r.data
+        assert not self.models.Action.query.first()
+        assert self.models.Copr.query.filter(self.models.Copr.id==self.c1.id).first()
 
 class TestCoprRepoGeneration(CoprsTestCase):
     @pytest.fixture
-- 
1.8.1.4

