##// END OF EJS Templates
tests: moved tests of admin user auth tokens into pyramid apps.
marcink -
r1519:897366ac default
parent child Browse files
Show More
1 NO CONTENT: new file 100644
@@ -0,0 +1,114 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
21 import pytest
22
23 from rhodecode.model.db import User, UserApiKeys
24
25 from rhodecode.apps._base import ADMIN_PREFIX
26 from rhodecode.tests import (
27 TestController, TEST_USER_REGULAR_LOGIN, assert_session_flash)
28 from rhodecode.tests.fixture import Fixture
29 from rhodecode.tests.utils import AssertResponse
30
31 fixture = Fixture()
32
33
34
35 def route_path(name, **kwargs):
36 return {
37 'users':
38 ADMIN_PREFIX + '/users',
39 'users_data':
40 ADMIN_PREFIX + '/users_data',
41 'edit_user_auth_tokens':
42 ADMIN_PREFIX + '/users/{user_id}/edit/auth_tokens',
43 'edit_user_auth_tokens_add':
44 ADMIN_PREFIX + '/users/{user_id}/edit/auth_tokens/new',
45 'edit_user_auth_tokens_delete':
46 ADMIN_PREFIX + '/users/{user_id}/edit/auth_tokens/delete',
47 }[name].format(**kwargs)
48
49
50 class TestAdminUsersView(TestController):
51
52 def test_auth_tokens_default_user(self):
53 self.log_user()
54 user = User.get_default_user()
55 response = self.app.get(
56 route_path('edit_user_auth_tokens', user_id=user.user_id),
57 status=302)
58
59 def test_auth_tokens(self):
60 self.log_user()
61
62 user = User.get_by_username(TEST_USER_REGULAR_LOGIN)
63 response = self.app.get(
64 route_path('edit_user_auth_tokens', user_id=user.user_id))
65 for token in user.auth_tokens:
66 response.mustcontain(token)
67 response.mustcontain('never')
68
69 @pytest.mark.parametrize("desc, lifetime", [
70 ('forever', -1),
71 ('5mins', 60*5),
72 ('30days', 60*60*24*30),
73 ])
74 def test_add_auth_token(self, desc, lifetime, user_util):
75 self.log_user()
76 user = user_util.create_user()
77 user_id = user.user_id
78
79 response = self.app.post(
80 route_path('edit_user_auth_tokens_add', user_id=user_id),
81 {'description': desc, 'lifetime': lifetime,
82 'csrf_token': self.csrf_token})
83 assert_session_flash(response, 'Auth token successfully created')
84
85 response = response.follow()
86 user = User.get(user_id)
87 for auth_token in user.auth_tokens:
88 response.mustcontain(auth_token)
89
90 def test_delete_auth_token(self, user_util):
91 self.log_user()
92 user = user_util.create_user()
93 user_id = user.user_id
94 keys = user.extra_auth_tokens
95 assert 2 == len(keys)
96
97 response = self.app.post(
98 route_path('edit_user_auth_tokens_add', user_id=user_id),
99 {'description': 'desc', 'lifetime': -1,
100 'csrf_token': self.csrf_token})
101 assert_session_flash(response, 'Auth token successfully created')
102 response.follow()
103
104 # now delete our key
105 keys = UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all()
106 assert 3 == len(keys)
107
108 response = self.app.post(
109 route_path('edit_user_auth_tokens_delete', user_id=user_id),
110 {'del_auth_token': keys[0].api_key, 'csrf_token': self.csrf_token})
111
112 assert_session_flash(response, 'Auth token successfully deleted')
113 keys = UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all()
114 assert 2 == len(keys)
@@ -44,10 +44,6 b' class TestAdminUsersController(TestContr'
44 44 def teardown_method(cls, method):
45 45 fixture.destroy_users(cls.destroy_users)
46 46
47 def test_index(self):
48 self.log_user()
49 self.app.get(url('users'))
50
51 47 def test_create(self):
52 48 self.log_user()
53 49 username = 'newtestuser'
@@ -563,58 +559,3 b' class TestAdminUsersController(TestContr'
563 559 response.mustcontain('All IP addresses are allowed')
564 560 response.mustcontain(no=[ip])
565 561 response.mustcontain(no=[ip_range])
566
567 def test_auth_tokens(self):
568 self.log_user()
569
570 user = User.get_by_username(TEST_USER_REGULAR_LOGIN)
571 response = self.app.get(
572 url('edit_user_auth_tokens', user_id=user.user_id))
573 for token in user.auth_tokens:
574 response.mustcontain(token)
575 response.mustcontain('never')
576
577 @pytest.mark.parametrize("desc, lifetime", [
578 ('forever', -1),
579 ('5mins', 60*5),
580 ('30days', 60*60*24*30),
581 ])
582 def test_add_auth_token(self, desc, lifetime, user_util):
583 self.log_user()
584 user = user_util.create_user()
585 user_id = user.user_id
586
587 response = self.app.post(
588 url('edit_user_auth_tokens', user_id=user_id),
589 {'_method': 'put', 'description': desc, 'lifetime': lifetime,
590 'csrf_token': self.csrf_token})
591 assert_session_flash(response, 'Auth token successfully created')
592
593 response = response.follow()
594 user = User.get(user_id)
595 for auth_token in user.auth_tokens:
596 response.mustcontain(auth_token)
597
598 def test_remove_auth_token(self, user_util):
599 self.log_user()
600 user = user_util.create_user()
601 user_id = user.user_id
602
603 response = self.app.post(
604 url('edit_user_auth_tokens', user_id=user_id),
605 {'_method': 'put', 'description': 'desc', 'lifetime': -1,
606 'csrf_token': self.csrf_token})
607 assert_session_flash(response, 'Auth token successfully created')
608 response = response.follow()
609
610 # now delete our key
611 keys = UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all()
612 assert 3 == len(keys)
613
614 response = self.app.post(
615 url('edit_user_auth_tokens', user_id=user_id),
616 {'_method': 'delete', 'del_auth_token': keys[0].api_key,
617 'csrf_token': self.csrf_token})
618 assert_session_flash(response, 'Auth token successfully deleted')
619 keys = UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all()
620 assert 2 == len(keys)
@@ -212,8 +212,6 b' def http_environ(http_host_stub):'
212 212
213 213 @pytest.fixture(scope='function')
214 214 def app(request, pylonsapp, http_environ):
215
216
217 215 app = CustomTestApp(
218 216 pylonsapp,
219 217 extra_environ=http_environ)
General Comments 0
You need to be logged in to leave comments. Login now