Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another authlib migration related changes #1858

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ script_location = anitya/db/migrations
# sourceless = false

#sqlalchemy.url = driver://user:pass@localhost/dbname
sqlalchemy.url = sqlite:////var/tmp/anitya-dev.sqlite
sqlalchemy.url = postgresql://postgres:anypasswordworkslocally@postgres/anitya


# Logging configuration
Expand Down
4 changes: 4 additions & 0 deletions anitya/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def auth(name): # pragma: no cover
Session.add(new_user)
Session.commit()
user = new_user
elif user.username != user_info["username"]:
user.username = user_info["username"]
Session.add(user)
Session.commit()
_log.debug("Logging as user %s", user.email)
flask_login.login_user(user)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Remove unique attribute from username

Revision ID: ebc827e80373
Revises: 8ba7d4c42044
Create Date: 2024-12-05 16:24:01.098473
"""

from alembic import op

# revision identifiers, used by Alembic.
revision = "ebc827e80373"
down_revision = "8ba7d4c42044"


def upgrade():
"""Alembic migration."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint("ix_users_username", "users", type_="unique")
# ### end Alembic commands ###


def downgrade():
"""Downgrade migration."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_unique_constraint("ix_users_username", "users", ["username"])
# ### end Alembic commands ###
4 changes: 3 additions & 1 deletion anitya/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ class User(Base):
# SMTP says 256 is the maximum length of a path:
# https://tools.ietf.org/html/rfc5321#section-4.5.3
email = sa.Column(sa.String(256), nullable=False, index=True, unique=True)
username = sa.Column(sa.String(256), nullable=False, index=True, unique=True)
username = sa.Column(sa.String(256), nullable=False, index=True)
active = sa.Column(sa.Boolean, default=True)
admin = sa.Column(sa.Boolean, default=False)

Expand All @@ -1001,6 +1001,8 @@ def is_admin(self):
if not self.admin:
if six.text_type(self.id) in anitya_config.get("ANITYA_WEB_ADMINS", []):
self.admin = True
if six.text_type(self.email) in anitya_config.get("ANITYA_WEB_ADMINS", []):
self.admin = True
return self.admin

@property
Expand Down
26 changes: 15 additions & 11 deletions anitya/tests/db/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,17 +1083,6 @@ def test_user_email_unique(self):
self.session.add(user)
self.assertRaises(IntegrityError, self.session.commit)

def test_username_unique(self):
"""Assert User usernames have a uniqueness constraint on them."""
user = models.User(email="[email protected]", username="user")

self.session.add(user)
self.session.commit()

user = models.User(email="[email protected]", username="user")
self.session.add(user)
self.assertRaises(IntegrityError, self.session.commit)

def test_default_active(self):
"""Assert User usernames have a uniqueness constraint on them."""
user = models.User(email="[email protected]", username="user")
Expand Down Expand Up @@ -1139,6 +1128,21 @@ def test_is_admin_configured(self):
self.assertTrue(user.is_admin)
self.assertTrue(user.admin)

def test_is_admin_configured_email(self):
"""Assert default value for admin flag."""
user = models.User(email="[email protected]", username="user", admin=False)
self.session.add(user)
self.session.commit()

mock_dict = mock.patch.dict(
"anitya.config.config", {"ANITYA_WEB_ADMINS": [six.text_type(user.email)]}
)

with mock_dict:
self.assertFalse(user.admin)
self.assertTrue(user.is_admin)
self.assertTrue(user.admin)

def test_to_dict(self):
"""Assert the correct dictionary is returned."""
user = models.User(email="[email protected]", username="user")
Expand Down
Loading