Shotca avatar Shotca committed fd3fa7e

Fixed bunch of bugs, added even more tests

Comments (0)

Files changed (4)

 syntax: glob
 *.pyc
-.ropeproject
 dist
 build
 *egg-info
+.*

flaskext/auth/auth.py

         self.salt = salt
         self.role = role
 
-    def set_and_encrypt_password(self, password, salt=str(int(time.time())),
-                                hash_algorithm=None):
+    def set_and_encrypt_password(self, password, salt=str(int(time.time()))):
         """
         Encrypts and sets the password. If no salt is provided, a new
         one is generated.
         """
         self.salt = salt
-        self.password = encrypt(password, self.salt, hash_algorithm)
+        self.password = encrypt(password, self.salt)
 
     def authenticate(self, password):
         """
         return False
 
     def __eq__(self, other):
-        return self.username == other.username
+        return hasattr(other, 'username') and self.username == other.username
 
     def is_logged_in(self):
-        return get_current_user == self
+        return get_current_user() == self
 
 def encrypt(password, salt=None, hash_algorithm=None):
     """Encrypts a password based on the hashing algorithm."""
     session.pop(SESSION_LOGIN_KEY, None)
     return session.pop(SESSION_USER_KEY, None)
     
-def get_current_user(timeout=True):
+def get_current_user(apply_timeout=True):
     """
     Returns the current user if there is one and he didn't time out yet. If
-    timeout should be ignored, provide timeout=False.
+    timeout should be ignored, provide apply_timeout=False.
     """
     user = session.get(SESSION_USER_KEY, None)
     if user is None:
         return None
-    if not timeout:
+    if not apply_timeout:
         return user
     login_datetime = session[SESSION_LOGIN_KEY]
     now = datetime.datetime.utcnow()
-    timeout = current_app.auth.user_timeout
-    if timeout > 0 and (now - login_datetime).seconds > timeout:
+    user_timeout = current_app.auth.user_timeout
+    if user_timeout > 0 and now - login_datetime > \
+       datetime.timedelta(seconds=user_timeout):
         logout()
         return None
     return user

flaskext/auth/permissions.py

         self.action = action
 
     def __eq__(self, other):
-        return self.resource == other.resource and self.action == other.resource
+        return self.resource == other.resource and self.action == other.action
 
 class Role(object):
     """
 import unittest, hashlib
-from flask import Flask, session
+from flask import Flask, session, url_for
 from flaskext.auth import Auth, AuthUser, login, logout, get_current_user, \
-        encrypt, Role, Permission, has_permission
+        encrypt, Role, Permission, has_permission, login_required, \
+        permission_required
 from flaskext.auth.auth import SESSION_USER_KEY, SESSION_LOGIN_KEY
 
 class EncryptionTestCase(unittest.TestCase):
     HASH_ALGORITHM = hashlib.sha1
     PRECOMPUTED_RESULT = 'cbfdac6008f9cab4083784cbd1874f76618d2a97'
 
+    def setUp(self):
+        app = Flask(__name__)
+        auth = Auth(app)
+        self.app = app
+        auth.hash_algorithm = self.HASH_ALGORITHM
+        user = AuthUser(username='user')
+        self.user = user
+
     def test_encryption(self):
-        sha1_encrypted_result = self.PRECOMPUTED_RESULT
         assert encrypt(password=self.PASSWORD, salt=self.SALT,
-                       hash_algorithm=self.HASH_ALGORITHM) == sha1_encrypted_result
+                       hash_algorithm=self.HASH_ALGORITHM) == self.PRECOMPUTED_RESULT
 
     def test_set_and_encrypt_password(self):
-        user = AuthUser(username='user')
-        user.set_and_encrypt_password(self.PASSWORD, self.SALT, self.HASH_ALGORITHM)
-        assert user.password == self.PRECOMPUTED_RESULT
-        assert user.salt == self.SALT
+        with self.app.test_request_context():
+            self.user.set_and_encrypt_password(self.PASSWORD, self.SALT)
+        assert self.user.password == self.PRECOMPUTED_RESULT
+        assert self.user.salt == self.SALT
 
 class LoginTestCase(unittest.TestCase):
