forked from mintel/dex-k8s-authenticator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tenancy_test.go
158 lines (137 loc) · 3.75 KB
/
tenancy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/mesosphere/dex-k8s-authenticator/pkg/tenancy"
)
func TestGetTenancyTemplate(t *testing.T) {
assert.NotNil(t, getTenancyTemplate("index-multitenant.html"))
assert.NotNil(t, getTenancyTemplate("landing-multitenant.html"))
}
func TestTenancyHandler(t *testing.T) {
m := &tenantsMock{
existsCall: struct {
exists bool
err error
}{
exists: true,
},
filterClusterNamesCall: struct {
names []string
err error
}{
names: []string{"cluster-2"},
},
}
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.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 {
exists bool
err error
}{
exists: true,
},
getCall: struct {
tenant *tenancy.Tenant
err error
}{
tenant: &tenancy.Tenant{
Name: "Test Tenant name",
ID: tenancy.TenantId("test-tenant"),
},
},
}
rr := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/test-tenant", nil)
c := &Config{
Web_Path_Prefix: "/",
}
h := NewLandingHandler(m, c, getTenancyTemplate("landing-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.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(), "Test Tenant name")
}
var _ tenancy.Tenants = &tenantsMock{}
type tenantsMock struct {
existsCall struct {
exists bool
err error
}
filterClusterNamesCall struct {
names []string
err error
}
getCall struct {
tenant *tenancy.Tenant
err error
}
}
func (t *tenantsMock) Exists(ctx context.Context, tenantId tenancy.TenantId) (bool, error) {
return t.existsCall.exists, t.existsCall.err
}
func (t *tenantsMock) Get(ctx context.Context, tenantId tenancy.TenantId) (*tenancy.Tenant, error) {
return t.getCall.tenant, t.getCall.err
}
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")
}