Show More
@@ -1,152 +1,156 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2016-2020 RhodeCode GmbH |
|
3 | # Copyright (C) 2016-2020 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
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 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
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/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | import json |
|
21 | import json | |
|
22 | import os | |||
|
23 | ||||
22 | import mock |
|
24 | import mock | |
23 | import pytest |
|
25 | import pytest | |
24 |
|
26 | |||
25 | from rhodecode.apps.ssh_support.lib.backends.git import GitServer |
|
27 | from rhodecode.apps.ssh_support.lib.backends.git import GitServer | |
26 | from rhodecode.apps.ssh_support.tests.conftest import plain_dummy_env, plain_dummy_user |
|
28 | from rhodecode.apps.ssh_support.tests.conftest import plain_dummy_env, plain_dummy_user | |
27 |
|
29 | |||
28 |
|
30 | |||
29 | class GitServerCreator(object): |
|
31 | class GitServerCreator(object): | |
30 | root = '/tmp/repo/path/' |
|
32 | root = '/tmp/repo/path/' | |
31 | git_path = '/usr/local/bin/git' |
|
33 | git_path = '/usr/local/bin/git' | |
32 | config_data = { |
|
34 | config_data = { | |
33 | 'app:main': { |
|
35 | 'app:main': { | |
34 | 'ssh.executable.git': git_path, |
|
36 | 'ssh.executable.git': git_path, | |
35 | 'vcs.hooks.protocol': 'http', |
|
37 | 'vcs.hooks.protocol': 'http', | |
36 | } |
|
38 | } | |
37 | } |
|
39 | } | |
38 | repo_name = 'test_git' |
|
40 | repo_name = 'test_git' | |
39 | repo_mode = 'receive-pack' |
|
41 | repo_mode = 'receive-pack' | |
40 | user = plain_dummy_user() |
|
42 | user = plain_dummy_user() | |
41 |
|
43 | |||
42 | def __init__(self): |
|
44 | def __init__(self): | |
43 | def config_get(part, key): |
|
45 | def config_get(part, key): | |
44 | return self.config_data.get(part, {}).get(key) |
|
46 | return self.config_data.get(part, {}).get(key) | |
45 | self.config_mock = mock.Mock() |
|
47 | self.config_mock = mock.Mock() | |
46 | self.config_mock.get = mock.Mock(side_effect=config_get) |
|
48 | self.config_mock.get = mock.Mock(side_effect=config_get) | |
47 |
|
49 | |||
48 | def create(self, **kwargs): |
|
50 | def create(self, **kwargs): | |
49 | parameters = { |
|
51 | parameters = { | |
50 | 'store': self.root, |
|
52 | 'store': self.root, | |
51 | 'ini_path': '', |
|
53 | 'ini_path': '', | |
52 | 'user': self.user, |
|
54 | 'user': self.user, | |
53 | 'repo_name': self.repo_name, |
|
55 | 'repo_name': self.repo_name, | |
54 | 'repo_mode': self.repo_mode, |
|
56 | 'repo_mode': self.repo_mode, | |
55 | 'user_permissions': { |
|
57 | 'user_permissions': { | |
56 | self.repo_name: 'repository.admin' |
|
58 | self.repo_name: 'repository.admin' | |
57 | }, |
|
59 | }, | |
58 | 'config': self.config_mock, |
|
60 | 'config': self.config_mock, | |
59 | 'env': plain_dummy_env() |
|
61 | 'env': plain_dummy_env() | |
60 | } |
|
62 | } | |
61 | parameters.update(kwargs) |
|
63 | parameters.update(kwargs) | |
62 | server = GitServer(**parameters) |
|
64 | server = GitServer(**parameters) | |
63 | return server |
|
65 | return server | |
64 |
|
66 | |||
65 |
|
67 | |||
66 | @pytest.fixture() |
|
68 | @pytest.fixture() | |
67 | def git_server(app): |
|
69 | def git_server(app): | |
68 | return GitServerCreator() |
|
70 | return GitServerCreator() | |
69 |
|
71 | |||
70 |
|
72 | |||
71 | class TestGitServer(object): |
|
73 | class TestGitServer(object): | |
72 |
|
74 | |||
73 | def test_command(self, git_server): |
|
75 | def test_command(self, git_server): | |
74 | server = git_server.create() |
|
76 | server = git_server.create() | |
75 | expected_command = ( |
|
77 | expected_command = ( | |
76 | 'cd {root}; {git_path} {repo_mode} \'{root}{repo_name}\''.format( |
|
78 | 'cd {root}; {git_path} {repo_mode} \'{root}{repo_name}\''.format( | |
77 | root=git_server.root, git_path=git_server.git_path, |
|
79 | root=git_server.root, git_path=git_server.git_path, | |
78 | repo_mode=git_server.repo_mode, repo_name=git_server.repo_name) |
|
80 | repo_mode=git_server.repo_mode, repo_name=git_server.repo_name) | |
79 | ) |
|
81 | ) | |
80 | assert expected_command == server.tunnel.command() |
|
82 | assert expected_command == server.tunnel.command() | |
81 |
|
83 | |||
82 | @pytest.mark.parametrize('permissions, action, code', [ |
|
84 | @pytest.mark.parametrize('permissions, action, code', [ | |
83 | ({}, 'pull', -2), |
|
85 | ({}, 'pull', -2), | |
84 | ({'test_git': 'repository.read'}, 'pull', 0), |
|
86 | ({'test_git': 'repository.read'}, 'pull', 0), | |
85 | ({'test_git': 'repository.read'}, 'push', -2), |
|
87 | ({'test_git': 'repository.read'}, 'push', -2), | |
86 | ({'test_git': 'repository.write'}, 'push', 0), |
|
88 | ({'test_git': 'repository.write'}, 'push', 0), | |
87 | ({'test_git': 'repository.admin'}, 'push', 0), |
|
89 | ({'test_git': 'repository.admin'}, 'push', 0), | |
88 |
|
90 | |||
89 | ]) |
|
91 | ]) | |
90 | def test_permission_checks(self, git_server, permissions, action, code): |
|
92 | def test_permission_checks(self, git_server, permissions, action, code): | |
91 | server = git_server.create(user_permissions=permissions) |
|
93 | server = git_server.create(user_permissions=permissions) | |
92 | result = server._check_permissions(action) |
|
94 | result = server._check_permissions(action) | |
93 | assert result is code |
|
95 | assert result is code | |
94 |
|
96 | |||
95 | @pytest.mark.parametrize('permissions, value', [ |
|
97 | @pytest.mark.parametrize('permissions, value', [ | |
96 | ({}, False), |
|
98 | ({}, False), | |
97 | ({'test_git': 'repository.read'}, False), |
|
99 | ({'test_git': 'repository.read'}, False), | |
98 | ({'test_git': 'repository.write'}, True), |
|
100 | ({'test_git': 'repository.write'}, True), | |
99 | ({'test_git': 'repository.admin'}, True), |
|
101 | ({'test_git': 'repository.admin'}, True), | |
100 |
|
102 | |||
101 | ]) |
|
103 | ]) | |
102 | def test_has_write_permissions(self, git_server, permissions, value): |
|
104 | def test_has_write_permissions(self, git_server, permissions, value): | |
103 | server = git_server.create(user_permissions=permissions) |
|
105 | server = git_server.create(user_permissions=permissions) | |
104 | result = server.has_write_perm() |
|
106 | result = server.has_write_perm() | |
105 | assert result is value |
|
107 | assert result is value | |
106 |
|
108 | |||
107 | def test_run_returns_executes_command(self, git_server): |
|
109 | def test_run_returns_executes_command(self, git_server): | |
108 | server = git_server.create() |
|
110 | server = git_server.create() | |
109 | from rhodecode.apps.ssh_support.lib.backends.git import GitTunnelWrapper |
|
111 | from rhodecode.apps.ssh_support.lib.backends.git import GitTunnelWrapper | |
|
112 | ||||
|
113 | os.environ['SSH_CLIENT'] = '127.0.0.1' | |||
110 | with mock.patch.object(GitTunnelWrapper, 'create_hooks_env') as _patch: |
|
114 | with mock.patch.object(GitTunnelWrapper, 'create_hooks_env') as _patch: | |
111 | _patch.return_value = 0 |
|
115 | _patch.return_value = 0 | |
112 | with mock.patch.object(GitTunnelWrapper, 'command', return_value='date'): |
|
116 | with mock.patch.object(GitTunnelWrapper, 'command', return_value='date'): | |
113 | exit_code = server.run() |
|
117 | exit_code = server.run() | |
114 |
|
118 | |||
115 | assert exit_code == (0, False) |
|
119 | assert exit_code == (0, False) | |
116 |
|
120 | |||
117 | @pytest.mark.parametrize( |
|
121 | @pytest.mark.parametrize( | |
118 | 'repo_mode, action', [ |
|
122 | 'repo_mode, action', [ | |
119 | ['receive-pack', 'push'], |
|
123 | ['receive-pack', 'push'], | |
120 | ['upload-pack', 'pull'] |
|
124 | ['upload-pack', 'pull'] | |
121 | ]) |
|
125 | ]) | |
122 | def test_update_environment(self, git_server, repo_mode, action): |
|
126 | def test_update_environment(self, git_server, repo_mode, action): | |
123 | server = git_server.create(repo_mode=repo_mode) |
|
127 | server = git_server.create(repo_mode=repo_mode) | |
124 | store = server.store |
|
128 | store = server.store | |
125 |
|
129 | |||
126 | with mock.patch('os.environ', {'SSH_CLIENT': '10.10.10.10 b'}): |
|
130 | with mock.patch('os.environ', {'SSH_CLIENT': '10.10.10.10 b'}): | |
127 | with mock.patch('os.putenv') as putenv_mock: |
|
131 | with mock.patch('os.putenv') as putenv_mock: | |
128 | server.update_environment(action) |
|
132 | server.update_environment(action) | |
129 |
|
133 | |||
130 | expected_data = { |
|
134 | expected_data = { | |
131 | 'username': git_server.user.username, |
|
135 | 'username': git_server.user.username, | |
132 | 'user_id': git_server.user.user_id, |
|
136 | 'user_id': git_server.user.user_id, | |
133 | 'scm': 'git', |
|
137 | 'scm': 'git', | |
134 | 'repository': git_server.repo_name, |
|
138 | 'repository': git_server.repo_name, | |
135 | 'make_lock': None, |
|
139 | 'make_lock': None, | |
136 | 'action': action, |
|
140 | 'action': action, | |
137 | 'ip': '10.10.10.10', |
|
141 | 'ip': '10.10.10.10', | |
138 | 'locked_by': [None, None], |
|
142 | 'locked_by': [None, None], | |
139 | 'config': '', |
|
143 | 'config': '', | |
140 | 'repo_store': store, |
|
144 | 'repo_store': store, | |
141 | 'server_url': None, |
|
145 | 'server_url': None, | |
142 | 'hooks': ['push', 'pull'], |
|
146 | 'hooks': ['push', 'pull'], | |
143 | 'is_shadow_repo': False, |
|
147 | 'is_shadow_repo': False, | |
144 | 'hooks_module': 'rhodecode.lib.hooks_daemon', |
|
148 | 'hooks_module': 'rhodecode.lib.hooks_daemon', | |
145 | 'check_branch_perms': False, |
|
149 | 'check_branch_perms': False, | |
146 | 'detect_force_push': False, |
|
150 | 'detect_force_push': False, | |
147 | 'user_agent': u'ssh-user-agent', |
|
151 | 'user_agent': u'ssh-user-agent', | |
148 | 'SSH': True, |
|
152 | 'SSH': True, | |
149 | 'SSH_PERMISSIONS': 'repository.admin', |
|
153 | 'SSH_PERMISSIONS': 'repository.admin', | |
150 | } |
|
154 | } | |
151 | args, kwargs = putenv_mock.call_args |
|
155 | args, kwargs = putenv_mock.call_args | |
152 | assert json.loads(args[1]) == expected_data |
|
156 | assert json.loads(args[1]) == expected_data |
@@ -1,119 +1,120 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2016-2020 RhodeCode GmbH |
|
3 | # Copyright (C) 2016-2020 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
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 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
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/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | import os |
|
21 | import os | |
22 | import mock |
|
22 | import mock | |
23 | import pytest |
|
23 | import pytest | |
24 |
|
24 | |||
25 | from rhodecode.apps.ssh_support.lib.backends.hg import MercurialServer |
|
25 | from rhodecode.apps.ssh_support.lib.backends.hg import MercurialServer | |
26 | from rhodecode.apps.ssh_support.tests.conftest import plain_dummy_env, plain_dummy_user |
|
26 | from rhodecode.apps.ssh_support.tests.conftest import plain_dummy_env, plain_dummy_user | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | class MercurialServerCreator(object): |
|
29 | class MercurialServerCreator(object): | |
30 | root = '/tmp/repo/path/' |
|
30 | root = '/tmp/repo/path/' | |
31 | hg_path = '/usr/local/bin/hg' |
|
31 | hg_path = '/usr/local/bin/hg' | |
32 |
|
32 | |||
33 | config_data = { |
|
33 | config_data = { | |
34 | 'app:main': { |
|
34 | 'app:main': { | |
35 | 'ssh.executable.hg': hg_path, |
|
35 | 'ssh.executable.hg': hg_path, | |
36 | 'vcs.hooks.protocol': 'http', |
|
36 | 'vcs.hooks.protocol': 'http', | |
37 | } |
|
37 | } | |
38 | } |
|
38 | } | |
39 | repo_name = 'test_hg' |
|
39 | repo_name = 'test_hg' | |
40 | user = plain_dummy_user() |
|
40 | user = plain_dummy_user() | |
41 |
|
41 | |||
42 | def __init__(self): |
|
42 | def __init__(self): | |
43 | def config_get(part, key): |
|
43 | def config_get(part, key): | |
44 | return self.config_data.get(part, {}).get(key) |
|
44 | return self.config_data.get(part, {}).get(key) | |
45 | self.config_mock = mock.Mock() |
|
45 | self.config_mock = mock.Mock() | |
46 | self.config_mock.get = mock.Mock(side_effect=config_get) |
|
46 | self.config_mock.get = mock.Mock(side_effect=config_get) | |
47 |
|
47 | |||
48 | def create(self, **kwargs): |
|
48 | def create(self, **kwargs): | |
49 | parameters = { |
|
49 | parameters = { | |
50 | 'store': self.root, |
|
50 | 'store': self.root, | |
51 | 'ini_path': '', |
|
51 | 'ini_path': '', | |
52 | 'user': self.user, |
|
52 | 'user': self.user, | |
53 | 'repo_name': self.repo_name, |
|
53 | 'repo_name': self.repo_name, | |
54 | 'user_permissions': { |
|
54 | 'user_permissions': { | |
55 | 'test_hg': 'repository.admin' |
|
55 | 'test_hg': 'repository.admin' | |
56 | }, |
|
56 | }, | |
57 | 'config': self.config_mock, |
|
57 | 'config': self.config_mock, | |
58 | 'env': plain_dummy_env() |
|
58 | 'env': plain_dummy_env() | |
59 | } |
|
59 | } | |
60 | parameters.update(kwargs) |
|
60 | parameters.update(kwargs) | |
61 | server = MercurialServer(**parameters) |
|
61 | server = MercurialServer(**parameters) | |
62 | return server |
|
62 | return server | |
63 |
|
63 | |||
64 |
|
64 | |||
65 | @pytest.fixture() |
|
65 | @pytest.fixture() | |
66 | def hg_server(app): |
|
66 | def hg_server(app): | |
67 | return MercurialServerCreator() |
|
67 | return MercurialServerCreator() | |
68 |
|
68 | |||
69 |
|
69 | |||
70 | class TestMercurialServer(object): |
|
70 | class TestMercurialServer(object): | |
71 |
|
71 | |||
72 | def test_command(self, hg_server, tmpdir): |
|
72 | def test_command(self, hg_server, tmpdir): | |
73 | server = hg_server.create() |
|
73 | server = hg_server.create() | |
74 | custom_hgrc = os.path.join(str(tmpdir), 'hgrc') |
|
74 | custom_hgrc = os.path.join(str(tmpdir), 'hgrc') | |
75 | expected_command = ( |
|
75 | expected_command = ( | |
76 | 'cd {root}; HGRCPATH={custom_hgrc} {hg_path} -R {root}{repo_name} serve --stdio'.format( |
|
76 | 'cd {root}; HGRCPATH={custom_hgrc} {hg_path} -R {root}{repo_name} serve --stdio'.format( | |
77 | root=hg_server.root, custom_hgrc=custom_hgrc, hg_path=hg_server.hg_path, |
|
77 | root=hg_server.root, custom_hgrc=custom_hgrc, hg_path=hg_server.hg_path, | |
78 | repo_name=hg_server.repo_name) |
|
78 | repo_name=hg_server.repo_name) | |
79 | ) |
|
79 | ) | |
80 | server_command = server.tunnel.command(custom_hgrc) |
|
80 | server_command = server.tunnel.command(custom_hgrc) | |
81 | assert expected_command == server_command |
|
81 | assert expected_command == server_command | |
82 |
|
82 | |||
83 | @pytest.mark.parametrize('permissions, action, code', [ |
|
83 | @pytest.mark.parametrize('permissions, action, code', [ | |
84 | ({}, 'pull', -2), |
|
84 | ({}, 'pull', -2), | |
85 | ({'test_hg': 'repository.read'}, 'pull', 0), |
|
85 | ({'test_hg': 'repository.read'}, 'pull', 0), | |
86 | ({'test_hg': 'repository.read'}, 'push', -2), |
|
86 | ({'test_hg': 'repository.read'}, 'push', -2), | |
87 | ({'test_hg': 'repository.write'}, 'push', 0), |
|
87 | ({'test_hg': 'repository.write'}, 'push', 0), | |
88 | ({'test_hg': 'repository.admin'}, 'push', 0), |
|
88 | ({'test_hg': 'repository.admin'}, 'push', 0), | |
89 |
|
89 | |||
90 | ]) |
|
90 | ]) | |
91 | def test_permission_checks(self, hg_server, permissions, action, code): |
|
91 | def test_permission_checks(self, hg_server, permissions, action, code): | |
92 | server = hg_server.create(user_permissions=permissions) |
|
92 | server = hg_server.create(user_permissions=permissions) | |
93 | result = server._check_permissions(action) |
|
93 | result = server._check_permissions(action) | |
94 | assert result is code |
|
94 | assert result is code | |
95 |
|
95 | |||
96 | @pytest.mark.parametrize('permissions, value', [ |
|
96 | @pytest.mark.parametrize('permissions, value', [ | |
97 | ({}, False), |
|
97 | ({}, False), | |
98 | ({'test_hg': 'repository.read'}, False), |
|
98 | ({'test_hg': 'repository.read'}, False), | |
99 | ({'test_hg': 'repository.write'}, True), |
|
99 | ({'test_hg': 'repository.write'}, True), | |
100 | ({'test_hg': 'repository.admin'}, True), |
|
100 | ({'test_hg': 'repository.admin'}, True), | |
101 |
|
101 | |||
102 | ]) |
|
102 | ]) | |
103 | def test_has_write_permissions(self, hg_server, permissions, value): |
|
103 | def test_has_write_permissions(self, hg_server, permissions, value): | |
104 | server = hg_server.create(user_permissions=permissions) |
|
104 | server = hg_server.create(user_permissions=permissions) | |
105 | result = server.has_write_perm() |
|
105 | result = server.has_write_perm() | |
106 | assert result is value |
|
106 | assert result is value | |
107 |
|
107 | |||
108 | def test_run_returns_executes_command(self, hg_server): |
|
108 | def test_run_returns_executes_command(self, hg_server): | |
109 | server = hg_server.create() |
|
109 | server = hg_server.create() | |
110 | from rhodecode.apps.ssh_support.lib.backends.hg import MercurialTunnelWrapper |
|
110 | from rhodecode.apps.ssh_support.lib.backends.hg import MercurialTunnelWrapper | |
|
111 | os.environ['SSH_CLIENT'] = '127.0.0.1' | |||
111 | with mock.patch.object(MercurialTunnelWrapper, 'create_hooks_env') as _patch: |
|
112 | with mock.patch.object(MercurialTunnelWrapper, 'create_hooks_env') as _patch: | |
112 | _patch.return_value = 0 |
|
113 | _patch.return_value = 0 | |
113 | with mock.patch.object(MercurialTunnelWrapper, 'command', return_value='date'): |
|
114 | with mock.patch.object(MercurialTunnelWrapper, 'command', return_value='date'): | |
114 | exit_code = server.run() |
|
115 | exit_code = server.run() | |
115 |
|
116 | |||
116 | assert exit_code == (0, False) |
|
117 | assert exit_code == (0, False) | |
117 |
|
118 | |||
118 |
|
119 | |||
119 |
|
120 |
@@ -1,206 +1,207 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2016-2020 RhodeCode GmbH |
|
3 | # Copyright (C) 2016-2020 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
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 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
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/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | import os | ||
21 | import mock |
|
21 | import mock | |
22 | import pytest |
|
22 | import pytest | |
23 |
|
23 | |||
24 | from rhodecode.apps.ssh_support.lib.backends.svn import SubversionServer |
|
24 | from rhodecode.apps.ssh_support.lib.backends.svn import SubversionServer | |
25 | from rhodecode.apps.ssh_support.tests.conftest import plain_dummy_env, plain_dummy_user |
|
25 | from rhodecode.apps.ssh_support.tests.conftest import plain_dummy_env, plain_dummy_user | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | class SubversionServerCreator(object): |
|
28 | class SubversionServerCreator(object): | |
29 | root = '/tmp/repo/path/' |
|
29 | root = '/tmp/repo/path/' | |
30 | svn_path = '/usr/local/bin/svnserve' |
|
30 | svn_path = '/usr/local/bin/svnserve' | |
31 | config_data = { |
|
31 | config_data = { | |
32 | 'app:main': { |
|
32 | 'app:main': { | |
33 | 'ssh.executable.svn': svn_path, |
|
33 | 'ssh.executable.svn': svn_path, | |
34 | 'vcs.hooks.protocol': 'http', |
|
34 | 'vcs.hooks.protocol': 'http', | |
35 | } |
|
35 | } | |
36 | } |
|
36 | } | |
37 | repo_name = 'test-svn' |
|
37 | repo_name = 'test-svn' | |
38 | user = plain_dummy_user() |
|
38 | user = plain_dummy_user() | |
39 |
|
39 | |||
40 | def __init__(self): |
|
40 | def __init__(self): | |
41 | def config_get(part, key): |
|
41 | def config_get(part, key): | |
42 | return self.config_data.get(part, {}).get(key) |
|
42 | return self.config_data.get(part, {}).get(key) | |
43 | self.config_mock = mock.Mock() |
|
43 | self.config_mock = mock.Mock() | |
44 | self.config_mock.get = mock.Mock(side_effect=config_get) |
|
44 | self.config_mock.get = mock.Mock(side_effect=config_get) | |
45 |
|
45 | |||
46 | def create(self, **kwargs): |
|
46 | def create(self, **kwargs): | |
47 | parameters = { |
|
47 | parameters = { | |
48 | 'store': self.root, |
|
48 | 'store': self.root, | |
49 | 'repo_name': self.repo_name, |
|
49 | 'repo_name': self.repo_name, | |
50 | 'ini_path': '', |
|
50 | 'ini_path': '', | |
51 | 'user': self.user, |
|
51 | 'user': self.user, | |
52 | 'user_permissions': { |
|
52 | 'user_permissions': { | |
53 | self.repo_name: 'repository.admin' |
|
53 | self.repo_name: 'repository.admin' | |
54 | }, |
|
54 | }, | |
55 | 'config': self.config_mock, |
|
55 | 'config': self.config_mock, | |
56 | 'env': plain_dummy_env() |
|
56 | 'env': plain_dummy_env() | |
57 | } |
|
57 | } | |
58 |
|
58 | |||
59 | parameters.update(kwargs) |
|
59 | parameters.update(kwargs) | |
60 | server = SubversionServer(**parameters) |
|
60 | server = SubversionServer(**parameters) | |
61 | return server |
|
61 | return server | |
62 |
|
62 | |||
63 |
|
63 | |||
64 | @pytest.fixture() |
|
64 | @pytest.fixture() | |
65 | def svn_server(app): |
|
65 | def svn_server(app): | |
66 | return SubversionServerCreator() |
|
66 | return SubversionServerCreator() | |
67 |
|
67 | |||
68 |
|
68 | |||
69 | class TestSubversionServer(object): |
|
69 | class TestSubversionServer(object): | |
70 | def test_command(self, svn_server): |
|
70 | def test_command(self, svn_server): | |
71 | server = svn_server.create() |
|
71 | server = svn_server.create() | |
72 | expected_command = [ |
|
72 | expected_command = [ | |
73 | svn_server.svn_path, '-t', |
|
73 | svn_server.svn_path, '-t', | |
74 | '--config-file', server.tunnel.svn_conf_path, |
|
74 | '--config-file', server.tunnel.svn_conf_path, | |
75 | '--tunnel-user', svn_server.user.username, |
|
75 | '--tunnel-user', svn_server.user.username, | |
76 | '-r', svn_server.root |
|
76 | '-r', svn_server.root | |
77 | ] |
|
77 | ] | |
78 |
|
78 | |||
79 | assert expected_command == server.tunnel.command() |
|
79 | assert expected_command == server.tunnel.command() | |
80 |
|
80 | |||
81 | @pytest.mark.parametrize('permissions, action, code', [ |
|
81 | @pytest.mark.parametrize('permissions, action, code', [ | |
82 | ({}, 'pull', -2), |
|
82 | ({}, 'pull', -2), | |
83 | ({'test-svn': 'repository.read'}, 'pull', 0), |
|
83 | ({'test-svn': 'repository.read'}, 'pull', 0), | |
84 | ({'test-svn': 'repository.read'}, 'push', -2), |
|
84 | ({'test-svn': 'repository.read'}, 'push', -2), | |
85 | ({'test-svn': 'repository.write'}, 'push', 0), |
|
85 | ({'test-svn': 'repository.write'}, 'push', 0), | |
86 | ({'test-svn': 'repository.admin'}, 'push', 0), |
|
86 | ({'test-svn': 'repository.admin'}, 'push', 0), | |
87 |
|
87 | |||
88 | ]) |
|
88 | ]) | |
89 | def test_permission_checks(self, svn_server, permissions, action, code): |
|
89 | def test_permission_checks(self, svn_server, permissions, action, code): | |
90 | server = svn_server.create(user_permissions=permissions) |
|
90 | server = svn_server.create(user_permissions=permissions) | |
91 | result = server._check_permissions(action) |
|
91 | result = server._check_permissions(action) | |
92 | assert result is code |
|
92 | assert result is code | |
93 |
|
93 | |||
94 | @pytest.mark.parametrize('permissions, access_paths, expected_match', [ |
|
94 | @pytest.mark.parametrize('permissions, access_paths, expected_match', [ | |
95 | # not matched repository name |
|
95 | # not matched repository name | |
96 | ({ |
|
96 | ({ | |
97 | 'test-svn': '' |
|
97 | 'test-svn': '' | |
98 | }, ['test-svn-1', 'test-svn-1/subpath'], |
|
98 | }, ['test-svn-1', 'test-svn-1/subpath'], | |
99 | None), |
|
99 | None), | |
100 |
|
100 | |||
101 | # exact match |
|
101 | # exact match | |
102 | ({ |
|
102 | ({ | |
103 | 'test-svn': '' |
|
103 | 'test-svn': '' | |
104 | }, |
|
104 | }, | |
105 | ['test-svn'], |
|
105 | ['test-svn'], | |
106 | 'test-svn'), |
|
106 | 'test-svn'), | |
107 |
|
107 | |||
108 | # subdir commits |
|
108 | # subdir commits | |
109 | ({ |
|
109 | ({ | |
110 | 'test-svn': '' |
|
110 | 'test-svn': '' | |
111 | }, |
|
111 | }, | |
112 | ['test-svn/foo', |
|
112 | ['test-svn/foo', | |
113 | 'test-svn/foo/test-svn', |
|
113 | 'test-svn/foo/test-svn', | |
114 | 'test-svn/trunk/development.txt', |
|
114 | 'test-svn/trunk/development.txt', | |
115 | ], |
|
115 | ], | |
116 | 'test-svn'), |
|
116 | 'test-svn'), | |
117 |
|
117 | |||
118 | # subgroups + similar patterns |
|
118 | # subgroups + similar patterns | |
119 | ({ |
|
119 | ({ | |
120 | 'test-svn': '', |
|
120 | 'test-svn': '', | |
121 | 'test-svn-1': '', |
|
121 | 'test-svn-1': '', | |
122 | 'test-svn-subgroup/test-svn': '', |
|
122 | 'test-svn-subgroup/test-svn': '', | |
123 |
|
123 | |||
124 | }, |
|
124 | }, | |
125 | ['test-svn-1', |
|
125 | ['test-svn-1', | |
126 | 'test-svn-1/foo/test-svn', |
|
126 | 'test-svn-1/foo/test-svn', | |
127 | 'test-svn-1/test-svn', |
|
127 | 'test-svn-1/test-svn', | |
128 | ], |
|
128 | ], | |
129 | 'test-svn-1'), |
|
129 | 'test-svn-1'), | |
130 |
|
130 | |||
131 | # subgroups + similar patterns |
|
131 | # subgroups + similar patterns | |
132 | ({ |
|
132 | ({ | |
133 | 'test-svn-1': '', |
|
133 | 'test-svn-1': '', | |
134 | 'test-svn-10': '', |
|
134 | 'test-svn-10': '', | |
135 | 'test-svn-100': '', |
|
135 | 'test-svn-100': '', | |
136 | }, |
|
136 | }, | |
137 | ['test-svn-10', |
|
137 | ['test-svn-10', | |
138 | 'test-svn-10/foo/test-svn', |
|
138 | 'test-svn-10/foo/test-svn', | |
139 | 'test-svn-10/test-svn', |
|
139 | 'test-svn-10/test-svn', | |
140 | ], |
|
140 | ], | |
141 | 'test-svn-10'), |
|
141 | 'test-svn-10'), | |
142 |
|
142 | |||
143 | # subgroups + similar patterns |
|
143 | # subgroups + similar patterns | |
144 | ({ |
|
144 | ({ | |
145 | 'name': '', |
|
145 | 'name': '', | |
146 | 'nameContains': '', |
|
146 | 'nameContains': '', | |
147 | 'nameContainsThis': '', |
|
147 | 'nameContainsThis': '', | |
148 | }, |
|
148 | }, | |
149 | ['nameContains', |
|
149 | ['nameContains', | |
150 | 'nameContains/This', |
|
150 | 'nameContains/This', | |
151 | 'nameContains/This/test-svn', |
|
151 | 'nameContains/This/test-svn', | |
152 | ], |
|
152 | ], | |
153 | 'nameContains'), |
|
153 | 'nameContains'), | |
154 |
|
154 | |||
155 | # subgroups + similar patterns |
|
155 | # subgroups + similar patterns | |
156 | ({ |
|
156 | ({ | |
157 | 'test-svn': '', |
|
157 | 'test-svn': '', | |
158 | 'test-svn-1': '', |
|
158 | 'test-svn-1': '', | |
159 | 'test-svn-subgroup/test-svn': '', |
|
159 | 'test-svn-subgroup/test-svn': '', | |
160 |
|
160 | |||
161 | }, |
|
161 | }, | |
162 | ['test-svn-subgroup/test-svn', |
|
162 | ['test-svn-subgroup/test-svn', | |
163 | 'test-svn-subgroup/test-svn/foo/test-svn', |
|
163 | 'test-svn-subgroup/test-svn/foo/test-svn', | |
164 | 'test-svn-subgroup/test-svn/trunk/example.txt', |
|
164 | 'test-svn-subgroup/test-svn/trunk/example.txt', | |
165 | ], |
|
165 | ], | |
166 | 'test-svn-subgroup/test-svn'), |
|
166 | 'test-svn-subgroup/test-svn'), | |
167 | ]) |
|
167 | ]) | |
168 | def test_repo_extraction_on_subdir(self, svn_server, permissions, access_paths, expected_match): |
|
168 | def test_repo_extraction_on_subdir(self, svn_server, permissions, access_paths, expected_match): | |
169 | server = svn_server.create(user_permissions=permissions) |
|
169 | server = svn_server.create(user_permissions=permissions) | |
170 | for path in access_paths: |
|
170 | for path in access_paths: | |
171 | repo_name = server.tunnel._match_repo_name(path) |
|
171 | repo_name = server.tunnel._match_repo_name(path) | |
172 | assert repo_name == expected_match |
|
172 | assert repo_name == expected_match | |
173 |
|
173 | |||
174 | def test_run_returns_executes_command(self, svn_server): |
|
174 | def test_run_returns_executes_command(self, svn_server): | |
175 | server = svn_server.create() |
|
175 | server = svn_server.create() | |
176 | from rhodecode.apps.ssh_support.lib.backends.svn import SubversionTunnelWrapper |
|
176 | from rhodecode.apps.ssh_support.lib.backends.svn import SubversionTunnelWrapper | |
|
177 | os.environ['SSH_CLIENT'] = '127.0.0.1' | |||
177 | with mock.patch.object( |
|
178 | with mock.patch.object( | |
178 | SubversionTunnelWrapper, 'get_first_client_response', |
|
179 | SubversionTunnelWrapper, 'get_first_client_response', | |
179 | return_value={'url': 'http://server/test-svn'}): |
|
180 | return_value={'url': 'http://server/test-svn'}): | |
180 | with mock.patch.object( |
|
181 | with mock.patch.object( | |
181 | SubversionTunnelWrapper, 'patch_first_client_response', |
|
182 | SubversionTunnelWrapper, 'patch_first_client_response', | |
182 | return_value=0): |
|
183 | return_value=0): | |
183 | with mock.patch.object( |
|
184 | with mock.patch.object( | |
184 | SubversionTunnelWrapper, 'sync', |
|
185 | SubversionTunnelWrapper, 'sync', | |
185 | return_value=0): |
|
186 | return_value=0): | |
186 | with mock.patch.object( |
|
187 | with mock.patch.object( | |
187 | SubversionTunnelWrapper, 'command', |
|
188 | SubversionTunnelWrapper, 'command', | |
188 | return_value=['date']): |
|
189 | return_value=['date']): | |
189 |
|
190 | |||
190 | exit_code = server.run() |
|
191 | exit_code = server.run() | |
191 | # SVN has this differently configured, and we get in our mock env |
|
192 | # SVN has this differently configured, and we get in our mock env | |
192 | # None as return code |
|
193 | # None as return code | |
193 | assert exit_code == (None, False) |
|
194 | assert exit_code == (None, False) | |
194 |
|
195 | |||
195 | def test_run_returns_executes_command_that_cannot_extract_repo_name(self, svn_server): |
|
196 | def test_run_returns_executes_command_that_cannot_extract_repo_name(self, svn_server): | |
196 | server = svn_server.create() |
|
197 | server = svn_server.create() | |
197 | from rhodecode.apps.ssh_support.lib.backends.svn import SubversionTunnelWrapper |
|
198 | from rhodecode.apps.ssh_support.lib.backends.svn import SubversionTunnelWrapper | |
198 | with mock.patch.object( |
|
199 | with mock.patch.object( | |
199 | SubversionTunnelWrapper, 'command', |
|
200 | SubversionTunnelWrapper, 'command', | |
200 | return_value=['date']): |
|
201 | return_value=['date']): | |
201 | with mock.patch.object( |
|
202 | with mock.patch.object( | |
202 | SubversionTunnelWrapper, 'get_first_client_response', |
|
203 | SubversionTunnelWrapper, 'get_first_client_response', | |
203 | return_value=None): |
|
204 | return_value=None): | |
204 | exit_code = server.run() |
|
205 | exit_code = server.run() | |
205 |
|
206 | |||
206 | assert exit_code == (1, False) |
|
207 | assert exit_code == (1, False) |
General Comments 0
You need to be logged in to leave comments.
Login now