package store import ( "context" "database/sql" "testing" "time" ) func TestEquipoLinkRoundTrip(t *testing.T) { ctx, db := context.Background(), open(t) ana, _, err := Invite(ctx, db, "ana@example.com") if err != nil { t.Fatal(err) } if _, ok, err := EquipoLinkFor(ctx, db, ana.ID); ok || err != nil { t.Fatalf("fresh member has a link: %v %v", ok, err) } exp := time.Now().Add(10 * time.Minute).UTC().Truncate(time.Second) in := EquipoLink{MemberID: ana.ID, Origin: "https://equipo.test", State: EquipoPending, DeviceID: "DEV1", UserCode: "ABCD1234", VerificationURI: "https://equipo.test/auth/devices/DEV1", Sealed: []byte("sealed"), ExpiresAt: exp} if err := SaveEquipoLink(ctx, db, in); err != nil { t.Fatal(err) } got, ok, err := EquipoLinkFor(ctx, db, ana.ID) if !ok || err != nil || got.DeviceID != "DEV1" || got.State != EquipoPending || !got.ExpiresAt.Equal(exp) || string(got.Sealed) != "sealed" || got.PolledAt != nil { t.Fatalf("got %+v %v %v", got, ok, err) } got.State, got.Sealed, got.Revision = EquipoConnected, []byte("token"), 3 if err := SaveEquipoLink(ctx, db, got); err != nil { t.Fatal(err) } got, _, _ = EquipoLinkFor(ctx, db, ana.ID) if got.State != EquipoConnected || string(got.Sealed) != "token" || got.Revision != 3 { t.Fatalf("replace: %+v", got) } if err := SetEquipoLinkState(ctx, db, ana.ID, "DEV1", EquipoLost); err != nil { t.Fatal(err) } if got, _, _ = EquipoLinkFor(ctx, db, ana.ID); got.State != EquipoLost { t.Fatalf("state: %+v", got) } if err := DeleteEquipoLink(ctx, db, ana.ID, "DEV1"); err != nil { t.Fatal(err) } if _, ok, _ := EquipoLinkFor(ctx, db, ana.ID); ok { t.Fatal("link survived delete") } } // A member's Equipo link goes with the member. func TestRemoveMemberDeletesEquipoLink(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 := SaveEquipoLink(ctx, db, EquipoLink{MemberID: ana.ID, Origin: "https://equipo.test", State: EquipoConnected, DeviceID: "D", Sealed: []byte("x"), ExpiresAt: time.Now()}); err != nil { t.Fatal(err) } if err := RemoveMember(ctx, db, ana.ID); err != nil { t.Fatal(err) } var n int if err := db.QueryRow(`SELECT count(*) FROM equipo_links`).Scan(&n); err != nil || n != 0 { t.Fatalf("links left: %d %v", n, err) } } func pendingLink(t *testing.T, db *sql.DB, device string) Member { t.Helper() m, _, err := Invite(context.Background(), db, device+"@example.com") if err != nil { t.Fatal(err) } if err := SaveEquipoLink(context.Background(), db, EquipoLink{MemberID: m.ID, Origin: "https://equipo.test", State: EquipoPending, DeviceID: device, Sealed: []byte("x"), ExpiresAt: time.Now().Add(time.Minute)}); err != nil { t.Fatal(err) } return m } // Two polls at once: exactly one claims the right to ask Equipo, and no // one may ask again inside the interval. func TestClaimEquipoPollOnce(t *testing.T) { ctx, db := context.Background(), open(t) m := pendingLink(t, db, "D1") now := time.Now() first, err := ClaimEquipoPoll(ctx, db, m.ID, "D1", now, 5*time.Second, 30*time.Second) if err != nil || !first { t.Fatalf("first claim: %v %v", first, err) } if again, _ := ClaimEquipoPoll(ctx, db, m.ID, "D1", now, 5*time.Second, 30*time.Second); again { t.Fatal("a second poll claimed inside the interval") } if err := ReleaseEquipoPoll(ctx, db, m.ID, "D1"); err != nil { t.Fatal(err) } if later, _ := ClaimEquipoPoll(ctx, db, m.ID, "D1", now.Add(6*time.Second), 5*time.Second, 30*time.Second); !later { t.Fatal("no claim after the interval") } if other, _ := ClaimEquipoPoll(ctx, db, m.ID, "D2", now.Add(time.Hour), 5*time.Second, 30*time.Second); other { t.Fatal("claimed for a device that is not the member's") } } // Completing an exchange needs the same pending device still there: a // Disconnect (or a new Connect) in the meantime wins. func TestConnectEquipoLinkNeedsSamePendingDevice(t *testing.T) { ctx, db := context.Background(), open(t) m := pendingLink(t, db, "D1") exp := time.Now().Add(time.Hour) if err := DeleteEquipoLink(ctx, db, m.ID, "D1"); err != nil { t.Fatal(err) } if ok, err := ConnectEquipoLink(ctx, db, m.ID, "D1", []byte("bearer"), exp, 3); ok || err != nil { t.Fatalf("completed a disconnected link: %v %v", ok, err) } if _, found, _ := EquipoLinkFor(ctx, db, m.ID); found { t.Fatal("completing brought the link back") } m2 := pendingLink(t, db, "D2") if ok, _ := ConnectEquipoLink(ctx, db, m2.ID, "OLD", []byte("bearer"), exp, 3); ok { t.Fatal("completed for a replaced device") } if ok, _ := ConnectEquipoLink(ctx, db, m2.ID, "D2", []byte("bearer"), exp, 3); !ok { t.Fatal("did not complete the pending device") } l, _, _ := EquipoLinkFor(ctx, db, m2.ID) if l.State != EquipoConnected || string(l.Sealed) != "bearer" || l.Revision != 3 || l.UserCode != "" { t.Fatalf("link: %+v", l) } // Stale device ids change nothing. if err := SetEquipoLinkState(ctx, db, m2.ID, "OLD", EquipoLost); err != nil { t.Fatal(err) } if err := DeleteEquipoLink(ctx, db, m2.ID, "OLD"); err != nil { t.Fatal(err) } if l, found, _ := EquipoLinkFor(ctx, db, m2.ID); !found || l.State != EquipoConnected { t.Fatalf("a stale device id changed the link: %+v %v", l, found) } } // A claim holds the link for the whole exchange, which may outlast the // poll interval: no one else claims until it is released or runs out. func TestClaimEquipoPollHeldDuringExchange(t *testing.T) { ctx, db := context.Background(), open(t) m := pendingLink(t, db, "D1") now := time.Now() if ok, err := ClaimEquipoPoll(ctx, db, m.ID, "D1", now, 5*time.Second, 30*time.Second); !ok || err != nil { t.Fatalf("first claim: %v %v", ok, err) } if ok, _ := ClaimEquipoPoll(ctx, db, m.ID, "D1", now.Add(6*time.Second), 5*time.Second, 30*time.Second); ok { t.Fatal("claimed while an exchange was in flight") } if ok, _ := ClaimEquipoPoll(ctx, db, m.ID, "D1", now.Add(31*time.Second), 5*time.Second, 30*time.Second); !ok { t.Fatal("a claim that ran out still held the link") } if err := ReleaseEquipoPoll(ctx, db, m.ID, "D1"); err != nil { t.Fatal(err) } if ok, _ := ClaimEquipoPoll(ctx, db, m.ID, "D1", now.Add(37*time.Second), 5*time.Second, 30*time.Second); !ok { t.Fatal("no claim after release and the interval") } }