package configread import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" ) func TestClientReadTaskStatusRequestsOnlyTheAuthorizedTask(t *testing.T) { fixtures := validConfigFixtures(t) path := configReadPath + "/task/" + mockTaskID var requests []string server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { requests = append(requests, r.Method+" "+r.URL.RequestURI()) if r.Header.Get(dispatcherIDHeader) != mockDispatcherID || r.Header.Get(dispatcherSecretHeader) != mockTestSecret { t.Errorf("request identity headers = %q/%q", r.Header.Get(dispatcherIDHeader), r.Header.Get(dispatcherSecretHeader)) } if r.Method != http.MethodGet || r.URL.Path != path { http.NotFound(w, r) return } w.Header().Set("Content-Type", "application/json") _, _ = w.Write(fixtures[path]) })) defer server.Close() got, err := newMockClient(t, server).ReadTaskStatus(context.Background(), mockTaskID, mockTenantID, mockTenantKey) if err != nil { t.Fatal(err) } var expected taskConfigResponse if err := json.Unmarshal(fixtures[path], &expected); err != nil { t.Fatal(err) } if got.DispatcherID != mockDispatcherID || got.TaskID != expected.TaskID || got.TenantID != expected.TenantID || got.TenantKey != expected.TenantKey || got.Status != expected.Status || got.TaskRevision != expected.TaskRevision { t.Fatalf("task status = %+v; response = %+v", got, expected) } if len(requests) != 1 || requests[0] != "GET "+path { t.Fatalf("requests = %#v, want one task GET", requests) } } func TestClientReadTaskStatusRejectsTenantBindingMismatch(t *testing.T) { fixtures := validConfigFixtures(t) path := configReadPath + "/task/" + mockTaskID fixtures[path] = replaceFixtureText(t, fixtures[path], `"tenant_key": "tenant-mock"`, `"tenant_key": "tenant-other"`) server := configFixtureServer(t, fixtures) defer server.Close() if _, err := newMockClient(t, server).ReadTaskStatus(context.Background(), mockTaskID, mockTenantID, mockTenantKey); err == nil { t.Fatal("ReadTaskStatus accepted a task bound to another tenant key") } }