Show More
|
1 | NO CONTENT: new file 100644 |
@@ -0,0 +1,128 b'' | |||
|
1 | # -*- coding: utf-8 -*- | |
|
2 | ||
|
3 | # Copyright (C) 2016-2016 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 | ||
|
22 | import mock | |
|
23 | import pytest | |
|
24 | ||
|
25 | from rhodecode.config.routing import ADMIN_PREFIX | |
|
26 | from rhodecode.login.views import LoginView, CaptchaData | |
|
27 | from rhodecode.model.settings import SettingsModel | |
|
28 | from rhodecode.tests.utils import AssertResponse | |
|
29 | ||
|
30 | ||
|
31 | class RhodeCodeSetting(object): | |
|
32 | def __init__(self, name, value): | |
|
33 | self.name = name | |
|
34 | self.value = value | |
|
35 | ||
|
36 | def __enter__(self): | |
|
37 | from rhodecode.model.settings import SettingsModel | |
|
38 | model = SettingsModel() | |
|
39 | self.old_setting = model.get_setting_by_name(self.name) | |
|
40 | model.create_or_update_setting(name=self.name, val=self.value) | |
|
41 | return self | |
|
42 | ||
|
43 | def __exit__(self, type, value, traceback): | |
|
44 | model = SettingsModel() | |
|
45 | if self.old_setting: | |
|
46 | model.create_or_update_setting( | |
|
47 | name=self.name, val=self.old_setting.app_settings_value) | |
|
48 | else: | |
|
49 | model.create_or_update_setting(name=self.name) | |
|
50 | ||
|
51 | ||
|
52 | class TestRegisterCaptcha(object): | |
|
53 | ||
|
54 | @pytest.mark.parametrize('private_key, public_key, expected', [ | |
|
55 | ('', '', CaptchaData(False, '', '')), | |
|
56 | ('', 'pubkey', CaptchaData(False, '', 'pubkey')), | |
|
57 | ('privkey', '', CaptchaData(True, 'privkey', '')), | |
|
58 | ('privkey', 'pubkey', CaptchaData(True, 'privkey', 'pubkey')), | |
|
59 | ]) | |
|
60 | def test_get_captcha_data(self, private_key, public_key, expected, db): | |
|
61 | login_view = LoginView(mock.Mock(), mock.Mock()) | |
|
62 | with RhodeCodeSetting('captcha_private_key', private_key): | |
|
63 | with RhodeCodeSetting('captcha_public_key', public_key): | |
|
64 | captcha = login_view._get_captcha_data() | |
|
65 | assert captcha == expected | |
|
66 | ||
|
67 | @pytest.mark.parametrize('active', [False, True]) | |
|
68 | @mock.patch.object(LoginView, '_get_captcha_data') | |
|
69 | def test_private_key_does_not_leak_to_html( | |
|
70 | self, m_get_captcha_data, active, app): | |
|
71 | captcha = CaptchaData( | |
|
72 | active=active, private_key='PRIVATE_KEY', public_key='PUBLIC_KEY') | |
|
73 | m_get_captcha_data.return_value = captcha | |
|
74 | ||
|
75 | response = app.get(ADMIN_PREFIX + '/register') | |
|
76 | assert 'PRIVATE_KEY' not in response | |
|
77 | ||
|
78 | @pytest.mark.parametrize('active', [False, True]) | |
|
79 | @mock.patch.object(LoginView, '_get_captcha_data') | |
|
80 | def test_register_view_renders_captcha( | |
|
81 | self, m_get_captcha_data, active, app): | |
|
82 | captcha = CaptchaData( | |
|
83 | active=active, private_key='PRIVATE_KEY', public_key='PUBLIC_KEY') | |
|
84 | m_get_captcha_data.return_value = captcha | |
|
85 | ||
|
86 | response = app.get(ADMIN_PREFIX + '/register') | |
|
87 | ||
|
88 | assertr = AssertResponse(response) | |
|
89 | if active: | |
|
90 | assertr.one_element_exists('#recaptcha_field') | |
|
91 | else: | |
|
92 | assertr.no_element_exists('#recaptcha_field') | |
|
93 | ||
|
94 | @pytest.mark.parametrize('valid', [False, True]) | |
|
95 | @mock.patch('rhodecode.login.views.submit') | |
|
96 | @mock.patch.object(LoginView, '_get_captcha_data') | |
|
97 | def test_register_with_active_captcha( | |
|
98 | self, m_get_captcha_data, m_submit, valid, app, csrf_token): | |
|
99 | captcha = CaptchaData( | |
|
100 | active=True, private_key='PRIVATE_KEY', public_key='PUBLIC_KEY') | |
|
101 | m_get_captcha_data.return_value = captcha | |
|
102 | m_response = mock.Mock() | |
|
103 | m_response.is_valid = valid | |
|
104 | m_submit.return_value = m_response | |
|
105 | ||
|
106 | params = { | |
|
107 | 'csrf_token': csrf_token, | |
|
108 | 'email': 'pytest@example.com', | |
|
109 | 'firstname': 'pytest-firstname', | |
|
110 | 'lastname': 'pytest-lastname', | |
|
111 | 'password': 'secret', | |
|
112 | 'password_confirmation': 'secret', | |
|
113 | 'username': 'pytest', | |
|
114 | } | |
|
115 | response = app.post(ADMIN_PREFIX + '/register', params=params) | |
|
116 | ||
|
117 | if valid: | |
|
118 | # If we provided a valid captcha input we expect a successful | |
|
119 | # registration and redirect to the login page. | |
|
120 | assert response.status_int == 302 | |
|
121 | assert 'location' in response.headers | |
|
122 | assert ADMIN_PREFIX + '/login' in response.headers['location'] | |
|
123 | else: | |
|
124 | # If captche input is invalid we expect to stay on the registration | |
|
125 | # page with an error message displayed. | |
|
126 | assertr = AssertResponse(response) | |
|
127 | assert response.status_int == 200 | |
|
128 | assertr.one_element_exists('#recaptcha_field ~ span.error-message') |
General Comments 0
You need to be logged in to leave comments.
Login now