Skip to content

Commit 0ce520f

Browse files
Sarumyanagneum
authored andcommitted
fix(platform): guard nil recognized_org in billing usage response
1 parent 7a6f647 commit 0ce520f

2 files changed

Lines changed: 60 additions & 5 deletions

File tree

engine/pkg/client/platform/client_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
"github.com/stretchr/testify/assert"
1616
"github.com/stretchr/testify/require"
17+
18+
"gitlab.com/postgres-ai/database-lab/v3/pkg/config/global"
1719
)
1820

1921
// roundTripFunc represents a mock type.
@@ -191,3 +193,52 @@ func TestClientChecksPlatformTokenFailed(t *testing.T) {
191193
assert.Equal(t, expectedResponse.APIResponse.Hint, platformToken.Hint)
192194
assert.Equal(t, expectedResponse.APIResponse.Details, platformToken.Details)
193195
}
196+
197+
func TestClientSendUsage(t *testing.T) {
198+
testCases := []struct {
199+
name string
200+
response BillingResponse
201+
wantBillingPage string
202+
}{
203+
{name: "response without recognized org", response: BillingResponse{Result: "ok", BillingActive: true}},
204+
{name: "response with recognized org", response: BillingResponse{Result: "ok", BillingActive: true, Org: &Org{ID: 1, Alias: "acme"}},
205+
wantBillingPage: "https://example.com/console/acme/billing"},
206+
}
207+
208+
for _, tc := range testCases {
209+
t.Run(tc.name, func(t *testing.T) {
210+
testClient := NewTestClient(func(req *http.Request) *http.Response {
211+
body, err := json.Marshal(EditionResponse{BillingResponse: tc.response})
212+
require.NoError(t, err)
213+
214+
return &http.Response{
215+
StatusCode: http.StatusOK,
216+
Body: io.NopCloser(bytes.NewBuffer(body)),
217+
}
218+
})
219+
220+
platformClient, err := NewClient(ClientConfig{
221+
URL: "https://example.com/",
222+
AccessToken: "testVerify",
223+
OrgKey: "testOrgKey",
224+
})
225+
require.NoError(t, err)
226+
platformClient.client = testClient
227+
228+
props := &global.EngineProps{}
229+
respData, err := platformClient.SendUsage(context.Background(), props, InstanceUsage{InstanceID: "test"})
230+
require.NoError(t, err)
231+
232+
assert.True(t, respData.BillingActive)
233+
assert.True(t, props.BillingActive)
234+
235+
if tc.wantBillingPage == "" {
236+
assert.Nil(t, respData.Org)
237+
return
238+
}
239+
240+
require.NotNil(t, respData.Org)
241+
assert.Equal(t, tc.wantBillingPage, respData.Org.BillingPage)
242+
})
243+
}
244+
}

engine/pkg/client/platform/telemetry.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,15 @@ func (p *Client) SendUsage(ctx context.Context, props *global.EngineProps, usage
147147
log.Dbg("Instance state updated. Billing is active:", respData.BillingActive)
148148
}
149149

150-
respData.Org.BillingPage = (&url.URL{
151-
Scheme: p.url.Scheme,
152-
Host: p.url.Host,
153-
Path: path.Join(consolePath, respData.Org.Alias, billingPath),
154-
}).String()
150+
// the org is optional in the response ("recognized_org,omitempty"); build the billing
151+
// page URL only when the platform recognized the organization
152+
if respData.Org != nil {
153+
respData.Org.BillingPage = (&url.URL{
154+
Scheme: p.url.Scheme,
155+
Host: p.url.Host,
156+
Path: path.Join(consolePath, respData.Org.Alias, billingPath),
157+
}).String()
158+
}
155159

156160
log.Dbg("Usage event response", respData)
157161

0 commit comments

Comments
 (0)