| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/conversation" |
| 6 | "amadan.net/rastrillo/rastrillo/mail" |
| 7 | "context" |
| 8 | "crypto/rand" |
| 9 | "gorm.io/gorm" |
| 10 | "io" |
| 11 | "log/slog" |
| 12 | "net/http" |
| 13 | "net/http/httptest" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | ) |
| 17 | |
| 18 | func TestFinalFixConversationReadLookup(t *testing.T) { |
| 19 | f := newFixture(t) |
| 20 | p := proposed(t, f) |
| 21 | var thread string |
| 22 | must(t, f.DB.Writer().QueryRow("SELECT id FROM conversation_threads WHERE task_id=?", p.TaskID).Scan(&thread)) |
| 23 | writes := 0 |
| 24 | 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 { |
| 25 | return conversation.New(tx, conversation.Dependencies{Authorize: func(*gorm.DB, equipo.Actor) (string, error) { return f.TeamID, nil }}) |
| 26 | }} |
| 27 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 28 | auth, err := equipo.NewAuth(f.DB, equipo.Config{Origin: origin, InstanceKey: rand.Text(), AllowLoggedMail: true}, mail.Logged(logger), f.Store, logger) |
| 29 | must(t, err) |
| 30 | must(t, f.DB.G.Callback().Query().After("gorm:query").Register("finalfix_thread_lookup", func(tx *gorm.DB) { |
| 31 | if tx.Statement.Table == "conversation_threads" && !strings.Contains(tx.Statement.SQL.String(), "conversation_threads.id=") { |
| 32 | t.Error("thread lookup scanned team") |
| 33 | } |
| 34 | })) |
| 35 | key := strings.TrimPrefix(conversation.ConversationTopic(f.TeamID, "", thread), "conversation/") |
| 36 | h := auth.RequireSession(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 37 | if !app.AllowConversation(r, key) { |
| 38 | t.Error("shared thread denied") |
| 39 | } |
| 40 | })) |
| 41 | r := httptest.NewRequest("GET", "/", nil) |
| 42 | r.AddCookie(f.Cookies[peer.Address]) |
| 43 | h.ServeHTTP(httptest.NewRecorder(), r) |
| 44 | if writes != 0 { |
| 45 | t.Fatal("live subscription acquired writer", writes) |
| 46 | } |
| 47 | } |
| 48 | |