diff --git a/server/szurubooru/api/tag_category_api.py b/server/szurubooru/api/tag_category_api.py index ccbe4e89..0b9eb660 100644 --- a/server/szurubooru/api/tag_category_api.py +++ b/server/szurubooru/api/tag_category_api.py @@ -57,11 +57,14 @@ class TagCategoryDetailApi(BaseApi): if not category: raise tag_categories.TagCategoryNotFoundError( 'Tag category %r not found.' % category_name) + auth.verify_privilege(ctx.user, 'tag_categories:delete') + if len(tag_categories.get_all_category_names()) == 1: + raise tag_categories.TagCategoryIsInUseError( + 'Cannot delete the default category.') if category.tag_count > 0: raise tag_categories.TagCategoryIsInUseError( 'Tag category has some usages and cannot be deleted. ' + 'Please remove this category from relevant tags first..') - auth.verify_privilege(ctx.user, 'tag_categories:delete') ctx.session.delete(category) ctx.session.commit() tags.export_to_json() diff --git a/server/szurubooru/tests/api/test_tag_category_deleting.py b/server/szurubooru/tests/api/test_tag_category_deleting.py index c298a557..7d7422ed 100644 --- a/server/szurubooru/tests/api/test_tag_category_deleting.py +++ b/server/szurubooru/tests/api/test_tag_category_deleting.py @@ -54,6 +54,15 @@ def test_trying_to_delete_used(test_ctx, tag_factory): 'category') assert db.session().query(db.TagCategory).count() == 1 +def test_trying_to_delete_last(test_ctx, tag_factory): + db.session().add(test_ctx.tag_category_factory(name='root')) + db.session().commit() + with pytest.raises(tag_categories.TagCategoryIsInUseError): + result = test_ctx.api.delete( + test_ctx.context_factory( + user=test_ctx.user_factory(rank='regular_user')), + 'root') + def test_trying_to_delete_non_existing(test_ctx): with pytest.raises(tag_categories.TagCategoryNotFoundError): test_ctx.api.delete(