Browse Source

added tests for root resource and security module

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

2
ordr/models/meta.py

@ -10,7 +10,7 @@ NAMING_CONVENTION = { @@ -10,7 +10,7 @@ NAMING_CONVENTION = {
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s"
}
}
metadata = MetaData(naming_convention=NAMING_CONVENTION)
Base = declarative_base(metadata=metadata)

2
ordr/models/mymodel.py

@ -3,7 +3,7 @@ from sqlalchemy import ( @@ -3,7 +3,7 @@ from sqlalchemy import (
Index,
Integer,
Text,
)
)
from .meta import Base

2
ordr/resources/__init__.py

@ -10,7 +10,7 @@ class RootResource: @@ -10,7 +10,7 @@ class RootResource:
'''
def __init__(self, request):
''' Create the root resource
''' Create the root resource
:param pyramid.request.Request request: the current request object
'''

11
ordr/security.py

@ -13,17 +13,16 @@ def crypt_context_settings_to_string(settings, prefix='passlib.'): @@ -13,17 +13,16 @@ def crypt_context_settings_to_string(settings, prefix='passlib.'):
:rtype: (str) config string in INI format for CryptContext.load()
This looks at first like a dump hack, but the parsing of all possible
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.
'''
as_list_keys = {'schemes', 'deprecated'}
config_lines = ['[passlib]']
for ini_key, value in settings.items():
if ini_key.startswith(prefix):
context_key = ini_key.replace(prefix, '')
# the pyramid .ini format is different on lists
context_key = ini_key.replace(prefix, '')
# the pyramid .ini format is different on lists
# 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))
config_lines.append(f'{context_key} = {value}')
return '\n'.join(config_lines)
@ -32,7 +31,7 @@ def crypt_context_settings_to_string(settings, prefix='passlib.'): @@ -32,7 +31,7 @@ def crypt_context_settings_to_string(settings, prefix='passlib.'):
def includeme(config):
''' 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()

1
ordr/tests/_functional/__init__.py

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

4
ordr/tests/_functional/errors.py

@ -1,8 +1,8 @@ @@ -1,8 +1,8 @@
''' 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)
assert '404' in result

6
ordr/tests/_functional/pages.py

@ -1,13 +1,13 @@ @@ -1,13 +1,13 @@
''' 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('/')
assert 'Ordr' in result
def test_faq(testapp):
def test_faq(testapp): # noqa: F811
result = testapp.get('/faq')
assert 'FAQ' in result

1
ordr/tests/resources/__init__.py

@ -0,0 +1 @@ @@ -0,0 +1 @@

14
ordr/tests/resources/root_resource.py

@ -0,0 +1,14 @@ @@ -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 @@ @@ -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 @@ @@ -0,0 +1 @@

1
ordr/tests/views/errors.py

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

2
ordr/tests/views/pages.py

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

4
ordr/views/pages.py

@ -2,7 +2,7 @@ from pyramid.view import view_config @@ -2,7 +2,7 @@ from pyramid.view import view_config
@view_config(
context='ordr.resources.RootResource',
context='ordr.resources.RootResource',
permission='view',
renderer='ordr:templates/pages/welcome.jinja2',
)
@ -11,7 +11,7 @@ def welcome(context, request): @@ -11,7 +11,7 @@ def welcome(context, request):
@view_config(
context='ordr.resources.RootResource',
context='ordr.resources.RootResource',
name='faq',
permission='view',
renderer='ordr:templates/pages/faq.jinja2'