fix: invoke auth via Fission router JWT login
This commit is contained in:
+72
-2
@@ -26,7 +26,7 @@ func newTestServer(objs ...runtime.Object) *server {
|
||||
timeTrigGVR: "TimeTriggerList",
|
||||
}
|
||||
dyn := dynamicfake.NewSimpleDynamicClientWithCustomListKinds(runtime.NewScheme(), listKinds, objs...)
|
||||
return &server{dyn: dyn, ns: "default", routerURL: "http://example.invalid", http: &http.Client{}}
|
||||
return &server{dyn: dyn, ns: "default", routerURL: "http://example.invalid", http: &http.Client{}, saTokenPath: ""}
|
||||
}
|
||||
|
||||
func TestNormalizeMethods(t *testing.T) {
|
||||
@@ -109,7 +109,7 @@ func TestUpdateFunctionCode(t *testing.T) {
|
||||
env := &unstructured.Unstructured{Object: map[string]any{
|
||||
"apiVersion": "fission.io/v1",
|
||||
"kind": "Environment",
|
||||
"metadata": map[string]any{"name": "python", "namespace": "default"},
|
||||
"metadata": map[string]any{"name": "python", "namespace": "default"},
|
||||
}}
|
||||
s := newTestServer(env)
|
||||
|
||||
@@ -141,3 +141,73 @@ func TestUpdateFunctionCode(t *testing.T) {
|
||||
t.Fatalf("expected new-code, got %q", string(decoded))
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvokeFunctionWithJWTAuth(t *testing.T) {
|
||||
// Mock router: /auth/login returns JWT, /inv-fn returns hello
|
||||
var gotAuth string
|
||||
mockRouter := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/auth/login" && r.Method == http.MethodPost {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"accesstoken":"fake-jwt-token-xyz","tokentype":"Bearer"}`))
|
||||
return
|
||||
}
|
||||
gotAuth = r.Header.Get("Authorization")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"hello":"world"}`))
|
||||
}))
|
||||
defer mockRouter.Close()
|
||||
|
||||
env := &unstructured.Unstructured{Object: map[string]any{
|
||||
"apiVersion": "fission.io/v1",
|
||||
"kind": "Environment",
|
||||
"metadata": map[string]any{"name": "python", "namespace": "default"},
|
||||
}}
|
||||
|
||||
listKinds := map[schema.GroupVersionResource]string{
|
||||
environmentGVR: "EnvironmentList",
|
||||
packageGVR: "PackageList",
|
||||
functionGVR: "FunctionList",
|
||||
httpTrigGVR: "HTTPTriggerList",
|
||||
timeTrigGVR: "TimeTriggerList",
|
||||
}
|
||||
dyn := dynamicfake.NewSimpleDynamicClientWithCustomListKinds(runtime.NewScheme(), listKinds, env)
|
||||
s := &server{
|
||||
dyn: dyn,
|
||||
ns: "default",
|
||||
routerURL: mockRouter.URL,
|
||||
http: mockRouter.Client(),
|
||||
authUser: "admin",
|
||||
authPass: "pass",
|
||||
}
|
||||
|
||||
// Create function first
|
||||
createBody := `{"name":"inv-fn","environment":"python","code":"print(1)","route":"/inv-fn","methods":["GET"]}`
|
||||
createRec := httptest.NewRecorder()
|
||||
s.handleCreateFunction(createRec, httptest.NewRequest(http.MethodPost, "/api/functions", strings.NewReader(createBody)))
|
||||
if createRec.Code != http.StatusCreated {
|
||||
t.Fatalf("create: %d %s", createRec.Code, createRec.Body.String())
|
||||
}
|
||||
|
||||
// Invoke
|
||||
invokeRec := httptest.NewRecorder()
|
||||
s.handleInvokeFunction(invokeRec, httptest.NewRequest(http.MethodPost, "/api/functions/inv-fn/invoke", strings.NewReader(`{}`)), "inv-fn")
|
||||
if invokeRec.Code != http.StatusOK {
|
||||
t.Fatalf("invoke: %d %s", invokeRec.Code, invokeRec.Body.String())
|
||||
}
|
||||
|
||||
// Verify JWT was obtained via login and sent
|
||||
if gotAuth != "Bearer fake-jwt-token-xyz" {
|
||||
t.Fatalf("expected 'Bearer fake-jwt-token-xyz', got %q", gotAuth)
|
||||
}
|
||||
|
||||
var out map[string]any
|
||||
if err := json.Unmarshal(invokeRec.Body.Bytes(), &out); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if out["status"] != float64(200) {
|
||||
t.Fatalf("expected status 200, got %v", out["status"])
|
||||
}
|
||||
if !strings.Contains(out["response_raw"].(string), "hello") {
|
||||
t.Fatalf("unexpected response: %v", out["response_raw"])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user