Skip to content

Commit

Permalink
fix tests part 3
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar committed May 31, 2024
1 parent b6a7225 commit e1f321a
Show file tree
Hide file tree
Showing 23 changed files with 1,449 additions and 1,429 deletions.
14 changes: 7 additions & 7 deletions examples/quickhowto/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ def __repr__(self):


class Contact(Model):
id = mapped_column(Integer, primary_key=True)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(150), unique=True, nullable=False)
address: Mapped[str] = mapped_column(String(564))
birthday: Mapped[Optional[datetime.date]] = mapped_column(Date, nullable=True)
personal_phone: Mapped[str] = mapped_column(String(20))
personal_celphone: Mapped[str] = mapped_column(String(20))
contact_group_id = mapped_column(
address: Mapped[Optional[str]] = mapped_column(String(564))
birthday: Mapped[Optional[datetime.date]] = mapped_column(Date)
personal_phone: Mapped[Optional[str]] = mapped_column(String(20))
personal_celphone: Mapped[Optional[str]] = mapped_column(String(20))
contact_group_id: Mapped[int] = mapped_column(
Integer, ForeignKey("contact_group.id"), nullable=False
)
contact_group: Mapped[ContactGroup] = relationship("ContactGroup")
gender_id = mapped_column(Integer, ForeignKey("gender.id"), nullable=False)
gender_id: Mapped[int] = mapped_column(Integer, ForeignKey("gender.id"), nullable=False)
gender: Mapped[Gender] = relationship("Gender")

def __repr__(self):
Expand Down
2 changes: 1 addition & 1 deletion examples/quickhowto/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def custom_password_validator(password: str) -> None:

# FAB_PASSWORD_COMPLEXITY_VALIDATOR = custom_password_validator

FAB_PASSWORD_COMPLEXITY_ENABLED = True
FAB_PASSWORD_COMPLEXITY_ENABLED = False

# ------------------------------
# GLOBALS FOR GENERAL APP's
Expand Down
2 changes: 1 addition & 1 deletion flask_appbuilder/baseviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ def _edit(self, pk):
self.post_update(item)
flash(self.edit_row_message, "success")
except Exception as e:
flash(str(e), "danger")
flash(self.database_error_message, "danger")
finally:
return None
else:
Expand Down
4 changes: 2 additions & 2 deletions flask_appbuilder/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
log = logging.getLogger(__name__)


class FieldConverter(object):
class FieldConverter:
"""
Helper class that converts model fields into WTForm fields
Expand Down Expand Up @@ -107,7 +107,7 @@ def convert(self):
log.error("Column %s Type not supported", self.colname)


class GeneralModelConverter(object):
class GeneralModelConverter:
"""
Returns a form from a model only one public exposed
method 'create_form'
Expand Down
8 changes: 4 additions & 4 deletions flask_appbuilder/models/sqla/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,23 @@ def apply_outer_select_joins(
continue

root_relation = get_column_root_relation(column)
leaf_column = get_column_leaf(column)

related_model = self.get_related_model(root_relation)
leaf_column = getattr(related_model, get_column_leaf(column))

if self.is_relation_many_to_many(
root_relation
) or self.is_relation_one_to_many(root_relation):
if outer_default_load:
query = query.options(
Load(self.obj)
.defaultload(self.obj, root_relation)
.load_only(getattr(related_model, leaf_column))
.load_only(leaf_column)
)
else:
query = query.options(
Load(self.obj)
.joinedload(getattr(self.obj, root_relation))
.load_only(getattr(related_model, leaf_column))
.load_only(leaf_column)
)
else:
query = query.options(Load(related_model).load_only(leaf_column))
Expand Down
6 changes: 3 additions & 3 deletions flask_appbuilder/security/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1608,9 +1608,9 @@ def has_access(self, permission_name: str, view_name: str) -> bool:
"""
Check if current user or public has access to view or menu
"""
if current_user.is_authenticated:
if current_user.is_authenticated and current_user.active:
return self._has_view_access(g.user, permission_name, view_name)
elif current_user_jwt:
elif current_user_jwt and current_user_jwt.active:
return self._has_view_access(current_user_jwt, permission_name, view_name)
else:
return self.is_item_public(permission_name, view_name)
Expand Down Expand Up @@ -2168,7 +2168,7 @@ def load_user(self, pk: int) -> Any | None:
def load_user_jwt(self, _jwt_header, jwt_data):
identity = jwt_data["sub"]
user = self.load_user(identity)
if user.is_active:
if user and user.is_active:
# Set flask g.user to JWT user, we can't do it on before request
g.user = user
return user
Expand Down
2 changes: 1 addition & 1 deletion flask_appbuilder/security/sqla/apis/role/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def add_role_permissions(self, role_id):
permissions.append(permission)

role.permissions = permissions
self.datamodel.edit(role, raise_exception=True)
self.datamodel.edit(role)
return self.response(
200,
**{
Expand Down
4 changes: 2 additions & 2 deletions flask_appbuilder/security/sqla/apis/user/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def post(self):
model.roles = roles

self.pre_add(model)
self.datamodel.add(model, raise_exception=True)
self.datamodel.add(model)
return self.response(201, id=model.id)
except ValidationError as error:
return self.response_400(message=error.messages)
Expand Down Expand Up @@ -205,7 +205,7 @@ def put(self, pk):
model.roles = roles

self.pre_update(model)
self.datamodel.edit(model, raise_exception=True)
self.datamodel.edit(model)
return self.response(
200,
**{API_RESULT_RES_KEY: self.edit_model_schema.dump(item, many=False)},
Expand Down
38 changes: 24 additions & 14 deletions flask_appbuilder/security/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
class Permission(Model):
__tablename__ = "ab_permission"

id = mapped_column(Integer, Sequence("ab_permission_id_seq"), primary_key=True)
id: Mapped[int] = mapped_column(
Integer, Sequence("ab_permission_id_seq"), primary_key=True
)
name: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)

def __repr__(self):
Expand All @@ -35,7 +37,9 @@ def __repr__(self):
class ViewMenu(Model):
__tablename__ = "ab_view_menu"

id = mapped_column(Integer, Sequence("ab_view_menu_id_seq"), primary_key=True)
id: Mapped[int] = mapped_column(
Integer, Sequence("ab_view_menu_id_seq"), primary_key=True
)
name: Mapped[str] = mapped_column(String(250), unique=True, nullable=False)

def __eq__(self, other):
Expand All @@ -60,7 +64,9 @@ def __repr__(self):
class Role(Model):
__tablename__ = "ab_role"

id = mapped_column(Integer, Sequence("ab_role_id_seq"), primary_key=True)
id: Mapped[int] = mapped_column(
Integer, Sequence("ab_role_id_seq"), primary_key=True
)
name: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
permissions: Mapped[List["PermissionView"]] = relationship(
"PermissionView",
Expand All @@ -75,10 +81,12 @@ def __repr__(self):
class PermissionView(Model):
__tablename__ = "ab_permission_view"
__table_args__ = (UniqueConstraint("permission_id", "view_menu_id"),)
id = mapped_column(Integer, Sequence("ab_permission_view_id_seq"), primary_key=True)
permission_id = mapped_column(Integer, ForeignKey("ab_permission.id"))
id: Mapped[int] = mapped_column(
Integer, Sequence("ab_permission_view_id_seq"), primary_key=True
)
permission_id: Mapped[int] = mapped_column(Integer, ForeignKey("ab_permission.id"))
permission: Mapped[Permission] = relationship("Permission", lazy="joined")
view_menu_id = mapped_column(Integer, ForeignKey("ab_view_menu.id"))
view_menu_id: Mapped[int] = mapped_column(Integer, ForeignKey("ab_view_menu.id"))
view_menu: Mapped[ViewMenu] = relationship("ViewMenu", lazy="joined")

def __repr__(self):
Expand All @@ -96,12 +104,14 @@ def __repr__(self):

class User(Model):
__tablename__ = "ab_user"
id = mapped_column(Integer, Sequence("ab_user_id_seq"), primary_key=True)
id: Mapped[int] = mapped_column(
Integer, Sequence("ab_user_id_seq"), primary_key=True
)
first_name: Mapped[str] = mapped_column(String(64), nullable=False)
last_name: Mapped[str] = mapped_column(String(64), nullable=False)
username: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
password: Mapped[str] = mapped_column(String(256))
active: Mapped[bool] = mapped_column(Boolean, default=True)
password: Mapped[Optional[str]] = mapped_column(String(256))
active: Mapped[Optional[bool]] = mapped_column(Boolean, default=True)
email: Mapped[str] = mapped_column(String(320), unique=True, nullable=False)
last_login: Mapped[Optional[datetime.datetime]] = mapped_column(
DateTime, nullable=True
Expand All @@ -111,10 +121,10 @@ class User(Model):
roles: Mapped[List[Role]] = relationship(
"Role", secondary=assoc_user_role, backref="user"
)
created_on: Mapped[datetime.datetime] = mapped_column(
created_on: Mapped[Optional[datetime.datetime]] = mapped_column(
DateTime, default=lambda: datetime.datetime.now(), nullable=True
)
changed_on: Mapped[datetime.datetime] = mapped_column(
changed_on: Mapped[Optional[datetime.datetime]] = mapped_column(
DateTime, default=lambda: datetime.datetime.now(), nullable=True
)

Expand Down Expand Up @@ -180,9 +190,9 @@ class RegisterUser(Model):
first_name: Mapped[str] = mapped_column(String(64), nullable=False)
last_name: Mapped[str] = mapped_column(String(64), nullable=False)
username: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
password: Mapped[str] = mapped_column(String(256))
password: Mapped[Optional[str]] = mapped_column(String(256))
email: Mapped[str] = mapped_column(String(320), unique=True, nullable=False)
registration_date: Mapped[datetime.datetime] = mapped_column(
registration_date: Mapped[Optional[datetime.datetime]] = mapped_column(
DateTime, default=lambda: datetime.datetime.now(), nullable=True
)
registration_hash: Mapped[str] = mapped_column(String(256))
registration_hash: Mapped[Optional[str]] = mapped_column(String(256))
7 changes: 4 additions & 3 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Dict, List, Optional, Set
import unittest

from flask import Flask, Response
from flask import Flask
from flask.testing import FlaskClient
from flask_appbuilder import AppBuilder
from flask_appbuilder.const import (
Expand All @@ -23,6 +23,7 @@
USERNAME_ADMIN,
USERNAME_READONLY,
)
from werkzeug.test import TestResponse


class FABTestCase(unittest.TestCase):
Expand Down Expand Up @@ -75,7 +76,7 @@ def browser_login(
password: str,
next_url: Optional[str] = None,
follow_redirects: bool = True,
) -> Response:
) -> TestResponse:
login_url = "/login/"
if next_url:
login_url = f"{login_url}?next={next_url}"
Expand Down Expand Up @@ -162,8 +163,8 @@ def tearDown(self):
from flask_appbuilder.extensions import db

db.drop_all()
self.appbuilder = None
self.ctx.pop()
self.appbuilder = None
self.ctx = None
self.app = None

Expand Down
8 changes: 4 additions & 4 deletions tests/config_security_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

basedir = os.path.abspath(os.path.dirname(__file__))

SQLALCHEMY_DATABASE_URI = (
os.environ.get("SQLALCHEMY_DATABASE_URI")
or "postgresql+psycopg2://pguser:[email protected]:5432/app"
)
SQLALCHEMY_DATABASE_URI = os.environ.get(
"SQLALCHEMY_DATABASE_URI"
) or "sqlite:///" + os.path.join(basedir, "app.db")


FAB_ADD_SECURITY_API = True
SECRET_KEY = "thisismyscretkey"
Expand Down
Loading

0 comments on commit e1f321a

Please sign in to comment.