Files
@ 8d98924c58b1
Branch filter:
Location: kallithea/kallithea/tests/functional/test_admin_auth_settings.py
8d98924c58b1
10.9 KiB
text/x-python
tests: add as little code as possible in __init__.py
kallithea/tests/__init__.py contained quite a lot of code, including the test
base class TestController. This in itself may be considered bad practice.
Specifically, this poses a problem when using pytest 3.0+, in which asserts
in some files are not automatically rewritten to give improved assert
output. That problem can be fixed by explicitly registering such files for
assertion rewriting, but that register call should be executed _before_ said
files are imported. I.e. if the register call is in
kallithea/tests/__init__.py, assert calls in __init__.py itself can not be
rewritten.
Since the TestController base class does effectively contain asserts, and we
do not want to execute the register call from somewhere outside the
kallithea/tests directory, we need to move the TestController class to
another file (kallithea/tests/base.py) so we can have a register call in
__init__.py before loading base.py.
While not strictly necessary to fix the mentioned pytest problem, we take
the opportunity to fully clean __init__.py and move everything to
the new kallithea/tests/base.py. While doing so, unnecessary imports are
removed, and imports are ordered alphabetically. Explicit imports of symbols
from modules that were already imported as a whole, are removed in favor of
fully qualifying the references (e.g. tempfile._RandomNameSequence).
kallithea/tests/__init__.py contained quite a lot of code, including the test
base class TestController. This in itself may be considered bad practice.
Specifically, this poses a problem when using pytest 3.0+, in which asserts
in some files are not automatically rewritten to give improved assert
output. That problem can be fixed by explicitly registering such files for
assertion rewriting, but that register call should be executed _before_ said
files are imported. I.e. if the register call is in
kallithea/tests/__init__.py, assert calls in __init__.py itself can not be
rewritten.
Since the TestController base class does effectively contain asserts, and we
do not want to execute the register call from somewhere outside the
kallithea/tests directory, we need to move the TestController class to
another file (kallithea/tests/base.py) so we can have a register call in
__init__.py before loading base.py.
While not strictly necessary to fix the mentioned pytest problem, we take
the opportunity to fully clean __init__.py and move everything to
the new kallithea/tests/base.py. While doing so, unnecessary imports are
removed, and imports are ordered alphabetically. Explicit imports of symbols
from modules that were already imported as a whole, are removed in favor of
fully qualifying the references (e.g. tempfile._RandomNameSequence).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | from kallithea.tests.base import *
from kallithea.model.db import Setting
class TestAuthSettingsController(TestController):
def _enable_plugins(self, plugins_list):
test_url = url(controller='admin/auth_settings',
action='auth_settings')
params={'auth_plugins': plugins_list, '_authentication_token': self.authentication_token()}
for plugin in plugins_list.split(','):
enable = plugin.partition('kallithea.lib.auth_modules.')[-1]
params.update({'%s_enabled' % enable: True})
response = self.app.post(url=test_url, params=params)
return params
#self.checkSessionFlash(response, 'Auth settings updated successfully')
def test_index(self):
self.log_user()
response = self.app.get(url(controller='admin/auth_settings',
action='index'))
response.mustcontain('Authentication Plugins')
@skipif(not ldap_lib_installed, reason='skipping due to missing ldap lib')
def test_ldap_save_settings(self):
self.log_user()
params = self._enable_plugins('kallithea.lib.auth_modules.auth_internal,kallithea.lib.auth_modules.auth_ldap')
params.update({'auth_ldap_host': u'dc.example.com',
'auth_ldap_port': '999',
'auth_ldap_tls_kind': 'PLAIN',
'auth_ldap_tls_reqcert': 'NEVER',
'auth_ldap_dn_user': 'test_user',
'auth_ldap_dn_pass': 'test_pass',
'auth_ldap_base_dn': 'test_base_dn',
'auth_ldap_filter': 'test_filter',
'auth_ldap_search_scope': 'BASE',
'auth_ldap_attr_login': 'test_attr_login',
'auth_ldap_attr_firstname': 'ima',
'auth_ldap_attr_lastname': 'tester',
'auth_ldap_attr_email': 'test@example.com'})
test_url = url(controller='admin/auth_settings',
action='auth_settings')
response = self.app.post(url=test_url, params=params)
self.checkSessionFlash(response, 'Auth settings updated successfully')
new_settings = Setting.get_auth_settings()
assert new_settings['auth_ldap_host'] == u'dc.example.com', 'fail db write compare'
@skipif(not ldap_lib_installed, reason='skipping due to missing ldap lib')
def test_ldap_error_form_wrong_port_number(self):
self.log_user()
params = self._enable_plugins('kallithea.lib.auth_modules.auth_internal,kallithea.lib.auth_modules.auth_ldap')
params.update({'auth_ldap_host': '',
'auth_ldap_port': 'i-should-be-number', # bad port num
'auth_ldap_tls_kind': 'PLAIN',
'auth_ldap_tls_reqcert': 'NEVER',
'auth_ldap_dn_user': '',
'auth_ldap_dn_pass': '',
'auth_ldap_base_dn': '',
'auth_ldap_filter': '',
'auth_ldap_search_scope': 'BASE',
'auth_ldap_attr_login': '',
'auth_ldap_attr_firstname': '',
'auth_ldap_attr_lastname': '',
'auth_ldap_attr_email': ''})
test_url = url(controller='admin/auth_settings',
action='auth_settings')
response = self.app.post(url=test_url, params=params)
response.mustcontain("""<span class="error-message">"""
"""Please enter a number</span>""")
@skipif(not ldap_lib_installed, reason='skipping due to missing ldap lib')
def test_ldap_error_form(self):
self.log_user()
params = self._enable_plugins('kallithea.lib.auth_modules.auth_internal,kallithea.lib.auth_modules.auth_ldap')
params.update({'auth_ldap_host': 'Host',
'auth_ldap_port': '123',
'auth_ldap_tls_kind': 'PLAIN',
'auth_ldap_tls_reqcert': 'NEVER',
'auth_ldap_dn_user': '',
'auth_ldap_dn_pass': '',
'auth_ldap_base_dn': '',
'auth_ldap_filter': '',
'auth_ldap_search_scope': 'BASE',
'auth_ldap_attr_login': '', # <----- missing required input
'auth_ldap_attr_firstname': '',
'auth_ldap_attr_lastname': '',
'auth_ldap_attr_email': ''})
test_url = url(controller='admin/auth_settings',
action='auth_settings')
response = self.app.post(url=test_url, params=params)
response.mustcontain("""<span class="error-message">The LDAP Login"""
""" attribute of the CN must be specified""")
def test_ldap_login(self):
pass
def test_ldap_login_incorrect(self):
pass
def _container_auth_setup(self, **settings):
self.log_user()
params = self._enable_plugins('kallithea.lib.auth_modules.auth_internal,kallithea.lib.auth_modules.auth_container')
params.update(settings)
test_url = url(controller='admin/auth_settings',
action='auth_settings')
response = self.app.post(url=test_url, params=params)
response = response.follow()
response.click('Log Out') # end admin login session
def _container_auth_verify_login(self, resulting_username, **get_kwargs):
response = self.app.get(
url=url(controller='admin/my_account', action='my_account'),
**get_kwargs
)
response.mustcontain('My Account %s' % resulting_username)
def test_container_auth_login_header(self):
self._container_auth_setup(
auth_container_header='THE_USER_NAME',
auth_container_email_header='',
auth_container_firstname_header='',
auth_container_lastname_header='',
auth_container_fallback_header='',
auth_container_clean_username='False',
)
self._container_auth_verify_login(
extra_environ={'THE_USER_NAME': 'john@example.org'},
resulting_username='john@example.org',
)
def test_container_auth_login_header_attr(self):
self._container_auth_setup(
auth_container_header='THE_USER_NAME',
auth_container_email_header='THE_USER_EMAIL',
auth_container_firstname_header='THE_USER_FIRSTNAME',
auth_container_lastname_header='THE_USER_LASTNAME',
auth_container_fallback_header='',
auth_container_clean_username='False',
)
response = self.app.get(
url=url(controller='admin/my_account', action='my_account'),
extra_environ={'THE_USER_NAME': 'johnd',
'THE_USER_EMAIL': 'john@example.org',
'THE_USER_FIRSTNAME': 'John',
'THE_USER_LASTNAME': 'Doe',
}
)
assert response.form['email'].value == 'john@example.org'
assert response.form['firstname'].value == 'John'
assert response.form['lastname'].value == 'Doe'
def test_container_auth_login_fallback_header(self):
self._container_auth_setup(
auth_container_header='THE_USER_NAME',
auth_container_email_header='',
auth_container_firstname_header='',
auth_container_lastname_header='',
auth_container_fallback_header='HTTP_X_YZZY',
auth_container_clean_username='False',
)
self._container_auth_verify_login(
headers={'X-Yzzy': r'foo\bar'},
resulting_username=r'foo\bar',
)
def test_container_auth_clean_username_at(self):
self._container_auth_setup(
auth_container_header='REMOTE_USER',
auth_container_email_header='',
auth_container_firstname_header='',
auth_container_lastname_header='',
auth_container_fallback_header='',
auth_container_clean_username='True',
)
self._container_auth_verify_login(
extra_environ={'REMOTE_USER': 'john@example.org'},
resulting_username='john',
)
def test_container_auth_clean_username_backslash(self):
self._container_auth_setup(
auth_container_header='REMOTE_USER',
auth_container_email_header='',
auth_container_firstname_header='',
auth_container_lastname_header='',
auth_container_fallback_header='',
auth_container_clean_username='True',
)
self._container_auth_verify_login(
extra_environ={'REMOTE_USER': r'example\jane'},
resulting_username=r'jane',
)
def test_container_auth_no_logout(self):
self._container_auth_setup(
auth_container_header='REMOTE_USER',
auth_container_email_header='',
auth_container_firstname_header='',
auth_container_lastname_header='',
auth_container_fallback_header='',
auth_container_clean_username='True',
)
response = self.app.get(
url=url(controller='admin/my_account', action='my_account'),
extra_environ={'REMOTE_USER': 'john'},
)
assert 'Log Out' not in response.normal_body
def test_crowd_save_settings(self):
self.log_user()
params = self._enable_plugins('kallithea.lib.auth_modules.auth_internal,kallithea.lib.auth_modules.auth_crowd')
params.update({'auth_crowd_host': ' hostname ',
'auth_crowd_app_password': 'secret',
'auth_crowd_admin_groups': 'mygroup',
'auth_crowd_port': '123',
'auth_crowd_app_name': 'xyzzy'})
test_url = url(controller='admin/auth_settings',
action='auth_settings')
response = self.app.post(url=test_url, params=params)
self.checkSessionFlash(response, 'Auth settings updated successfully')
new_settings = Setting.get_auth_settings()
assert new_settings['auth_crowd_host'] == u'hostname', 'fail db write compare'
@skipif(not pam_lib_installed, reason='skipping due to missing pam lib')
def test_pam_save_settings(self):
self.log_user()
params = self._enable_plugins('kallithea.lib.auth_modules.auth_internal,kallithea.lib.auth_modules.auth_pam')
params.update({'auth_pam_service': 'kallithea',
'auth_pam_gecos': '^foo-.*'})
test_url = url(controller='admin/auth_settings',
action='auth_settings')
response = self.app.post(url=test_url, params=params)
self.checkSessionFlash(response, 'Auth settings updated successfully')
new_settings = Setting.get_auth_settings()
assert new_settings['auth_pam_service'] == u'kallithea', 'fail db write compare'
|