| 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 allOn(t *testing.T, sw Switches, want bool) { |
| 13 | t.Helper() |
| 14 | for _, k := range wire.Abilities { |
| 15 | if sw.Can(k) != want { |
| 16 | t.Errorf("%s: Can %v, want %v", k, sw.Can(k), want) |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | // With no EQUIPO_ASSISTANT_HOSTS (a test instance), every key is on and |
| 22 | // an Equipo owner administers. |
| 23 | func TestSwitchesDefaultsWithoutHosts(t *testing.T) { |
| 24 | h := newAbilitiesHarness(t) |
| 25 | allOn(t, h.switchesOf(t, "owner@example.com"), true) |
| 26 | if !h.switchesOf(t, "owner@example.com").Admin() || h.switchesOf(t, abPerson).Admin() { |
| 27 | t.Error("admin is not the Equipo owner") |
| 28 | } |
| 29 | if off := h.switchesOf(t, abPerson).Off(); len(off) != 0 { |
| 30 | t.Errorf("off %v", off) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // With EQUIPO_ASSISTANT_HOSTS set, a person with no verified Base host |
| 35 | // has everything switched off, as unavailable. |
| 36 | func TestNoVerifiedHostNothingSwitched(t *testing.T) { |
| 37 | base := startBoardBase(t, "host-token-0123456789abcdef") |
| 38 | h := newAbilitiesHarness(t, base.origin()) |
| 39 | sw := h.switchesOf(t, abPerson) |
| 40 | allOn(t, sw, false) |
| 41 | if code := sw.Refusal(wire.AbilityWeb); code != "unavailable" { |
| 42 | t.Errorf("refusal %q", code) |
| 43 | } |
| 44 | // A host registered but never verified (no board) is the same. |
| 45 | h.register(t, abPerson, base.origin(), base.token) |
| 46 | allOn(t, h.switchesOf(t, abPerson), false) |
| 47 | if _, _, err := h.s.RefreshAbilities(context.Background(), abPerson); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | allOn(t, h.switchesOf(t, abPerson), true) |
| 51 | } |
| 52 | |
| 53 | // Base unreachable for more than 10 minutes past the last answer that |
| 54 | // counted: everything switched is refused until a fetch succeeds. |
| 55 | func TestAbilitiesExpireAfter10Minutes(t *testing.T) { |
| 56 | h, base := boardBase(t) |
| 57 | refresh(t, h) |
| 58 | h.advance(10 * time.Minute) |
| 59 | allOn(t, h.switchesOf(t, abPerson), true) |
| 60 | base.set(func(f *fakeBoardBase) { f.status = 503 }) |
| 61 | refresh(t, h) |
| 62 | h.advance(time.Second) |
| 63 | sw := h.switchesOf(t, abPerson) |
| 64 | allOn(t, sw, false) |
| 65 | if code := sw.Refusal(wire.AbilityBuild); code != "unavailable" { |
| 66 | t.Errorf("refusal %q", code) |
| 67 | } |
| 68 | base.set(func(f *fakeBoardBase) { f.status = 200 }) |
| 69 | refresh(t, h) |
| 70 | allOn(t, h.switchesOf(t, abPerson), true) |
| 71 | } |
| 72 | |
| 73 | // The operator's fixed keys narrow for everyone: web=off for the admin |
| 74 | // too, services_add=owners for the admin only. |
| 75 | func TestAbilitiesOperatorFixed(t *testing.T) { |
| 76 | h, base := boardBase(t) |
| 77 | fixed, err := ParseFixed("web=off, services_add=owners") |
| 78 | if err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | h.s.SetFixed(fixed) |
| 82 | refresh(t, h) |
| 83 | sw := h.switchesOf(t, abPerson) |
| 84 | if sw.Can(wire.AbilityWeb) || sw.Can(wire.AbilityServicesAdd) || !sw.Can(wire.AbilityServices) || sw.Refusal(wire.AbilityWeb) != wire.CodeAbilityOff { |
| 85 | t.Errorf("member: web %v services_add %v", sw.Can(wire.AbilityWeb), sw.Can(wire.AbilityServicesAdd)) |
| 86 | } |
| 87 | base.set(func(f *fakeBoardBase) { f.board.Admin, f.board.Revision = true, 2 }) |
| 88 | refresh(t, h) |
| 89 | if sw := h.switchesOf(t, abPerson); sw.Can(wire.AbilityWeb) || !sw.Can(wire.AbilityServicesAdd) { |
| 90 | t.Errorf("admin: web %v services_add %v", sw.Can(wire.AbilityWeb), sw.Can(wire.AbilityServicesAdd)) |
| 91 | } |
| 92 | for _, bad := range []string{"web=on", "future=off", "web", "web=off,web=owners", "=off"} { |
| 93 | if _, err := ParseFixed(bad); err == nil { |
| 94 | t.Errorf("%q accepted", bad) |
| 95 | } |
| 96 | } |
| 97 | if f, err := ParseFixed(""); err != nil || len(f) != 0 { |
| 98 | t.Errorf("empty: %v %v", f, err) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // apps_write needs apps_read, and services_add needs services. |
| 103 | func TestAppsWriteImpliesRead(t *testing.T) { |
| 104 | h, base := boardBase(t) |
| 105 | base.set(func(f *fakeBoardBase) { f.board.Abilities["apps_read"], f.board.Abilities["services"] = false, false }) |
| 106 | refresh(t, h) |
| 107 | sw := h.switchesOf(t, abPerson) |
| 108 | if sw.Can(wire.AbilityAppsWrite) || sw.Can(wire.AbilityServicesAdd) || !sw.Can(wire.AbilityBuild) { |
| 109 | t.Errorf("apps_write %v services_add %v", sw.Can(wire.AbilityAppsWrite), sw.Can(wire.AbilityServicesAdd)) |
| 110 | } |
| 111 | if got := sw.Off(); len(got) != 4 || got[0] != wire.AbilityAppsRead || got[1] != wire.AbilityAppsWrite { |
| 112 | t.Errorf("off %v", got) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Switches are read in the caller's transaction: a board update made |
| 117 | // meanwhile is not seen until it commits after; and a read error denies. |
| 118 | func TestSwitchesReadInsideTx(t *testing.T) { |
| 119 | h, base := boardBase(t) |
| 120 | refresh(t, h) |
| 121 | updated := make(chan error, 1) |
| 122 | err := h.s.tx(context.Background(), func(tx *txn) error { |
| 123 | first, err := tx.switches(abPerson) |
| 124 | if err != nil { |
| 125 | return err |
| 126 | } |
| 127 | go func() { |
| 128 | base.set(func(f *fakeBoardBase) { f.board.Abilities["build"], f.board.Revision = false, 2 }) |
| 129 | _, _, err := h.s.RefreshAbilities(context.Background(), abPerson) |
| 130 | updated <- err |
| 131 | }() |
| 132 | time.Sleep(100 * time.Millisecond) |
| 133 | second, err := tx.switches(abPerson) |
| 134 | if err != nil { |
| 135 | return err |
| 136 | } |
| 137 | if first.Can(wire.AbilityBuild) != second.Can(wire.AbilityBuild) || !second.Can(wire.AbilityBuild) { |
| 138 | t.Errorf("the update showed inside the transaction") |
| 139 | } |
| 140 | return nil |
| 141 | }) |
| 142 | if err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | if err := <-updated; err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | if h.switchesOf(t, abPerson).Can(wire.AbilityBuild) { |
| 149 | t.Error("the update didn't apply after") |
| 150 | } |
| 151 | ctx, cancel := context.WithCancel(context.Background()) |
| 152 | cancel() |
| 153 | tx := &txn{s: h.s, ctx: ctx, db: h.s.db.G.WithContext(ctx)} |
| 154 | sw, err := tx.switches(abPerson) |
| 155 | if err == nil || sw.Can(wire.AbilityWeb) { |
| 156 | t.Errorf("a read error: %v, web %v", err, sw.Can(wire.AbilityWeb)) |
| 157 | } |
| 158 | if !errors.Is(err, context.Canceled) { |
| 159 | t.Logf("read error %v", err) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Draws comes from Base-Capabilities and only decides presentation: it |
| 164 | // never makes Can true. |
| 165 | func TestCapableButNotAllowed(t *testing.T) { |
| 166 | h, base := boardBase(t) |
| 167 | base.set(func(f *fakeBoardBase) { f.board.Abilities["build"] = false }) |
| 168 | refresh(t, h) |
| 169 | if err := h.s.SetCapabilities(context.Background(), abPerson, "job_actions,lookup_answers,future"); err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | sw := h.switchesOf(t, abPerson) |
| 173 | if !sw.Draws(wire.CapJobActions) || !sw.Draws(wire.CapLookupAnswers) || sw.Draws("future") || sw.Can(wire.AbilityBuild) { |
| 174 | t.Errorf("draws %v can %v", sw.Draws(wire.CapJobActions), sw.Can(wire.AbilityBuild)) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Capabilities are recorded for a person who never registered a host. |
| 179 | func TestCapabilityRecordedWithoutPutHost(t *testing.T) { |
| 180 | h := newAbilitiesHarness(t, "https://base.example.com") |
| 181 | if err := h.s.SetCapabilities(context.Background(), abPerson, wire.CapLookupAnswers); err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | if sw := h.switchesOf(t, abPerson); !sw.Draws(wire.CapLookupAnswers) || sw.Can(wire.AbilityWeb) { |
| 185 | t.Errorf("draws %v can %v", sw.Draws(wire.CapLookupAnswers), sw.Can(wire.AbilityWeb)) |
| 186 | } |
| 187 | if _, _, ok, err := h.s.Host(context.Background(), abPerson); ok || err != nil { |
| 188 | t.Errorf("a host appeared: %v %v", ok, err) |
| 189 | } |
| 190 | if err := h.s.SetCapabilities(context.Background(), abPerson, ""); err != nil { |
| 191 | t.Fatal(err) |
| 192 | } |
| 193 | if h.switchesOf(t, abPerson).Draws(wire.CapLookupAnswers) { |
| 194 | t.Error("an empty header kept the capability") |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // A person no longer on the roster has nothing switched, with or without |
| 199 | // Base's hosts, whatever their stored copy says (Astra phase 2, P1-3). |
| 200 | func TestSwitchesDenyRemovedMember(t *testing.T) { |
| 201 | allOn(t, newAbilitiesHarness(t).switchesOf(t, goneMember), false) |
| 202 | base := startBoardBase(t, "host-token-0123456789abcdef") |
| 203 | h := newAbilitiesHarness(t, base.origin()) |
| 204 | h.register(t, goneMember, base.origin(), base.token) |
| 205 | if err := h.s.tx(context.Background(), func(tx *txn) error { |
| 206 | _, err := tx.applyBoard(goneMember, base.origin(), 1, wire.AbilityBoard{Revision: 1, Abilities: defaultBoard()}, fetchOK) |
| 207 | return err |
| 208 | }); err != nil { |
| 209 | t.Fatal(err) |
| 210 | } |
| 211 | allOn(t, h.switchesOf(t, goneMember), false) |
| 212 | } |
| 213 | |