oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1package store
2
3import (
4 "context"
5 "database/sql"
6 "testing"
7 "time"
8)
9
10func TestEquipoLinkRoundTrip(t *testing.T) {
11 ctx, db := context.Background(), open(t)
12 ana, _, err := Invite(ctx, db, "ana@example.com")
13 if err != nil {
14 t.Fatal(err)
15 }
16 if _, ok, err := EquipoLinkFor(ctx, db, ana.ID); ok || err != nil {
17 t.Fatalf("fresh member has a link: %v %v", ok, err)
18 }
19 exp := time.Now().Add(10 * time.Minute).UTC().Truncate(time.Second)
20 in := EquipoLink{MemberID: ana.ID, Origin: "https://equipo.test", State: EquipoPending,
21 DeviceID: "DEV1", UserCode: "ABCD1234", VerificationURI: "https://equipo.test/auth/devices/DEV1",
22 Sealed: []byte("sealed"), ExpiresAt: exp}
23 if err := SaveEquipoLink(ctx, db, in); err != nil {
24 t.Fatal(err)
25 }
26 got, ok, err := EquipoLinkFor(ctx, db, ana.ID)
27 if !ok || err != nil || got.DeviceID != "DEV1" || got.State != EquipoPending || !got.ExpiresAt.Equal(exp) ||
28 string(got.Sealed) != "sealed" || got.PolledAt != nil {
29 t.Fatalf("got %+v %v %v", got, ok, err)
30 }
31 got.State, got.Sealed, got.Revision = EquipoConnected, []byte("token"), 3
32 if err := SaveEquipoLink(ctx, db, got); err != nil {
33 t.Fatal(err)
34 }
35 got, _, _ = EquipoLinkFor(ctx, db, ana.ID)
36 if got.State != EquipoConnected || string(got.Sealed) != "token" || got.Revision != 3 {
37 t.Fatalf("replace: %+v", got)
38 }
39 if err := SetEquipoLinkState(ctx, db, ana.ID, "DEV1", EquipoLost); err != nil {
40 t.Fatal(err)
41 }
42 if got, _, _ = EquipoLinkFor(ctx, db, ana.ID); got.State != EquipoLost {
43 t.Fatalf("state: %+v", got)
44 }
45 if err := DeleteEquipoLink(ctx, db, ana.ID, "DEV1"); err != nil {
46 t.Fatal(err)
47 }
48 if _, ok, _ := EquipoLinkFor(ctx, db, ana.ID); ok {
49 t.Fatal("link survived delete")
50 }
51}
52
53// A member's Equipo link goes with the member.
54func TestRemoveMemberDeletesEquipoLink(t *testing.T) {
55 ctx, db := context.Background(), open(t)
56 ana, _, err := Invite(ctx, db, "ana@example.com")
57 if err != nil {
58 t.Fatal(err)
59 }
60 if err := SaveEquipoLink(ctx, db, EquipoLink{MemberID: ana.ID, Origin: "https://equipo.test",
61 State: EquipoConnected, DeviceID: "D", Sealed: []byte("x"), ExpiresAt: time.Now()}); err != nil {
62 t.Fatal(err)
63 }
64 if err := RemoveMember(ctx, db, ana.ID); err != nil {
65 t.Fatal(err)
66 }
67 var n int
68 if err := db.QueryRow(`SELECT count(*) FROM equipo_links`).Scan(&n); err != nil || n != 0 {
69 t.Fatalf("links left: %d %v", n, err)
70 }
71}
72
73func pendingLink(t *testing.T, db *sql.DB, device string) Member {
74 t.Helper()
75 m, _, err := Invite(context.Background(), db, device+"@example.com")
76 if err != nil {
77 t.Fatal(err)
78 }
79 if err := SaveEquipoLink(context.Background(), db, EquipoLink{MemberID: m.ID, Origin: "https://equipo.test",
80 State: EquipoPending, DeviceID: device, Sealed: []byte("x"), ExpiresAt: time.Now().Add(time.Minute)}); err != nil {
81 t.Fatal(err)
82 }
83 return m
84}
85
86// Two polls at once: exactly one claims the right to ask Equipo, and no
87// one may ask again inside the interval.
88func TestClaimEquipoPollOnce(t *testing.T) {
89 ctx, db := context.Background(), open(t)
90 m := pendingLink(t, db, "D1")
91 now := time.Now()
92 first, err := ClaimEquipoPoll(ctx, db, m.ID, "D1", now, 5*time.Second, 30*time.Second)
93 if err != nil || !first {
94 t.Fatalf("first claim: %v %v", first, err)
95 }
96 if again, _ := ClaimEquipoPoll(ctx, db, m.ID, "D1", now, 5*time.Second, 30*time.Second); again {
97 t.Fatal("a second poll claimed inside the interval")
98 }
99 if err := ReleaseEquipoPoll(ctx, db, m.ID, "D1"); err != nil {
100 t.Fatal(err)
101 }
102 if later, _ := ClaimEquipoPoll(ctx, db, m.ID, "D1", now.Add(6*time.Second), 5*time.Second, 30*time.Second); !later {
103 t.Fatal("no claim after the interval")
104 }
105 if other, _ := ClaimEquipoPoll(ctx, db, m.ID, "D2", now.Add(time.Hour), 5*time.Second, 30*time.Second); other {
106 t.Fatal("claimed for a device that is not the member's")
107 }
108}
109
110// Completing an exchange needs the same pending device still there: a
111// Disconnect (or a new Connect) in the meantime wins.
112func TestConnectEquipoLinkNeedsSamePendingDevice(t *testing.T) {
113 ctx, db := context.Background(), open(t)
114 m := pendingLink(t, db, "D1")
115 exp := time.Now().Add(time.Hour)
116 if err := DeleteEquipoLink(ctx, db, m.ID, "D1"); err != nil {
117 t.Fatal(err)
118 }
119 if ok, err := ConnectEquipoLink(ctx, db, m.ID, "D1", []byte("bearer"), exp, 3); ok || err != nil {
120 t.Fatalf("completed a disconnected link: %v %v", ok, err)
121 }
122 if _, found, _ := EquipoLinkFor(ctx, db, m.ID); found {
123 t.Fatal("completing brought the link back")
124 }
125 m2 := pendingLink(t, db, "D2")
126 if ok, _ := ConnectEquipoLink(ctx, db, m2.ID, "OLD", []byte("bearer"), exp, 3); ok {
127 t.Fatal("completed for a replaced device")
128 }
129 if ok, _ := ConnectEquipoLink(ctx, db, m2.ID, "D2", []byte("bearer"), exp, 3); !ok {
130 t.Fatal("did not complete the pending device")
131 }
132 l, _, _ := EquipoLinkFor(ctx, db, m2.ID)
133 if l.State != EquipoConnected || string(l.Sealed) != "bearer" || l.Revision != 3 || l.UserCode != "" {
134 t.Fatalf("link: %+v", l)
135 }
136 // Stale device ids change nothing.
137 if err := SetEquipoLinkState(ctx, db, m2.ID, "OLD", EquipoLost); err != nil {
138 t.Fatal(err)
139 }
140 if err := DeleteEquipoLink(ctx, db, m2.ID, "OLD"); err != nil {
141 t.Fatal(err)
142 }
143 if l, found, _ := EquipoLinkFor(ctx, db, m2.ID); !found || l.State != EquipoConnected {
144 t.Fatalf("a stale device id changed the link: %+v %v", l, found)
145 }
146}
147
148// A claim holds the link for the whole exchange, which may outlast the
149// poll interval: no one else claims until it is released or runs out.
150func TestClaimEquipoPollHeldDuringExchange(t *testing.T) {
151 ctx, db := context.Background(), open(t)
152 m := pendingLink(t, db, "D1")
153 now := time.Now()
154 if ok, err := ClaimEquipoPoll(ctx, db, m.ID, "D1", now, 5*time.Second, 30*time.Second); !ok || err != nil {
155 t.Fatalf("first claim: %v %v", ok, err)
156 }
157 if ok, _ := ClaimEquipoPoll(ctx, db, m.ID, "D1", now.Add(6*time.Second), 5*time.Second, 30*time.Second); ok {
158 t.Fatal("claimed while an exchange was in flight")
159 }
160 if ok, _ := ClaimEquipoPoll(ctx, db, m.ID, "D1", now.Add(31*time.Second), 5*time.Second, 30*time.Second); !ok {
161 t.Fatal("a claim that ran out still held the link")
162 }
163 if err := ReleaseEquipoPoll(ctx, db, m.ID, "D1"); err != nil {
164 t.Fatal(err)
165 }
166 if ok, _ := ClaimEquipoPoll(ctx, db, m.ID, "D1", now.Add(37*time.Second), 5*time.Second, 30*time.Second); !ok {
167 t.Fatal("no claim after release and the interval")
168 }
169}
170