Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #37 from postatum/94334404_code_cleanup2
Browse files Browse the repository at this point in the history
Functions/methods renaming
  • Loading branch information
jstoiko committed May 14, 2015
2 parents 7f38970 + 2d1e849 commit 6636da7
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 67 deletions.
22 changes: 11 additions & 11 deletions nefertari/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def is_admin(cls, user):
return 'admin' in user.groups

@classmethod
def token_credentials(cls, username, request):
def get_token_credentials(cls, username, request):
""" Get api token for user with username of :username:
Used by Token-based auth as `credentials_callback` kwarg.
Expand All @@ -54,7 +54,7 @@ def token_credentials(cls, username, request):
return user.api_key.token

@classmethod
def groups_by_token(cls, username, token, request):
def get_groups_by_token(cls, username, token, request):
""" Get user's groups if user with :username: exists and their api key
token equals :token:
Expand Down Expand Up @@ -94,7 +94,7 @@ def verify_password(user, password):
return success, user

@classmethod
def groups_by_userid(cls, userid, request):
def get_groups_by_userid(cls, userid, request):
""" Return group identifiers of user with id :userid:
Used by Ticket-based auth as `callback` kwarg.
Expand Down Expand Up @@ -125,7 +125,7 @@ def create_account(cls, params):
raise JHTTPBadRequest('Failed to create account.')

@classmethod
def authuser_by_userid(cls, request):
def get_authuser_by_userid(cls, request):
""" Get user by ID.
Used by Ticket-based auth. Is added as request method to populate
Expand All @@ -136,7 +136,7 @@ def authuser_by_userid(cls, request):
return cls.get_resource(**{cls.pk_field(): _id})

@classmethod
def authuser_by_name(cls, request):
def get_authuser_by_name(cls, request):
""" Get user by username
Used by Token-based auth. Is added as request method to populate
Expand All @@ -151,7 +151,7 @@ def lower_strip(value):
return (value or '').lower().strip()


def crypt_password(password):
def encrypt_password(password):
""" Crypt :password: if it's not crypted yet. """
if password and not crypt.match(password):
password = unicode(crypt.encode(password))
Expand All @@ -173,18 +173,18 @@ class AuthUser(AuthModelDefaultMixin, engine.BaseDocument):
email = engine.StringField(
unique=True, required=True, processors=[lower_strip])
password = engine.StringField(
min_length=3, required=True, processors=[crypt_password])
min_length=3, required=True, processors=[encrypt_password])
groups = engine.ListField(
item_type=engine.StringField,
choices=['admin', 'user'], default=['user'])


def apikey_token():
def create_apikey_token():
""" Generate ApiKey.token using uuid library. """
return uuid.uuid4().hex.replace('-', '')


def apikey_model(user_model):
def create_apikey_model(user_model):
""" Generate ApiKey model class and connect it with :user_model:.
ApiKey is generated with relationship to user model class :user_model:
Expand Down Expand Up @@ -215,7 +215,7 @@ class ApiKey(engine.BaseDocument):
__tablename__ = 'nefertari_apikey'

id = engine.IdField(primary_key=True)
token = engine.StringField(default=apikey_token)
token = engine.StringField(default=create_apikey_token)
user = engine.Relationship(
document=user_model.__name__,
uselist=False,
Expand All @@ -226,7 +226,7 @@ class ApiKey(engine.BaseDocument):
**fk_kwargs)

def reset_token(self):
self.update({'token': apikey_token()})
self.update({'token': create_apikey_token()})
return self.token

# Setup ApiKey autogeneration on :user_model: creation
Expand Down
6 changes: 3 additions & 3 deletions nefertari/authentication/policies.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pyramid.authentication import CallbackAuthenticationPolicy

from nefertari import engine
from .models import apikey_model
from .models import create_apikey_model


class ApiKeyAuthenticationPolicy(CallbackAuthenticationPolicy):
Expand All @@ -16,7 +16,7 @@ class ApiKeyAuthenticationPolicy(CallbackAuthenticationPolicy):
You may also find useful `nefertari.authentication.views.
TokenAuthenticationView`
view which offers basic functionality to create, claim, and reset the
view which offers basic functionality to create, claim, and reset the
token.
"""
def __init__(self, user_model, check=None, credentials_callback=None):
Expand All @@ -39,7 +39,7 @@ def __init__(self, user_model, check=None, credentials_callback=None):
self.user_model = user_model
if isinstance(self.user_model, basestring):
self.user_model = engine.get_document_cls(self.user_model)
apikey_model(self.user_model)
create_apikey_model(self.user_model)

