The endpoints screen could only create and edit servers, so a mistyped or retired endpoint stayed in the list forever. Tasks reference endpoints without ON DELETE CASCADE, so a referenced endpoint is refused with 409 and a count of the tasks using it rather than cascading away migration history. The foreign-key violation is mapped to the same status to cover a task created between check and delete. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
102 lines
3.6 KiB
Go
102 lines
3.6 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestUpdateEndpoint(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
id, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "src", Host: "old.host", Port: 143, TLSMode: "plain"})
|
|
if err := s.UpdateEndpoint(ctx, Endpoint{ID: id, RoleLabel: "src2", Host: "new.host", Port: 993, TLSMode: "ssl"}); err != nil {
|
|
t.Fatalf("update: %v", err)
|
|
}
|
|
got, _ := s.GetEndpoint(ctx, id)
|
|
if got.Host != "new.host" || got.Port != 993 || got.TLSMode != "ssl" || got.RoleLabel != "src2" {
|
|
t.Fatalf("update not applied: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestDeleteEndpoint(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
id, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "src", Host: "a.com", Port: 993, TLSMode: "ssl"})
|
|
if err := s.DeleteEndpoint(ctx, id); err != nil {
|
|
t.Fatalf("delete: %v", err)
|
|
}
|
|
eps, _ := s.ListEndpoints(ctx)
|
|
if len(eps) != 0 {
|
|
t.Fatalf("endpoint not deleted: %d remain", len(eps))
|
|
}
|
|
}
|
|
|
|
func TestDeleteEndpointUsedByTaskRefused(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
ep1, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "s", Host: "a", Port: 993, TLSMode: "ssl"})
|
|
ep2, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "d", Host: "b", Port: 993, TLSMode: "ssl"})
|
|
if _, err := s.CreateTask(ctx, Task{Name: "t", SrcEndpointID: ep1, DstEndpointID: ep2}); err != nil {
|
|
t.Fatalf("create task: %v", err)
|
|
}
|
|
for _, id := range []int64{ep1, ep2} {
|
|
n, err := s.CountTasksUsingEndpoint(ctx, id)
|
|
if err != nil {
|
|
t.Fatalf("count: %v", err)
|
|
}
|
|
if n != 1 {
|
|
t.Fatalf("count for ep %d = %d, want 1", id, n)
|
|
}
|
|
if err := s.DeleteEndpoint(ctx, id); err == nil {
|
|
t.Fatalf("delete of referenced endpoint %d succeeded, want FK violation", id)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDeleteAccountCascadesJournal(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
ep1, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "s", Host: "a", Port: 993, TLSMode: "ssl"})
|
|
ep2, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "d", Host: "b", Port: 993, TLSMode: "ssl"})
|
|
taskID, _ := s.CreateTask(ctx, Task{Name: "t", SrcEndpointID: ep1, DstEndpointID: ep2})
|
|
accID, _ := s.CreateAccount(ctx, Account{TaskID: taskID, SrcLogin: "u", SrcPassEnc: "x", DstLogin: "v", DstPassEnc: "y"})
|
|
_ = s.MarkMigrated(ctx, accID, "INBOX", "<m1>")
|
|
|
|
if err := s.DeleteAccount(ctx, accID); err != nil {
|
|
t.Fatalf("delete account: %v", err)
|
|
}
|
|
accs, _ := s.ListAccountsByTask(ctx, taskID)
|
|
if len(accs) != 0 {
|
|
t.Fatalf("account not deleted: %d remain", len(accs))
|
|
}
|
|
// journal row must be gone via ON DELETE CASCADE
|
|
var n int
|
|
_ = s.Pool.QueryRow(ctx, `SELECT count(*) FROM migrated_messages WHERE account_id=$1`, accID).Scan(&n)
|
|
if n != 0 {
|
|
t.Fatalf("migrated_messages not cascaded: %d rows", n)
|
|
}
|
|
}
|
|
|
|
func TestDeleteTaskCascades(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
ep1, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "s", Host: "a", Port: 993, TLSMode: "ssl"})
|
|
ep2, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "d", Host: "b", Port: 993, TLSMode: "ssl"})
|
|
taskID, _ := s.CreateTask(ctx, Task{Name: "t", SrcEndpointID: ep1, DstEndpointID: ep2})
|
|
accID, _ := s.CreateAccount(ctx, Account{TaskID: taskID, SrcLogin: "u", SrcPassEnc: "x", DstLogin: "v", DstPassEnc: "y"})
|
|
_, _ = s.CreateRun(ctx, taskID, "manual")
|
|
|
|
if err := s.DeleteTask(ctx, taskID); err != nil {
|
|
t.Fatalf("delete task: %v", err)
|
|
}
|
|
tasks, _ := s.ListTasks(ctx)
|
|
if len(tasks) != 0 {
|
|
t.Fatalf("task not deleted: %d remain", len(tasks))
|
|
}
|
|
accs, _ := s.ListAccountsByTask(ctx, taskID)
|
|
if len(accs) != 0 {
|
|
t.Fatalf("accounts not cascaded: %d remain", len(accs))
|
|
}
|
|
_ = accID
|
|
}
|