oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package linkstore
2
3import (
4 "context"
5 "database/sql"
6 "errors"
7 "testing"
8 "time"
9)
10
11func TestResolveOnUsesHeldTransactionAndCompleteBinding(t *testing.T) {
12 s, _ := fixture(t)
13 s.db.SetMaxOpenConns(1)
14 l := local(1)
15 proof := complete(t, s, l, "subject-one", 42)
16 if _, e := s.Confirm(ctx, proof, l, testNow); e != nil {
17 t.Fatal(e)
18 }
19 id, label, e := s.Resolve(ctx, issuer, "subject-one")
20 if e != nil || id != 1 || label != "one@example.com" {
21 t.Fatal(id, label, e)
22 }
23 for _, mode := range []string{"valid", "binding", "unlink", "address", "admission", "storage", "issuer", "subject", "nil"} {
24 t.Run(mode, func(t *testing.T) {
25 c, cancel := context.WithTimeout(ctx, time.Second)
26 defer cancel()
27 tx, e := s.db.BeginTx(c, nil)
28 if e != nil {
29 t.Fatal(e)
30 }
31 defer tx.Rollback()
32 if _, e = tx.ExecContext(c, "UPDATE home_link_clock SET high_water=high_water WHERE id=1"); e != nil {
33 t.Fatal(e)
34 }
35 old := s.lookup
36 defer func() { s.lookup = old }()
37 q := Queryer(tx)
38 iss, sub := issuer, "subject-one"
39 switch mode {
40 case "binding":
41 _, e = tx.ExecContext(c, "UPDATE home_identity_links SET person_binding='broken'")
42 case "unlink":
43 _, e = tx.ExecContext(c, "DELETE FROM home_identity_links")
44 case "address":
45 _, e = tx.ExecContext(c, "UPDATE people SET email='changed@example.com' WHERE id=1")
46 case "admission":
47 s.lookup = func(context.Context, Queryer, int64) (string, error) { return "", sql.ErrNoRows }
48 case "storage":
49 s.lookup = func(context.Context, Queryer, int64) (string, error) { return "", errors.New("secret-diagnostic") }
50 case "issuer":
51 iss = "https://wrong.example.com"
52 case "subject":
53 sub = ""
54 case "nil":
55 q = nil
56 }
57 if e != nil {
58 t.Fatal(e)
59 }
60 id, label, e = s.ResolveOn(c, q, iss, sub)
61 if mode == "valid" {
62 if e != nil || id != 1 || label != "one@example.com" {
63 t.Fatal("escaped transaction", id, label, e)
64 }
65 return
66 }
67 want := ErrRefused
68 if mode == "storage" {
69 want = ErrStorage
70 }
71 if e != want || id != 0 || label != "" {
72 t.Fatal("incomplete or leaked identity", mode, id, label, e)
73 }
74 })
75 }
76}
77