Skip to content

Commit

Permalink
feat: add migration script to make User.active not-nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterlukasse committed Nov 12, 2024
1 parent 3ecd0f4 commit 27c5d74
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
5 changes: 2 additions & 3 deletions fence/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ def set_flask_session_values(user):

user = query_for_user(session=current_app.scoped_session(), username=username)
if user:
if user.active is False:
# Abort login if user.active is False (user.active is None or True are both
# considered active in this case):
if user.active == False:
# Abort login if user.active == False:
raise Unauthorized(
"User is known but not authorized/activated in the system"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Make User.active field non nullable
Revision ID: 3a5712474808
Revises: 9b3a5a7145d7
Create Date: 2024-11-08 22:00:41.161934
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "3a5712474808" # pragma: allowlist secret
down_revision = "9b3a5a7145d7" # pragma: allowlist secret
branch_labels = None
depends_on = None


def upgrade():
op.execute('UPDATE "User" SET active = True WHERE active IS NULL')
op.alter_column("User", "active", nullable=False, server_default="True")


def downgrade():
op.alter_column("User", "active", nullable=True, server_default=None)
op.execute('UPDATE "User" SET active = NULL WHERE active = True')
2 changes: 1 addition & 1 deletion tests/admin/test_admin_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_soft_delete_user(db_session, awg_users):
user = db_session.query(User).filter(User.username == username).first()
assert user != None
assert user.username == username
assert user.active is None
assert user.active == True
adm.soft_delete_user(db_session, username)
user = db_session.query(User).filter(User.username == username).first()
assert user != None
Expand Down
2 changes: 1 addition & 1 deletion tests/admin/test_admin_users_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ def test_soft_delete_user_username(
username = "test_user_d"
user = db_session.query(User).filter_by(username=username).one()
assert user.username == username
assert user.active is None
assert user.active == True
# now soft-delete and assert "active" changed to False:
r = client.delete(
f"/admin/users/{username}/soft",
Expand Down

0 comments on commit 27c5d74

Please sign in to comment.