Stop publishing repository-wide prompt ledgers and enforce per-user prompt isolation through authenticated Gitea IDs.
Hop-State: A_06FN8NFDRFAFN5VCJTHJAAG Hop-Proposal: R_06FN8NEGJ054CPMJF9ZAN70 Hop-Task: T_06FN8HT6XG332C3XY6PYCCG Hop-Attempt: AT_06FN8HT6XGRXW0B4WPFFBJG
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
||||
type fakeStore struct {
|
||||
repositories []store.Repository
|
||||
prompts []store.Prompt
|
||||
listUserID int64
|
||||
delivery store.WebhookDelivery
|
||||
duplicate bool
|
||||
}
|
||||
@@ -33,11 +34,12 @@ 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) {
|
||||
func (f *fakeStore) ListPrompts(_ context.Context, _, _ string, giteaUserID int64, _ int) ([]store.Prompt, error) {
|
||||
f.listUserID = giteaUserID
|
||||
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}
|
||||
prompt := store.Prompt{ID: "PM_TEST", GiteaUserID: input.GiteaUserID, Prompt: input.Prompt, Status: input.Status, AgentName: input.AgentName, AgentModel: input.AgentModel, Metadata: input.Metadata}
|
||||
f.prompts = append(f.prompts, prompt)
|
||||
return prompt, nil
|
||||
}
|
||||
@@ -48,13 +50,21 @@ func (f *fakeStore) RecordWebhook(_ context.Context, delivery store.WebhookDeliv
|
||||
|
||||
type fakeGitea struct {
|
||||
repository gitea.Repository
|
||||
user gitea.User
|
||||
err error
|
||||
viewerAllowed bool
|
||||
authenticated bool
|
||||
}
|
||||
|
||||
func (f fakeGitea) Repository(context.Context, string, string) (gitea.Repository, error) {
|
||||
return f.repository, f.err
|
||||
}
|
||||
func (f fakeGitea) User(context.Context, string) (gitea.User, error) {
|
||||
return f.user, f.err
|
||||
}
|
||||
func (f fakeGitea) AuthenticatedUser(context.Context, string) (gitea.User, bool, error) {
|
||||
return f.user, f.authenticated, f.err
|
||||
}
|
||||
func (f fakeGitea) ViewerCanReadRepository(context.Context, string, string, string) (bool, error) {
|
||||
return f.viewerAllowed, f.err
|
||||
}
|
||||
@@ -130,7 +140,7 @@ func TestWebhookRejectsInvalidSignature(t *testing.T) {
|
||||
|
||||
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})
|
||||
handler := testServer(data, fakeGitea{user: gitea.User{ID: 42, Login: "alice"}, viewerAllowed: true, authenticated: 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()
|
||||
@@ -141,6 +151,9 @@ func TestListPromptsRequiresGiteaSession(t *testing.T) {
|
||||
if !bytes.Contains(response.Body.Bytes(), []byte("Ship the prompt view")) {
|
||||
t.Fatalf("prompt missing from response: %s", response.Body.String())
|
||||
}
|
||||
if data.listUserID != 42 {
|
||||
t.Fatalf("prompts listed for Gitea user %d, want 42", data.listUserID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListPromptsRejectsUnauthenticatedViewer(t *testing.T) {
|
||||
@@ -155,8 +168,8 @@ func TestListPromptsRejectsUnauthenticatedViewer(t *testing.T) {
|
||||
|
||||
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}}`))
|
||||
handler := testServer(data, fakeGitea{user: gitea.User{ID: 42, Login: "alice"}})
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/repositories/hop/demo/prompts", bytes.NewBufferString(`{"user_login":"alice","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)
|
||||
@@ -166,4 +179,18 @@ func TestCreatePrompt(t *testing.T) {
|
||||
if len(data.prompts) != 1 || data.prompts[0].Prompt != "Ship the prompt view" {
|
||||
t.Fatalf("prompt not created: %#v", data.prompts)
|
||||
}
|
||||
if data.prompts[0].GiteaUserID != 42 {
|
||||
t.Fatalf("prompt owner = %d, want 42", data.prompts[0].GiteaUserID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreatePromptRequiresOwner(t *testing.T) {
|
||||
handler := testServer(&fakeStore{}, fakeGitea{})
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/repositories/hop/demo/prompts", bytes.NewBufferString(`{"prompt":"private work","status":"completed"}`))
|
||||
req.Header.Set("Authorization", "Bearer admin-secret")
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, req)
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d; body=%s", response.Code, http.StatusBadRequest, response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user