package plan3test import ( "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/equipo/conversation" "amadan.net/rastrillo/rastrillo/mail" "context" "crypto/rand" "gorm.io/gorm" "io" "log/slog" "net/http" "net/http/httptest" "strings" "testing" ) func TestFinalFixConversationReadLookup(t *testing.T) { f := newFixture(t) p := proposed(t, f) var thread string must(t, f.DB.Writer().QueryRow("SELECT id FROM conversation_threads WHERE task_id=?", p.TaskID).Scan(&thread)) writes := 0 app := conversation.Application{Normalize: strings.ToLower, Read: func(_ context.Context, fn func(*gorm.DB) error) error { return fn(f.DB.G) }, Write: func(_ context.Context, fn func(*gorm.DB) error) error { writes++; return fn(f.DB.G) }, Conversation: func(tx *gorm.DB) *conversation.Store { return conversation.New(tx, conversation.Dependencies{Authorize: func(*gorm.DB, equipo.Actor) (string, error) { return f.TeamID, nil }}) }} logger := slog.New(slog.NewTextHandler(io.Discard, nil)) auth, err := equipo.NewAuth(f.DB, equipo.Config{Origin: origin, InstanceKey: rand.Text(), AllowLoggedMail: true}, mail.Logged(logger), f.Store, logger) must(t, err) must(t, f.DB.G.Callback().Query().After("gorm:query").Register("finalfix_thread_lookup", func(tx *gorm.DB) { if tx.Statement.Table == "conversation_threads" && !strings.Contains(tx.Statement.SQL.String(), "conversation_threads.id=") { t.Error("thread lookup scanned team") } })) key := strings.TrimPrefix(conversation.ConversationTopic(f.TeamID, "", thread), "conversation/") h := auth.RequireSession(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if !app.AllowConversation(r, key) { t.Error("shared thread denied") } })) r := httptest.NewRequest("GET", "/", nil) r.AddCookie(f.Cookies[peer.Address]) h.ServeHTTP(httptest.NewRecorder(), r) if writes != 0 { t.Fatal("live subscription acquired writer", writes) } }