Added invalid parameter tests - #108
Conversation
Signed-off-by: HardingEthan15 <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR adds integration tests that validate the OpenCost allocation and asset APIs return HTTP 400 errors (rather than 500 errors or successful responses) when invalid query parameters are provided. It also updates pkg/api/api.go to properly short-circuit HTTP error responses (4xx/5xx) before attempting JSON deserialization.
Changes:
- Added
decodeJSONResponselogic inapi.goto detect HTTP 4xx/5xx status codes and return a formatted error immediately, avoiding JSON unmarshal of error bodies. - Added new
invalid_query_parameter_test.gotest files for both asset and allocation APIs, covering cases like reversed windows, invalid formats, and invalid filters/aggregates. - Updated both
test.batsfiles to include the new test invocations.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
pkg/api/api.go |
Adds early-return error handling for HTTP 4xx/5xx responses in decodeJSONResponse |
test/integration/api/allocation/invalid_query_parameter_test.go |
New test validating allocation API returns 400 for invalid parameters |
test/integration/api/asset/invalid_query_parameter_test.go |
New test validating asset API returns 400 for invalid parameters |
test/integration/api/allocation/test.bats |
Registers the new allocation invalid parameter test |
test/integration/api/asset/test.bats |
Registers the new asset invalid parameter test |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: HardingEthan15 <[email protected]>
Signed-off-by: HardingEthan15 <[email protected]>
| filter string | ||
| aggregate string | ||
| accumulate string | ||
| includeidle string |
Signed-off-by: HardingEthan15 <[email protected]>
| } | ||
|
|
||
| // Assert that it is a 400 error. | ||
| if !strings.Contains(err.Error(), "HTTP 400") { |
There was a problem hiding this comment.
I don't think the tests work. You are checking for 400 errors but the API returns 502
|
@ameijer, I think we have a found a bug here. The step in opencost - https://github.com/opencost/opencost/blob/e7e93745dfb3b9589efe5cef3cb03f381d83058b/pkg/costmodel/aggregation.go#L399-L404 that parses the string expects "bad request" but References |
DESCRIPTION
Adds invalid parameter tests for the asset and allocation api. This test validates that the apis handle invalid parameters correctly.
CHANGES
Adds a new invalid_query_parameter_test.go in the allocation and asset folder.
Adds to the test.bats file to run the tests.
MOTIVATION
Invalid Parameters should not return 500 errors or panics. Furthermore, they should not return successful responses, since the responses would then have to guess what is meant by the parameters.
TESTING
This test can be run from the new sanity test directory using the included Bats test file.
Signed-off-by: HardingEthan15 [email protected]