diff --git a/internal/gateway/session_save_test.go b/internal/gateway/session_save_test.go new file mode 100644 index 000000000..915e0f6f0 --- /dev/null +++ b/internal/gateway/session_save_test.go @@ -0,0 +1,67 @@ +package gateway + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/samcharles93/archie-core/internal/domain/access" + "github.com/samcharles93/archie-core/internal/domain/messaging" + "github.com/samcharles93/archie-core/internal/domain/org" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb" +) + +// recordingSaver stands in for the SaveSession upsert. rows is what the query +// reports as affected: 0 is the upsert matching no row, which is how the SQL +// refuses another org's session or another person's conversation. +type recordingSaver struct { + rows int64 + params postgresdb.SaveSessionParams + calls int +} + +func (r *recordingSaver) SaveSession(_ context.Context, p postgresdb.SaveSessionParams) (int64, error) { + r.calls++ + r.params = p + return r.rows, nil +} + +func TestSaveSession(t *testing.T) { + for _, tc := range []struct { + name string + platform string + org org.OrgID + rows int64 + wantErr error + }{ + {"a messaging chat creates its session", "telegram", "org-a", 1, nil}, + {"a web conversation creates its session", "web", "org-a", 1, nil}, + {"another org's session is refused by the upsert", "telegram", "org-b", 0, ErrSessionNotFound}, + {"another person's thread is refused by the upsert", "web", "org-a", 0, ErrSessionNotFound}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx := access.WithPrincipal(t.Context(), access.Principal{IdentityID: "alice"}) + ctx = org.WithOrg(ctx, tc.org) + saved := &recordingSaver{rows: tc.rows} + sc := SessionContext{ + SessionID: "session-1", + Source: messaging.SessionSource{Platform: tc.platform, ChannelID: "chat-1", ThreadID: "thread-1"}, + CreatedAt: time.Unix(1, 0), + LastActiveAt: time.Unix(2, 0), + } + + err := saveSession(ctx, saved, sc) + if !errors.Is(err, tc.wantErr) { + t.Fatalf("save err=%v want=%v", err, tc.wantErr) + } + if saved.calls != 1 { + t.Fatalf("writes=%d want=1", saved.calls) + } + if saved.params.Platform != tc.platform || saved.params.OrgID != string(tc.org) { + t.Fatalf("platform=%q org=%q want platform=%q org=%q", + saved.params.Platform, saved.params.OrgID, tc.platform, tc.org) + } + }) + } +} diff --git a/internal/gateway/session_store_postgres.go b/internal/gateway/session_store_postgres.go index 9dfb3d93f..4231d7879 100644 --- a/internal/gateway/session_store_postgres.go +++ b/internal/gateway/session_store_postgres.go @@ -40,11 +40,20 @@ func (s *postgresSessionStore) Close() error { // ── Sessions ──────────────────────────────────────────────────────────────── +// sessionSaver is the one query Save needs. +type sessionSaver interface { + SaveSession(context.Context, postgresdb.SaveSessionParams) (int64, error) +} + func (s *postgresSessionStore) Save(ctx context.Context, sc SessionContext) error { - if !sessionVisible(ctx, sc) { - return ErrSessionNotFound - } - q := postgresdb.New(s.pool) + return saveSession(ctx, postgresdb.New(s.pool), sc) +} + +// saveSession writes sc for the caller's org. Visibility (sessionVisible) is a +// read rule: the SaveSession upsert already refuses a row owned by another org +// or an existing conversation with a different source, so a write needs no +// visibility check and no platform may be excluded from creating its session. +func saveSession(ctx context.Context, q sessionSaver, sc SessionContext) error { saved, err := q.SaveSession(ctx, postgresdb.SaveSessionParams{ SessionID: sc.SessionID, Platform: sc.Source.Platform,