Files

61 lines
1.7 KiB
Go
Raw Permalink Normal View History

package onboarding
import (
"data-terminal-cloud/internal/identity"
"github.com/google/uuid"
"testing"
)
func TestBLEPlaintextBindingState(t *testing.T) {
f := newFixture(t)
in := BLEDeviceInput{DeviceID: f.d.FactoryID}
state, err := f.s.BLEDeviceState(f.ctx, f.user, in)
must(t, err)
if state["bound"] != false || state["id"] != f.d.PublicID {
t.Fatal(state)
}
device, err := f.s.DeviceBLEState(f.ctx, f.d)
must(t, err)
if device["bound"] != false {
t.Fatal(device)
}
_, err = f.s.BindBLE(f.ctx, f.d, uuid.NewString(), BLEBindInput{Token: ""})
if err == nil {
t.Fatal("empty token must not create a binding")
}
_, err = f.s.BLEDeviceState(f.ctx, identity.Principal{Issuer: f.user.Issuer, Subject: uuid.NewString()}, in)
if err == nil {
t.Fatal("unrelated account read binding state")
}
token := f.bleToken()
_, err = f.s.BindBLE(f.ctx, f.d, uuid.NewString(), BLEBindInput{Token: token})
must(t, err)
f.refresh()
generation := f.d.Generation
state, err = f.s.BLEDeviceState(f.ctx, f.user, in)
must(t, err)
if state["bound"] != true {
t.Fatal(state)
}
for i := 0; i < 3; i++ {
device, err = f.s.DeviceBLEState(f.ctx, f.d)
must(t, err)
if device["bound"] != true {
t.Fatal(device)
}
}
f.refresh()
if f.d.Generation != generation {
t.Fatal("WiFi checks modified ownership")
}
_, err = f.s.pool.Exec(f.ctx, `UPDATE device_binding SET unbound_at=now() WHERE device_id=$1::uuid AND unbound_at IS NULL`, f.d.ID)
must(t, err)
f.web.Hash = "" // Unbinding intentionally expires the fixture's old setup session.
f.refresh()
device, err = f.s.DeviceBLEState(f.ctx, f.d)
must(t, err)
if device["bound"] != false {
t.Fatal("stale bound state after unbind", device)
}
}