forked from kubeflow/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamespaces_handler_test.go
More file actions
201 lines (172 loc) · 6.88 KB
/
namespaces_handler_test.go
File metadata and controls
201 lines (172 loc) · 6.88 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package api
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"github.com/kubeflow/model-registry/ui/bff/internal/config"
"github.com/kubeflow/model-registry/ui/bff/internal/constants"
"github.com/kubeflow/model-registry/ui/bff/internal/integrations/kubernetes"
"github.com/kubeflow/model-registry/ui/bff/internal/integrations/kubernetes/k8mocks"
"github.com/kubeflow/model-registry/ui/bff/internal/models"
"github.com/kubeflow/model-registry/ui/bff/internal/repositories"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("TestNamespacesHandler", func() {
Context("when running in dev mode with k8 service account client", Ordered, func() {
var testApp App
BeforeAll(func() {
By("setting up the test app in dev mode")
testApp = App{
config: config.EnvConfig{DevMode: true},
kubernetesClientFactory: kubernetesMockedStaticClientFactory,
repositories: repositories.NewRepositories(mockMRClient, mockModelCatalogClient),
logger: logger,
}
})
It("should return only dora-namespace for doraNonAdmin@example.com", func() {
By("creating the HTTP request with the kubeflow-userid header")
req, err := http.NewRequest(http.MethodGet, NamespaceListPath, nil)
reqIdentity := &kubernetes.RequestIdentity{
UserID: DoraNonAdminUser,
}
ctx := context.WithValue(req.Context(), constants.RequestIdentityKey, reqIdentity)
req = req.WithContext(ctx)
Expect(err).NotTo(HaveOccurred())
rr := httptest.NewRecorder()
By("calling the GetNamespacesHandler")
testApp.GetNamespacesHandler(rr, req, nil)
rs := rr.Result()
defer rs.Body.Close()
body, err := io.ReadAll(rs.Body)
Expect(err).NotTo(HaveOccurred())
By("unmarshalling the response")
var actual NamespacesEnvelope
err = json.Unmarshal(body, &actual)
Expect(err).NotTo(HaveOccurred())
Expect(rr.Code).To(Equal(http.StatusOK))
By("validating the response contains only dora-namespace")
doraDisplayName := "dora-namespace"
expected := []models.NamespaceModel{{Name: "dora-namespace", DisplayName: &doraDisplayName}}
Expect(actual.Data).To(ConsistOf(expected))
})
It("should return all namespaces for user@example.com", func() {
By("creating the HTTP request with the kubeflow-userid header")
req, err := http.NewRequest(http.MethodGet, NamespaceListPath, nil)
reqIdentity := &kubernetes.RequestIdentity{
UserID: KubeflowUserIDHeaderValue,
}
ctx := context.WithValue(req.Context(), constants.RequestIdentityKey, reqIdentity)
req = req.WithContext(ctx)
Expect(err).NotTo(HaveOccurred())
req.Header.Set("kubeflow-userid", "user@example.com")
rr := httptest.NewRecorder()
By("calling the GetNamespacesHandler")
testApp.GetNamespacesHandler(rr, req, nil)
rs := rr.Result()
defer rs.Body.Close()
body, err := io.ReadAll(rs.Body)
Expect(err).NotTo(HaveOccurred())
By("unmarshalling the response")
var actual NamespacesEnvelope
err = json.Unmarshal(body, &actual)
Expect(err).NotTo(HaveOccurred())
Expect(rr.Code).To(Equal(http.StatusOK))
By("validating the response contains all namespaces")
kubeflowDisplayName := "kubeflow"
doraDisplayName := "dora-namespace"
expected := []models.NamespaceModel{
{Name: "kubeflow", DisplayName: &kubeflowDisplayName},
{Name: "dora-namespace", DisplayName: &doraDisplayName},
}
Expect(actual.Data).To(ContainElements(expected))
})
It("should return no namespaces for non-existent user", func() {
By("creating the HTTP request with a non-existent kubeflow-userid")
req, err := http.NewRequest(http.MethodGet, NamespaceListPath, nil)
reqIdentity := &kubernetes.RequestIdentity{
UserID: "nonexistent@example.com",
}
ctx := context.WithValue(req.Context(), constants.RequestIdentityKey, reqIdentity)
req = req.WithContext(ctx)
Expect(err).NotTo(HaveOccurred())
rr := httptest.NewRecorder()
By("calling the GetNamespacesHandler")
testApp.GetNamespacesHandler(rr, req, nil)
rs := rr.Result()
defer rs.Body.Close()
body, err := io.ReadAll(rs.Body)
Expect(err).NotTo(HaveOccurred())
By("unmarshalling the response")
var actual NamespacesEnvelope
err = json.Unmarshal(body, &actual)
Expect(err).NotTo(HaveOccurred())
Expect(rr.Code).To(Equal(http.StatusOK))
By("validating the response contains no namespaces")
Expect(actual.Data).To(BeEmpty())
})
})
Context("when running in dev mode with k8 token client", Ordered, func() {
var testApp App
BeforeAll(func() {
By("setting up the test app in dev mode")
kubernetesMockedTokenClientFactory, err := k8mocks.NewTokenClientFactory(clientset, restConfig, logger)
Expect(err).NotTo(HaveOccurred())
testApp = App{
config: config.EnvConfig{DevMode: true},
kubernetesClientFactory: kubernetesMockedTokenClientFactory,
repositories: repositories.NewRepositories(mockMRClient, mockModelCatalogClient),
logger: logger,
}
})
It("should return namespaces for user@example.com - with token", func() {
By("creating the HTTP request with the kubeflow-userid header")
req, err := http.NewRequest(http.MethodGet, NamespaceListPath, nil)
reqIdentity := &kubernetes.RequestIdentity{
//UserID: user@example.com,
Token: k8mocks.DefaultTestUsers[0].Token,
}
ctx := context.WithValue(req.Context(), constants.RequestIdentityKey, reqIdentity)
req = req.WithContext(ctx)
Expect(err).NotTo(HaveOccurred())
rr := httptest.NewRecorder()
By("calling the GetNamespacesHandler")
testApp.GetNamespacesHandler(rr, req, nil)
rs := rr.Result()
defer rs.Body.Close()
body, err := io.ReadAll(rs.Body)
Expect(err).NotTo(HaveOccurred())
By("unmarshalling the response")
var actual NamespacesEnvelope
err = json.Unmarshal(body, &actual)
Expect(err).NotTo(HaveOccurred())
Expect(rr.Code).To(Equal(http.StatusOK))
By("validating the response contains namespaces")
Expect(actual.Data).ToNot(BeEmpty())
})
It("should return no namespaces for non-authorized existent user", func() {
By("creating the HTTP request with a non-authorized user")
req, err := http.NewRequest(http.MethodGet, NamespaceListPath, nil)
reqIdentity := &kubernetes.RequestIdentity{
Token: k8mocks.DefaultTestUsers[1].Token,
}
ctx := context.WithValue(req.Context(), constants.RequestIdentityKey, reqIdentity)
req = req.WithContext(ctx)
Expect(err).NotTo(HaveOccurred())
rr := httptest.NewRecorder()
By("calling the GetNamespacesHandler")
testApp.GetNamespacesHandler(rr, req, nil)
rs := rr.Result()
defer rs.Body.Close()
body, err := io.ReadAll(rs.Body)
Expect(err).NotTo(HaveOccurred())
By("unmarshalling the response")
var actual NamespacesEnvelope
err = json.Unmarshal(body, &actual)
Expect(err).NotTo(HaveOccurred())
Expect(rr.Code).To(Equal(http.StatusInternalServerError))
})
})
})