Add a secure repository Prompts review surface and prompt metadata API
Hop-State: A_06FN6MRSHG4B64ZV13086G8 Hop-Proposal: R_06FN6MQDE14FFNR65H1D28G Hop-Task: T_06FN6J8ECE1Z9XQ5PQ3Z3D8 Hop-Attempt: AT_06FN6J8ECC1SC794JMZ0TF8
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
||||
|
||||
type fakeStore struct {
|
||||
repositories []store.Repository
|
||||
prompts []store.Prompt
|
||||
delivery store.WebhookDelivery
|
||||
duplicate bool
|
||||
}
|
||||
@@ -32,19 +33,31 @@ func (f *fakeStore) UpsertRepository(_ context.Context, repository gitea.Reposit
|
||||
f.repositories = append(f.repositories, saved)
|
||||
return saved, nil
|
||||
}
|
||||
func (f *fakeStore) ListPrompts(context.Context, string, string, int) ([]store.Prompt, error) {
|
||||
return f.prompts, nil
|
||||
}
|
||||
func (f *fakeStore) CreatePrompt(_ context.Context, input store.CreatePromptInput) (store.Prompt, error) {
|
||||
prompt := store.Prompt{ID: "PM_TEST", Prompt: input.Prompt, Status: input.Status, AgentName: input.AgentName, AgentModel: input.AgentModel, Metadata: input.Metadata}
|
||||
f.prompts = append(f.prompts, prompt)
|
||||
return prompt, nil
|
||||
}
|
||||
func (f *fakeStore) RecordWebhook(_ context.Context, delivery store.WebhookDelivery) (bool, error) {
|
||||
f.delivery = delivery
|
||||
return f.duplicate, nil
|
||||
}
|
||||
|
||||
type fakeGitea struct {
|
||||
repository gitea.Repository
|
||||
err error
|
||||
repository gitea.Repository
|
||||
err error
|
||||
viewerAllowed bool
|
||||
}
|
||||
|
||||
func (f fakeGitea) Repository(context.Context, string, string) (gitea.Repository, error) {
|
||||
return f.repository, f.err
|
||||
}
|
||||
func (f fakeGitea) ViewerCanReadRepository(context.Context, string, string, string) (bool, error) {
|
||||
return f.viewerAllowed, f.err
|
||||
}
|
||||
|
||||
func testServer(data store.Store, client giteaClient) http.Handler {
|
||||
return New(data, client, "admin-secret", "webhook-secret", slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
@@ -114,3 +127,43 @@ func TestWebhookRejectsInvalidSignature(t *testing.T) {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListPromptsRequiresGiteaSession(t *testing.T) {
|
||||
data := &fakeStore{prompts: []store.Prompt{{ID: "PM_TEST", Prompt: "Ship the prompt view", Status: "completed"}}}
|
||||
handler := testServer(data, fakeGitea{viewerAllowed: true})
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/prompts?owner=hop&repo=demo", nil)
|
||||
req.Header.Set("Cookie", "i_like_gitea=session")
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, req)
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d; body=%s", response.Code, http.StatusOK, response.Body.String())
|
||||
}
|
||||
if !bytes.Contains(response.Body.Bytes(), []byte("Ship the prompt view")) {
|
||||
t.Fatalf("prompt missing from response: %s", response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestListPromptsRejectsUnauthenticatedViewer(t *testing.T) {
|
||||
handler := testServer(&fakeStore{}, fakeGitea{})
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/prompts?owner=hop&repo=demo", nil)
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, req)
|
||||
if response.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want %d; body=%s", response.Code, http.StatusUnauthorized, response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreatePrompt(t *testing.T) {
|
||||
data := &fakeStore{}
|
||||
handler := testServer(data, fakeGitea{})
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/repositories/hop/demo/prompts", bytes.NewBufferString(`{"prompt":"Ship the prompt view","agent_name":"codex","status":"completed","metadata":{"duration_ms":1200}}`))
|
||||
req.Header.Set("Authorization", "Bearer admin-secret")
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, req)
|
||||
if response.Code != http.StatusCreated {
|
||||
t.Fatalf("status = %d, want %d; body=%s", response.Code, http.StatusCreated, response.Body.String())
|
||||
}
|
||||
if len(data.prompts) != 1 || data.prompts[0].Prompt != "Ship the prompt view" {
|
||||
t.Fatalf("prompt not created: %#v", data.prompts)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user