From 6415238f6913128a7e1b7ca9e6d1c5e0a26f3525 Mon Sep 17 00:00:00 2001 From: Martin Hrabovcin Date: Thu, 18 Jan 2024 13:49:42 +0100 Subject: [PATCH] fix: show no clusters message on workspace handler --- templates/index-multitenant.html | 6 +++++ tenancy_test.go | 42 +++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/templates/index-multitenant.html b/templates/index-multitenant.html index 7a37eed..57c05f7 100644 --- a/templates/index-multitenant.html +++ b/templates/index-multitenant.html @@ -27,9 +27,15 @@

Generate Kubernetes Token

+ {{ if .Clusters }}

Select which cluster you require a token for:

+ {{ else }} +

+ There are no clusters attached to the workspace. +

+ {{ end }} {{ range $cluster := .Clusters }} diff --git a/tenancy_test.go b/tenancy_test.go index 62e73c9..7d0084a 100644 --- a/tenancy_test.go +++ b/tenancy_test.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "net/http" "net/http/httptest" "testing" @@ -51,6 +52,41 @@ func TestTenancyHandler(t *testing.T) { assert.Contains(t, rr.Body.String(), "/login/cluster-2?tenant-id=test-tenant") } +func TestTenancyHandler_NoClusters(t *testing.T) { + m := &tenantsMock{ + existsCall: struct { + exists bool + err error + }{ + exists: true, + }, + filterClusterNamesCall: struct { + names []string + err error + }{ + names: []string{}, + }, + } + rr := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/test-tenant", nil) + c := &Config{ + Clusters: []Cluster{ + {Name: "cluster-1"}, + {Name: "cluster-2"}, + }, + Web_Path_Prefix: "/", + } + + h := NewTenancyHandler(m, c, getTenancyTemplate("index-multitenant.html")) + r := mux.NewRouter() + r.HandleFunc("/{tenantId}", h) + r.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusOK, rr.Code, "respbody: %s", rr.Body.String()) + assert.NotContains(t, rr.Body.String(), "Select which cluster you require a token for:") + assert.Contains(t, rr.Body.String(), "There are no clusters attached to the workspace.") +} + func TestLandingHandler(t *testing.T) { m := &tenantsMock{ existsCall: struct { @@ -82,7 +118,7 @@ func TestLandingHandler(t *testing.T) { assert.Equal(t, http.StatusOK, rr.Code, "respbody: %s", rr.Body.String()) assert.Contains(t, rr.Body.String(), "/workspace/test-tenant") - assert.Contains(t, rr.Body.String(), "/dkp/kommander/dashboard?tenant-id=test-tenant") + assert.Contains(t, rr.Body.String(), "/dkp/kommander/dashboard/?tenant-id=test-tenant") assert.Contains(t, rr.Body.String(), "Test Tenant name") } @@ -116,3 +152,7 @@ func (t *tenantsMock) Get(ctx context.Context, tenantId tenancy.TenantId) (*tena func (t *tenantsMock) FilterClusterNames(ctx context.Context, tenantId tenancy.TenantId, names []string) ([]string, error) { return t.filterClusterNamesCall.names, t.filterClusterNamesCall.err } + +func (t *tenantsMock) GetTenantsByCluster(ctx context.Context) (map[string]*tenancy.Tenant, error) { + return nil, fmt.Errorf("not implemented") +}