package httpapi import ( "bytes" "context" "net/http" "net/http/httptest" "testing" "github.com/go-chi/chi/v5" "github.com/google/uuid" ) func TestHandleUpdateAPIKeyRejectsEmptyName(t *testing.T) { t.Parallel() s := &Server{} cid := uuid.MustParse("11111111-1111-1111-1111-111111111111") uid := uuid.MustParse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") keyID := uuid.MustParse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb") ctx := context.WithValue(context.Background(), ctxUserID, uid) ctx = context.WithValue(ctx, ctxCompanyID, cid) ctx = context.WithValue(ctx, ctxRole, "admin") rctx := chi.NewRouteContext() rctx.URLParams.Add("id", keyID.String()) ctx = context.WithValue(ctx, chi.RouteCtxKey, rctx) req := httptest.NewRequest(http.MethodPatch, "/api/api-keys/"+keyID.String(), bytes.NewBufferString(`{"name":" "}`)).WithContext(ctx) rec := httptest.NewRecorder() s.handleUpdateAPIKey(rec, req) if rec.Code != http.StatusBadRequest { t.Fatalf("status = %d body=%s, want 400", rec.Code, rec.Body.String()) } } func TestHandleUpdateAPIKeyRejectsInvalidID(t *testing.T) { t.Parallel() s := &Server{} cid := uuid.MustParse("11111111-1111-1111-1111-111111111111") uid := uuid.MustParse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") ctx := context.WithValue(context.Background(), ctxUserID, uid) ctx = context.WithValue(ctx, ctxCompanyID, cid) ctx = context.WithValue(ctx, ctxRole, "admin") rctx := chi.NewRouteContext() rctx.URLParams.Add("id", "not-a-uuid") ctx = context.WithValue(ctx, chi.RouteCtxKey, rctx) req := httptest.NewRequest(http.MethodPatch, "/api/api-keys/not-a-uuid", bytes.NewBufferString(`{"name":"ok"}`)).WithContext(ctx) rec := httptest.NewRecorder() s.handleUpdateAPIKey(rec, req) if rec.Code != http.StatusBadRequest { t.Fatalf("status = %d body=%s, want 400", rec.Code, rec.Body.String()) } } // API-key management is separate from company-admin / team / billing privileges. // Explicit permission checks are also enforced for handler calls that do not // pass through the router's RequireMemberFeature middleware. func TestAllowAPIKeyManagerPrivilegesAndMissingGrant(t *testing.T) { t.Parallel() uid := uuid.MustParse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") cid := uuid.MustParse("11111111-1111-1111-1111-111111111111") cases := []struct { name string role string platform bool owner bool want int }{ {name: "company admin", role: "admin", want: http.StatusOK}, {name: "platform admin", role: "member", platform: true, want: http.StatusOK}, {name: "company owner", role: "member", owner: true, want: http.StatusOK}, {name: "ordinary member without loaded permissions", role: "member", want: http.StatusForbidden}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { s := &Server{ testPlatformAdmin: func(context.Context, uuid.UUID) (bool, error) { return tc.platform, nil }, testCompanyOwner: func(context.Context, uuid.UUID) (bool, error) { return tc.owner, nil }, } ctx := context.WithValue(context.Background(), ctxUserID, uid) ctx = context.WithValue(ctx, ctxCompanyID, cid) ctx = context.WithValue(ctx, ctxRole, tc.role) req := httptest.NewRequest(http.MethodPost, "/api/api-keys", nil).WithContext(ctx) rec := httptest.NewRecorder() allowed := s.allowAPIKeyManager(rec, req) if allowed != (tc.want == http.StatusOK) { t.Fatalf("allowAPIKeyManager allowed=%v status=%d", allowed, rec.Code) } if !allowed && rec.Code != tc.want { t.Fatalf("status=%d body=%s, want %d", rec.Code, rec.Body.String(), tc.want) } }) } }