Show More
@@ -1,176 +1,176 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import pytest |
|
22 | 22 | |
|
23 | 23 | from rhodecode.model.db import User, UserSshKeys |
|
24 | 24 | |
|
25 | 25 | from rhodecode.tests import TestController, assert_session_flash |
|
26 | 26 | from rhodecode.tests.fixture import Fixture |
|
27 | 27 | |
|
28 | 28 | fixture = Fixture() |
|
29 | 29 | |
|
30 | 30 | |
|
31 | 31 | def route_path(name, params=None, **kwargs): |
|
32 | 32 | import urllib |
|
33 | 33 | from rhodecode.apps._base import ADMIN_PREFIX |
|
34 | 34 | |
|
35 | 35 | base_url = { |
|
36 | 36 | 'edit_user_ssh_keys': |
|
37 | 37 | ADMIN_PREFIX + '/users/{user_id}/edit/ssh_keys', |
|
38 | 38 | 'edit_user_ssh_keys_generate_keypair': |
|
39 | 39 | ADMIN_PREFIX + '/users/{user_id}/edit/ssh_keys/generate', |
|
40 | 40 | 'edit_user_ssh_keys_add': |
|
41 | 41 | ADMIN_PREFIX + '/users/{user_id}/edit/ssh_keys/new', |
|
42 | 42 | 'edit_user_ssh_keys_delete': |
|
43 | 43 | ADMIN_PREFIX + '/users/{user_id}/edit/ssh_keys/delete', |
|
44 | 44 | |
|
45 | 45 | }[name].format(**kwargs) |
|
46 | 46 | |
|
47 | 47 | if params: |
|
48 | 48 | base_url = '{}?{}'.format(base_url, urllib.urlencode(params)) |
|
49 | 49 | return base_url |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | class TestAdminUsersSshKeysView(TestController): |
|
53 | 53 | INVALID_KEY = """\ |
|
54 | 54 | ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDk+77sjDzVeB6vevJsuZds1iNU5 |
|
55 | 55 | LANOa5CU5G/9JYIA6RYsWWMO7mbsR82IUckdqOHmxSykfR1D1TdluyIpQLrwgH5kb |
|
56 | 56 | n8FkVI8zBMCKakxowvN67B0R7b1BT4PPzW2JlOXei/m9W12ZY484VTow6/B+kf2Q8 |
|
57 | 57 | cP8tmCJmKWZma5Em7OTUhvjyQVNz3v7HfeY5Hq0Ci4ECJ59hepFDabJvtAXg9XrI6 |
|
58 | 58 | jvdphZTc30I4fG8+hBHzpeFxUGvSGNtXPUbwaAY8j/oHYrTpMgkj6pUEFsiKfC5zP |
|
59 | 59 | qPFR5HyKTCHW0nFUJnZsbyFT5hMiF/hZkJc9A0ZbdSvJwCRQ/g3bmdL |
|
60 | 60 | your_email@example.com |
|
61 | 61 | """ |
|
62 | 62 | VALID_KEY = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDk+77sjDzVeB6vev' \ |
|
63 | 63 | 'JsuZds1iNU5LANOa5CU5G/9JYIA6RYsWWMO7mbsR82IUckdqOHmxSy' \ |
|
64 | 64 | 'kfR1D1TdluyIpQLrwgH5kbn8FkVI8zBMCKakxowvN67B0R7b1BT4PP' \ |
|
65 | 65 | 'zW2JlOXei/m9W12ZY484VTow6/B+kf2Q8cP8tmCJmKWZma5Em7OTUh' \ |
|
66 | 66 | 'vjyQVNz3v7HfeY5Hq0Ci4ECJ59hepFDabJvtAXg9XrI6jvdphZTc30' \ |
|
67 | 67 | 'I4fG8+hBHzpeFxUGvSGNtXPUbwaAY8j/oHYrTpMgkj6pUEFsiKfC5zPq' \ |
|
68 | 68 | 'PFR5HyKTCHW0nFUJnZsbyFT5hMiF/hZkJc9A0ZbdSvJwCRQ/g3bmdL ' \ |
|
69 | 69 | 'your_email@example.com' |
|
70 | 70 | FINGERPRINT = 'MD5:01:4f:ad:29:22:6e:01:37:c9:d2:52:26:52:b0:2d:93' |
|
71 | 71 | |
|
72 | 72 | def test_ssh_keys_default_user(self): |
|
73 | 73 | self.log_user() |
|
74 | 74 | user = User.get_default_user() |
|
75 | 75 | self.app.get( |
|
76 | 76 | route_path('edit_user_ssh_keys', user_id=user.user_id), |
|
77 | 77 | status=302) |
|
78 | 78 | |
|
79 | 79 | def test_add_ssh_key_error(self, user_util): |
|
80 | 80 | self.log_user() |
|
81 | 81 | user = user_util.create_user() |
|
82 | 82 | user_id = user.user_id |
|
83 | 83 | |
|
84 | 84 | key_data = self.INVALID_KEY |
|
85 | 85 | |
|
86 | 86 | desc = 'MY SSH KEY' |
|
87 | 87 | response = self.app.post( |
|
88 | 88 | route_path('edit_user_ssh_keys_add', user_id=user_id), |
|
89 | 89 | {'description': desc, 'key_data': key_data, |
|
90 | 90 | 'csrf_token': self.csrf_token}) |
|
91 | 91 | assert_session_flash(response, 'An error occurred during ssh ' |
|
92 | 92 | 'key saving: Unable to decode the key') |
|
93 | 93 | |
|
94 | 94 | def test_ssh_key_duplicate(self, user_util): |
|
95 | 95 | self.log_user() |
|
96 | 96 | user = user_util.create_user() |
|
97 | 97 | user_id = user.user_id |
|
98 | 98 | |
|
99 | 99 | key_data = self.VALID_KEY |
|
100 | 100 | |
|
101 | 101 | desc = 'MY SSH KEY' |
|
102 | 102 | response = self.app.post( |
|
103 | 103 | route_path('edit_user_ssh_keys_add', user_id=user_id), |
|
104 | 104 | {'description': desc, 'key_data': key_data, |
|
105 | 105 | 'csrf_token': self.csrf_token}) |
|
106 | 106 | assert_session_flash(response, 'Ssh Key successfully created') |
|
107 | 107 | response.follow() # flush session flash |
|
108 | 108 | |
|
109 | 109 | # add the same key AGAIN |
|
110 | 110 | desc = 'MY SSH KEY' |
|
111 | 111 | response = self.app.post( |
|
112 | 112 | route_path('edit_user_ssh_keys_add', user_id=user_id), |
|
113 | 113 | {'description': desc, 'key_data': key_data, |
|
114 | 114 | 'csrf_token': self.csrf_token}) |
|
115 | 115 | |
|
116 | 116 | err = 'Such key with fingerprint `{}` already exists, ' \ |
|
117 | 117 | 'please use a different one'.format(self.FINGERPRINT) |
|
118 | 118 | assert_session_flash(response, 'An error occurred during ssh key ' |
|
119 | 119 | 'saving: {}'.format(err)) |
|
120 | 120 | |
|
121 | 121 | def test_add_ssh_key(self, user_util): |
|
122 | 122 | self.log_user() |
|
123 | 123 | user = user_util.create_user() |
|
124 | 124 | user_id = user.user_id |
|
125 | 125 | |
|
126 | 126 | key_data = self.VALID_KEY |
|
127 | 127 | |
|
128 | 128 | desc = 'MY SSH KEY' |
|
129 | 129 | response = self.app.post( |
|
130 | 130 | route_path('edit_user_ssh_keys_add', user_id=user_id), |
|
131 | 131 | {'description': desc, 'key_data': key_data, |
|
132 | 132 | 'csrf_token': self.csrf_token}) |
|
133 | 133 | assert_session_flash(response, 'Ssh Key successfully created') |
|
134 | 134 | |
|
135 | 135 | response = response.follow() |
|
136 | 136 | response.mustcontain(desc) |
|
137 | 137 | |
|
138 | 138 | def test_delete_ssh_key(self, user_util): |
|
139 | 139 | self.log_user() |
|
140 | 140 | user = user_util.create_user() |
|
141 | 141 | user_id = user.user_id |
|
142 | 142 | |
|
143 | 143 | key_data = self.VALID_KEY |
|
144 | 144 | |
|
145 | 145 | desc = 'MY SSH KEY' |
|
146 | 146 | response = self.app.post( |
|
147 | 147 | route_path('edit_user_ssh_keys_add', user_id=user_id), |
|
148 | 148 | {'description': desc, 'key_data': key_data, |
|
149 | 149 | 'csrf_token': self.csrf_token}) |
|
150 | 150 | assert_session_flash(response, 'Ssh Key successfully created') |
|
151 | 151 | response = response.follow() # flush the Session flash |
|
152 | 152 | |
|
153 | 153 | # now delete our key |
|
154 | 154 | keys = UserSshKeys.query().filter(UserSshKeys.user_id == user_id).all() |
|
155 | 155 | assert 1 == len(keys) |
|
156 | 156 | |
|
157 | 157 | response = self.app.post( |
|
158 | 158 | route_path('edit_user_ssh_keys_delete', user_id=user_id), |
|
159 | 159 | {'del_ssh_key': keys[0].ssh_key_id, |
|
160 | 160 | 'csrf_token': self.csrf_token}) |
|
161 | 161 | |
|
162 | 162 | assert_session_flash(response, 'Ssh key successfully deleted') |
|
163 | 163 | keys = UserSshKeys.query().filter(UserSshKeys.user_id == user_id).all() |
|
164 | 164 | assert 0 == len(keys) |
|
165 | 165 | |
|
166 | 166 | def test_generate_keypair(self, user_util): |
|
167 | 167 | self.log_user() |
|
168 | 168 | user = user_util.create_user() |
|
169 | 169 | user_id = user.user_id |
|
170 | 170 | |
|
171 | 171 | response = self.app.get( |
|
172 | 172 | route_path('edit_user_ssh_keys_generate_keypair', user_id=user_id)) |
|
173 | 173 | |
|
174 | 174 | response.mustcontain('Private key') |
|
175 | 175 | response.mustcontain('Public key') |
|
176 |
response.mustcontain('-----BEGIN |
|
|
176 | response.mustcontain('-----BEGIN PRIVATE KEY-----') |
@@ -1,163 +1,163 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import pytest |
|
22 | 22 | |
|
23 | 23 | from rhodecode.model.db import User, UserSshKeys |
|
24 | 24 | |
|
25 | 25 | from rhodecode.tests import TestController, assert_session_flash |
|
26 | 26 | from rhodecode.tests.fixture import Fixture |
|
27 | 27 | |
|
28 | 28 | fixture = Fixture() |
|
29 | 29 | |
|
30 | 30 | |
|
31 | 31 | def route_path(name, params=None, **kwargs): |
|
32 | 32 | import urllib |
|
33 | 33 | from rhodecode.apps._base import ADMIN_PREFIX |
|
34 | 34 | |
|
35 | 35 | base_url = { |
|
36 | 36 | 'my_account_ssh_keys': |
|
37 | 37 | ADMIN_PREFIX + '/my_account/ssh_keys', |
|
38 | 38 | 'my_account_ssh_keys_generate': |
|
39 | 39 | ADMIN_PREFIX + '/my_account/ssh_keys/generate', |
|
40 | 40 | 'my_account_ssh_keys_add': |
|
41 | 41 | ADMIN_PREFIX + '/my_account/ssh_keys/new', |
|
42 | 42 | 'my_account_ssh_keys_delete': |
|
43 | 43 | ADMIN_PREFIX + '/my_account/ssh_keys/delete', |
|
44 | 44 | }[name].format(**kwargs) |
|
45 | 45 | |
|
46 | 46 | if params: |
|
47 | 47 | base_url = '{}?{}'.format(base_url, urllib.urlencode(params)) |
|
48 | 48 | return base_url |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | class TestMyAccountSshKeysView(TestController): |
|
52 | 52 | INVALID_KEY = """\ |
|
53 | 53 | ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDk+77sjDzVeB6vevJsuZds1iNU5 |
|
54 | 54 | LANOa5CU5G/9JYIA6RYsWWMO7mbsR82IUckdqOHmxSykfR1D1TdluyIpQLrwgH5kb |
|
55 | 55 | n8FkVI8zBMCKakxowvN67B0R7b1BT4PPzW2JlOXei/m9W12ZY484VTow6/B+kf2Q8 |
|
56 | 56 | cP8tmCJmKWZma5Em7OTUhvjyQVNz3v7HfeY5Hq0Ci4ECJ59hepFDabJvtAXg9XrI6 |
|
57 | 57 | jvdphZTc30I4fG8+hBHzpeFxUGvSGNtXPUbwaAY8j/oHYrTpMgkj6pUEFsiKfC5zP |
|
58 | 58 | qPFR5HyKTCHW0nFUJnZsbyFT5hMiF/hZkJc9A0ZbdSvJwCRQ/g3bmdL |
|
59 | 59 | your_email@example.com |
|
60 | 60 | """ |
|
61 | 61 | VALID_KEY = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDk+77sjDzVeB6vev' \ |
|
62 | 62 | 'JsuZds1iNU5LANOa5CU5G/9JYIA6RYsWWMO7mbsR82IUckdqOHmxSy' \ |
|
63 | 63 | 'kfR1D1TdluyIpQLrwgH5kbn8FkVI8zBMCKakxowvN67B0R7b1BT4PP' \ |
|
64 | 64 | 'zW2JlOXei/m9W12ZY484VTow6/B+kf2Q8cP8tmCJmKWZma5Em7OTUh' \ |
|
65 | 65 | 'vjyQVNz3v7HfeY5Hq0Ci4ECJ59hepFDabJvtAXg9XrI6jvdphZTc30' \ |
|
66 | 66 | 'I4fG8+hBHzpeFxUGvSGNtXPUbwaAY8j/oHYrTpMgkj6pUEFsiKfC5zPq' \ |
|
67 | 67 | 'PFR5HyKTCHW0nFUJnZsbyFT5hMiF/hZkJc9A0ZbdSvJwCRQ/g3bmdL ' \ |
|
68 | 68 | 'your_email@example.com' |
|
69 | 69 | FINGERPRINT = 'MD5:01:4f:ad:29:22:6e:01:37:c9:d2:52:26:52:b0:2d:93' |
|
70 | 70 | |
|
71 | 71 | def test_add_ssh_key_error(self, user_util): |
|
72 | 72 | user = user_util.create_user(password='qweqwe') |
|
73 | 73 | self.log_user(user.username, 'qweqwe') |
|
74 | 74 | |
|
75 | 75 | key_data = self.INVALID_KEY |
|
76 | 76 | |
|
77 | 77 | desc = 'MY SSH KEY' |
|
78 | 78 | response = self.app.post( |
|
79 | 79 | route_path('my_account_ssh_keys_add'), |
|
80 | 80 | {'description': desc, 'key_data': key_data, |
|
81 | 81 | 'csrf_token': self.csrf_token}) |
|
82 | 82 | assert_session_flash(response, 'An error occurred during ssh ' |
|
83 | 83 | 'key saving: Unable to decode the key') |
|
84 | 84 | |
|
85 | 85 | def test_ssh_key_duplicate(self, user_util): |
|
86 | 86 | user = user_util.create_user(password='qweqwe') |
|
87 | 87 | self.log_user(user.username, 'qweqwe') |
|
88 | 88 | key_data = self.VALID_KEY |
|
89 | 89 | |
|
90 | 90 | desc = 'MY SSH KEY' |
|
91 | 91 | response = self.app.post( |
|
92 | 92 | route_path('my_account_ssh_keys_add'), |
|
93 | 93 | {'description': desc, 'key_data': key_data, |
|
94 | 94 | 'csrf_token': self.csrf_token}) |
|
95 | 95 | assert_session_flash(response, 'Ssh Key successfully created') |
|
96 | 96 | response.follow() # flush session flash |
|
97 | 97 | |
|
98 | 98 | # add the same key AGAIN |
|
99 | 99 | desc = 'MY SSH KEY' |
|
100 | 100 | response = self.app.post( |
|
101 | 101 | route_path('my_account_ssh_keys_add'), |
|
102 | 102 | {'description': desc, 'key_data': key_data, |
|
103 | 103 | 'csrf_token': self.csrf_token}) |
|
104 | 104 | |
|
105 | 105 | err = 'Such key with fingerprint `{}` already exists, ' \ |
|
106 | 106 | 'please use a different one'.format(self.FINGERPRINT) |
|
107 | 107 | assert_session_flash(response, 'An error occurred during ssh key ' |
|
108 | 108 | 'saving: {}'.format(err)) |
|
109 | 109 | |
|
110 | 110 | def test_add_ssh_key(self, user_util): |
|
111 | 111 | user = user_util.create_user(password='qweqwe') |
|
112 | 112 | self.log_user(user.username, 'qweqwe') |
|
113 | 113 | |
|
114 | 114 | key_data = self.VALID_KEY |
|
115 | 115 | |
|
116 | 116 | desc = 'MY SSH KEY' |
|
117 | 117 | response = self.app.post( |
|
118 | 118 | route_path('my_account_ssh_keys_add'), |
|
119 | 119 | {'description': desc, 'key_data': key_data, |
|
120 | 120 | 'csrf_token': self.csrf_token}) |
|
121 | 121 | assert_session_flash(response, 'Ssh Key successfully created') |
|
122 | 122 | |
|
123 | 123 | response = response.follow() |
|
124 | 124 | response.mustcontain(desc) |
|
125 | 125 | |
|
126 | 126 | def test_delete_ssh_key(self, user_util): |
|
127 | 127 | user = user_util.create_user(password='qweqwe') |
|
128 | 128 | user_id = user.user_id |
|
129 | 129 | self.log_user(user.username, 'qweqwe') |
|
130 | 130 | |
|
131 | 131 | key_data = self.VALID_KEY |
|
132 | 132 | |
|
133 | 133 | desc = 'MY SSH KEY' |
|
134 | 134 | response = self.app.post( |
|
135 | 135 | route_path('my_account_ssh_keys_add'), |
|
136 | 136 | {'description': desc, 'key_data': key_data, |
|
137 | 137 | 'csrf_token': self.csrf_token}) |
|
138 | 138 | assert_session_flash(response, 'Ssh Key successfully created') |
|
139 | 139 | response = response.follow() # flush the Session flash |
|
140 | 140 | |
|
141 | 141 | # now delete our key |
|
142 | 142 | keys = UserSshKeys.query().filter(UserSshKeys.user_id == user_id).all() |
|
143 | 143 | assert 1 == len(keys) |
|
144 | 144 | |
|
145 | 145 | response = self.app.post( |
|
146 | 146 | route_path('my_account_ssh_keys_delete'), |
|
147 | 147 | {'del_ssh_key': keys[0].ssh_key_id, |
|
148 | 148 | 'csrf_token': self.csrf_token}) |
|
149 | 149 | |
|
150 | 150 | assert_session_flash(response, 'Ssh key successfully deleted') |
|
151 | 151 | keys = UserSshKeys.query().filter(UserSshKeys.user_id == user_id).all() |
|
152 | 152 | assert 0 == len(keys) |
|
153 | 153 | |
|
154 | 154 | def test_generate_keypair(self, user_util): |
|
155 | 155 | user = user_util.create_user(password='qweqwe') |
|
156 | 156 | self.log_user(user.username, 'qweqwe') |
|
157 | 157 | |
|
158 | 158 | response = self.app.get( |
|
159 | 159 | route_path('my_account_ssh_keys_generate')) |
|
160 | 160 | |
|
161 | 161 | response.mustcontain('Private key') |
|
162 | 162 | response.mustcontain('Public key') |
|
163 |
response.mustcontain('-----BEGIN |
|
|
163 | response.mustcontain('-----BEGIN PRIVATE KEY-----') |
General Comments 0
You need to be logged in to leave comments.
Login now