Browse Source

added tests for root resource and security module

rework
Holger Frey 7 years ago
parent
commit
3b9246633a
  1. 5
      ordr/security.py
  2. 1
      ordr/tests/_functional/__init__.py
  3. 4
      ordr/tests/_functional/errors.py
  4. 6
      ordr/tests/_functional/pages.py
  5. 1
      ordr/tests/resources/__init__.py
  6. 14
      ordr/tests/resources/root_resource.py
  7. 17
      ordr/tests/security.py
  8. 1
      ordr/tests/views/__init__.py
  9. 1
      ordr/tests/views/errors.py
  10. 2
      ordr/tests/views/pages.py

5
ordr/security.py

@ -16,14 +16,13 @@ def crypt_context_settings_to_string(settings, prefix='passlib.'):
context settings is quite a task. Since passlib has a context parser context settings is quite a task. Since passlib has a context parser
included, this seems the most reliable way to do it. included, this seems the most reliable way to do it.
''' '''
as_list_keys = {'schemes', 'deprecated'}
config_lines = ['[passlib]'] config_lines = ['[passlib]']
for ini_key, value in settings.items(): for ini_key, value in settings.items():
if ini_key.startswith(prefix): if ini_key.startswith(prefix):
context_key = ini_key.replace(prefix, '') context_key = ini_key.replace(prefix, '')
# the pyramid .ini format is different on lists # the pyramid .ini format is different on lists
# than the .ini format used by passlib. # than the .ini format used by passlib.
if context_key in as_list_keys and ',' not in value: if context_key in {'schemes', 'deprecated'} and ',' not in value:
value = ','.join(aslist(value)) value = ','.join(aslist(value))
config_lines.append(f'{context_key} = {value}') config_lines.append(f'{context_key} = {value}')
return '\n'.join(config_lines) return '\n'.join(config_lines)
@ -32,7 +31,7 @@ def crypt_context_settings_to_string(settings, prefix='passlib.'):
def includeme(config): def includeme(config):
''' initializing authentication, authorization and password hash settings ''' initializing authentication, authorization and password hash settings
Activate this setup using ``config.include('ordr2.security')``. Activate this setup using ``config.include('ordr.security')``.
''' '''
settings = config.get_settings() settings = config.get_settings()

1
ordr/tests/_functional/__init__.py

@ -15,7 +15,6 @@ class CustomTestApp(webtest.TestApp):
pass pass
@pytest.fixture(scope='module') @pytest.fixture(scope='module')
def testapp(): def testapp():
''' fixture for using webtest ''' ''' fixture for using webtest '''

4
ordr/tests/_functional/errors.py

@ -1,8 +1,8 @@
''' functional tests for ordr2.views.errors ''' ''' functional tests for ordr2.views.errors '''
from . import testapp from . import testapp # noqa: F401
def test_404(testapp): def test_404(testapp): # noqa: F811
result = testapp.get('/unknown', status=404) result = testapp.get('/unknown', status=404)
assert '404' in result assert '404' in result

6
ordr/tests/_functional/pages.py

@ -1,13 +1,13 @@
''' functional tests for ordr2.views.pages ''' ''' functional tests for ordr2.views.pages '''
from . import testapp from . import testapp # noqa: F401
def test_welcome(testapp): def test_welcome(testapp): # noqa: F811
result = testapp.get('/') result = testapp.get('/')
assert 'Ordr' in result assert 'Ordr' in result
def test_faq(testapp): def test_faq(testapp): # noqa: F811
result = testapp.get('/faq') result = testapp.get('/faq')
assert 'FAQ' in result assert 'FAQ' in result

1
ordr/tests/resources/__init__.py

@ -0,0 +1 @@

14
ordr/tests/resources/root_resource.py

@ -0,0 +1,14 @@
def test_root_init():
from ordr.resources import RootResource
root = RootResource('request')
assert root.__name__ == None
assert root.__parent__ == None
assert root.request == 'request'
def test_root_acl():
from pyramid.security import Allow, Everyone, DENY_ALL
from ordr.resources import RootResource
root = RootResource(None)
assert root.__acl__() == [(Allow, Everyone, 'view'), DENY_ALL]

17
ordr/tests/security.py

@ -0,0 +1,17 @@
def test_crypt_context_to_settings():
from ordr.security import crypt_context_settings_to_string
settings = {
'no_prefix': 'should not appear',
'prefix.something': 'left unchanged',
'prefix.schemes': 'adjust list',
'prefix.depreceated': 'do, not, adjust, this, list'
}
result = crypt_context_settings_to_string(settings, 'prefix.')
expected_lines = {
'[passlib]',
'something = left unchanged',
'schemes = adjust,list',
'depreceated = do, not, adjust, this, list',
}
assert set(result.split('\n')) == expected_lines

1
ordr/tests/views/__init__.py

@ -0,0 +1 @@

1
ordr/tests/views/errors.py

@ -1,5 +1,6 @@
from pyramid.testing import DummyRequest from pyramid.testing import DummyRequest
def test_welcome(): def test_welcome():
from ordr.views.errors import notfound_view from ordr.views.errors import notfound_view
request = DummyRequest() request = DummyRequest()

2
ordr/tests/views/pages.py

@ -10,5 +10,3 @@ def test_faq():
from ordr.views.pages import faq from ordr.views.pages import faq
result = faq(None, None) result = faq(None, None)
assert result == {} assert result == {}