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

Raise 422 SchemaNotFoundError while uploading a schema referring an unexisting schemanode #4329

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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: 6 additions & 3 deletions backend/infrahub/core/schema_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,9 +903,12 @@ def validate_menu_placements(self) -> None:
if node.menu_placement:
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

except SchemaNotFoundError as exc:
raise SchemaNotFoundError(
branch_name=self.name,
identifier=node.menu_placement,
message=f"{node.kind} refers to an invalid menu placement node: {node.menu_placement}.",
) from exc
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,30 @@
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}],
}
],
}

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 to an invalid 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 to an invalid menu placement node: NoSuchObject."


async def test_schema_branch_validate_same_node_menu_placement():
Expand Down
Loading