fix(growth): bound SSE authorization reads

This commit is contained in:
2026-08-21 01:50:11 +03:30
parent a15c04cac1
commit d3189ac8f4
2 changed files with 37 additions and 14 deletions

View File

@@ -54,14 +54,19 @@ func TestGrowthReportIDPatternRejectsTenantEnumerationInputs(t *testing.T) {
}
type fakeGrowthService struct {
get func(int64, string) (growthreport.Record, error)
create func() (growthreport.Record, bool, error)
get func(int64, string) (growthreport.Record, error)
getContext func(context.Context, int64, string) (growthreport.Record, error)
create func() (growthreport.Record, bool, error)
}
func (f fakeGrowthService) Create(_ context.Context, _, _ int64, _ growthreport.CreateRequest, _ string) (growthreport.Record, bool, error) {
return f.create()
}
func (f fakeGrowthService) Get(_ context.Context, merchantID int64, id string) (growthreport.Record, error) {
func (f fakeGrowthService) Get(ctx context.Context, merchantID int64, id string) (growthreport.Record, error) {
if f.getContext != nil {
return f.getContext(ctx, merchantID, id)
}
return f.get(merchantID, id)
}
func (f fakeGrowthService) Latest(_ context.Context, merchantID int64, terminal string) (growthreport.Record, error) {
@@ -169,3 +174,21 @@ func TestGrowthSSEEndsAtConfiguredDeadline(t *testing.T) {
t.Fatalf("database reads=%d", reads)
}
}
func TestGrowthSSEBoundsInitialDatabaseRead(t *testing.T) {
t.Parallel()
service := fakeGrowthService{getContext: func(ctx context.Context, _ int64, _ string) (growthreport.Record, error) {
<-ctx.Done()
return growthreport.Record{}, ctx.Err()
}}
server := &Server{logger: slog.New(slog.NewTextHandler(io.Discard, nil)), growth: service, growthSSESlots: make(chan struct{}, 1), growthSSEMaxDuration: 5 * time.Millisecond}
request := httptest.NewRequest("GET", "/", nil)
request.SetPathValue("reportId", "growth_0123456789abcdef01234567")
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, identity.Session{MerchantID: 7}))
response := httptest.NewRecorder()
started := time.Now()
server.growthReportEvents(response, request)
if elapsed := time.Since(started); elapsed > 250*time.Millisecond || response.Code != http.StatusServiceUnavailable {
t.Fatalf("elapsed=%s status=%d", elapsed, response.Code)
}
}