comparison kallithea/tests/functional/test_admin_users.py @ 6144:e56d11a19d3c

tests: admin_users: make sure repo group is deleted test_delete_repo_group_err creates then deletes a repository group. However, if the delete fails the repository group remains. This later causes problems in the model tests. Introduce a pytest yield fixture to handle the creation _and_ deletion of the repository group (suggested by Søren Løvborg). The creation of the user needs to happen _before_ that of the user group, and we cannot share data between two pytest fixtures, so the user is created in the fixture as well.
author Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
date Fri, 19 Aug 2016 22:38:19 +0200
parents 4d7dcd25c149
children 9b80c2a64781
comparison
equal deleted inserted replaced
6143:4d7dcd25c149 6144:e56d11a19d3c
10 # GNU General Public License for more details. 10 # GNU General Public License for more details.
11 # 11 #
12 # You should have received a copy of the GNU General Public License 12 # You should have received a copy of the GNU General Public License
13 # along with this program. If not, see <http://www.gnu.org/licenses/>. 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14
15 from sqlalchemy.orm.exc import NoResultFound 15 from sqlalchemy.orm.exc import NoResultFound, ObjectDeletedError
16 16
17 import pytest 17 import pytest
18 from kallithea.tests import * 18 from kallithea.tests import *
19 from kallithea.tests.fixture import Fixture 19 from kallithea.tests.fixture import Fixture
20 from kallithea.controllers.admin.users import UsersController 20 from kallithea.controllers.admin.users import UsersController
26 from kallithea.model.meta import Session 26 from kallithea.model.meta import Session
27 from webob.exc import HTTPNotFound 27 from webob.exc import HTTPNotFound
28 28
29 fixture = Fixture() 29 fixture = Fixture()
30 30
31 @pytest.yield_fixture
32 def user_and_repo_group_fail():
33 username = 'repogrouperr'
34 groupname = u'repogroup_fail'
35 user = fixture.create_user(name=username)
36 repo_group = fixture.create_repo_group(name=groupname, cur_user=username)
37 yield user, repo_group
38 # cleanup
39 try:
40 fixture.destroy_repo_group(repo_group)
41 except ObjectDeletedError:
42 # delete already succeeded in test body
43 pass
31 44
32 class TestAdminUsersController(TestController): 45 class TestAdminUsersController(TestController):
33 test_user_1 = 'testme' 46 test_user_1 = 'testme'
34 47
35 @classmethod 48 @classmethod
199 212
200 response = self.app.post(url('delete_user', id=new_user.user_id), 213 response = self.app.post(url('delete_user', id=new_user.user_id),
201 params={'_authentication_token': self.authentication_token()}) 214 params={'_authentication_token': self.authentication_token()})
202 self.checkSessionFlash(response, 'Successfully deleted user') 215 self.checkSessionFlash(response, 'Successfully deleted user')
203 216
204 def test_delete_repo_group_err(self): 217 def test_delete_repo_group_err(self, user_and_repo_group_fail):
205 self.log_user() 218 self.log_user()
206 username = 'repogrouperr' 219 username = 'repogrouperr'
207 groupname = u'repogroup_fail' 220 groupname = u'repogroup_fail'
208
209 fixture.create_user(name=username)
210 fixture.create_repo_group(name=groupname, cur_user=username)
211 221
212 new_user = Session().query(User) \ 222 new_user = Session().query(User) \
213 .filter(User.username == username).one() 223 .filter(User.username == username).one()
214 response = self.app.post(url('delete_user', id=new_user.user_id), 224 response = self.app.post(url('delete_user', id=new_user.user_id),
215 params={'_authentication_token': self.authentication_token()}) 225 params={'_authentication_token': self.authentication_token()})