| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/personapi" |
| 5 | "fmt" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestFinalFixDeviceExchangeRevision(t *testing.T) { |
| 10 | f := newFixture(t) |
| 11 | c, key := deviceChallenge(t, f) |
| 12 | expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/auth/devices/"+c.ID+"/approve", fmt.Sprintf(`{"typed_code":%q}`, c.UserCode), "approve", `"1"`), 200) |
| 13 | f.exec(t, "UPDATE person_devices SET revision=17 WHERE id=?", c.ID) |
| 14 | w := deviceRequest(t, f, "", "POST", "/api/v1/auth/devices/exchange", signedDevice(t, f, c, key), "exchange", "") |
| 15 | expectHTTP(t, w, 200) |
| 16 | if w.Header().Get("ETag") != `"18"` { |
| 17 | t.Fatal("synthetic exchange revision", w.Header().Get("ETag")) |
| 18 | } |
| 19 | } |
| 20 | func TestFinalFixAckRevision(t *testing.T) { |
| 21 | f := newFixture(t) |
| 22 | c := approvedDevice(t, f) |
| 23 | path := "/api/v1/auth/devices/" + c.ID |
| 24 | f.exec(t, "CREATE TRIGGER advance_device_revision AFTER UPDATE ON person_devices WHEN NEW.revoked_at IS NOT NULL AND OLD.revoked_at IS NULL BEGIN UPDATE person_devices SET revision=revision+5 WHERE id=NEW.id; END") |
| 25 | w := apiRequest(t, f, owner, "DELETE", path, `{}`, "revoke", `"3"`) |
| 26 | expectHTTP(t, w, 200) |
| 27 | actual := f.count(t, "SELECT revision FROM person_devices WHERE id=?", c.ID) |
| 28 | if w.Header().Get("ETag") != fmt.Sprintf(`"%d"`, actual) || apiValue[personapi.Ack](t, w).Revision != actual { |
| 29 | t.Fatal("synthetic ack revision", w.Header(), w.Body.String(), actual) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestFinalFixAckSelfDemotion(t *testing.T) { |
| 34 | f := newFixture(t) |
| 35 | revision := f.count(t, "SELECT revision FROM members WHERE address=?", owner.Address) |
| 36 | w := apiRequest(t, f, owner, "PUT", "/api/v1/roster/"+owner.Address, `{"role":"member","active":true}`, "demote", fmt.Sprintf(`"%d"`, revision)) |
| 37 | expectHTTP(t, w, 200) |
| 38 | actual := f.count(t, "SELECT revision FROM members WHERE address=?", owner.Address) |
| 39 | if apiValue[personapi.Ack](t, w).Revision != actual { |
| 40 | t.Fatal("wrong self-demotion revision") |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestFinalFixAckReadback(t *testing.T) { |
| 45 | f, _ := modelChoiceFixture(t) |
| 46 | f.exec(t, "CREATE TRIGGER connection_revision_jump AFTER UPDATE ON connections WHEN NEW.label IS NOT OLD.label BEGIN UPDATE connections SET revision=revision+5 WHERE id=NEW.id; END") |
| 47 | w := apiRequest(t, f, owner, "PATCH", "/api/v1/connections/claude", `{"label":"Renamed account"}`, "rename", `"1"`) |
| 48 | expectHTTP(t, w, 200) |
| 49 | actual := f.count(t, "SELECT revision FROM connections WHERE id='claude'") |
| 50 | if apiValue[personapi.Ack](t, w).Revision != actual || w.Header().Get("ETag") != fmt.Sprintf(`"%d"`, actual) { |
| 51 | t.Fatal("ack did not read the stored revision", w.Body.String(), actual) |
| 52 | } |
| 53 | } |
| 54 | |