From be56e4a5398ea36d0963c6a930b1071793765ecd Mon Sep 17 00:00:00 2001 From: Pavel Brm <5097196+pavelbrm@users.noreply.github.com> Date: Mon, 9 Sep 2024 20:36:38 +1200 Subject: [PATCH] fix: fix issues with setting numintervals for leo (#2651) * fix: allow meaningful zero for issuer configuration in order api for premium * fix: remove irrelevant fields * fix: use better method for finding item * fix: implement truncating logic * test: add tests * test: add tests --- services/skus/credentials.go | 70 +++++-- services/skus/credentials_noint_test.go | 195 ++++++++++++++++++ services/skus/model/model.go | 29 ++- services/skus/model/model_test.go | 151 ++++++++++++++ services/skus/service.go | 2 +- services/skus/service_nonint_test.go | 66 +++--- services/skus/service_test.go | 12 +- services/skus/skus.go | 32 +-- services/skus/skus_test.go | 138 ++++++------- services/skus/storage/repository/mock.go | 6 +- services/skus/storage/repository/tlv2.go | 8 +- services/skus/storage/repository/tlv2_test.go | 37 ++-- 12 files changed, 558 insertions(+), 188 deletions(-) diff --git a/services/skus/credentials.go b/services/skus/credentials.go index a171e5982..60ac2fa65 100644 --- a/services/skus/credentials.go +++ b/services/skus/credentials.go @@ -240,19 +240,17 @@ func (s *Service) CreateOrderItemCredentials(ctx context.Context, orderID, itemI return model.ErrOrderNotPaid } - var orderItem *OrderItem - for _, item := range order.Items { - if item.ID == itemID { - orderItem = &item - break - } + item, ok := order.HasItem(itemID) + if !ok { + return errItemDoesNotExist } - if orderItem == nil { - return errItemDoesNotExist + nbcreds := len(blindedCreds) + if nbcreds == 0 { + return model.ErrTLV2InvalidCredNum } - if err := s.doCredentialsExist(ctx, requestID, orderItem, blindedCreds); err != nil { + if err := s.doCredentialsExist(ctx, requestID, item, blindedCreds[0]); err != nil { if errors.Is(err, errCredsAlreadySubmitted) { return nil } @@ -260,11 +258,15 @@ func (s *Service) CreateOrderItemCredentials(ctx context.Context, orderID, itemI return err } - if err := checkNumBlindedCreds(order, orderItem, len(blindedCreds)); err != nil { + // Check if truncation is necessary for in case of a Leo order with 8*192=1536 nbcreds. + // If yes, then truncate credentials to the desired number, 576. + creds := truncateTLV2BCreds(order, item, nbcreds, blindedCreds) + + if err := checkNumBlindedCreds(order, item, len(creds)); err != nil { return err } - issuerID, err := encodeIssuerID(order.MerchantID, orderItem.SKU) + issuerID, err := encodeIssuerID(order.MerchantID, item.SKU) if err != nil { return errorutils.Wrap(err, "error encoding issuer name") } @@ -275,10 +277,10 @@ func (s *Service) CreateOrderItemCredentials(ctx context.Context, orderID, itemI } metadata := &Metadata{ - ItemID: orderItem.ID, + ItemID: item.ID, OrderID: order.ID, IssuerID: issuer.ID, - CredentialType: orderItem.CredentialType, + CredentialType: item.CredentialType, } associatedData, err := json.Marshal(metadata) @@ -292,20 +294,20 @@ func (s *Service) CreateOrderItemCredentials(ctx context.Context, orderID, itemI { IssuerType: issuerID, IssuerCohort: defaultCohort, - BlindedTokens: blindedCreds, + BlindedTokens: creds, AssociatedData: associatedData, }, }, } - if err := s.Datastore.InsertSigningOrderRequestOutbox(ctx, requestID, order.ID, orderItem.ID, signReq); err != nil { + if err := s.Datastore.InsertSigningOrderRequestOutbox(ctx, requestID, order.ID, item.ID, signReq); err != nil { return fmt.Errorf("error inserting signing order request outbox orderID %s: %w", order.ID, err) } return nil } -func (s *Service) doCredentialsExist(ctx context.Context, requestID uuid.UUID, item *model.OrderItem, blindedCreds []string) error { +func (s *Service) doCredentialsExist(ctx context.Context, requestID uuid.UUID, item *model.OrderItem, firstBCred string) error { switch item.CredentialType { case timeLimitedV2: // NOTE: There was a possible race condition that would allow exceeding limits on the number of cred batches. @@ -313,25 +315,25 @@ func (s *Service) doCredentialsExist(ctx context.Context, requestID uuid.UUID, i // - checking the number of active batches before accepting a request to create creds; // - checking the number of active batches before inserting the signed creds. - return s.doTLV2Exist(ctx, requestID, item, blindedCreds) + return s.doTLV2Exist(ctx, requestID, item, firstBCred) default: return s.doCredsExist(ctx, item) } } -func (s *Service) doTLV2Exist(ctx context.Context, reqID uuid.UUID, item *model.OrderItem, bcreds []string) error { +func (s *Service) doTLV2Exist(ctx context.Context, reqID uuid.UUID, item *model.OrderItem, firstBCred string) error { now := time.Now() - return s.doTLV2ExistTxTime(ctx, s.Datastore.RawDB(), reqID, item, bcreds, now, now) + return s.doTLV2ExistTxTime(ctx, s.Datastore.RawDB(), reqID, item, firstBCred, now, now) } -func (s *Service) doTLV2ExistTxTime(ctx context.Context, dbi sqlx.QueryerContext, reqID uuid.UUID, item *model.OrderItem, bcreds []string, from, to time.Time) error { +func (s *Service) doTLV2ExistTxTime(ctx context.Context, dbi sqlx.QueryerContext, reqID uuid.UUID, item *model.OrderItem, firstBCred string, from, to time.Time) error { if item.CredentialType != timeLimitedV2 { return model.ErrUnsupportedCredType } // Check TLV2 to see if we have credentials signed that match incoming blinded tokens. - report, err := s.tlv2Repo.GetCredSubmissionReport(ctx, dbi, item.OrderID, item.ID, reqID, bcreds...) + report, err := s.tlv2Repo.GetCredSubmissionReport(ctx, dbi, item.OrderID, item.ID, reqID, firstBCred) if err != nil { return err } @@ -805,3 +807,29 @@ func checkTLV2BatchLimit(lim, nact int) error { return nil } + +func truncateTLV2BCreds(ord *model.Order, item *model.OrderItem, nSrcCreds int, srcCreds []string) []string { + result := srcCreds + if targetn, ok := shouldTruncateTLV2Creds(ord, item, nSrcCreds); ok { + result = srcCreds[:targetn] + } + + return result +} + +// shouldTruncateTLV2Creds reports whether supplied blinded tokens should be truncated. +// +// At present, the function is only concerned with Leo. +func shouldTruncateTLV2Creds(ord *model.Order, item *model.OrderItem, ncreds int) (int, bool) { + if !item.IsLeo() { + return 0, false + } + + const target = 3 * 192 + + if ncreds <= target { + return 0, false + } + + return target, true +} diff --git a/services/skus/credentials_noint_test.go b/services/skus/credentials_noint_test.go index fa4cd3a34..221fde2b9 100644 --- a/services/skus/credentials_noint_test.go +++ b/services/skus/credentials_noint_test.go @@ -1,9 +1,15 @@ package skus import ( + "strconv" "testing" + uuid "github.com/satori/go.uuid" should "github.com/stretchr/testify/assert" + + "github.com/brave-intl/bat-go/libs/datastore" + + "github.com/brave-intl/bat-go/services/skus/model" ) func TestCheckTLV2BatchLimit(t *testing.T) { @@ -52,3 +58,192 @@ func TestCheckTLV2BatchLimit(t *testing.T) { }) } } + +func TestTruncateTLV2BCreds(t *testing.T) { + type tcGiven struct { + ord *model.Order + item *model.OrderItem + srcCreds []string + } + + type testCase struct { + name string + given tcGiven + exp []string + } + + tests := []testCase{ + { + name: "no_trunc_not_leo", + given: tcGiven{ + ord: &model.Order{ + ID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), + Metadata: datastore.Metadata{ + "numIntervals": int(33), + "numPerInterval": int(2), + }, + }, + item: &model.OrderItem{ + ID: uuid.Must(uuid.FromString("f100ded0-0000-4000-a000-000000000000")), + OrderID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), + SKU: "brave-vpn-premium", + }, + srcCreds: genNumStrings(2 * 33), + }, + exp: genNumStrings(2 * 33), + }, + + { + name: "no_trunc_leo_3_192", + given: tcGiven{ + ord: &model.Order{ + ID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), + Metadata: datastore.Metadata{ + "numIntervals": int(3), + "numPerInterval": int(192), + }, + }, + item: &model.OrderItem{ + ID: uuid.Must(uuid.FromString("f100ded0-0000-4000-a000-000000000000")), + OrderID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), + SKU: "brave-leo-premium", + }, + srcCreds: genNumStrings(3 * 192), + }, + exp: genNumStrings(3 * 192), + }, + + { + name: "trunc_leo_8_192", + given: tcGiven{ + ord: &model.Order{ + ID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), + Metadata: datastore.Metadata{ + "numIntervals": int(8), + "numPerInterval": int(192), + }, + }, + item: &model.OrderItem{ + ID: uuid.Must(uuid.FromString("f100ded0-0000-4000-a000-000000000000")), + OrderID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), + SKU: "brave-leo-premium", + }, + srcCreds: genNumStrings(8 * 192), + }, + exp: genNumStrings(3 * 192), + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := truncateTLV2BCreds(tc.given.ord, tc.given.item, len(tc.given.srcCreds), tc.given.srcCreds) + should.Equal(t, tc.exp, actual) + }) + } +} + +func TestShouldTruncateTLV2Creds(t *testing.T) { + type tcGiven struct { + ord *model.Order + item *model.OrderItem + ncreds int + } + + type tcExpected struct { + val int + ok bool + } + + type testCase struct { + name string + given tcGiven + exp tcExpected + } + + tests := []testCase{ + { + name: "not_leo", + given: tcGiven{ + ord: &model.Order{ + ID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), + Metadata: datastore.Metadata{ + "numIntervals": int(33), + "numPerInterval": int(2), + }, + }, + item: &model.OrderItem{ + ID: uuid.Must(uuid.FromString("f100ded0-0000-4000-a000-000000000000")), + OrderID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), + SKU: "brave-vpn-premium", + }, + ncreds: 2 * 33, + }, + exp: tcExpected{}, + }, + + { + name: "false_3_192", + given: tcGiven{ + ord: &model.Order{ + ID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), + Metadata: datastore.Metadata{ + "numIntervals": int(3), + "numPerInterval": int(192), + }, + }, + item: &model.OrderItem{ + ID: uuid.Must(uuid.FromString("f100ded0-0000-4000-a000-000000000000")), + OrderID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), + SKU: "brave-leo-premium", + }, + ncreds: 3 * 192, + }, + exp: tcExpected{}, + }, + + { + name: "true_8_192", + given: tcGiven{ + ord: &model.Order{ + ID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), + Metadata: datastore.Metadata{ + "numIntervals": int(8), + "numPerInterval": int(192), + }, + }, + item: &model.OrderItem{ + ID: uuid.Must(uuid.FromString("f100ded0-0000-4000-a000-000000000000")), + OrderID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), + SKU: "brave-leo-premium", + }, + ncreds: 8 * 192, + }, + exp: tcExpected{ + val: 3 * 192, + ok: true, + }, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual, ok := shouldTruncateTLV2Creds(tc.given.ord, tc.given.item, tc.given.ncreds) + should.Equal(t, tc.exp.ok, ok) + should.Equal(t, tc.exp.val, actual) + }) + } +} + +func genNumStrings(n int) []string { + result := make([]string, n) + + for i := 0; i < n; i++ { + result[i] = strconv.Itoa(i) + } + + return result +} diff --git a/services/skus/model/model.go b/services/skus/model/model.go index 749b3977d..8676ffd76 100644 --- a/services/skus/model/model.go +++ b/services/skus/model/model.go @@ -482,14 +482,15 @@ type CreateOrderRequestNew struct { // OrderItemRequestNew represents an item in an order request. type OrderItemRequestNew struct { Quantity int `json:"quantity" validate:"required,gte=1"` - IssuerTokenBuffer int `json:"issuer_token_buffer"` - IssuerTokenOverlap int `json:"issuer_token_overlap"` SKU string `json:"sku" validate:"required"` + Period string `json:"period"` // Not used yet. Location string `json:"location" validate:"required"` Description string `json:"description" validate:"required"` CredentialType string `json:"credential_type" validate:"required"` CredentialValidDuration string `json:"credential_valid_duration" validate:"required"` Price decimal.Decimal `json:"price"` + IssuerTokenBuffer *int `json:"issuer_token_buffer"` + IssuerTokenOverlap *int `json:"issuer_token_overlap"` CredentialValidDurationEach *string `json:"each_credential_valid_duration"` IssuanceInterval *string `json:"issuance_interval"` StripeMetadata *ItemStripeMetadata `json:"stripe_metadata"` @@ -500,11 +501,15 @@ func (r *OrderItemRequestNew) TokenBufferOrDefault() int { return 0 } - if r.IssuerTokenBuffer == 0 { + if !r.IsTLV2() { + return 1 + } + + if r.IssuerTokenBuffer == nil { return issuerBufferDefault } - return r.IssuerTokenBuffer + return *r.IssuerTokenBuffer } func (r *OrderItemRequestNew) TokenOverlapOrDefault() int { @@ -512,11 +517,23 @@ func (r *OrderItemRequestNew) TokenOverlapOrDefault() int { return 0 } - if r.IssuerTokenOverlap == 0 { + if !r.IsTLV2() { + return 0 + } + + if r.IssuerTokenOverlap == nil { return issuerOverlapDefault } - return r.IssuerTokenOverlap + return *r.IssuerTokenOverlap +} + +func (r *OrderItemRequestNew) IsTLV2() bool { + if r == nil { + return false + } + + return r.CredentialType == "time-limited-v2" } // OrderStripeMetadata holds data relevant to the order in Stripe. diff --git a/services/skus/model/model_test.go b/services/skus/model/model_test.go index 0f028699c..8bc44525c 100644 --- a/services/skus/model/model_test.go +++ b/services/skus/model/model_test.go @@ -370,6 +370,7 @@ func TestOrderItemRequestNew_Unmarshal(t *testing.T) { { name: "optional_fields_together", given: []byte(`{ + "period": "month", "price": "1", "each_credential_valid_duration": "P1D", "issuance_interval": "P1M", @@ -379,6 +380,7 @@ func TestOrderItemRequestNew_Unmarshal(t *testing.T) { } }`), exp: &model.OrderItemRequestNew{ + Period: "month", Price: decimal.RequireFromString("1"), CredentialValidDurationEach: ptrTo("P1D"), IssuanceInterval: ptrTo("P1M"), @@ -1331,6 +1333,155 @@ func TestOrderItem_StripeItemID(t *testing.T) { } } +func TestOrderItemRequestNew_TokenBufferOrDefault(t *testing.T) { + type testCase struct { + name string + given *model.OrderItemRequestNew + exp int + } + + tests := []testCase{ + { + name: "zero_nil", + }, + + { + name: "one_non_tlv2", + given: &model.OrderItemRequestNew{ + CredentialType: "time-limited", + }, + exp: 1, + }, + + { + name: "default", + given: &model.OrderItemRequestNew{ + CredentialType: "time-limited-v2", + }, + exp: 30, + }, + + { + name: "set_value", + given: &model.OrderItemRequestNew{ + CredentialType: "time-limited-v2", + IssuerTokenBuffer: ptrTo(3), + }, + exp: 3, + }, + + { + name: "set_value_zero", + given: &model.OrderItemRequestNew{ + CredentialType: "time-limited-v2", + IssuerTokenBuffer: ptrTo(0), + }, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := tc.given.TokenBufferOrDefault() + should.Equal(t, tc.exp, actual) + }) + } +} + +func TestOrderItemRequestNew_TokenOverlapOrDefault(t *testing.T) { + type testCase struct { + name string + given *model.OrderItemRequestNew + exp int + } + + tests := []testCase{ + { + name: "zero_nil", + }, + + { + name: "one_non_tlv2", + given: &model.OrderItemRequestNew{ + CredentialType: "time-limited", + }, + }, + + { + name: "default", + given: &model.OrderItemRequestNew{ + CredentialType: "time-limited-v2", + }, + exp: 5, + }, + + { + name: "set_value", + given: &model.OrderItemRequestNew{ + CredentialType: "time-limited-v2", + IssuerTokenOverlap: ptrTo(2), + }, + exp: 2, + }, + + { + name: "set_value_zero", + given: &model.OrderItemRequestNew{ + CredentialType: "time-limited-v2", + IssuerTokenOverlap: ptrTo(0), + }, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := tc.given.TokenOverlapOrDefault() + should.Equal(t, tc.exp, actual) + }) + } +} + +func TestOrderItemRequestNew_IsTLV2(t *testing.T) { + type testCase struct { + name string + given *model.OrderItemRequestNew + exp bool + } + + tests := []testCase{ + { + name: "false_nil", + }, + + { + name: "false_non_tlv2", + given: &model.OrderItemRequestNew{ + CredentialType: "time-limited", + }, + }, + + { + name: "true_tlv2", + given: &model.OrderItemRequestNew{ + CredentialType: "time-limited-v2", + }, + exp: true, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := tc.given.IsTLV2() + should.Equal(t, tc.exp, actual) + }) + } +} + func ptrTo[T any](v T) *T { return &v } diff --git a/services/skus/service.go b/services/skus/service.go index 54328a9a5..ce9f7ac33 100644 --- a/services/skus/service.go +++ b/services/skus/service.go @@ -105,7 +105,7 @@ type orderStoreSvc interface { } type tlv2Store interface { - GetCredSubmissionReport(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, creds ...string) (model.TLV2CredSubmissionReport, error) + GetCredSubmissionReport(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, firstBCred string) (model.TLV2CredSubmissionReport, error) UniqBatches(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID uuid.UUID, from, to time.Time) (int, error) DeleteLegacy(ctx context.Context, dbi sqlx.ExecerContext, orderID uuid.UUID) error } diff --git a/services/skus/service_nonint_test.go b/services/skus/service_nonint_test.go index 3826ff11a..df7e8cfc4 100644 --- a/services/skus/service_nonint_test.go +++ b/services/skus/service_nonint_test.go @@ -1838,12 +1838,12 @@ func TestService_checkOrderReceipt(t *testing.T) { func TestService_doTLV2ExistTxTime(t *testing.T) { type tcGiven struct { - reqID uuid.UUID - item *model.OrderItem - creds []string - from time.Time - to time.Time - repo *repository.MockTLV2 + reqID uuid.UUID + item *model.OrderItem + firstBCred string + from time.Time + to time.Time + repo *repository.MockTLV2 } type testCase struct { @@ -1862,10 +1862,10 @@ func TestService_doTLV2ExistTxTime(t *testing.T) { OrderID: uuid.Must(uuid.FromString("c0c0a000-0000-4000-a000-000000000000")), CredentialType: "time-limited", }, - creds: []string{"cred_01", "cred_02"}, - from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), - to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), - repo: &repository.MockTLV2{}, + firstBCred: "cred_01", + from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + repo: &repository.MockTLV2{}, }, exp: model.ErrUnsupportedCredType, }, @@ -1879,11 +1879,11 @@ func TestService_doTLV2ExistTxTime(t *testing.T) { OrderID: uuid.Must(uuid.FromString("c0c0a000-0000-4000-a000-000000000000")), CredentialType: "time-limited-v2", }, - creds: []string{"cred_01", "cred_02"}, - from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), - to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + firstBCred: "cred_01", + from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), repo: &repository.MockTLV2{ - FnGetCredSubmissionReport: func(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, creds ...string) (model.TLV2CredSubmissionReport, error) { + FnGetCredSubmissionReport: func(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, firstBCred string) (model.TLV2CredSubmissionReport, error) { return model.TLV2CredSubmissionReport{}, model.Error("something_went_wrong") }, }, @@ -1900,11 +1900,11 @@ func TestService_doTLV2ExistTxTime(t *testing.T) { OrderID: uuid.Must(uuid.FromString("c0c0a000-0000-4000-a000-000000000000")), CredentialType: "time-limited-v2", }, - creds: []string{"cred_01", "cred_02"}, - from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), - to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + firstBCred: "cred_01", + from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), repo: &repository.MockTLV2{ - FnGetCredSubmissionReport: func(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, creds ...string) (model.TLV2CredSubmissionReport, error) { + FnGetCredSubmissionReport: func(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, firstBCred string) (model.TLV2CredSubmissionReport, error) { return model.TLV2CredSubmissionReport{Submitted: true}, nil }, }, @@ -1921,11 +1921,11 @@ func TestService_doTLV2ExistTxTime(t *testing.T) { OrderID: uuid.Must(uuid.FromString("c0c0a000-0000-4000-a000-000000000000")), CredentialType: "time-limited-v2", }, - creds: []string{"cred_01", "cred_02"}, - from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), - to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + firstBCred: "cred_01", + from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), repo: &repository.MockTLV2{ - FnGetCredSubmissionReport: func(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, creds ...string) (model.TLV2CredSubmissionReport, error) { + FnGetCredSubmissionReport: func(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, firstBCred string) (model.TLV2CredSubmissionReport, error) { return model.TLV2CredSubmissionReport{ReqIDMismatch: true}, nil }, }, @@ -1942,9 +1942,9 @@ func TestService_doTLV2ExistTxTime(t *testing.T) { OrderID: uuid.Must(uuid.FromString("c0c0a000-0000-4000-a000-000000000000")), CredentialType: "time-limited-v2", }, - creds: []string{"cred_01", "cred_02"}, - from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), - to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + firstBCred: "cred_01", + from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), repo: &repository.MockTLV2{ FnUniqBatches: func(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID uuid.UUID, from, to time.Time) (int, error) { return 0, model.Error("something_went_wrong") @@ -1963,9 +1963,9 @@ func TestService_doTLV2ExistTxTime(t *testing.T) { OrderID: uuid.Must(uuid.FromString("c0c0a000-0000-4000-a000-000000000000")), CredentialType: "time-limited-v2", }, - creds: []string{"cred_01", "cred_02"}, - from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), - to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + firstBCred: "cred_01", + from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), repo: &repository.MockTLV2{ FnUniqBatches: func(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID uuid.UUID, from, to time.Time) (int, error) { return 10, nil @@ -1984,10 +1984,10 @@ func TestService_doTLV2ExistTxTime(t *testing.T) { OrderID: uuid.Must(uuid.FromString("c0c0a000-0000-4000-a000-000000000000")), CredentialType: "time-limited-v2", }, - creds: []string{"cred_01", "cred_02"}, - from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), - to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), - repo: &repository.MockTLV2{}, + firstBCred: "cred_01", + from: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + to: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + repo: &repository.MockTLV2{}, }, }, } @@ -2000,7 +2000,7 @@ func TestService_doTLV2ExistTxTime(t *testing.T) { ctx := context.Background() - actual := svc.doTLV2ExistTxTime(ctx, nil, tc.given.reqID, tc.given.item, tc.given.creds, tc.given.from, tc.given.to) + actual := svc.doTLV2ExistTxTime(ctx, nil, tc.given.reqID, tc.given.item, tc.given.firstBCred, tc.given.from, tc.given.to) should.Equal(t, tc.exp, actual) }) } diff --git a/services/skus/service_test.go b/services/skus/service_test.go index 683203ba0..4d4e26e02 100644 --- a/services/skus/service_test.go +++ b/services/skus/service_test.go @@ -1,5 +1,3 @@ -//go:build integration - package skus import ( @@ -135,8 +133,7 @@ func TestCreateOrderItems(t *testing.T) { }, }, IssuerConfig: &model.IssuerConfig{ - Buffer: 30, - Overlap: 5, + Buffer: 1, }, Subtotal: decimal.NewFromInt(0), }, @@ -158,8 +155,7 @@ func TestCreateOrderItems(t *testing.T) { }, IssuerConfig: &model.IssuerConfig{ - Buffer: 30, - Overlap: 5, + Buffer: 1, }, Subtotal: decimal.NewFromInt(0), }, @@ -241,7 +237,6 @@ func TestCreateOrderItem(t *testing.T) { ProductID: "product_id", ItemID: "item_id", }, - IssuerTokenBuffer: 10, }, exp: tcExpected{ result: &model.OrderItem{ @@ -271,8 +266,7 @@ func TestCreateOrderItem(t *testing.T) { }, Subtotal: decimal.NewFromInt(10).Mul(decimal.NewFromInt(int64(2))), IssuerConfig: &model.IssuerConfig{ - Buffer: 10, - Overlap: 5, + Buffer: 1, }, }, }, diff --git a/services/skus/skus.go b/services/skus/skus.go index 6baa4c0cc..9837b9f83 100644 --- a/services/skus/skus.go +++ b/services/skus/skus.go @@ -236,43 +236,43 @@ func newPaymentProcessorConfig(env string) *premiumPaymentProcConfig { func newOrderItemReqNewMobileSet(env string) map[string]model.OrderItemRequestNew { leom := model.OrderItemRequestNew{ - Quantity: 1, - IssuerTokenBuffer: 3, - SKU: "brave-leo-premium", + Quantity: 1, + SKU: "brave-leo-premium", // Location depends on env. Description: "Premium access to Leo", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("14.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), // StripeMetadata depends on env. } leoa := model.OrderItemRequestNew{ - Quantity: 1, - IssuerTokenBuffer: 3, - SKU: "brave-leo-premium-year", + Quantity: 1, + SKU: "brave-leo-premium-year", // Location depends on env. Description: "Premium access to Leo Yearly", CredentialType: "time-limited-v2", - CredentialValidDuration: "P1Y", - Price: decimal.RequireFromString("150.00"), + CredentialValidDuration: "P1M", + Price: decimal.RequireFromString("149.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), // StripeMetadata depends on env. } vpnm := model.OrderItemRequestNew{ - Quantity: 1, - IssuerTokenBuffer: 31, - IssuerTokenOverlap: 2, - SKU: "brave-vpn-premium", + Quantity: 1, + SKU: "brave-vpn-premium", // Location depends on env. Description: "brave-vpn-premium", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("9.99"), + IssuerTokenBuffer: ptrTo(31), + IssuerTokenOverlap: ptrTo(2), CredentialValidDurationEach: ptrTo("P1D"), // StripeMetadata depends on env. } @@ -288,7 +288,7 @@ func newOrderItemReqNewMobileSet(env string) map[string]model.OrderItemRequestNe leoa.Location = "leo.brave.com" leoa.StripeMetadata = &model.ItemStripeMetadata{ ProductID: "prod_O9uKDYsRPXNgfB", - ItemID: "price_1NXmfTBSm1mtrN9nybnyolId", + ItemID: "price_1PqvBPBSm1mtrN9nYgXdiP2h", } vpnm.Location = "vpn.brave.com" @@ -307,7 +307,7 @@ func newOrderItemReqNewMobileSet(env string) map[string]model.OrderItemRequestNe leoa.Location = "leo.bravesoftware.com" leoa.StripeMetadata = &model.ItemStripeMetadata{ ProductID: "prod_OKRYJ77wYOk771", - ItemID: "price_1NXmfTBSm1mtrN9nybnyolId", + ItemID: "price_1PpSAWBSm1mtrN9nF66jM7fk", } vpnm.Location = "vpn.bravesoftware.com" diff --git a/services/skus/skus_test.go b/services/skus/skus_test.go index 845c65157..83146c200 100644 --- a/services/skus/skus_test.go +++ b/services/skus/skus_test.go @@ -214,15 +214,15 @@ func TestNewOrderItemReqForSubID(t *testing.T) { exp: tcExpected{ req: model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium", Location: "leo.brave.software", Description: "Premium access to Leo", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("14.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_OtZCXOCIO3AJE6", ItemID: "price_1OuRqmHof20bphG6RXl7EHP2", @@ -240,15 +240,15 @@ func TestNewOrderItemReqForSubID(t *testing.T) { exp: tcExpected{ req: model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium", Location: "leo.brave.software", Description: "Premium access to Leo", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("14.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_OtZCXOCIO3AJE6", ItemID: "price_1OuRqmHof20bphG6RXl7EHP2", @@ -266,15 +266,15 @@ func TestNewOrderItemReqForSubID(t *testing.T) { exp: tcExpected{ req: model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium-year", Location: "leo.brave.software", Description: "Premium access to Leo Yearly", CredentialType: "time-limited-v2", - CredentialValidDuration: "P1Y", - Price: decimal.RequireFromString("150.00"), + CredentialValidDuration: "P1M", + Price: decimal.RequireFromString("149.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_OtZCXOCIO3AJE6", ItemID: "price_1O6re8Hof20bphG6tqdNEEAp", @@ -292,15 +292,15 @@ func TestNewOrderItemReqForSubID(t *testing.T) { exp: tcExpected{ req: model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium-year", Location: "leo.brave.software", Description: "Premium access to Leo Yearly", CredentialType: "time-limited-v2", - CredentialValidDuration: "P1Y", - Price: decimal.RequireFromString("150.00"), + CredentialValidDuration: "P1M", + Price: decimal.RequireFromString("149.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_OtZCXOCIO3AJE6", ItemID: "price_1O6re8Hof20bphG6tqdNEEAp", @@ -318,14 +318,14 @@ func TestNewOrderItemReqForSubID(t *testing.T) { exp: tcExpected{ req: model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 31, - IssuerTokenOverlap: 2, SKU: "brave-vpn-premium", Location: "vpn.brave.software", Description: "brave-vpn-premium", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("9.99"), + IssuerTokenBuffer: ptrTo(31), + IssuerTokenOverlap: ptrTo(2), CredentialValidDurationEach: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_K1c8W3oM4mUsGw", @@ -344,14 +344,14 @@ func TestNewOrderItemReqForSubID(t *testing.T) { exp: tcExpected{ req: model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 31, - IssuerTokenOverlap: 2, SKU: "brave-vpn-premium", Location: "vpn.brave.software", Description: "brave-vpn-premium", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("9.99"), + IssuerTokenBuffer: ptrTo(31), + IssuerTokenOverlap: ptrTo(2), CredentialValidDurationEach: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_K1c8W3oM4mUsGw", @@ -370,14 +370,14 @@ func TestNewOrderItemReqForSubID(t *testing.T) { exp: tcExpected{ req: model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 31, - IssuerTokenOverlap: 2, SKU: "brave-vpn-premium", Location: "vpn.brave.software", Description: "brave-vpn-premium", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("9.99"), + IssuerTokenBuffer: ptrTo(31), + IssuerTokenOverlap: ptrTo(2), CredentialValidDurationEach: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_K1c8W3oM4mUsGw", @@ -396,14 +396,14 @@ func TestNewOrderItemReqForSubID(t *testing.T) { exp: tcExpected{ req: model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 31, - IssuerTokenOverlap: 2, SKU: "brave-vpn-premium", Location: "vpn.brave.software", Description: "brave-vpn-premium", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("9.99"), + IssuerTokenBuffer: ptrTo(31), + IssuerTokenOverlap: ptrTo(2), CredentialValidDurationEach: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_K1c8W3oM4mUsGw", @@ -457,15 +457,15 @@ func TestNewCreateOrderReqNewMobile(t *testing.T) { Items: []model.OrderItemRequestNew{ { Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium", Location: "leo.brave.software", Description: "Premium access to Leo", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("14.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_OtZCXOCIO3AJE6", ItemID: "price_1OuRqmHof20bphG6RXl7EHP2", @@ -492,18 +492,18 @@ func TestNewCreateOrderReqNewMobile(t *testing.T) { Items: []model.OrderItemRequestNew{ { Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium-year", Location: "leo.bravesoftware.com", Description: "Premium access to Leo Yearly", CredentialType: "time-limited-v2", - CredentialValidDuration: "P1Y", - Price: decimal.RequireFromString("150.00"), + CredentialValidDuration: "P1M", + Price: decimal.RequireFromString("149.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_OKRYJ77wYOk771", - ItemID: "price_1NXmfTBSm1mtrN9nybnyolId", + ItemID: "price_1PpSAWBSm1mtrN9nF66jM7fk", }, }, }, @@ -527,15 +527,15 @@ func TestNewCreateOrderReqNewMobile(t *testing.T) { Items: []model.OrderItemRequestNew{ { Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium", Location: "leo.brave.com", Description: "Premium access to Leo", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("14.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_O9uKDYsRPXNgfB", ItemID: "price_1OoS8YBSm1mtrN9nB5gKoYwh", @@ -563,14 +563,14 @@ func TestNewCreateOrderReqNewMobile(t *testing.T) { Items: []model.OrderItemRequestNew{ { Quantity: 1, - IssuerTokenBuffer: 31, - IssuerTokenOverlap: 2, SKU: "brave-vpn-premium", Location: "vpn.brave.software", Description: "brave-vpn-premium", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("9.99"), + IssuerTokenBuffer: ptrTo(31), + IssuerTokenOverlap: ptrTo(2), CredentialValidDurationEach: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_K1c8W3oM4mUsGw", @@ -598,14 +598,14 @@ func TestNewCreateOrderReqNewMobile(t *testing.T) { Items: []model.OrderItemRequestNew{ { Quantity: 1, - IssuerTokenBuffer: 31, - IssuerTokenOverlap: 2, SKU: "brave-vpn-premium", Location: "vpn.bravesoftware.com", Description: "brave-vpn-premium", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("9.99"), + IssuerTokenBuffer: ptrTo(31), + IssuerTokenOverlap: ptrTo(2), CredentialValidDurationEach: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_Lhv4OM1aAPxflY", @@ -633,14 +633,14 @@ func TestNewCreateOrderReqNewMobile(t *testing.T) { Items: []model.OrderItemRequestNew{ { Quantity: 1, - IssuerTokenBuffer: 31, - IssuerTokenOverlap: 2, SKU: "brave-vpn-premium", Location: "vpn.brave.com", Description: "brave-vpn-premium", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("9.99"), + IssuerTokenBuffer: ptrTo(31), + IssuerTokenOverlap: ptrTo(2), CredentialValidDurationEach: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_Lhv8qsPsn6WHrx", @@ -676,15 +676,15 @@ func TestNewOrderItemReqNewMobileSet(t *testing.T) { exp: map[string]model.OrderItemRequestNew{ "brave-leo-premium": model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium", Location: "leo.brave.com", Description: "Premium access to Leo", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("14.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_O9uKDYsRPXNgfB", ItemID: "price_1OoS8YBSm1mtrN9nB5gKoYwh", @@ -693,31 +693,31 @@ func TestNewOrderItemReqNewMobileSet(t *testing.T) { "brave-leo-premium-year": model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium-year", Location: "leo.brave.com", Description: "Premium access to Leo Yearly", CredentialType: "time-limited-v2", - CredentialValidDuration: "P1Y", - Price: decimal.RequireFromString("150.00"), + CredentialValidDuration: "P1M", + Price: decimal.RequireFromString("149.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_O9uKDYsRPXNgfB", - ItemID: "price_1NXmfTBSm1mtrN9nybnyolId", + ItemID: "price_1PqvBPBSm1mtrN9nYgXdiP2h", }, }, "brave-vpn-premium": model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 31, - IssuerTokenOverlap: 2, SKU: "brave-vpn-premium", Location: "vpn.brave.com", Description: "brave-vpn-premium", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("9.99"), + IssuerTokenBuffer: ptrTo(31), + IssuerTokenOverlap: ptrTo(2), CredentialValidDurationEach: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_Lhv8qsPsn6WHrx", @@ -733,15 +733,15 @@ func TestNewOrderItemReqNewMobileSet(t *testing.T) { exp: map[string]model.OrderItemRequestNew{ "brave-leo-premium": model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium", Location: "leo.bravesoftware.com", Description: "Premium access to Leo", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("14.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_OKRYJ77wYOk771", ItemID: "price_1OuRuUBSm1mtrN9nWFtJYSML", @@ -750,31 +750,31 @@ func TestNewOrderItemReqNewMobileSet(t *testing.T) { "brave-leo-premium-year": model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium-year", Location: "leo.bravesoftware.com", Description: "Premium access to Leo Yearly", CredentialType: "time-limited-v2", - CredentialValidDuration: "P1Y", - Price: decimal.RequireFromString("150.00"), + CredentialValidDuration: "P1M", + Price: decimal.RequireFromString("149.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_OKRYJ77wYOk771", - ItemID: "price_1NXmfTBSm1mtrN9nybnyolId", + ItemID: "price_1PpSAWBSm1mtrN9nF66jM7fk", }, }, "brave-vpn-premium": model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 31, - IssuerTokenOverlap: 2, SKU: "brave-vpn-premium", Location: "vpn.bravesoftware.com", Description: "brave-vpn-premium", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("9.99"), + IssuerTokenBuffer: ptrTo(31), + IssuerTokenOverlap: ptrTo(2), CredentialValidDurationEach: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_Lhv4OM1aAPxflY", @@ -790,15 +790,15 @@ func TestNewOrderItemReqNewMobileSet(t *testing.T) { exp: map[string]model.OrderItemRequestNew{ "brave-leo-premium": model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium", Location: "leo.brave.software", Description: "Premium access to Leo", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("14.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_OtZCXOCIO3AJE6", ItemID: "price_1OuRqmHof20bphG6RXl7EHP2", @@ -807,15 +807,15 @@ func TestNewOrderItemReqNewMobileSet(t *testing.T) { "brave-leo-premium-year": model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium-year", Location: "leo.brave.software", Description: "Premium access to Leo Yearly", CredentialType: "time-limited-v2", - CredentialValidDuration: "P1Y", - Price: decimal.RequireFromString("150.00"), + CredentialValidDuration: "P1M", + Price: decimal.RequireFromString("149.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_OtZCXOCIO3AJE6", ItemID: "price_1O6re8Hof20bphG6tqdNEEAp", @@ -824,14 +824,14 @@ func TestNewOrderItemReqNewMobileSet(t *testing.T) { "brave-vpn-premium": model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 31, - IssuerTokenOverlap: 2, SKU: "brave-vpn-premium", Location: "vpn.brave.software", Description: "brave-vpn-premium", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("9.99"), + IssuerTokenBuffer: ptrTo(31), + IssuerTokenOverlap: ptrTo(2), CredentialValidDurationEach: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_K1c8W3oM4mUsGw", @@ -847,15 +847,15 @@ func TestNewOrderItemReqNewMobileSet(t *testing.T) { exp: map[string]model.OrderItemRequestNew{ "brave-leo-premium": model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium", Location: "leo.brave.software", Description: "Premium access to Leo", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("14.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_OtZCXOCIO3AJE6", ItemID: "price_1OuRqmHof20bphG6RXl7EHP2", @@ -864,15 +864,15 @@ func TestNewOrderItemReqNewMobileSet(t *testing.T) { "brave-leo-premium-year": model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 3, SKU: "brave-leo-premium-year", Location: "leo.brave.software", Description: "Premium access to Leo Yearly", CredentialType: "time-limited-v2", - CredentialValidDuration: "P1Y", - Price: decimal.RequireFromString("150.00"), + CredentialValidDuration: "P1M", + Price: decimal.RequireFromString("149.99"), + IssuerTokenBuffer: ptrTo(3), + IssuerTokenOverlap: ptrTo(0), CredentialValidDurationEach: ptrTo("P1D"), - IssuanceInterval: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_OtZCXOCIO3AJE6", ItemID: "price_1O6re8Hof20bphG6tqdNEEAp", @@ -881,14 +881,14 @@ func TestNewOrderItemReqNewMobileSet(t *testing.T) { "brave-vpn-premium": model.OrderItemRequestNew{ Quantity: 1, - IssuerTokenBuffer: 31, - IssuerTokenOverlap: 2, SKU: "brave-vpn-premium", Location: "vpn.brave.software", Description: "brave-vpn-premium", CredentialType: "time-limited-v2", CredentialValidDuration: "P1M", Price: decimal.RequireFromString("9.99"), + IssuerTokenBuffer: ptrTo(31), + IssuerTokenOverlap: ptrTo(2), CredentialValidDurationEach: ptrTo("P1D"), StripeMetadata: &model.ItemStripeMetadata{ ProductID: "prod_K1c8W3oM4mUsGw", diff --git a/services/skus/storage/repository/mock.go b/services/skus/storage/repository/mock.go index 6605de639..59cefaa7b 100644 --- a/services/skus/storage/repository/mock.go +++ b/services/skus/storage/repository/mock.go @@ -218,17 +218,17 @@ func (r *MockOrderPayHistory) Insert(ctx context.Context, dbi sqlx.ExecerContext } type MockTLV2 struct { - FnGetCredSubmissionReport func(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, creds ...string) (model.TLV2CredSubmissionReport, error) + FnGetCredSubmissionReport func(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, firstBCred string) (model.TLV2CredSubmissionReport, error) FnUniqBatches func(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID uuid.UUID, from, to time.Time) (int, error) FnDeleteLegacy func(ctx context.Context, dbi sqlx.ExecerContext, orderID uuid.UUID) error } -func (r *MockTLV2) GetCredSubmissionReport(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, creds ...string) (model.TLV2CredSubmissionReport, error) { +func (r *MockTLV2) GetCredSubmissionReport(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, firstBCred string) (model.TLV2CredSubmissionReport, error) { if r.FnGetCredSubmissionReport == nil { return model.TLV2CredSubmissionReport{}, nil } - return r.FnGetCredSubmissionReport(ctx, dbi, orderID, itemID, reqID, creds...) + return r.FnGetCredSubmissionReport(ctx, dbi, orderID, itemID, reqID, firstBCred) } func (r *MockTLV2) UniqBatches(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID uuid.UUID, from, to time.Time) (int, error) { diff --git a/services/skus/storage/repository/tlv2.go b/services/skus/storage/repository/tlv2.go index f7172d28a..b29dae215 100644 --- a/services/skus/storage/repository/tlv2.go +++ b/services/skus/storage/repository/tlv2.go @@ -14,11 +14,7 @@ type TLV2 struct{} func NewTLV2() *TLV2 { return &TLV2{} } -func (r *TLV2) GetCredSubmissionReport(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, creds ...string) (model.TLV2CredSubmissionReport, error) { - if len(creds) == 0 { - return model.TLV2CredSubmissionReport{}, model.ErrTLV2InvalidCredNum - } - +func (r *TLV2) GetCredSubmissionReport(ctx context.Context, dbi sqlx.QueryerContext, orderID, itemID, reqID uuid.UUID, firstBCred string) (model.TLV2CredSubmissionReport, error) { const q = `SELECT EXISTS( SELECT 1 FROM time_limited_v2_order_creds WHERE order_id=$1 AND item_id=$2 AND blinded_creds->>0 = $4 ) AS submitted, EXISTS( @@ -26,7 +22,7 @@ func (r *TLV2) GetCredSubmissionReport(ctx context.Context, dbi sqlx.QueryerCont ) AS req_id_mismatch` result := model.TLV2CredSubmissionReport{} - if err := sqlx.GetContext(ctx, dbi, &result, q, orderID, itemID, reqID, creds[0]); err != nil { + if err := sqlx.GetContext(ctx, dbi, &result, q, orderID, itemID, reqID, firstBCred); err != nil { return model.TLV2CredSubmissionReport{}, err } diff --git a/services/skus/storage/repository/tlv2_test.go b/services/skus/storage/repository/tlv2_test.go index 90449d15d..77207bb2d 100644 --- a/services/skus/storage/repository/tlv2_test.go +++ b/services/skus/storage/repository/tlv2_test.go @@ -26,10 +26,10 @@ func TestTLV2_GetCredSubmissionReport(t *testing.T) { }() type tcGiven struct { - orderID uuid.UUID - itemID uuid.UUID - reqID uuid.UUID - creds []string + orderID uuid.UUID + itemID uuid.UUID + reqID uuid.UUID + firstBCred string fnBefore func(ctx context.Context, dbi sqlx.ExtContext) error } @@ -46,24 +46,13 @@ func TestTLV2_GetCredSubmissionReport(t *testing.T) { } tests := []testCase{ - { - name: "invalid_param", - given: tcGiven{ - orderID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), - itemID: uuid.Must(uuid.FromString("decade00-0000-4000-a000-000000000000")), - reqID: uuid.Must(uuid.FromString("f100ded0-0000-4000-a000-000000000000")), - fnBefore: func(ctx context.Context, dbi sqlx.ExtContext) error { return nil }, - }, - exp: tcExpected{err: model.ErrTLV2InvalidCredNum}, - }, - { name: "submitted", given: tcGiven{ - orderID: uuid.Must(uuid.FromString("c0c0a000-0000-4000-a000-000000000000")), - itemID: uuid.Must(uuid.FromString("ad0be000-0000-4000-a000-000000000000")), - reqID: uuid.Must(uuid.FromString("f100ded0-0000-4000-a000-000000000000")), - creds: []string{"cred_01", "cred_02", "cred_03"}, + orderID: uuid.Must(uuid.FromString("c0c0a000-0000-4000-a000-000000000000")), + itemID: uuid.Must(uuid.FromString("ad0be000-0000-4000-a000-000000000000")), + reqID: uuid.Must(uuid.FromString("f100ded0-0000-4000-a000-000000000000")), + firstBCred: "cred_01", fnBefore: func(ctx context.Context, dbi sqlx.ExtContext) error { qs := []string{ @@ -97,10 +86,10 @@ func TestTLV2_GetCredSubmissionReport(t *testing.T) { { name: "mismatch", given: tcGiven{ - orderID: uuid.Must(uuid.FromString("c0c0a000-0000-4000-a000-000000000000")), - itemID: uuid.Must(uuid.FromString("ad0be000-0000-4000-a000-000000000000")), - reqID: uuid.Must(uuid.FromString("f100ded0-0000-4000-a000-000000000000")), - creds: []string{"cred_01", "cred_02", "cred_03"}, + orderID: uuid.Must(uuid.FromString("c0c0a000-0000-4000-a000-000000000000")), + itemID: uuid.Must(uuid.FromString("ad0be000-0000-4000-a000-000000000000")), + reqID: uuid.Must(uuid.FromString("f100ded0-0000-4000-a000-000000000000")), + firstBCred: "cred_01", fnBefore: func(ctx context.Context, dbi sqlx.ExtContext) error { qs := []string{ @@ -151,7 +140,7 @@ func TestTLV2_GetCredSubmissionReport(t *testing.T) { must.Equal(t, nil, err) } - actual, err := repo.GetCredSubmissionReport(ctx, tx, tc.given.orderID, tc.given.itemID, tc.given.reqID, tc.given.creds...) + actual, err := repo.GetCredSubmissionReport(ctx, tx, tc.given.orderID, tc.given.itemID, tc.given.reqID, tc.given.firstBCred) must.Equal(t, tc.exp.err, err) if tc.exp.err != nil {