| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "log/slog" |
| 8 | "net/http" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/rastrillo/rastrillo/db" |
| 13 | "amadan.net/rastrillo/rastrillo/dbtest" |
| 14 | ) |
| 15 | |
| 16 | // probeRouter builds the full router over a fresh database, bootstrapping |
| 17 | // the configured owners only when bootstrap is set. |
| 18 | func probeRouter(t *testing.T, env map[string]string, bootstrap bool) (*Store, Config, http.Handler) { |
| 19 | t.Helper() |
| 20 | schema := dbtest.FromSet(BootSchema) |
| 21 | t.Cleanup(schema.Remove) |
| 22 | d, err := db.Open(schema.Path(t), slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | t.Cleanup(func() { d.Close() }) |
| 27 | cfg, err := LoadConfig(func(k string) string { return env[k] }) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | s := NewStore(d) |
| 32 | if bootstrap { |
| 33 | if err = s.Bootstrap(context.Background(), cfg.Owners); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | } |
| 37 | h, err := RouterWithStore(d, cfg, agentMail{}, nil, s) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | return s, cfg, h |
| 42 | } |
| 43 | |
| 44 | func TestTeamProbe(t *testing.T) { |
| 45 | for _, tc := range []struct { |
| 46 | name string |
| 47 | bootstrap bool |
| 48 | roster string |
| 49 | }{ |
| 50 | // The router seeds EQUIPO_OWNERS at boot, so an instance that |
| 51 | // answers at all has its owners; owner-pending is the state |
| 52 | // after every owner is deactivated (below). |
| 53 | {"seeded", true, "ok"}, |
| 54 | } { |
| 55 | t.Run(tc.name, func(t *testing.T) { |
| 56 | env := agentEnv(t) |
| 57 | env["EQUIPO_HOME_CONSUMER_ID"] = "campbells-equipo" |
| 58 | _, _, h := probeRouter(t, env, tc.bootstrap) |
| 59 | w := agentRequest(h, "GET", "/api/team", "", "") |
| 60 | if w.Code != 200 || w.Header().Get("Content-Type") != "application/json" || w.Header().Get("Cache-Control") != "no-store" { |
| 61 | t.Fatalf("%d %v %s", w.Code, w.Header(), w.Body) |
| 62 | } |
| 63 | if strings.Contains(w.Body.String(), "@") { |
| 64 | t.Fatalf("probe carries an address: %s", w.Body) |
| 65 | } |
| 66 | var got map[string]any |
| 67 | if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | want := map[string]any{"team_mode": false, "seeded": true, "consumer": "campbells-equipo", "roster": tc.roster} |
| 71 | if len(got) != len(want) { |
| 72 | t.Fatalf("fields %v", got) |
| 73 | } |
| 74 | for k, v := range want { |
| 75 | if got[k] != v { |
| 76 | t.Fatalf("%s = %v, want %v (%s)", k, got[k], v, w.Body) |
| 77 | } |
| 78 | } |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestTeamProbeAfterOwnersLeave(t *testing.T) { |
| 84 | env := agentEnv(t) |
| 85 | s, _, h := probeRouter(t, env, true) |
| 86 | if err := s.db.G.Model(&Member{}).Where("role = ?", RoleOwner).Update("active", false).Error; err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | w := agentRequest(h, "GET", "/api/team", "", "") |
| 90 | if w.Code != 200 || !strings.Contains(w.Body.String(), `"roster":"owner-pending"`) { |
| 91 | t.Fatalf("%d %s", w.Code, w.Body) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestTeamProbeWithoutHome(t *testing.T) { |
| 96 | env := agentEnv(t) |
| 97 | for k := range env { |
| 98 | if strings.HasPrefix(k, "EQUIPO_HOME_") || strings.HasPrefix(k, "EQUIPO_AGENT_") { |
| 99 | delete(env, k) |
| 100 | } |
| 101 | } |
| 102 | _, _, h := probeRouter(t, env, true) |
| 103 | if w := agentRequest(h, "GET", "/api/team", "", ""); w.Code != 404 { |
| 104 | t.Fatalf("%d %s", w.Code, w.Body) |
| 105 | } |
| 106 | } |
| 107 | |