File size: 8,817 Bytes
b47934a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package e2e

import (
	"context"
	"net/http"
	"testing"
	"time"

	"github.com/brianvoe/gofakeit/v6"
	cloudevents "github.com/cloudevents/sdk-go/v2/event"
	"github.com/samber/lo"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	api "github.com/openmeterio/openmeter/api/client/go"
)

var (
	MultiSubjectFeatureKey        = "multi_subject_feature_1"
	MultiSubjectMeteredFeatureKey = "multi_subject_metered_feature_1"
	MultiSubjectPlanKey           = "multi_subject_plan"
	MultiSubjectMeterSlug         = "multi_subject_meter"
)

func TestMultiSubject(t *testing.T) {
	client := initClient(t)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	var customerId string
	subjectKeys := []string{"multi_subject_1", "multi_subject_2"}

	// Let's set up a customer with multiple subjects
	t.Run("Should create a customer with multiple subjects", func(t *testing.T) {
		customerAPIRes, err := client.CreateCustomerWithResponse(ctx, api.CreateCustomerJSONRequestBody{
			Name:         "Test Customer Multi Subject",
			Currency:     lo.ToPtr(api.CurrencyCode("USD")),
			Description:  lo.ToPtr("Test Customer Description"),
			PrimaryEmail: lo.ToPtr("customer-multi-subject@mail.com"),
			BillingAddress: &api.Address{
				City:        lo.ToPtr("City"),
				Country:     lo.ToPtr("US"),
				Line1:       lo.ToPtr("Line 1"),
				Line2:       lo.ToPtr("Line 2"),
				State:       lo.ToPtr("State"),
				PhoneNumber: lo.ToPtr("1234567890"),
				PostalCode:  lo.ToPtr("12345"),
			},
			UsageAttribution: &api.CustomerUsageAttribution{
				SubjectKeys: subjectKeys,
			},
		})
		require.Nil(t, err)

		require.Equal(t, 201, customerAPIRes.StatusCode(), "received the following body: %s", customerAPIRes.Body)
		customer1 := customerAPIRes.JSON201
		require.NotNil(t, customer1)
		require.Len(t, customer1.UsageAttribution.SubjectKeys, 2)

		customerId = customer1.Id
		require.NotEmpty(t, customerId)
	})

	var planId string

	// Now let's set up a plan (with no billables for simplicity)
	t.Run("Should be able to set up a plan as normal", func(t *testing.T) {
		// Now, let's create dedicated features for the plan
		featureAPIRes, err := client.CreateFeatureWithResponse(ctx, api.CreateFeatureJSONRequestBody{
			Key:  MultiSubjectFeatureKey,
			Name: "Test Plan Feature",
		})
		require.Nil(t, err)

		feature := featureAPIRes.JSON201
		require.NotNil(t, feature)

		// And let's create a dedicated metered feature
		meteredFeatureAPIRes, err := client.CreateFeatureWithResponse(ctx, api.CreateFeatureJSONRequestBody{
			Key:       MultiSubjectMeteredFeatureKey,
			Name:      "Test Plan Metered Feature",
			MeterSlug: lo.ToPtr(MultiSubjectMeterSlug),
		})
		require.Nil(t, err)

		meteredFeature := meteredFeatureAPIRes.JSON201
		require.NotNil(t, meteredFeature)

		et := &api.RateCardEntitlement{}
		err = et.FromRateCardBooleanEntitlement(api.RateCardBooleanEntitlement{
			Type: api.RateCardBooleanEntitlementType("boolean"),
		})
		require.Nil(t, err)

		met := &api.RateCardEntitlement{}
		err = met.FromRateCardMeteredEntitlement(api.RateCardMeteredEntitlement{
			Type:            api.RateCardMeteredEntitlementType("metered"),
			IssueAfterReset: lo.ToPtr(100.0),
		})
		require.Nil(t, err)

		// Lets build a PlanCreate input
		p1RC1 := api.RateCard{}
		err = p1RC1.FromRateCardFlatFee(api.RateCardFlatFee{
			Name:        "Simple Feature",
			Description: lo.ToPtr("Just a ratecard with an entitlement"),
			Key:         MultiSubjectFeatureKey,
			FeatureKey:  lo.ToPtr(MultiSubjectFeatureKey),
			TaxConfig: &api.TaxConfig{
				Stripe: &api.StripeTaxConfig{
					Code: "txcd_10000000",
				},
			},
			EntitlementTemplate: et,
			BillingCadence:      nil,
			Type:                api.RateCardFlatFeeType("flat"),
		})
		require.Nil(t, err)

		p1RC2 := api.RateCard{}
		err = p1RC2.FromRateCardFlatFee(api.RateCardFlatFee{
			Name:                "Metered Feature",
			Description:         lo.ToPtr("Has a monthly recurring price to grant access to a feature"),
			Key:                 MultiSubjectMeteredFeatureKey,
			FeatureKey:          lo.ToPtr(MultiSubjectMeteredFeatureKey),
			EntitlementTemplate: met,
			TaxConfig: &api.TaxConfig{
				Stripe: &api.StripeTaxConfig{
					Code: "txcd_10000000",
				},
			},
			Price: &api.FlatPriceWithPaymentTerm{
				Amount:      "1000",
				PaymentTerm: lo.ToPtr(api.PricePaymentTerm("in_advance")),
				Type:        api.FlatPriceWithPaymentTermType("flat"),
			},
			BillingCadence: lo.ToPtr("P1M"),
			Type:           api.RateCardFlatFeeType("flat"),
		})
		require.Nil(t, err)

		planCreate := api.PlanCreate{
			Currency:       api.CurrencyCode("USD"),
			Name:           "Test Plan",
			Description:    lo.ToPtr("Test Plan Description"),
			Key:            MultiSubjectPlanKey,
			BillingCadence: "P1M",
			Alignment: &api.Alignment{
				BillablesMustAlign: lo.ToPtr(true),
			},
			Phases: []api.PlanPhase{
				{
					Name:        "Test Plan Phase 1",
					Key:         "test_plan_phase_1",
					Description: lo.ToPtr("Test Plan Phase 1 Description"),
					Duration:    nil,
					RateCards:   []api.RateCard{p1RC1, p1RC2},
				},
			},
		}

		planAPIRes, err := client.CreatePlanWithResponse(ctx, planCreate)
		require.Nil(t, err)
		require.Equal(t, 201, planAPIRes.StatusCode())

		plan := planAPIRes.JSON201
		require.NotNil(t, plan, "received the following body: %s", planAPIRes.Body)

		assert.Equal(t, MultiSubjectPlanKey, plan.Key)
		require.NotNil(t, plan.Version)
		assert.Equal(t, 1, plan.Version)

		require.NotNil(t, plan.Id)
		planId = plan.Id

		require.NotEmpty(t, planId)
		apiRes, err := client.PublishPlanWithResponse(ctx, planId)
		require.Nil(t, err)

		assert.Equal(t, 200, apiRes.StatusCode(), "received the following body: %s", apiRes.Body)

		body := apiRes.JSON200
		require.NotNil(t, body)
	})

	t.Run("Should be able to create a subscription for customer with multiple subjects", func(t *testing.T) {
		immediate := &api.SubscriptionTiming{}
		require.NoError(t, immediate.FromSubscriptionTimingEnum(api.SubscriptionTimingEnumImmediate))

		req := api.CreateSubscriptionJSONRequestBody{}
		require.NoError(t, req.FromPlanSubscriptionCreate(api.PlanSubscriptionCreate{
			CustomerId: lo.ToPtr(customerId),
			Plan: api.PlanReferenceInput{
				Key:     MultiSubjectPlanKey,
				Version: lo.ToPtr(1),
			},
			Timing: immediate,
			Name:   lo.ToPtr("Test Subscription Multi Subject"),
		}))

		apiRes, err := client.CreateSubscriptionWithResponse(ctx, req)
		require.Nil(t, err)
		require.Equal(t, 201, apiRes.StatusCode(), "received the following body: %s", apiRes.Body)
	})

	t.Run("Should be able to check entitlement access through each subject", func(t *testing.T) {
		for _, subjectKey := range subjectKeys {
			apiRes, err := client.GetEntitlementValueWithResponse(ctx, subjectKey, MultiSubjectFeatureKey, nil)

			require.Nil(t, err)
			require.Equal(t, 200, apiRes.StatusCode(), "received the following body: %s", apiRes.Body)

			body := apiRes.JSON200
			require.NotNil(t, body)
			require.True(t, body.HasAccess)
		}
	})

	t.Run("Should aggregate usage across all subjects", func(t *testing.T) {
		// Let's ingest usage for each subject
		// Make clickhouse's job easier by sending events within a fix time range
		now := time.Now()

		for _, subjectKey := range subjectKeys {
			timestamp := gofakeit.DateRange(now, now.Add(time.Second*2))

			ev := cloudevents.New()
			ev.SetID(gofakeit.UUID())
			ev.SetSource("my-app")
			ev.SetType("multi_subject")
			ev.SetSubject(subjectKey)
			ev.SetTime(timestamp)

			resp, err := client.IngestEventWithResponse(context.Background(), ev)
			require.NoError(t, err)
			require.Equal(t, http.StatusNoContent, resp.StatusCode())
		}

		// Wait for events to be processed
		assert.EventuallyWithT(t, func(t *assert.CollectT) {
			resp, err := client.QueryMeterWithResponse(context.Background(), MultiSubjectMeterSlug, nil)
			require.NoError(t, err)
			require.Equal(t, http.StatusOK, resp.StatusCode())

			require.Len(t, resp.JSON200.Data, 1)
			assert.Equal(t, float64(len(subjectKeys)), resp.JSON200.Data[0].Value)
		}, time.Minute, time.Second)

		entResp, err := client.GetCustomerEntitlementValueV2WithResponse(ctx, customerId, MultiSubjectMeteredFeatureKey, nil)
		require.NoError(t, err)
		require.Equal(t, http.StatusOK, entResp.StatusCode())

		ent := entResp.JSON200
		require.NotNil(t, ent)
		require.True(t, ent.HasAccess)
		require.NotNil(t, ent.Usage)
		require.Equal(t, float64(len(subjectKeys)), *ent.Usage)
		require.NotNil(t, ent.Balance)
		require.NotNil(t, ent.GrantBalances)
		require.GreaterOrEqual(t, len(*ent.GrantBalances), 1)

		var grantBalancesSum float64
		for _, grantBalance := range *ent.GrantBalances {
			grantBalancesSum += grantBalance
		}
		assert.Equal(t, *ent.Balance, grantBalancesSum)
	})
}