forked from thatmattlove/hyperglass
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(webhook): handle raw dict input in mode='before' validator (#282) #3
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
Open
z23
wants to merge
1
commit into
main
Choose a base branch
from
fix/webhook-validator-dict-access
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """Regression tests for the Webhook model validator. | ||
|
|
||
| Tracks https://github.com/thatmattlove/hyperglass/issues/282 — Slack / MS Teams | ||
| / generic HTTP logging silently failed because the `mode="before"` validator | ||
| treated its raw-dict input as a model instance. | ||
| """ | ||
|
|
||
| # Standard Library | ||
| from datetime import datetime, timezone | ||
|
|
||
| # Project | ||
| from hyperglass.models.webhook import Webhook | ||
|
|
||
|
|
||
| def _query(source: str, network: dict | None = None) -> dict: | ||
| return { | ||
| "query_location": "test", | ||
| "query_type": "bgp_route", | ||
| "query_target": "192.0.2.0/24", | ||
| "headers": {}, | ||
| "source": source, | ||
| "network": network if network is not None else {"prefix": "192.0.2.0/24", "asn": "65000"}, | ||
| "timestamp": datetime.now(timezone.utc), | ||
| } | ||
|
|
||
|
|
||
| def test_webhook_constructs_with_public_source(): | ||
| """The validator must not raise on a normal public-IP source.""" | ||
| hook = Webhook(**_query(source="203.0.113.7")) | ||
| assert hook.source == "203.0.113.7" | ||
| assert hook.network.prefix == "192.0.2.0/24" | ||
| assert hook.network.asn == "65000" | ||
|
|
||
|
|
||
| def test_webhook_resets_network_for_localhost_v4(): | ||
| """A 127.0.0.1 source should clear the network info to defaults.""" | ||
| hook = Webhook(**_query(source="127.0.0.1")) | ||
| assert hook.source == "127.0.0.1" | ||
| # WebhookNetwork defaults all fields to "Unknown" when given an empty dict. | ||
| assert hook.network.prefix == "Unknown" | ||
| assert hook.network.asn == "Unknown" | ||
|
|
||
|
|
||
| def test_webhook_resets_network_for_localhost_v6(): | ||
| """`::1` should also be treated as localhost.""" | ||
| hook = Webhook(**_query(source="::1")) | ||
| assert hook.network.prefix == "Unknown" | ||
| assert hook.network.asn == "Unknown" | ||
|
|
||
|
|
||
| def test_webhook_slack_payload_renders(): | ||
| """End-to-end: the Slack payload must build without raising.""" | ||
| hook = Webhook(**_query(source="203.0.113.7")) | ||
| payload = hook.slack() | ||
| assert payload["text"] | ||
| assert any("203.0.113.7" in str(block) for block in payload["blocks"]) | ||
|
|
||
|
|
||
| def test_webhook_msteams_payload_renders(): | ||
| """End-to-end: the MS Teams payload must build without raising.""" | ||
| hook = Webhook(**_query(source="203.0.113.7")) | ||
| payload = hook.msteams() | ||
| assert payload["@type"] == "MessageCard" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@model_validatormethods elsewhere in this codebase are defined as plain methods (no@classmethod). Adding@classmethodhere is inconsistent and can be brittle depending on how Pydantic wraps validators; consider dropping@classmethodand keeping the same(cls, data)signature so it matches the established pattern (e.g.,DnsOverHttps.validate_dns,FRRPath.validate_path).