Skip to content

Commit 4b6727e

Browse files
authored
fix: grant creation mapping (#3454)
1 parent 1696b9c commit 4b6727e

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

e2e/e2e_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,9 @@ func TestCredit(t *testing.T) {
956956
Anchor: convert.ToPointer(time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC)),
957957
Interval: *apiYEAR,
958958
},
959+
Metadata: &api.Metadata{
960+
"some_key": "some_value",
961+
},
959962
})
960963
require.NoError(t, err)
961964
require.Equal(t, http.StatusCreated, resp.StatusCode(), "Invalid status code [response_body=%s]", resp.Body)
@@ -982,6 +985,9 @@ func TestCredit(t *testing.T) {
982985
Interval: *apiYEAR,
983986
IntervalISO: "P1Y",
984987
},
988+
Metadata: &api.Metadata{
989+
"some_key": "some_value",
990+
},
985991
}
986992

987993
// Exclude timestamps from comparison

e2e/entitlement_parity_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,39 @@ func TestEntitlementParitySuite(t *testing.T) {
368368
assert.Equal(t, v1ValueForV2ByKey, v2ValueForV2ByKey, "v1(by feature2Key) vs v2(by feature2Key) should match")
369369
})
370370
})
371+
372+
t.Run("Annotations and metadata parity (create and get)", func(t *testing.T) {
373+
t.Run("Annotations created with V2 API should show up in V1 API", func(t *testing.T) {
374+
createGrantResponse, err := client.CreateCustomerEntitlementGrantV2WithResponse(ctx, customerID, feature1Key, api.CreateCustomerEntitlementGrantV2JSONRequestBody{
375+
Amount: 100,
376+
EffectiveAt: time.Now().Truncate(time.Minute).Add(time.Minute),
377+
Expiration: nil,
378+
Annotations: &api.Annotations{
379+
"some_annotation": "some_annotation_value",
380+
},
381+
})
382+
require.NoError(t, err)
383+
require.Equal(t, http.StatusCreated, createGrantResponse.StatusCode(), "Invalid status code [response_body=%s]", string(createGrantResponse.Body))
384+
385+
getGrantResponse, err := client.ListEntitlementGrantsWithResponse(ctx, subjectKey, feature1Key, &api.ListEntitlementGrantsParams{})
386+
require.NoError(t, err)
387+
require.Equal(t, http.StatusOK, getGrantResponse.StatusCode(), "Invalid status code [response_body=%s]", string(getGrantResponse.Body))
388+
require.NotNil(t, getGrantResponse.JSON200)
389+
require.GreaterOrEqual(t, len(lo.FromPtr(getGrantResponse.JSON200)), 1, "Invalid number of grants [response_body=%s]", string(getGrantResponse.Body))
390+
391+
var found *api.EntitlementGrant
392+
393+
for _, grant := range lo.FromPtr(getGrantResponse.JSON200) {
394+
if grant.Id == createGrantResponse.JSON201.Id {
395+
found = &grant
396+
break
397+
}
398+
}
399+
400+
require.NotNil(t, found, "Grant not found [response_body=%s]", string(getGrantResponse.Body))
401+
require.Equal(t, "some_annotation_value", lo.FromPtr(found.Annotations)["some_annotation"])
402+
})
403+
})
371404
}
372405

373406
func TestEntitlementDifferences(t *testing.T) {

openmeter/entitlement/metered/entitlement_grant.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func (e *connector) CreateGrant(ctx context.Context, namespace string, customerI
7777
ResetMinRollover: inputGrant.ResetMinRollover,
7878
Recurrence: inputGrant.Recurrence,
7979
Annotations: inputGrant.Annotations,
80+
Metadata: inputGrant.Metadata,
8081
})
8182
if err != nil {
8283
if _, ok := err.(grant.OwnerNotFoundError); ok {

0 commit comments

Comments
 (0)