self.check = check
self.credentials_callback = credentials_callback
Expand Down
2 changes: 1 addition & 1 deletion nefertari/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def claim_token(self, **params):
else:
raise JHTTPNotFound('User not found')

def token_reset(self, **params):
def reset_token(self, **params):
""" Reset current token by POSTing 'login' and 'password'.
User's `Authorization` header value is returned in `WWW-Authenticate`
Expand Down
8 changes: 4 additions & 4 deletions nefertari/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def add_route_and_view(config, action, route_name, path, request_method,
return action_route


def default_view(resource):
def get_default_view_path(resource):
"Returns the dotted path to the default view class."

parts = [a.member_name for a in resource.ancestors] +\
Expand Down Expand Up @@ -243,7 +243,7 @@ def add(self, member_name, collection_name='', parent=None, uid='',
prefix=prefix)

view = maybe_dotted(
kwargs.pop('view', None) or default_view(new_resource))
kwargs.pop('view', None) or get_default_view_path(new_resource))

for name, val in kwargs.pop('view_args', {}).items():
setattr(view, name, val)
Expand Down Expand Up @@ -307,12 +307,12 @@ def add(self, member_name, collection_name='', parent=None, uid='',

return new_resource

def add_from(self, resource, **kwargs):
def add_from_child(self, resource, **kwargs):
""" Add a resource with its all children resources to the current
resource.
"""

new_resource = self.add(
resource.member_name, resource.collection_name, **kwargs)
for child in resource.children:
new_resource.add_from(child, **kwargs)
new_resource.add_from_child(child, **kwargs)
10 changes: 5 additions & 5 deletions nefertari/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ def snake2camel(text):
return ''.join([a.title() for a in text.split("_")])


def maybe_dotted(modul, throw=True):
""" If ``modul`` is a dotted string pointing to the modul,
imports and returns the modul object.
def maybe_dotted(module, throw=True):
""" If ``module`` is a dotted string pointing to the module,
imports and returns the module object.
"""
try:
return Configurator().maybe_dotted(modul)
return Configurator().maybe_dotted(module)
except ImportError as e:
err = '%s not found. %s' % (modul, e)
err = '%s not found. %s' % (module, e)
if throw:
raise ImportError(err)
else:
Expand Down
66 changes: 33 additions & 33 deletions tests/test_authentication/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ def test_lower_strip(self, engine_mock):
assert models.lower_strip('Foo ') == 'foo'
assert models.lower_strip(None) == ''

def test_crypt_password(self, engine_mock):
def test_encrypt_password(self, engine_mock):
from nefertari.authentication import models
encrypted = models.crypt_password('foo')
encrypted = models.encrypt_password('foo')
assert models.crypt.match(encrypted)
assert encrypted != 'foo'
assert encrypted == models.crypt_password(encrypted)
assert encrypted == models.encrypt_password(encrypted)

@patch('nefertari.authentication.models.uuid.uuid4')
def test_apikey_token(self, mock_uuid, engine_mock):
def test_create_apikey_token(self, mock_uuid, engine_mock):
from nefertari.authentication import models
mock_uuid.return_value = Mock(hex='foo-bar')
assert models.apikey_token() == 'foobar'
assert models.create_apikey_token() == 'foobar'


mixin_path = 'nefertari.authentication.models.AuthModelDefaultMixin.'
Expand All @@ -38,72 +38,72 @@ def test_is_admin(self, engine_mock):
assert models.AuthModelDefaultMixin.is_admin(user)

