@@ -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+ }
0 commit comments