|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// TestCmdInviteCreateUnlimitedSendsZero pins the CLI → API contract: the |
| 11 | +// unlimited flag must reach the API as max_uses=0, which the backend reads as |
| 12 | +// "no limit". |
| 13 | +func TestCmdInviteCreateUnlimitedSendsZero(t *testing.T) { |
| 14 | + var received map[string]any |
| 15 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 16 | + if r.URL.Path != "/api/v1/domains/team/invites" { |
| 17 | + http.NotFound(w, r) |
| 18 | + return |
| 19 | + } |
| 20 | + if err := json.NewDecoder(r.Body).Decode(&received); err != nil { |
| 21 | + t.Errorf("decode body: %v", err) |
| 22 | + } |
| 23 | + writeInviteResponse(t, w) |
| 24 | + })) |
| 25 | + defer srv.Close() |
| 26 | + |
| 27 | + apiBase = srv.URL |
| 28 | + apiToken = "" |
| 29 | + defer func() { apiBase = "" }() |
| 30 | + |
| 31 | + cmd := cmdInvite() |
| 32 | + cmd.SetArgs([]string{"create", "team", "--unlimited"}) |
| 33 | + if err := cmd.Execute(); err != nil { |
| 34 | + t.Fatalf("invite create: %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + if got, ok := received["max_uses"].(float64); !ok || got != 0 { |
| 38 | + t.Fatalf("max_uses sent = %v, want 0 for unlimited", received["max_uses"]) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// TestCmdInviteCreateMaxUsesStillSendsCount guards the bounded path. |
| 43 | +func TestCmdInviteCreateMaxUsesStillSendsCount(t *testing.T) { |
| 44 | + var received map[string]any |
| 45 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 46 | + if err := json.NewDecoder(r.Body).Decode(&received); err != nil { |
| 47 | + t.Errorf("decode body: %v", err) |
| 48 | + } |
| 49 | + writeInviteResponse(t, w) |
| 50 | + })) |
| 51 | + defer srv.Close() |
| 52 | + |
| 53 | + apiBase = srv.URL |
| 54 | + apiToken = "" |
| 55 | + defer func() { apiBase = "" }() |
| 56 | + |
| 57 | + cmd := cmdInvite() |
| 58 | + cmd.SetArgs([]string{"create", "team", "--max-uses", "5"}) |
| 59 | + if err := cmd.Execute(); err != nil { |
| 60 | + t.Fatalf("invite create: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + if got, ok := received["max_uses"].(float64); !ok || got != 5 { |
| 64 | + t.Fatalf("max_uses sent = %v, want 5", received["max_uses"]) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestCmdInviteCreateRejectsBothLimits(t *testing.T) { |
| 69 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 70 | + writeInviteResponse(t, w) |
| 71 | + })) |
| 72 | + defer srv.Close() |
| 73 | + |
| 74 | + apiBase = srv.URL |
| 75 | + apiToken = "" |
| 76 | + defer func() { apiBase = "" }() |
| 77 | + |
| 78 | + cmd := cmdInvite() |
| 79 | + cmd.SetArgs([]string{"create", "team", "--unlimited", "--max-uses", "3"}) |
| 80 | + if err := cmd.Execute(); err == nil { |
| 81 | + t.Fatal("expected mutually exclusive flags to fail") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func writeInviteResponse(t *testing.T, w http.ResponseWriter) { |
| 86 | + t.Helper() |
| 87 | + w.Header().Set("Content-Type", "application/json") |
| 88 | + if err := json.NewEncoder(w).Encode(map[string]any{ |
| 89 | + "id": "invite-1", "domain": "team", "role": "writer", "code": "OKP-TEST-CODE", |
| 90 | + "max_uses": 0, "used_count": 0, "unlimited": true, |
| 91 | + }); err != nil { |
| 92 | + t.Errorf("encode response: %v", err) |
| 93 | + } |
| 94 | +} |
0 commit comments