forked from mtusi2026/mtusi2026lab4
Prepare verified laboratory 4 implementation and evidence
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"backend/internal/adapter/repository/postgres"
|
||||
"backend/internal/adapter/storage"
|
||||
"backend/internal/config"
|
||||
"backend/internal/domain/model"
|
||||
"bytes"
|
||||
"context"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFixtureCleanupRejectsForeignMembership(t *testing.T) {
|
||||
if os.Getenv("RUN_ISOLATED_RECONCILIATION") != "1" {
|
||||
t.Skip("explicit isolated stand required")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
dsn, e := config.ReadSecret("/run/secrets/database_url")
|
||||
if e != nil {
|
||||
t.Fatal("database configuration unavailable")
|
||||
}
|
||||
pool, e := pgxpool.New(ctx, dsn)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer pool.Close()
|
||||
c, e := storage.ReadConfig("/run/secrets/s3_credentials")
|
||||
if e != nil {
|
||||
t.Fatal("S3 configuration unavailable")
|
||||
}
|
||||
objects, e := storage.FromConfig(c)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
repo := postgres.New(pool, nil)
|
||||
prefix := "st_" + uuid.NewString()[:8] + uuid.NewString()[:8]
|
||||
own, e := repo.CreateUser(ctx, model.UserDraft{Username: prefix, DisplayName: "Fixture owner", PasswordHash: "disabled-test-account"})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
foreign, e := repo.CreateUser(ctx, model.UserDraft{Username: "other_" + uuid.NewString()[:8], DisplayName: "Independent test user", PasswordHash: "disabled-test-account"})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
draft, e := model.NewChatDraft(foreign.ID, "Foreign-owned fixture boundary", []uuid.UUID{foreign.ID, own.ID})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
chat, e := repo.CreateChat(ctx, draft)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
id := uuid.New()
|
||||
key := "originals/" + id.String()
|
||||
defer func() {
|
||||
clean, cancelClean := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancelClean()
|
||||
for _, statement := range []string{`DELETE FROM attachments WHERE id=$1`, `DELETE FROM chat_members WHERE chat_id=$1`, `DELETE FROM chats WHERE id=$1`} {
|
||||
arg := any(chat.ID)
|
||||
if statement == `DELETE FROM attachments WHERE id=$1` {
|
||||
arg = id
|
||||
}
|
||||
if _, err := pool.Exec(clean, statement, arg); err != nil {
|
||||
t.Error("boundary fixture cleanup failed")
|
||||
}
|
||||
}
|
||||
if _, err := pool.Exec(clean, `DELETE FROM users WHERE id=ANY($1::uuid[])`, []uuid.UUID{own.ID, foreign.ID}); err != nil {
|
||||
t.Error("boundary users cleanup failed")
|
||||
}
|
||||
if err := objects.Remove(clean, key); err != nil {
|
||||
t.Error("boundary original cleanup failed")
|
||||
}
|
||||
}()
|
||||
_, e = repo.ReserveAttachment(ctx, model.Attachment{ID: id, ChatID: chat.ID, OwnerID: own.ID, Filename: "owned.png", ContentType: "image/png", SizeBytes: 3, SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e = objects.Put(ctx, key, bytes.NewReader([]byte("abc")), 3, "image/png"); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e = exec.CommandContext(ctx, "/fixture-cleanup", prefix).Run(); e == nil {
|
||||
t.Fatal("cleanup accepted a fixture referenced by another owner")
|
||||
}
|
||||
if _, e = objects.Stat(ctx, key); e != nil {
|
||||
t.Fatal("rejected cleanup removed the original")
|
||||
}
|
||||
if _, e = repo.GetAttachment(ctx, id); e != nil {
|
||||
t.Fatal("rejected cleanup removed metadata")
|
||||
}
|
||||
remaining, e := repo.GetChat(ctx, chat.ID)
|
||||
if e != nil || !remaining.HasMember(own.ID) || !remaining.HasMember(foreign.ID) {
|
||||
t.Fatal("rejected cleanup changed another user's chat")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user