package main import ( "bytes" "context" "encoding/json" "errors" "flag" "io" "net/http" "net/http/httptest" "os" "path/filepath" "reflect" "strings" "testing" "github.com/carlosframework/platform/internal/plans" ) func TestAccountsPlanParsesPacks(t *testing.T) { var p packFlags for _, v := range []string{"apps=2", "email=1"} { if err := p.Set(v); err != nil { t.Fatalf("Set(%q): %v", v, err) } } if !reflect.DeepEqual(map[string]int(p), map[string]int{"apps": 2, "email": 1}) { t.Fatalf("packs %v", p) } for _, bad := range []string{"apps", "apps=x", "=2", "apps=2=3"} { var q packFlags if err := q.Set(bad); err == nil { t.Errorf("Set(%q) accepted", bad) } } } // planServer answers the plan door with reply and records each request. func planServer(t *testing.T, reply accountPlanReply) (*httptest.Server, *[]string, *[]setAccountPlanRequest) { t.Helper() var paths []string var bodies []setAccountPlanRequest ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { paths = append(paths, r.Method+" "+r.URL.RequestURI()) if r.Method == http.MethodPost { var req setAccountPlanRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { t.Errorf("decode request: %v", err) } bodies = append(bodies, req) } json.NewEncoder(w).Encode(reply) })) t.Cleanup(ts.Close) return ts, &paths, &bodies } func TestAccountsPlanShowAndSet(t *testing.T) { reply := accountPlanReply{ Account: "bac", Plan: "personal", Packs: map[string]int{"apps": 2}, Source: "operator", Reason: "upgraded", UpdatedBy: "ops@example.com", Revision: 2, PublishedRevision: 2, Limits: &plans.Limits{RunningThings: 35, IdleSeconds: 900, MemoryTier: "default", DiskBytes: 10 << 30, EmailPerDay: 1000, CustomDomains: true, HistoryDays: 7}, } ts, paths, bodies := planServer(t, reply) s := newTestSession(ts.URL) var out bytes.Buffer if err := runAccountsPlanWithSession(context.Background(), s, []string{"--account", "bac"}, &out); err != nil { t.Fatal(err) } text := out.String() for _, want := range []string{"bac", "personal", "apps 2", "35", "15 minutes", "10 GB", "1000", "7 days", "ops@example.com", "upgraded"} { if !strings.Contains(text, want) { t.Errorf("show output lacks %q:\n%s", want, text) } } sponsored := filepath.Join(t.TempDir(), "limits.json") if err := os.WriteFile(sponsored, []byte(`{"running_things": 40}`), 0o600); err != nil { t.Fatal(err) } args := []string{"--account", "bac", "--set", "personal", "--pack", "apps=2", "--sponsored", sponsored, "--reason", "existing"} if err := runAccountsPlanWithSession(context.Background(), s, args, io.Discard); err != nil { t.Fatal(err) } want := []string{"GET /api/cli/accounts/bac/plan", "POST /api/cli/accounts/bac/plan"} if !reflect.DeepEqual(*paths, want) { t.Fatalf("paths %v, want %v", *paths, want) } got := (*bodies)[0] if got.Plan != "personal" || got.Reason != "existing" || got.Packs["apps"] != 2 || got.Sponsored == nil || got.Sponsored.RunningThings != 40 { t.Fatalf("request %+v", got) } } func TestAccountsPlanValidationStopsBeforeNetwork(t *testing.T) { hits := 0 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { hits++ })) defer ts.Close() s := newTestSession(ts.URL) missing := filepath.Join(t.TempDir(), "missing.json") for _, args := range [][]string{ {}, // no account {"--account", "bac", "--set", "free"}, // no reason {"--account", "bac", "--pack", "apps=1"}, // pack without set {"--account", "bac", "--reason", "r"}, // reason without set {"--account", "bac", "--set", "free", "--pack", "apps", "--reason", "r"}, {"--account", "bac", "--set", "free", "--sponsored", missing, "--reason", "r"}, {"--account", "bac", "extra"}, } { if err := runAccountsPlanWithSession(context.Background(), s, args, io.Discard); err == nil { t.Errorf("%v: accepted", args) } } var help bytes.Buffer if err := runAccountsPlanWithSession(context.Background(), s, []string{"--help"}, &help); !errors.Is(err, flag.ErrHelp) { t.Fatalf("--help: %v", err) } if hits != 0 { t.Fatalf("%d requests reached the console", hits) } } func TestAccountsPlanRefusesAMisspeltSponsoredFile(t *testing.T) { hits := 0 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { hits++ })) defer ts.Close() f := filepath.Join(t.TempDir(), "limits.json") if err := os.WriteFile(f, []byte(`{"running_thing": 40}`), 0o600); err != nil { t.Fatal(err) } args := []string{"--account", "bac", "--set", "free", "--sponsored", f, "--reason", "r"} if err := runAccountsPlanWithSession(context.Background(), newTestSession(ts.URL), args, io.Discard); err == nil { t.Fatal("accepted a sponsored file with an unknown field") } if hits != 0 { t.Fatalf("%d requests reached the console", hits) } }