|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/chatwoot/cli/internal/config" |
| 13 | +) |
| 14 | + |
| 15 | +func TestApiCmdCallsAccountScopedEndpoint(t *testing.T) { |
| 16 | + setupTestEnv(t) |
| 17 | + |
| 18 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 19 | + if r.URL.Path != "/api/v1/accounts/1/conversations/123" { |
| 20 | + http.Error(w, "unexpected path: "+r.URL.Path, http.StatusNotFound) |
| 21 | + return |
| 22 | + } |
| 23 | + if r.Header.Get("api_access_token") != "test-token" { |
| 24 | + t.Errorf("api_access_token = %q, want test-token", r.Header.Get("api_access_token")) |
| 25 | + } |
| 26 | + w.Header().Set("Content-Type", "application/json") |
| 27 | + _, _ = w.Write([]byte(`{"id":123,"status":"open"}`)) |
| 28 | + })) |
| 29 | + defer server.Close() |
| 30 | + |
| 31 | + if err := config.Save(&config.Config{BaseURL: server.URL, AccountID: 1}); err != nil { |
| 32 | + t.Fatalf("config.Save: %v", err) |
| 33 | + } |
| 34 | + app, err := NewApp(&CLI{Output: "text"}, false, "test") |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("NewApp: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + var out bytes.Buffer |
| 40 | + app.Printer.Writer = &out |
| 41 | + |
| 42 | + if err := (&ApiCmd{Path: "/conversations/123"}).Run(app); err != nil { |
| 43 | + t.Fatalf("Run: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + var got map[string]any |
| 47 | + if err := json.Unmarshal(out.Bytes(), &got); err != nil { |
| 48 | + t.Fatalf("output is not JSON: %v\n%s", err, out.String()) |
| 49 | + } |
| 50 | + if got["id"] != float64(123) || got["status"] != "open" { |
| 51 | + t.Fatalf("output = %#v, want conversation JSON", got) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestApiCmdUsesExactAPIPath(t *testing.T) { |
| 56 | + setupTestEnv(t) |
| 57 | + |
| 58 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 59 | + if r.URL.Path != "/api/v1/profile" { |
| 60 | + http.Error(w, "unexpected path: "+r.URL.Path, http.StatusNotFound) |
| 61 | + return |
| 62 | + } |
| 63 | + w.Header().Set("Content-Type", "application/json") |
| 64 | + _, _ = w.Write([]byte(`{"id":7,"name":"Ada"}`)) |
| 65 | + })) |
| 66 | + defer server.Close() |
| 67 | + |
| 68 | + if err := config.Save(&config.Config{BaseURL: server.URL, AccountID: 1}); err != nil { |
| 69 | + t.Fatalf("config.Save: %v", err) |
| 70 | + } |
| 71 | + app, err := NewApp(&CLI{Output: "text"}, false, "test") |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("NewApp: %v", err) |
| 74 | + } |
| 75 | + app.Printer.Writer = &bytes.Buffer{} |
| 76 | + |
| 77 | + if err := (&ApiCmd{Path: "/api/v1/profile"}).Run(app); err != nil { |
| 78 | + t.Fatalf("Run: %v", err) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestApiCmdSendsMethodBodyAndHeaders(t *testing.T) { |
| 83 | + setupTestEnv(t) |
| 84 | + |
| 85 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 86 | + if r.Method != http.MethodPatch { |
| 87 | + t.Errorf("method = %s, want PATCH", r.Method) |
| 88 | + } |
| 89 | + if r.URL.Path != "/api/v1/accounts/1/conversations/123/custom_attributes" { |
| 90 | + t.Errorf("path = %s", r.URL.Path) |
| 91 | + } |
| 92 | + if r.Header.Get("X-Test") != "yes" { |
| 93 | + t.Errorf("X-Test header = %q, want yes", r.Header.Get("X-Test")) |
| 94 | + } |
| 95 | + body, err := io.ReadAll(r.Body) |
| 96 | + if err != nil { |
| 97 | + t.Errorf("read body: %v", err) |
| 98 | + } |
| 99 | + if strings.TrimSpace(string(body)) != `{"priority":"urgent"}` { |
| 100 | + t.Errorf("body = %q", string(body)) |
| 101 | + } |
| 102 | + w.WriteHeader(http.StatusNoContent) |
| 103 | + })) |
| 104 | + defer server.Close() |
| 105 | + |
| 106 | + if err := config.Save(&config.Config{BaseURL: server.URL, AccountID: 1}); err != nil { |
| 107 | + t.Fatalf("config.Save: %v", err) |
| 108 | + } |
| 109 | + app, err := NewApp(&CLI{Output: "text"}, false, "test") |
| 110 | + if err != nil { |
| 111 | + t.Fatalf("NewApp: %v", err) |
| 112 | + } |
| 113 | + app.Printer.Writer = &bytes.Buffer{} |
| 114 | + |
| 115 | + err = (&ApiCmd{ |
| 116 | + Method: "patch", |
| 117 | + Path: "conversations/123/custom_attributes", |
| 118 | + Data: `{"priority":"urgent"}`, |
| 119 | + Header: []string{"X-Test: yes"}, |
| 120 | + }).Run(app) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("Run: %v", err) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func TestNormalizeAPIPathRejectsAbsoluteURLs(t *testing.T) { |
| 127 | + if _, _, err := normalizeAPIPath("https://example.com/api/v1/profile", false); err == nil { |
| 128 | + t.Fatal("expected absolute URL error") |
| 129 | + } |
| 130 | +} |
0 commit comments