| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/rastrillo/rastrillo/dbtest" |
| 8 | ) |
| 9 | |
| 10 | // homeTemplate is the database with Oficina ID's tables too. |
| 11 | var homeTemplate = dbtest.FromSet(Boot(true)) |
| 12 | |
| 13 | func countLinks(t *testing.T, q QueryRower, n int64) int { |
| 14 | t.Helper() |
| 15 | var c int |
| 16 | if err := q.QueryRowContext(context.Background(), `SELECT count(*) FROM home_identity_links WHERE person_id = ?`, n).Scan(&c); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | return c |
| 20 | } |
| 21 | |
| 22 | // Removing a member removes its Oficina links in the same transaction, as |
| 23 | // connect requires, so the Oficina account is free to link again. |
| 24 | func TestRemoveMemberDeletesItsLinks(t *testing.T) { |
| 25 | ctx, db := context.Background(), homeTemplate.Open(t) |
| 26 | ana, _, err := Invite(ctx, db, "ana@example.com") |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | bo, _, err := Invite(ctx, db, "bo@example.com") |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | na, _ := MemberNumber(ctx, db, ana.ID) |
| 35 | nb, _ := MemberNumber(ctx, db, bo.ID) |
| 36 | if _, err := db.Exec(`INSERT INTO home_identity_links (issuer, subject, person_id, person_binding) VALUES |
| 37 | ('https://id.example', 'subject-ana', ?, 'x'), ('https://id.example', 'subject-bo', ?, 'y')`, na, nb); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if err := RemoveMember(ctx, db, ana.ID); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | if _, ok, _ := MemberByEmail(ctx, db, "ana@example.com"); ok { |
| 44 | t.Error("ana is still a member") |
| 45 | } |
| 46 | if c := countLinks(t, db, na); c != 0 { |
| 47 | t.Errorf("ana's links: %d left", c) |
| 48 | } |
| 49 | if c := countLinks(t, db, nb); c != 1 { |
| 50 | t.Errorf("bo's links: %d, want 1", c) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // With Oficina ID off its tables don't exist, and removing a member still |
| 55 | // works. |
| 56 | func TestRemoveMemberWithoutOficinaTables(t *testing.T) { |
| 57 | ctx, db := context.Background(), open(t) |
| 58 | ana, _, err := Invite(ctx, db, "ana@example.com") |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | if err := RemoveMember(ctx, db, ana.ID); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | if _, ok, _ := MemberByEmail(ctx, db, "ana@example.com"); ok { |
| 66 | t.Error("ana is still a member") |
| 67 | } |
| 68 | } |
| 69 | |