Skip to content

Test : ConnectAndGetToken Returns Error on Non-200 Response - #555

Merged
yada merged 1 commit into
microcks:masterfrom
aniket866:fix/Test-ConnectAndGetToken
Oct 27, 2026
Merged

yada merged 1 commit into
microcks:masterfrom
aniket866:fix/Test-ConnectAndGetToken

Conversation

@aniket866

Copy link
Copy Markdown
Contributor

Description

keycloak_client.go ConnectAndGetToken has no tests at all. The entire
Keycloak 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 KindAPI error with the HTTP status code
and 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) == KindAPI 
Loading

Ai usage Disclosure: Chatgpt is used to review files and it's missing tests
Closes #554

@dipak0000812 dipak0000812 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@lekaf974

lekaf974 commented Oct 7, 2026

Copy link
Copy Markdown

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

@yada yada added kind/task Project maintenance task component/connectors labels Oct 15, 2026
@yada

yada commented Oct 27, 2026

Copy link
Copy Markdown
Member

Verified head a543669 locally: builds, go vet and go test ./... pass. Mutation check: when I remove the non-200 handling in ConnectAndGetToken, the new subtests fail, so they do guard the KindAPI error path (status code and body).

Disclosure: this comment was drafted with AI assistance, and the results were run locally.

@yada yada left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally. Disclosure: AI-assisted verification.

@yada
yada merged commit 024989d into microcks:master Oct 27, 2026
7 checks passed

This branch was previously deployed

1 inactive deployment
Build — a5436690 Deployed Sep 28, 2026 by aniket866 via build-verify-package #745
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/connectors kind/task Project maintenance task

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TEST: ConnectAndGetToken Returns Error on Non-200 Response

4 participants