mirror of
https://github.com/wassname/flask-security.git
synced 2026-08-12 12:00:37 +08:00
Starting a large refactor and adding confirmation abilities
This commit is contained in:
+63
-23
@@ -1,4 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import unittest
|
||||
|
||||
from example import app
|
||||
|
||||
|
||||
@@ -25,12 +28,14 @@ class SecurityTest(unittest.TestCase):
|
||||
def _post(self, route, data=None, content_type=None, follow_redirects=True):
|
||||
return self.client.post(route, data=data,
|
||||
follow_redirects=follow_redirects,
|
||||
content_type=content_type or 'text/html')
|
||||
content_type=content_type or 'application/x-www-form-urlencoded')
|
||||
|
||||
def authenticate(self, username, password, endpoint=None):
|
||||
data = dict(username=username, password=password)
|
||||
return self._post(endpoint or '/auth', data=data,
|
||||
content_type='application/x-www-form-urlencoded')
|
||||
def register(self, email, password, endpoint=None):
|
||||
return self._post(endpoint or '/register')
|
||||
|
||||
def authenticate(self, email, password, endpoint=None):
|
||||
data = dict(email=email, password=password)
|
||||
return self._post(endpoint or '/auth', data=data)
|
||||
|
||||
def logout(self, endpoint=None):
|
||||
return self._get(endpoint or '/logout', follow_redirects=True)
|
||||
@@ -43,15 +48,15 @@ class DefaultSecurityTests(SecurityTest):
|
||||
self.assertIn('Login Page', r.data)
|
||||
|
||||
def test_authenticate(self):
|
||||
r = self.authenticate("matt", "password")
|
||||
self.assertIn('Home Page', r.data)
|
||||
r = self.authenticate("matt@lp.com", "password")
|
||||
self.assertIn('Hello matt@lp.com', r.data)
|
||||
|
||||
def test_unprovided_username(self):
|
||||
r = self.authenticate("", "password")
|
||||
self.assertIn("Username not provided", r.data)
|
||||
self.assertIn("Email not provided", r.data)
|
||||
|
||||
def test_unprovided_password(self):
|
||||
r = self.authenticate("matt", "")
|
||||
r = self.authenticate("matt@lp.com", "")
|
||||
self.assertIn("Password not provided", r.data)
|
||||
|
||||
def test_invalid_user(self):
|
||||
@@ -59,15 +64,15 @@ class DefaultSecurityTests(SecurityTest):
|
||||
self.assertIn("Specified user does not exist", r.data)
|
||||
|
||||
def test_bad_password(self):
|
||||
r = self.authenticate("matt", "bogus")
|
||||
r = self.authenticate("matt@lp.com", "bogus")
|
||||
self.assertIn("Password does not match", r.data)
|
||||
|
||||
def test_inactive_user(self):
|
||||
r = self.authenticate("tiya", "password")
|
||||
r = self.authenticate("tiya@lp.com", "password")
|
||||
self.assertIn("Inactive user", r.data)
|
||||
|
||||
def test_logout(self):
|
||||
self.authenticate("matt", "password")
|
||||
self.authenticate("matt@lp.com", "password")
|
||||
r = self.logout()
|
||||
self.assertIn('Home Page', r.data)
|
||||
|
||||
@@ -76,28 +81,28 @@ class DefaultSecurityTests(SecurityTest):
|
||||
self.assertIn('Please log in to access this page', r.data)
|
||||
|
||||
def test_authorized_access(self):
|
||||
self.authenticate("matt", "password")
|
||||
self.authenticate("matt@lp.com", "password")
|
||||
r = self._get("/profile")
|
||||
self.assertIn('profile', r.data)
|
||||
|
||||
def test_valid_admin_role(self):
|
||||
self.authenticate("matt", "password")
|
||||
self.authenticate("matt@lp.com", "password")
|
||||
r = self._get("/admin")
|
||||
self.assertIn('Admin Page', r.data)
|
||||
|
||||
def test_invalid_admin_role(self):
|
||||
self.authenticate("joe", "password")
|
||||
self.authenticate("joe@lp.com", "password")
|
||||
r = self._get("/admin", follow_redirects=True)
|
||||
self.assertIn('Home Page', r.data)
|
||||
|
||||
def test_roles_accepted(self):
|
||||
for user in ("matt", "joe"):
|
||||
for user in ("matt@lp.com", "joe@lp.com"):
|
||||
self.authenticate(user, "password")
|
||||
r = self._get("/admin_or_editor")
|
||||
self.assertIn('Admin or Editor Page', r.data)
|
||||
self.logout()
|
||||
|
||||
self.authenticate("jill", "password")
|
||||
self.authenticate("jill@lp.com", "password")
|
||||
r = self._get("/admin_or_editor", follow_redirects=True)
|
||||
self.assertIn('Home Page', r.data)
|
||||
|
||||
@@ -105,17 +110,22 @@ class DefaultSecurityTests(SecurityTest):
|
||||
r = self._get('/admin', follow_redirects=True)
|
||||
self.assertIn('<input id="next"', r.data)
|
||||
|
||||
def test_register_valid_user(self):
|
||||
data = dict(email='dude@lp.com', password='password', password_confirm='password')
|
||||
self.client.post('/register', data=data, follow_redirects=True)
|
||||
r = self.authenticate('dude@lp.com', 'password')
|
||||
self.assertIn('Hello dude@lp.com', r.data)
|
||||
|
||||
class ConfiguredSecurityTests(SecurityTest):
|
||||
|
||||
class ConfiguredURLTests(SecurityTest):
|
||||
|
||||
AUTH_CONFIG = {
|
||||
'SECURITY_PASSWORD_HASH': 'bcrypt',
|
||||
'SECURITY_USER_DATASTORE': 'custom_datastore_name',
|
||||
'SECURITY_AUTH_URL': '/custom_auth',
|
||||
'SECURITY_LOGOUT_URL': '/custom_logout',
|
||||
'SECURITY_LOGIN_VIEW': '/custom_login',
|
||||
'SECURITY_POST_LOGIN_VIEW': '/post_login',
|
||||
'SECURITY_POST_LOGOUT_VIEW': '/post_logout'
|
||||
'SECURITY_POST_LOGOUT_VIEW': '/post_logout',
|
||||
'SECURITY_POST_REGISTER_VIEW': '/post_register'
|
||||
}
|
||||
|
||||
def test_login_view(self):
|
||||
@@ -123,14 +133,44 @@ class ConfiguredSecurityTests(SecurityTest):
|
||||
self.assertIn("Custom Login Page", r.data)
|
||||
|
||||
def test_authenticate(self):
|
||||
r = self.authenticate("matt", "password", endpoint="/custom_auth")
|
||||
r = self.authenticate("matt@lp.com", "password", endpoint="/custom_auth")
|
||||
self.assertIn('Post Login', r.data)
|
||||
|
||||
def test_logout(self):
|
||||
self.authenticate("matt", "password", endpoint="/custom_auth")
|
||||
self.authenticate("matt@lp.com", "password", endpoint="/custom_auth")
|
||||
r = self.logout(endpoint="/custom_logout")
|
||||
self.assertIn('Post Logout', r.data)
|
||||
|
||||
def test_register(self):
|
||||
data = dict(email='dude@lp.com', password='password', password_confirm='password')
|
||||
r = self.client.post('/register', data=data, follow_redirects=True)
|
||||
self.assertIn('Hello dude@lp.com', r.data)
|
||||
self.assertIn('Post Register', r.data)
|
||||
|
||||
|
||||
class ConfirmationTests(SecurityTest):
|
||||
AUTH_CONFIG = {
|
||||
'SECURITY_CONFIRM_EMAIL': True,
|
||||
'SECURITY_LOGIN_WITHOUT_CONFIRMATION': True
|
||||
}
|
||||
|
||||
def test_register_valid_user_automatically_signs_in(self):
|
||||
e = 'dude@lp.com'
|
||||
p = 'password'
|
||||
data = dict(email=e, password=p, password_confirm=p)
|
||||
r = self.client.post('/register', data=data, follow_redirects=True)
|
||||
self.assertIn(e, r.data)
|
||||
|
||||
def test_register_valid_user_sends_confirmation_email(self):
|
||||
e = 'dude@lp.com'
|
||||
p = 'password'
|
||||
data = dict(email=e, password=p, password_confirm=p)
|
||||
|
||||
with self.app.mail.record_messages() as outbox:
|
||||
self.client.post('/register', data=data, follow_redirects=True)
|
||||
self.assertEqual(len(outbox), 1)
|
||||
self.assertIn(e, outbox[0].html)
|
||||
|
||||
|
||||
class MongoEngineSecurityTests(DefaultSecurityTests):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user