+    PASSWORD = 'password'
+    
     def setUp(self):
         app = Flask(__name__)
         app.secret_key = 'N4buDSXfaHx2oO8g'
-        Auth(app)
+        auth = Auth(app)
+        auth.hash_algorithm = hashlib.sha1
+        user = AuthUser(username='user')
+        with app.test_request_context():
+            user.set_and_encrypt_password(self.PASSWORD)
         self.app = app
-        user = AuthUser(username='user')
-        user.set_and_encrypt_password('password', hash_algorithm=hashlib.sha1)
         self.user = user
 
     def tearDown(self):
 
     def test_user_expiration(self):
         import time
-        self.app.auth.user_timeout = 1
         with self.app.test_request_context():
+            self.app.auth.user_timeout = 0.01
             login(self.user)
-            time.sleep(2)
+            time.sleep(0.02)
             assert get_current_user() is None
 
+    def test_user_expiration_override(self):
+        import time
+        with self.app.test_request_context():
+            self.app.auth.user_timeout = 0.01
+            login(self.user)
+            time.sleep(0.02)
+            assert get_current_user(apply_timeout=False) == self.user
+
+    def test_authenticate(self):
+        with self.app.test_request_context():
+            assert self.user.authenticate(self.PASSWORD) == True
+            assert self.user.is_logged_in() == True
+            assert get_current_user() == self.user
+
+    def test_authenticate_fail(self):
+        with self.app.test_request_context():
+            assert self.user.authenticate('bla') == False
+            assert self.user.is_logged_in() == False
+
 class PermissionTestCase(unittest.TestCase):
-    ROLES = {'testuser': Role('testuser', [
-        Permission('post', 'view'),
-    ])}
+    post_view = Permission('post', 'view')
+    post_update = Permission('post', 'update')
+    ROLES = {'testuser': Role('testuser', [post_view])}
 
     def setUp(self):
         app = Flask(__name__)
-        app.secret_key = 'N4buDSXfaHx2oO8g'
         auth = Auth(app)
+        self.app = app
         def load_role(role_name):
             return self.ROLES.get(role_name)
         auth.load_role = load_role
-        self.app = app
         user = AuthUser(username='user')
         user.role = 'testuser'
         self.user = user
             assert has_permission(self.user, 'user', 'view') == False
             assert has_permission(self.user, 'user', 'create') == False
 
+    def test_permission_equals(self):
+        assert self.post_view == Permission('post', 'view')
+        assert self.post_update != self.post_view
+
+class RequestTestCase(unittest.TestCase):
+
+    def setUp(self):
+        app = Flask(__name__)
+        app.secret_key = 'N4buDSXfaHx2oO8g'
+        self.app = app
+        auth = Auth(app)
+        @login_required()
+        def needs_login():
+            return 'needs_login'
+        app.add_url_rule('/needs_login/', 'needs_login', needs_login)
+
+        @permission_required(resource='post', action='view')
+        def post_view():
+            return 'needs_post_view'
+        app.add_url_rule('/post_view/', 'post_view', post_view)
+
+        @app.route('/login_view/')
+        def login_view():
+            return 'login_view'
+
+        user = AuthUser(username='user')
+        user.role = 'testuser'
+        testuser_role = Role('testuser', [Permission('post', 'view')])
+        auth.load_role = lambda _: testuser_role
+        self.user = user
+
+    def tearDown(self):
+        with self.app.test_request_context():
+            logout()
+
+    def test_default_not_logged_in_callback(self):
+        with self.app.test_request_context():
+            with self.app.test_client() as client:
+                assert client.get('/needs_login/').status_code == 401
+
+    def test_login_url_callback(self):
+        self.app.auth = Auth(self.app, login_url_name='login_view')
+        with self.app.test_request_context():
+            with self.app.test_client() as client:
+                assert client.get('/needs_login/').status_code == 302 
+                assert client.get('/needs_login/',
+                                  follow_redirects=True).data == 'login_view'
+
+    def test_permission_required_no_login(self):
+        with self.app.test_request_context():
+            with self.app.test_client() as client:
+                assert client.get('/post_view/').status_code == 401
+
 if __name__ == '__main__':
     suite = unittest.TestLoader().discover(start_dir='.')
     unittest.TextTestRunner(verbosity=2).run(suite)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.