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

Domains: Add uniqueness constraint and tests for DomainCategory model and CLI #489

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion invenio_accounts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This file is part of Invenio.
# Copyright (C) 2015-2023 CERN.
# Copyright (C) 2024 Graz University of Technology.
# Copyright (C) 2024 KTH Royal Institute of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -170,8 +171,14 @@ def users_deactivate(user):
@with_appcontext
def domains_create(domain):
"""Create domain."""
domain = domain.lower()

if DomainCategory.get(domain):
click.secho(f"Domain {domain} already exists.", fg="red")
return

try:
domain_category = DomainCategory.create(domain.lower())
domain_category = DomainCategory.create(domain)
db.session.merge(domain_category)
db.session.commit()
except Exception as error:
Expand Down
4 changes: 2 additions & 2 deletions invenio_accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2024 CERN.
# Copyright (C) 2022 KTH Royal Institute of Technology
# Copyright (C) 2022-2024 KTH Royal Institute of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -526,7 +526,7 @@ class DomainCategory(db.Model):

id = db.Column(db.Integer(), primary_key=True, autoincrement=True)

label = db.Column(db.String(255))
label = db.Column(db.String(255), unique=True)

@classmethod
def create(cls, label):
Expand Down
27 changes: 27 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2018 CERN.
# Copyright (C) 2024 KTH Royal Institute of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -10,6 +11,7 @@
"""Module tests."""

from invenio_accounts.cli import (
domains_create,
roles_add,
roles_create,
roles_remove,
Expand Down Expand Up @@ -156,3 +158,28 @@ def test_cli_activate_deactivate(app):
assert result.exit_code == 0
result = runner.invoke(users_deactivate, ["[email protected]"])
assert result.exit_code == 0


def test_cli_createdomain(app):
"""Test create domain CLI."""
runner = app.test_cli_runner()

# Create a domain successfully
result = runner.invoke(domains_create, ["mailprovider"])
assert result.exit_code == 0
assert "Domain mailprovider created successfully" in result.output

# Reject Creating the same domain again
result = runner.invoke(domains_create, ["mailprovider"])
assert result.exit_code == 0
assert "Domain mailprovider already exists." in result.output

# Create another domain successfully
result = runner.invoke(domains_create, ["kth.se"])
assert result.exit_code == 0
assert "Domain kth.se created successfully" in result.output

# Create a domain with a fancy case should be treated like others
result = runner.invoke(domains_create, ["MailPrOvIdEr"])
assert result.exit_code == 0
assert "Domain mailprovider already exists." in result.output
19 changes: 18 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This file is part of Invenio.
# Copyright (C) 2015-2024 CERN.
# Copyright (C) 2022 TU Wien.
# Copyright (C) 2022 KTH Royal Institute of Technology
# Copyright (C) 2022-2024 KTH Royal Institute of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -14,6 +14,7 @@
from invenio_db import db
from marshmallow import Schema, fields
from sqlalchemy import inspect
from sqlalchemy.exc import IntegrityError

from invenio_accounts import testutils
from invenio_accounts.models import (
Expand Down Expand Up @@ -213,9 +214,25 @@ def test_domain_org(app):


def test_domain_category(app):
"""Test DomainCategory creation and retrieval."""
c1 = DomainCategory.create("spammer")
c2 = DomainCategory.create("organisation")
db.session.commit()

c = DomainCategory.get("spammer")
assert c.label == "spammer"

# Try to create a duplicate category
with pytest.raises(IntegrityError):
duplicate_category = DomainCategory.create("spammer")
db.session.commit()
# Clean the state after the IntegrityError
db.session.rollback()

# Assert a valid category can still be created after the error
c3 = DomainCategory.create("company")
db.session.commit()

# Make sure the new category is correctly stored
c = DomainCategory.get("company")
assert c.label == "company"