@patch(mixin_path + 'get_resource')
def test_token_credentials(self, mock_res, engine_mock):
def test_get_token_credentials(self, mock_res, engine_mock):
from nefertari.authentication import models
user = Mock()
user.api_key.token = 'foo-token'
mock_res.return_value = user
token = models.AuthModelDefaultMixin.token_credentials('user1', 1)
token = models.AuthModelDefaultMixin.get_token_credentials('user1', 1)
assert token == 'foo-token'
mock_res.assert_called_once_with(username='user1')

@patch(mixin_path + 'get_resource')
def test_token_credentials_user_not_found(self, mock_res, engine_mock):
def test_get_token_credentials_user_not_found(self, mock_res, engine_mock):
from nefertari.authentication import models
mock_res.return_value = None
token = models.AuthModelDefaultMixin.token_credentials('user1', 1)
token = models.AuthModelDefaultMixin.get_token_credentials('user1', 1)
assert token is None
mock_res.assert_called_once_with(username='user1')

@patch('nefertari.authentication.models.forget')
@patch(mixin_path + 'get_resource')
def test_token_credentials_query_error(
def test_get_token_credentials_query_error(
self, mock_res, mock_forg, engine_mock):
from nefertari.authentication import models
mock_res.side_effect = Exception
token = models.AuthModelDefaultMixin.token_credentials('user1', 1)
token = models.AuthModelDefaultMixin.get_token_credentials('user1', 1)
assert token is None
mock_res.assert_called_once_with(username='user1')
mock_forg.assert_called_once_with(1)

@patch(mixin_path + 'get_resource')
def test_groups_by_token(self, mock_res, engine_mock):
def test_get_groups_by_token(self, mock_res, engine_mock):
from nefertari.authentication import models
user = Mock(groups=['admin', 'user'])
user.api_key.token = 'token'
mock_res.return_value = user
groups = models.AuthModelDefaultMixin.groups_by_token(
groups = models.AuthModelDefaultMixin.get_groups_by_token(
'user1', 'token', 1)
assert groups == ['g:admin', 'g:user']
mock_res.assert_called_once_with(username='user1')

@patch(mixin_path + 'get_resource')
def test_groups_by_token_user_not_found(self, mock_res, engine_mock):
def test_get_groups_by_token_user_not_found(self, mock_res, engine_mock):
from nefertari.authentication import models
mock_res.return_value = None
groups = models.AuthModelDefaultMixin.groups_by_token(
groups = models.AuthModelDefaultMixin.get_groups_by_token(
'user1', 'token', 1)
assert groups is None
mock_res.assert_called_once_with(username='user1')

@patch(mixin_path + 'get_resource')
def test_groups_by_token_wrong_token(self, mock_res, engine_mock):
def test_get_groups_by_token_wrong_token(self, mock_res, engine_mock):
from nefertari.authentication import models
user = Mock(groups=['admin', 'user'])
user.api_key.token = 'dasdasd'
mock_res.return_value = user
groups = models.AuthModelDefaultMixin.groups_by_token(
groups = models.AuthModelDefaultMixin.get_groups_by_token(
'user1', 'token', 1)
assert groups is None
mock_res.assert_called_once_with(username='user1')

@patch('nefertari.authentication.models.forget')
@patch(mixin_path + 'get_resource')
def test_groups_by_token_query_error(
def test_get_groups_by_token_query_error(
self, mock_res, mock_forg, engine_mock):
from nefertari.authentication import models
mock_res.side_effect = Exception
groups = models.AuthModelDefaultMixin.groups_by_token(
groups = models.AuthModelDefaultMixin.get_groups_by_token(
'user1', 'token', 1)
assert groups is None
mock_res.assert_called_once_with(username='user1')
Expand Down Expand Up @@ -157,37 +157,37 @@ def test_authenticate_by_password_exception(self, mock_res, engine_mock):

@patch(mixin_path + 'pk_field')
@patch(mixin_path + 'get_resource')
def test_groups_by_userid(self, mock_res, mock_field, engine_mock):
def test_get_groups_by_userid(self, mock_res, mock_field, engine_mock):
from nefertari.authentication import models
mock_field.return_value = 'idid'
user = Mock(groups=['admin', 'user'])
mock_res.return_value = user
groups = models.AuthModelDefaultMixin.groups_by_userid(
groups = models.AuthModelDefaultMixin.get_groups_by_userid(
'user1', 1)
assert groups == ['g:admin', 'g:user']
mock_res.assert_called_once_with(idid='user1')

@patch(mixin_path + 'pk_field')
@patch(mixin_path + 'get_resource')
def test_groups_by_userid_user_not_found(
def test_get_groups_by_userid_user_not_found(
self, mock_res, mock_field, engine_mock):
from nefertari.authentication import models
mock_field.return_value = 'idid'
mock_res.return_value = None
groups = models.AuthModelDefaultMixin.groups_by_userid(
groups = models.AuthModelDefaultMixin.get_groups_by_userid(
'user1', 1)
assert groups is None
mock_res.assert_called_once_with(idid='user1')

@patch('nefertari.authentication.models.forget')
@patch(mixin_path + 'pk_field')
@patch(mixin_path + 'get_resource')
def test_groups_by_userid_query_error(
def test_get_groups_by_userid_query_error(
self, mock_res, mock_field, mock_forg, engine_mock):
from nefertari.authentication import models
mock_field.return_value = 'idid'
mock_res.side_effect = Exception
groups = models.AuthModelDefaultMixin.groups_by_userid(
groups = models.AuthModelDefaultMixin.get_groups_by_userid(
'user1', 1)
assert groups is None
mock_res.assert_called_once_with(idid='user1')
Expand All @@ -214,43 +214,43 @@ def test_create_account_bad_request(self, mock_get, engine_mock):
@patch('nefertari.authentication.models.authenticated_userid')
@patch(mixin_path + 'pk_field')
@patch(mixin_path + 'get_resource')
def test_authuser_by_userid(
def test_get_authuser_by_userid(
self, mock_res, mock_id, mock_auth, engine_mock):
from nefertari.authentication import models
mock_auth.return_value = 123
mock_id.return_value = 'idid'
models.AuthModelDefaultMixin.authuser_by_userid(1)
models.AuthModelDefaultMixin.get_authuser_by_userid(1)
mock_auth.assert_called_once_with(1)
mock_res.assert_called_once_with(idid=123)

@patch('nefertari.authentication.models.authenticated_userid')
@patch(mixin_path + 'pk_field')
@patch(mixin_path + 'get_resource')
def test_authuser_by_userid_not_authenticated(
def test_get_authuser_by_userid_not_authenticated(
self, mock_res, mock_id, mock_auth, engine_mock):
from nefertari.authentication import models
mock_auth.return_value = None
mock_id.return_value = 'idid'
models.AuthModelDefaultMixin.authuser_by_userid(1)
models.AuthModelDefaultMixin.get_authuser_by_userid(1)
mock_auth.assert_called_once_with(1)
assert not mock_res.called

@patch('nefertari.authentication.models.authenticated_userid')
@patch(mixin_path + 'get_resource')
def test_authuser_by_name(
def test_get_authuser_by_name(
self, mock_res, mock_auth, engine_mock):
from nefertari.authentication import models
mock_auth.return_value = 'user1'
models.AuthModelDefaultMixin.authuser_by_name(1)
models.AuthModelDefaultMixin.get_authuser_by_name(1)
mock_auth.assert_called_once_with(1)
mock_res.assert_called_once_with(username='user1')

@patch('nefertari.authentication.models.authenticated_userid')
@patch(mixin_path + 'get_resource')
def test_authuser_by_name_not_authenticated(
def test_get_authuser_by_name_not_authenticated(
self, mock_res, mock_auth, engine_mock):
from nefertari.authentication import models
mock_auth.return_value = None
models.AuthModelDefaultMixin.authuser_by_name(1)
models.AuthModelDefaultMixin.get_authuser_by_name(1)
mock_auth.assert_called_once_with(1)
assert not mock_res.called
2 changes: 1 addition & 1 deletion tests/test_authentication/test_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .fixtures import engine_mock


@patch('nefertari.authentication.policies.apikey_model')
@patch('nefertari.authentication.policies.create_apikey_model')
class TestApiKeyAuthenticationPolicy(object):

def test_init(self, mock_apikey, engine_mock):
Expand Down

0 comments on commit 6636da7

Please sign in to comment.