Skip to content

Commit

Permalink
Raise a proper 422 error when uploading a schemanode refering an unex…
Browse files Browse the repository at this point in the history
…isting schemanode
  • Loading branch information
LucasG0 committed Sep 11, 2024
1 parent 70c08aa commit 6f31bd6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
7 changes: 5 additions & 2 deletions backend/infrahub/core/schema_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,11 @@ def validate_menu_placements(self) -> None:
try:
placement_node = self.get(name=node.menu_placement, duplicate=False)
except SchemaNotFoundError:
raise ValueError(f"{node.kind}: {node.menu_placement} is not a valid menu placement") from None

raise SchemaNotFoundError(
branch_name=self.name,
identifier=node.menu_placement,
message=f"{node.kind} refers an unexisting menu placement node: {node.menu_placement}.",
)
if node == placement_node:
raise ValueError(f"{node.kind}: cannot be placed under itself in the menu") from None

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from infrahub_sdk import InfrahubClient

from .shared import (
TestSchemaLifecycleBase,
)


class TestSchemaMissingMenuPlacement(TestSchemaLifecycleBase):
async def test_schema_missing_menu_placement(self, client: InfrahubClient):
schema = {
"version": "1.0",
"nodes": [
{
"name": "BNode",
"namespace": "Infra",
"menu_placement": "UnexistingNode",
"label": "BNode",
"display_labels": ["name__value"],
"attributes": [{"name": "name", "kind": "Text", "unique": True}],
}
],
}

# Load the new schema and apply the migrations
response = await client.schema.load(schemas=[schema], branch="main")
assert response.schema_updated is False
assert response.errors["errors"][0]["extensions"]["code"] == 422
assert (
response.errors["errors"][0]["message"]
== "InfraBNode refers an unexisting menu placement node: UnexistingNode."
)
4 changes: 2 additions & 2 deletions backend/tests/unit/core/schema_manager/test_manager_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,10 @@ async def test_schema_branch_validate_menu_placement():
schema = SchemaBranch(cache={})
schema.load_schema(schema=SchemaRoot(**FULL_SCHEMA))

with pytest.raises(ValueError) as exc:
with pytest.raises(SchemaNotFoundError) as exc:
schema.validate_menu_placements()

assert str(exc.value) == "TestSubObject: NoSuchObject is not a valid menu placement"
assert exc.value.message == "TestSubObject refers an unexisting menu placement node: NoSuchObject."


async def test_schema_branch_validate_same_node_menu_placement():
Expand Down

0 comments on commit 6f31bd6

Please sign in to comment.