Repository navigation
Test : ConnectAndGetToken Returns Error on Non-200 Response - #555
Conversation
Signed-off-by: aniket866 <[email protected]>
dipak0000812
left a comment
There was a problem hiding this comment.
hey @aniket866 Can we also assert that the response body is included in the returned error?
The test cases already provide different response bodies, but we're only checking the status code here. Since ConnectAndGetToken is expected to return the Keycloak response details in the KindAPI error, checking tc.body as well would make sure we don't lose that information in a future change.
+1 |
|
Verified head Disclosure: this comment was drafted with AI assistance, and the results were run locally. |
yada
left a comment
There was a problem hiding this comment.
Verified locally. Disclosure: AI-assisted verification.
Description
keycloak_client.go ConnectAndGetTokenhas no tests at all. The entireKeycloak client package is untested. The most critical path to test is when
Keycloak returns a non-200 status (wrong credentials, realm not found, server
error) — the function should return a
KindAPIerror with the HTTP status codeand body. Currently nothing verifies this.
Why Necessary
Wrong credentials is the single most common failure a new user encounters. If
this error path regresses — e.g. the function panics, returns a nil error, or
swallows the body — users get no feedback and assume the server is down.
What If Not Present
A change to the response-parsing code (e.g. switching JSON library, restructuring
the token response) could silently break authentication for all users and no test
would catch it before release.
Diagram
sequenceDiagram participant T as Test participant KC as keycloakClient participant TS as httptest.Server T->>TS: register handler → always return 401 {"error":"unauthorized"} T->>KC: NewKeycloakClient(server.URL, "id", "secret") T->>KC: ConnectAndGetToken() KC->>TS: POST /protocol/openid-connect/token TS-->>KC: 401 {"error":"unauthorized"} KC-->>T: error (KindAPI) T->>T: assert error != nil T->>T: assert error contains "401" T->>T: assert KindOf(err) == KindAPIAi usage Disclosure: Chatgpt is used to review files and it's missing tests
Closes #554