| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | func putHost(h *abilitiesHarness, origin, token string) (wire.AssistantHost, error) { |
| 13 | return h.s.Lane(wire.LaneAssistant).SetHost(context.Background(), abPerson, wire.AssistantHost{Origin: origin, Token: token}) |
| 14 | } |
| 15 | |
| 16 | // hostOf is the person's registered origin and token. |
| 17 | func hostOf(t *testing.T, h *abilitiesHarness) (string, string) { |
| 18 | t.Helper() |
| 19 | origin, token, ok, err := h.s.Host(context.Background(), abPerson) |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | if !ok { |
| 24 | return "", "" |
| 25 | } |
| 26 | return origin, token |
| 27 | } |
| 28 | |
| 29 | // PUT host fetches the board with the offered token before it counts, |
| 30 | // and stores the board with the host. |
| 31 | func TestAbilitiesFetchOnHost(t *testing.T) { |
| 32 | base := startBoardBase(t, "host-token-0123456789abcdef") |
| 33 | h := newAbilitiesHarness(t, base.origin()) |
| 34 | base.set(func(f *fakeBoardBase) { f.board.Revision, f.board.Abilities["web"] = 3, false }) |
| 35 | if _, err := putHost(h, base.origin(), base.token); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if o, tok := hostOf(t, h); o != base.origin() || tok != base.token { |
| 39 | t.Errorf("host %q %q", o, tok) |
| 40 | } |
| 41 | r := h.row(t, abPerson, base.origin()) |
| 42 | if b, _ := r.board(); r.Revision != 3 || b.Abilities["web"] || r.ConfirmedAt == nil { |
| 43 | t.Errorf("board %+v", r) |
| 44 | } |
| 45 | if n, _ := base.counts(); n != 1 { |
| 46 | t.Errorf("board fetched %d times", n) |
| 47 | } |
| 48 | if got := h.audited(auditAssistantHost); len(got) != 1 { |
| 49 | t.Errorf("audits %v", got) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // A token Base doesn't honour, an error, or a 404 once a board was stored |
| 54 | // is refused host_unverified, and the earlier registration and board stay. |
| 55 | func TestHostUnverifiedRefused(t *testing.T) { |
| 56 | base := startBoardBase(t, "host-token-0123456789abcdef") |
| 57 | h := newAbilitiesHarness(t, base.origin()) |
| 58 | base.set(func(f *fakeBoardBase) { f.board.Abilities["build"] = false }) |
| 59 | if _, err := putHost(h, base.origin(), base.token); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | before := h.row(t, abPerson, base.origin()) |
| 63 | for name, c := range map[string]struct { |
| 64 | token string |
| 65 | setup func(f *fakeBoardBase) |
| 66 | }{ |
| 67 | "an invalid token": {"forged-token-0123456789abc", func(*fakeBoardBase) {}}, |
| 68 | "a 401": {"other-token-0123456789abcd", func(f *fakeBoardBase) {}}, |
| 69 | "a 5xx": {base.token, func(f *fakeBoardBase) { f.status = 503 }}, |
| 70 | "a 404 after a board": {base.token, func(f *fakeBoardBase) { |
| 71 | f.status = 404 |
| 72 | }}, |
| 73 | } { |
| 74 | base.set(c.setup) |
| 75 | if _, err := putHost(h, base.origin(), c.token); !errors.Is(err, ErrHostUnverified) { |
| 76 | t.Errorf("%s: %v, want ErrHostUnverified", name, err) |
| 77 | } |
| 78 | base.set(func(f *fakeBoardBase) { f.status = 200 }) |
| 79 | if o, tok := hostOf(t, h); o != base.origin() || tok != base.token { |
| 80 | t.Errorf("%s: the registration moved: %q %q", name, o, tok) |
| 81 | } |
| 82 | if r := h.row(t, abPerson, base.origin()); r.Abilities != before.Abilities || r.RevokedAt != nil { |
| 83 | t.Errorf("%s: the board changed: %+v", name, r) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // An old Base (board 404, operations 200) is accepted, with the defaults |
| 89 | // stored as legacy. |
| 90 | func TestHostLegacyAccepted(t *testing.T) { |
| 91 | base := startBoardBase(t, "host-token-0123456789abcdef") |
| 92 | base.set(func(f *fakeBoardBase) { f.status = 404 }) |
| 93 | h := newAbilitiesHarness(t, base.origin()) |
| 94 | if _, err := putHost(h, base.origin(), base.token); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | r := h.row(t, abPerson, base.origin()) |
| 98 | b, _ := r.board() |
| 99 | if r.Source != SourceLegacy || r.AuthoritativeSeen || len(b.Abilities) != len(wire.Abilities) { |
| 100 | t.Errorf("legacy row %+v %+v", r, b) |
| 101 | } |
| 102 | for _, k := range wire.Abilities { |
| 103 | if !b.Abilities[k] { |
| 104 | t.Errorf("%s off in the defaults", k) |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // One Base at a time: with two allowed origins, moving while the first |
| 110 | // still honours the person is refused host_elsewhere; after its 401, or |
| 111 | // once the operator takes it off the list, the move succeeds. Each time |
| 112 | // the person keeps one copy Equipo can refresh past 10 minutes. |
| 113 | func TestHostElsewhereRefused(t *testing.T) { |
| 114 | a := startBoardBase(t, "token-a-0123456789abcdefg") |
| 115 | b := startBoardBase(t, "token-b-0123456789abcdefg") |
| 116 | h := newAbilitiesHarness(t, a.origin(), b.origin()) |
| 117 | if _, err := putHost(h, a.origin(), a.token); err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | if _, err := putHost(h, b.origin(), b.token); !errors.Is(err, ErrHostElsewhere) { |
| 121 | t.Fatalf("moving while A honours them: %v", err) |
| 122 | } |
| 123 | if o, _ := hostOf(t, h); o != a.origin() { |
| 124 | t.Fatalf("host moved to %q", o) |
| 125 | } |
| 126 | // A answers 401 (the member left A): the move succeeds. |
| 127 | a.set(func(f *fakeBoardBase) { f.token = "gone-0123456789abcdefghij" }) |
| 128 | if _, _, err := h.s.RefreshAbilities(context.Background(), abPerson); err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | if _, err := putHost(h, b.origin(), b.token); err != nil { |
| 132 | t.Fatalf("moving after A's 401: %v", err) |
| 133 | } |
| 134 | h.advance(11 * time.Minute) |
| 135 | if _, fresh, err := h.s.RefreshAbilities(context.Background(), abPerson); err != nil || !fresh { |
| 136 | t.Errorf("B's copy not refreshable: %v %v", fresh, err) |
| 137 | } |
| 138 | |
| 139 | // The operator takes B off the list: the person may move to A again. |
| 140 | a.set(func(f *fakeBoardBase) { f.token = "token-a-0123456789abcdefg" }) |
| 141 | h2 := h.withHosts(a.origin()) |
| 142 | if _, err := h2.s.Lane(wire.LaneAssistant).SetHost(context.Background(), abPerson, wire.AssistantHost{Origin: a.origin(), Token: a.token}); err != nil { |
| 143 | t.Fatalf("moving after B left the list: %v", err) |
| 144 | } |
| 145 | h2.advance(11 * time.Minute) |
| 146 | if _, fresh, err := h2.s.RefreshAbilities(context.Background(), abPerson); err != nil || !fresh { |
| 147 | t.Errorf("A's copy not refreshable: %v %v", fresh, err) |
| 148 | } |
| 149 | } |
| 150 | |