package store import ( "context" "testing" "amadan.net/rastrillo/rastrillo/dbtest" ) // homeTemplate is the database with Oficina ID's tables too. var homeTemplate = dbtest.FromSet(Boot(true)) func countLinks(t *testing.T, q QueryRower, n int64) int { t.Helper() var c int if err := q.QueryRowContext(context.Background(), `SELECT count(*) FROM home_identity_links WHERE person_id = ?`, n).Scan(&c); err != nil { t.Fatal(err) } return c } // Removing a member removes its Oficina links in the same transaction, as // connect requires, so the Oficina account is free to link again. func TestRemoveMemberDeletesItsLinks(t *testing.T) { ctx, db := context.Background(), homeTemplate.Open(t) ana, _, err := Invite(ctx, db, "ana@example.com") if err != nil { t.Fatal(err) } bo, _, err := Invite(ctx, db, "bo@example.com") if err != nil { t.Fatal(err) } na, _ := MemberNumber(ctx, db, ana.ID) nb, _ := MemberNumber(ctx, db, bo.ID) if _, err := db.Exec(`INSERT INTO home_identity_links (issuer, subject, person_id, person_binding) VALUES ('https://id.example', 'subject-ana', ?, 'x'), ('https://id.example', 'subject-bo', ?, 'y')`, na, nb); err != nil { t.Fatal(err) } if err := RemoveMember(ctx, db, ana.ID); err != nil { t.Fatal(err) } if _, ok, _ := MemberByEmail(ctx, db, "ana@example.com"); ok { t.Error("ana is still a member") } if c := countLinks(t, db, na); c != 0 { t.Errorf("ana's links: %d left", c) } if c := countLinks(t, db, nb); c != 1 { t.Errorf("bo's links: %d, want 1", c) } } // With Oficina ID off its tables don't exist, and removing a member still // works. func TestRemoveMemberWithoutOficinaTables(t *testing.T) { ctx, db := context.Background(), open(t) ana, _, err := Invite(ctx, db, "ana@example.com") if err != nil { t.Fatal(err) } if err := RemoveMember(ctx, db, ana.ID); err != nil { t.Fatal(err) } if _, ok, _ := MemberByEmail(ctx, db, "ana@example.com"); ok { t.Error("ana is still a member") } }