From 4559268c05ef6e029c18d61ff2244dc4d8ab2a76 Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Fri, 9 Mar 2012 19:19:19 -0500 Subject: [PATCH] Added some tests, which are always good. Right? --- README.md | 8 ++++++- flask_security/__init__.py | 4 ++-- tests/unit_tests.py | 44 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 tests/unit_tests.py diff --git a/README.md b/README.md index 8b56fb3..d2610bd 100644 --- a/README.md +++ b/README.md @@ -111,4 +111,10 @@ Flask-Security comes packed with a few Flask-Script commands. They are: * `flask.ext.security.script.DeactivateUserCommand` * `flask.ext.security.script.ActivateUserCommand` -Register these on your script manager for pure convenience. \ No newline at end of file +Register these on your script manager for pure convenience. + +## Contributing + +Feel free to fork and contribute. If you decided to do so, just be sure to include relevant tests that you feel are necessary. To run the tests, please provide instructions for any requirements. For instance, if you write a new datastore implementation, please provide instructions on how best to setup a connection when testing. + +If you plan on running all the provided tests you'll need a local installation of MongoDB running on the standard port 27017 without username/password protection. \ No newline at end of file diff --git a/flask_security/__init__.py b/flask_security/__init__.py index 7174e09..d55c2dd 100644 --- a/flask_security/__init__.py +++ b/flask_security/__init__.py @@ -177,7 +177,7 @@ class UserMixin(BaseUserMixin): return self.active def has_role(self, role): - if type(role) is StringType: + if not isinstance(role, Role): role = Role(name=role) return role in self.roles @@ -188,7 +188,7 @@ class UserMixin(BaseUserMixin): class AnonymousUser(AnonymousUserBase): def __init__(self): super(AnonymousUser, self).__init__() - self.roles = [] # TODO: Make this immutable + self.roles = [] # TODO: Make this immutable? def has_role(self, *args): return False diff --git a/tests/unit_tests.py b/tests/unit_tests.py new file mode 100644 index 0000000..ece4c8e --- /dev/null +++ b/tests/unit_tests.py @@ -0,0 +1,44 @@ +import unittest +import flask_security +from flask_security import RoleMixin, UserMixin, AnonymousUser + +class Role(RoleMixin): + def __init__(self, name, description=None): + self.name = name + self.description = description + + +class User(UserMixin): + def __init__(self, username, email, roles): + self.username = username + self.email = email + self.roles = roles + +# set the models or we'll get errors +flask_security.User = User +flask_security.Role = Role + +admin = Role('admin') +admin2 = Role('admin') +editor = Role('editor') + +user = User('matt', 'matt@lp.com', [admin, editor]) + +class SecurityEntityTests(unittest.TestCase): + + def test_role_mixin_equal(self): + self.assertEqual(admin, admin2) + + def test_role_mixin_not_equal(self): + self.assertNotEqual(admin, editor) + + def test_user_mixin_has_role_with_string(self): + self.assertTrue(user.has_role('admin')) + + def test_user_mixin_has_role_with_role_obj(self): + self.assertTrue(user.has_role(Role('admin'))) + + def test_anonymous_user_has_no_roles(self): + au = AnonymousUser() + self.assertEqual(0, len(au.roles)) + self.assertFalse(au.has_role('admin')) \ No newline at end of file