Skip to content

Commit 77b685a

Browse files
committed
test(external_data_source): cover API contract and CRUD shapes
Lock in the wire shapes against regression — the prefix-inside-payload and payload-instead-of-job_inputs bugs would have surfaced immediately with these tests in place. Also covers parse edge cases, schema RequiresReplace assertions, and the import vs ongoing-read split for the redacted job_inputs_json field.
1 parent 561efc9 commit 77b685a

2 files changed

Lines changed: 481 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package httpclient
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
10+
"github.com/posthog/terraform-provider/internal/util"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
const (
16+
testEDSID = "01900000-0000-7000-8000-000000000000"
17+
testEDSProject = "proj-1"
18+
externalSourceAPI = "/api/projects/"
19+
externalSourceSub = "/external_data_sources/"
20+
)
21+
22+
func externalDataSourceCollectionPath() string {
23+
return externalSourceAPI + testEDSProject + externalSourceSub
24+
}
25+
26+
func externalDataSourceItemPath() string {
27+
return externalDataSourceCollectionPath() + testEDSID + "/"
28+
}
29+
30+
func TestCreateExternalDataSource(t *testing.T) {
31+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
32+
assert.Equal(t, http.MethodPost, r.Method)
33+
assert.Equal(t, externalDataSourceCollectionPath(), r.URL.Path)
34+
35+
var body map[string]any
36+
require.NoError(t, json.NewDecoder(r.Body).Decode(&body))
37+
assert.Equal(t, "Stripe", body["source_type"])
38+
assert.Equal(t, "stripe_", body["prefix"])
39+
40+
writeJSONResponse(t, w, ExternalDataSource{
41+
ID: testEDSID,
42+
SourceType: "Stripe",
43+
Prefix: util.StringPtr("stripe_"),
44+
})
45+
}))
46+
defer server.Close()
47+
48+
client := newTestPosthogClient(server)
49+
resp, err := client.CreateExternalDataSource(context.Background(), testEDSProject, map[string]any{
50+
"source_type": "Stripe",
51+
"prefix": "stripe_",
52+
"payload": map[string]any{
53+
"stripe_account_id": "acct_123",
54+
"schemas": []map[string]any{{"name": "charges", "should_sync": true}},
55+
},
56+
})
57+
58+
require.NoError(t, err)
59+
assert.Equal(t, testEDSID, resp.ID)
60+
require.NotNil(t, resp.Prefix)
61+
assert.Equal(t, "stripe_", *resp.Prefix)
62+
}
63+
64+
func TestGetExternalDataSource(t *testing.T) {
65+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
66+
assert.Equal(t, http.MethodGet, r.Method)
67+
assert.Equal(t, externalDataSourceItemPath(), r.URL.Path)
68+
writeJSONResponse(t, w, ExternalDataSource{
69+
ID: testEDSID,
70+
SourceType: "Stripe",
71+
Schemas: []ExternalDataSourceSchema{
72+
{Name: "charges", ShouldSync: true, SyncFrequency: util.StringPtr("6hour")},
73+
},
74+
})
75+
}))
76+
defer server.Close()
77+
78+
client := newTestPosthogClient(server)
79+
resp, status, err := client.GetExternalDataSource(context.Background(), testEDSProject, testEDSID)
80+
81+
require.NoError(t, err)
82+
assert.Equal(t, http.StatusOK, int(status))
83+
require.Len(t, resp.Schemas, 1)
84+
assert.Equal(t, "charges", resp.Schemas[0].Name)
85+
require.NotNil(t, resp.Schemas[0].SyncFrequency)
86+
assert.Equal(t, "6hour", *resp.Schemas[0].SyncFrequency)
87+
}
88+
89+
func TestUpdateExternalDataSource(t *testing.T) {
90+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
91+
assert.Equal(t, http.MethodPatch, r.Method)
92+
assert.Equal(t, externalDataSourceItemPath(), r.URL.Path)
93+
94+
var body map[string]any
95+
require.NoError(t, json.NewDecoder(r.Body).Decode(&body))
96+
// PATCH must use job_inputs at top level — that's what the PostHog
97+
// update serializer reads. Sending under `payload` would silently no-op.
98+
_, ok := body["job_inputs"].(map[string]any)
99+
assert.True(t, ok, "PATCH body must include job_inputs")
100+
101+
writeJSONResponse(t, w, ExternalDataSource{ID: testEDSID, SourceType: "Stripe"})
102+
}))
103+
defer server.Close()
104+
105+
client := newTestPosthogClient(server)
106+
resp, status, err := client.UpdateExternalDataSource(context.Background(), testEDSProject, testEDSID, map[string]any{
107+
"job_inputs": map[string]any{"stripe_account_id": "acct_999"},
108+
})
109+
110+
require.NoError(t, err)
111+
assert.Equal(t, http.StatusOK, int(status))
112+
assert.Equal(t, testEDSID, resp.ID)
113+
}
114+
115+
func TestDeleteExternalDataSource(t *testing.T) {
116+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
117+
assert.Equal(t, http.MethodDelete, r.Method)
118+
assert.Equal(t, externalDataSourceItemPath(), r.URL.Path)
119+
w.WriteHeader(http.StatusOK)
120+
}))
121+
defer server.Close()
122+
123+
client := newTestPosthogClient(server)
124+
status, err := client.DeleteExternalDataSource(context.Background(), testEDSProject, testEDSID)
125+
126+
require.NoError(t, err)
127+
assert.Equal(t, http.StatusOK, int(status))
128+
}

0 commit comments

Comments
 (0)