Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/connectors/microcks_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ func (c *microcksClient) GetKeycloakURL() (string, error) {

// Retrieve auth server url and realm name.
enabled := configResp["enabled"].(bool)
authServerURL := configResp["auth-server-url"].(string)
realmName := configResp["realm"].(string)

// Return a proper URL or 'null' if Keycloak is disables.
if enabled {
authServerURL := configResp["auth-server-url"].(string)
realmName := configResp["realm"].(string)
return authServerURL + "/realms/" + realmName + "/", nil
}
return "null", nil
Expand Down
62 changes: 62 additions & 0 deletions pkg/connectors/microcks_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package connectors

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestGetKeycloakURL_Disabled(t *testing.T) {
// Create a mock server that returns keycloak disabled response
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/keycloak/config" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"enabled": false}`))
return
}
w.WriteHeader(http.StatusNotFound)
}))
defer ts.Close()

// Initialize the MicrocksClient with the mock server URL
client := NewMicrocksClient(ts.URL)

// Call GetKeycloakURL and check for panic/error
url, err := client.GetKeycloakURL()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

if url != "null" {
t.Errorf("Expected 'null', got '%s'", url)
}
}

func TestGetKeycloakURL_Enabled(t *testing.T) {
// Create a mock server that returns keycloak enabled response
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/keycloak/config" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"enabled": true, "auth-server-url": "http://keycloak:8080/auth", "realm": "microcks"}`))
return
}
w.WriteHeader(http.StatusNotFound)
}))
defer ts.Close()

// Initialize the MicrocksClient with the mock server URL
client := NewMicrocksClient(ts.URL)

// Call GetKeycloakURL and check for panic/error
url, err := client.GetKeycloakURL()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

expected := "http://keycloak:8080/auth/realms/microcks/"
if url != expected {
t.Errorf("Expected '%s', got '%s'", expected, url)
}
}
Loading