-
Notifications
You must be signed in to change notification settings - Fork 1
Add risk_intelligence and event_id to fixtures
#5
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
114 changes: 114 additions & 0 deletions
114
friendly-captcha-sdk-testserver/fixtures/fixture_test.go
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,114 @@ | ||
| package fixtures | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/guregu/null/v6" | ||
| ) | ||
|
|
||
| type SiteverifyResponse struct { | ||
| Success bool `json:"success"` | ||
| Data SiteverifyResponseSuccessData `json:"data,omitempty"` | ||
| } | ||
|
|
||
| type SiteverifyResponseSuccessData struct { | ||
| EventID string `json:"event_id"` | ||
|
|
||
| Challenge SiteverifyResponseSuccessDataChallenge `json:"challenge"` | ||
|
|
||
| RiskIntelligence null.Value[RiskIntelligenceData] `json:"risk_intelligence"` | ||
| } | ||
|
|
||
| type SiteverifyResponseSuccessDataChallenge struct { | ||
| Timestamp string `json:"timestamp"` | ||
| Origin string `json:"origin"` | ||
| } | ||
|
|
||
| type RiskIntelligenceData struct { | ||
| // Note: this only contains a subset of the actual API response values. | ||
|
|
||
| RiskScores RiskIntelligenceDataRiskScores `json:"risk_scores"` | ||
|
|
||
| Client RiskIntelligenceDataClient `json:"client"` | ||
| } | ||
|
|
||
| type RiskIntelligenceDataRiskScores struct { | ||
| Overall uint8 `json:"overall"` | ||
| Network uint8 `json:"network"` | ||
| Browser uint8 `json:"browser"` | ||
| } | ||
|
|
||
| type RiskIntelligenceDataClient struct { | ||
| // Note: this only contains a subset of the actual API response values. | ||
|
|
||
| HeaderUserAgent string `json:"header_user_agent"` | ||
| Browser null.Value[RiskIntelligenceDataClientBrowser] `json:"browser"` | ||
| } | ||
|
|
||
| type RiskIntelligenceDataClientBrowser struct { | ||
| // Note: this only contains a subset of the actual API response values. | ||
|
|
||
| ID string `json:"id"` | ||
| } | ||
|
|
||
| func TestFixtures(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testCases, err := Load("") | ||
| if err != nil { | ||
| t.Fatalf("Failed to load embedded test cases: %v", err) | ||
| } | ||
|
|
||
| // For each fixture make sure it has a name, response and expectation. | ||
| for i, tc := range testCases.Tests { | ||
| if tc.Name == "" { | ||
| t.Errorf("Test case %d has an empty name", i) | ||
| } | ||
| if tc.Response == "" { | ||
| t.Errorf("Test case %d (%s) has an empty response", i, tc.Name) | ||
| } | ||
|
|
||
| // Parse as SiteverifyResponse to ensure it's valid JSON. | ||
| var svr SiteverifyResponse | ||
| err := json.Unmarshal(tc.SiteverifyResponse, &svr) | ||
| if err != nil { | ||
| // Some responses are completely invalid JSON (e.g., HTML error pages). | ||
| // We only validate the ones that are supposed to be valid JSON. | ||
|
|
||
| // We hardcode those that we expect to fail to parse: | ||
| invalidJSONCases := map[string]bool{ | ||
| "bad_response_200": true, | ||
| "bad_response_200_strict": true, | ||
| "bad_response_500": true, | ||
| "bad_response_400_strict": true, | ||
| "empty_string_response_200": true, | ||
| "empty_string_response_200_strict": true, | ||
| } | ||
| if !invalidJSONCases[tc.Name] { | ||
| t.Errorf("Test case %d (%s) has invalid siteverify_response JSON: %v", i, tc.Name, err) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if !svr.Success { // For non-success cases we don't validate further. | ||
| continue | ||
| } | ||
|
|
||
| ri := svr.Data.RiskIntelligence | ||
|
|
||
| // If risk intelligence data is present, ensure it has expected fields. | ||
| if ri.Valid { | ||
| if ri.V.Client.HeaderUserAgent == "" { | ||
| t.Errorf("Test case %d (%s) has risk intelligence data with empty client header_user_agent", i, tc.Name) | ||
| } | ||
|
|
||
| if ri.V.Client.Browser.Valid { | ||
| if ri.V.Client.Browser.V.ID == "" { | ||
| t.Errorf("Test case %d (%s) has risk intelligence data with empty browser id", i, tc.Name) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } |
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
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 |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| module github.com/friendlycaptcha/friendly-captcha-sdk-tooling/friendly-captcha-sdk-testserver | ||
|
|
||
| go 1.21 | ||
| go 1.22.12 | ||
|
|
||
| toolchain go1.24.3 | ||
|
|
||
| require github.com/alecthomas/kong v0.8.0 | ||
|
|
||
| require github.com/guregu/null/v6 v6.0.0 // indirect |
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
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
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
Oops, something went wrong.
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.
should there be an
event_idas part of this expectation? and the one above it? maybe it's supposed to be like this but i noticed all the ones you added above have one