Skip to content

Commit c671a66

Browse files
committed
Add additional test coverage for helper functions in Util
Currently helper functions in Util lacks test coverage This commit adds test coverage for RevokeToken function. Signed-off-by: Kugamoorthy Gajananan <[email protected]>
1 parent 00dd4ba commit c671a66

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

internal/util/helpers_test.go

+71-2
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,78 @@ func TestLoadCredentials(t *testing.T) {
420420
}
421421

422422
// TestRevokeToken tests the RevokeToken function
423-
// func TestRevokeToken(t *testing.T) {
423+
func TestRevokeToken(t *testing.T) {
424+
t.Parallel()
425+
tests := []struct {
426+
name string
427+
token string
428+
issuerUrl string
429+
clientId string
430+
tokenHint string
431+
expectedPath string
432+
expectError bool
433+
}{
434+
{
435+
name: "Valid token revocation",
436+
token: "test-token",
437+
issuerUrl: "http://localhost:8081",
438+
clientId: "minder-cli",
439+
tokenHint: "refresh_token",
440+
expectedPath: "/realms/stacklok/protocol/openid-connect/revoke",
441+
expectError: false,
442+
},
443+
{
444+
name: "Invalid issuer URL",
445+
token: "test-token",
446+
issuerUrl: "://invalid-url",
447+
clientId: "minder-cli",
448+
tokenHint: "refresh_token",
449+
expectedPath: "",
450+
expectError: true,
451+
},
452+
}
424453

425-
// }
454+
for _, tt := range tests {
455+
t.Run(tt.name, func(t *testing.T) {
456+
t.Parallel()
457+
var server *httptest.Server
458+
if tt.name != "Invalid issuer URL" {
459+
server = httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
460+
if r.URL.Path != tt.expectedPath {
461+
t.Errorf("expected path %s, got %s", tt.expectedPath, r.URL.Path)
462+
}
463+
464+
if err := r.ParseForm(); err != nil {
465+
t.Fatalf("error parsing form: %v", err)
466+
}
467+
468+
if r.Form.Get("client_id") != tt.clientId {
469+
t.Errorf("expected client_id %s, got %s", tt.clientId, r.Form.Get("client_id"))
470+
}
471+
472+
if r.Form.Get("token") != tt.token {
473+
t.Errorf("expected token %s, got %s", tt.token, r.Form.Get("token"))
474+
}
475+
476+
if r.Form.Get("token_type_hint") != tt.tokenHint {
477+
t.Errorf("expected token_type_hint %s, got %s", tt.tokenHint, r.Form.Get("token_type_hint"))
478+
}
479+
}))
480+
defer server.Close()
481+
}
482+
483+
issuerUrl := tt.issuerUrl
484+
if tt.name != "Invalid issuer URL" {
485+
issuerUrl = server.URL
486+
}
487+
488+
err := RevokeToken(tt.token, issuerUrl, tt.clientId, tt.tokenHint)
489+
if (err != nil) != tt.expectError {
490+
t.Errorf("RevokeToken() error = %v, expectError %v", err, tt.expectError)
491+
}
492+
})
493+
}
494+
}
426495

427496
// TestGetJsonFromProto tests the GetJsonFromProto function
428497
func TestGetJsonFromProto(t *testing.T) {

0 commit comments

Comments
 (0)