Skip to content

Commit

Permalink
Add uniqueness constraint and tests for DomainCategory model and CLI
Browse files Browse the repository at this point in the history
* Added `unique=True` constraint to the `label` field in `DomainCategory` model to enforce uniqueness at the database level.
* Modified `domains_create` CLI command to check for duplicates before creating a new domain category, providing feedback if the domain already exists.
* Added tests for `DomainCategory` model to validate creation, retrieval, and handling of duplicate labels.
* Enhanced `test_cli_createdomain`
* closes inveniosoftware/invenio-app-rdm#2796
  • Loading branch information
Samk13 committed Aug 21, 2024
1 parent 2db2286 commit 2b58d97
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
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"

0 comments on commit 2b58d97

Please sign in to comment.