forked from lingthio/Flask-User
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug fixes for MongoAlchemyDbAdapter.
- Loading branch information
Showing
10 changed files
with
91 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,28 +74,25 @@ def test_init(db): | |
|
||
hashed_password = um.hash_password('Password1') | ||
User = um.UserModel | ||
add_object = um.db_adapter.add_object | ||
|
||
# Create user1 with username and email | ||
user1 = User(username='user1', email='[email protected]', password=hashed_password) | ||
user1 = add_object(User, username='user1', email='[email protected]', password=hashed_password) | ||
assert user1 | ||
db.session.add(user1) | ||
|
||
# Create user1 with email only | ||
user2 = User(email='[email protected]', password=hashed_password,) | ||
user2 = add_object(User, email='[email protected]', password=hashed_password,) | ||
assert user2 | ||
db.session.add(user2) | ||
|
||
# Create user3 with username and email | ||
user3 = User(username='user3', email='[email protected]', password=hashed_password) | ||
user3 = add_object(User, username='user3', email='[email protected]', password=hashed_password) | ||
assert user3 | ||
db.session.add(user3) | ||
|
||
# Create user4 with email only | ||
user4 = User(email='[email protected]', password=hashed_password) | ||
user4 = add_object(User, email='[email protected]', password=hashed_password) | ||
assert user4 | ||
db.session.add(user4) | ||
|
||
db.session.commit() | ||
um.db_adapter.commit() | ||
|
||
|
||
def test_invalid_register_with_username_form(client): | ||
|
@@ -469,11 +466,12 @@ def test_cleanup(db): | |
Delete user1 and user2 | ||
""" | ||
global user1, user2, user3, user4 | ||
db.session.delete(user1) | ||
db.session.delete(user2) | ||
db.session.delete(user3) | ||
db.session.delete(user4) | ||
db.session.commit() | ||
um = current_app.user_manager | ||
um.db_adapter.delete_object(user1) | ||
um.db_adapter.delete_object(user2) | ||
um.db_adapter.delete_object(user3) | ||
um.db_adapter.delete_object(user4) | ||
um.db_adapter.commit() | ||
user1 = None | ||
user2 = None | ||
user3 = None | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
from flask_user import login_required, UserManager, UserMixin | ||
from flask_user import roles_required, confirm_email_required | ||
|
||
ORM_type = 'SQLAlchemy' | ||
ORM_type = 'MongoAlchemy' # SQLAlchemy or MongoAlchemy | ||
|
||
app = Flask(__name__) | ||
|
||
|
@@ -31,6 +31,11 @@ class ConfigClass(object): | |
MAIL_PORT = int(os.getenv('MAIL_PORT', '465')) | ||
MAIL_USE_SSL = os.getenv('MAIL_USE_SSL', True) | ||
|
||
# Disable email sending | ||
USER_SEND_PASSWORD_CHANGED_EMAIL=False | ||
USER_SEND_REGISTERED_EMAIL=False | ||
USER_SEND_USERNAME_CHANGED_EMAIL=False | ||
|
||
# Read config from ConfigClass defined above | ||
app.config.from_object(__name__+'.ConfigClass') | ||
|
||
|
@@ -97,6 +102,12 @@ class UserRoles(db.Model): | |
from flask_mongoalchemy import MongoAlchemy | ||
db = MongoAlchemy(app) | ||
|
||
|
||
class Role(db.Document): | ||
name = db.StringField() | ||
label = db.StringField(default='') | ||
|
||
|
||
# Define the User data model. | ||
# NB: Make sure to add flask_user UserMixin !!! | ||
class User(db.Document, UserMixin): | ||
|
@@ -114,18 +125,17 @@ def id(self): | |
# self._id = format(value, 'x') | ||
|
||
# User authentication information | ||
username = db.StringField(required=False) | ||
email = db.StringField(required=False) | ||
username = db.StringField(default='') | ||
email = db.StringField(default='') | ||
password = db.StringField() | ||
confirmed_at = db.DateTimeField(required=False) | ||
confirmed_at = db.DateTimeField(default=None) | ||
|
||
# User information | ||
first_name = db.StringField(required=False) | ||
last_name = db.StringField(required=False) | ||
|
||
|
||
|
||
first_name = db.StringField(default='') | ||
last_name = db.StringField(default='') | ||
|
||
# Relationships | ||
roles = db.ListField(db.DocumentField(Role), required=False, default=[]) | ||
|
||
|
||
# Define custom UserManager class | ||
|
@@ -155,12 +165,23 @@ def init_app(app, test_config=None): # For automated tests | |
if ORM_type == 'SQLAlchemy': | ||
db.create_all() | ||
|
||
if ORM_type == 'MongoAlchemy': | ||
# Drop existing table | ||
db.session.db.connection.drop_database(app.config.get('MONGOALCHEMY_DATABASE', '')) | ||
|
||
|
||
# Setup Flask-User | ||
if ORM_type == 'SQLAlchemy': | ||
user_manager = CustomUserManager(app, db, User, UserInvitationClass=UserInvitation) | ||
else: | ||
user_manager = CustomUserManager(app, db, User) | ||
|
||
# For debugging purposes | ||
# id = int('59a2258f9ebea4e67d20596f', 16) | ||
# encrypted_id = user_manager._encrypt_id(id) | ||
# decrypted_id = user_manager._decrypt_id(encrypted_id) | ||
# assert(decrypted_id==id) | ||
|
||
# Create regular 'member' user | ||
if not User.query.filter(User.username=='member').first(): | ||
user = User(username='member', email='[email protected]', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters