Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ABCMeta.abstractmethod to prevent invalid classes from being instantiated. #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions swampdragon/permissions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from abc import ABCMeta, abstractmethod
from django.utils import six


def login_required(func):
def not_logged_in(self, **kwargs):
self.send_login_required({'signin_required': 'you need to sign in'})
Expand All @@ -11,12 +15,14 @@ def check_user(self, **kwargs):
return check_user


class RoutePermission(object):
class RoutePermission(six.with_metaclass(ABCMeta, object)):
@abstractmethod
def test_permission(self, handler, verb, **kwargs):
raise NotImplementedError("You need to implement test_permission")
pass

@abstractmethod
def permission_failed(self, handler):
raise NotImplementedError("You need to implement permission_failed")
pass


class LoginRequired(RoutePermission):
Expand Down
7 changes: 5 additions & 2 deletions swampdragon/serializers/field_deserializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from abc import ABCMeta, abstractmethod
from dateutil.parser import parse
from django.utils import six


deserializer_map = {}
Expand All @@ -8,9 +10,10 @@ def register_field_deserializer(field_name, deserializer):
deserializer_map[field_name] = deserializer


class BaseFieldDeserializer(object):
class BaseFieldDeserializer(six.with_metaclass(ABCMeta, object)):
@abstractmethod
def __call__(self, *args, **kwargs):
raise NotImplemented()
pass


class DateTimeDeserializer(BaseFieldDeserializer):
Expand Down
15 changes: 11 additions & 4 deletions swampdragon/sessions/session_store.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
class BaseSessionStore(object):
from abc import ABCMeta, abstractmethod
from django.utils import six


class BaseSessionStore(six.with_metaclass(ABCMeta, object)):
def __init__(self, connection):
self.connection = connection
self.keys = []

@abstractmethod
def set(self, key, val):
raise NotImplemented()
pass

@abstractmethod
def get(self, key):
raise NotImplemented()
pass

@abstractmethod
def refresh_key_timeout(self, key):
raise NotImplemented()
pass

def refresh_all_keys(self):
for key in self.keys:
Expand Down
36 changes: 14 additions & 22 deletions tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,11 @@ class BrokenPermission(RoutePermission):
pass


class BrokenPermissionRouter(BaseRouter):
valid_verbs = ['do_something']
permission_classes = [BrokenPermission()]

def do_something(self, **kwargs):
pass


class HalfAPermission(RoutePermission):
def test_permission(self, handler, verb, **kwargs):
return self.permission_failed(handler)


class HalfAPermissionRouter(BaseRouter):
valid_verbs = ['do_something']
permission_classes = [HalfAPermission()]

def do_something(self, **kwargs):
pass


class TestPermissions(DragonTestCase):
def test_login_required_decorator(self):
router = TestRouterDecorated(self.connection)
Expand Down Expand Up @@ -130,11 +114,19 @@ def test_pass_custom_permission(self):
self.assertEqual(self.connection.last_message['context']['state'], SUCCESS)

def test_broken_permission(self):
router = BrokenPermissionRouter(self.connection)
with self.assertRaises(NotImplementedError):
router.handle({'verb': 'do_something'})
with self.assertRaises(TypeError):
class BrokenPermissionRouter(BaseRouter):
valid_verbs = ['do_something']
permission_classes = [BrokenPermission()]

def do_something(self, **kwargs):
pass

def test_permission_missing_permission_failed(self):
router = HalfAPermissionRouter(self.connection)
with self.assertRaises(NotImplementedError):
router.handle({'verb': 'do_something'})
with self.assertRaises(TypeError):
class HalfAPermissionRouter(BaseRouter):
valid_verbs = ['do_something']
permission_classes = [HalfAPermission()]

def do_something(self, **kwargs):
pass
3 changes: 3 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def set(self, key, val):
def get(self, key):
return self.data[key]

def refresh_key_timeout(self, key):
pass


class TestSessions(DragonTestCase):
def setUp(self):
Expand Down