From e0b3c24644db8166fabc24f2c326eea85b8cfe20 Mon Sep 17 00:00:00 2001 From: eV <8796196+evq@users.noreply.github.com> Date: Tue, 6 Feb 2024 20:03:24 +0000 Subject: [PATCH] fix query to check for request id conflict case ( mismatch ) (#2344) --- services/skus/datastore.go | 2 +- services/skus/datastore_noint_test.go | 24 +++++++++++++++++++++++- services/skus/datastore_test.go | 25 ++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/services/skus/datastore.go b/services/skus/datastore.go index 6d426e672..7041bc801 100644 --- a/services/skus/datastore.go +++ b/services/skus/datastore.go @@ -975,7 +975,7 @@ func areTimeLimitedV2CredsSubmitted(ctx context.Context, dbi getContext, request select 1 from time_limited_v2_order_creds where blinded_creds->>0 = $1 ) as already_submitted, exists( - select 1 from time_limited_v2_order_creds where blinded_creds->>0 = $1 and request_id != $2 + select 1 from time_limited_v2_order_creds where blinded_creds->>0 != $1 and request_id = $2 ) as mismatch ` err := dbi.GetContext(ctx, &result, query, blindedCreds[0], requestID) diff --git a/services/skus/datastore_noint_test.go b/services/skus/datastore_noint_test.go index 6fddab83d..49951f8c4 100644 --- a/services/skus/datastore_noint_test.go +++ b/services/skus/datastore_noint_test.go @@ -38,11 +38,31 @@ func TestAreTimeLimitedV2CredsSubmitted(t *testing.T) { tests := []testCase{ { - name: "mismatch", + name: "already_submitted", dbi: &mockGetContext{ getContext: func(ctx context.Context, dest interface{}, query string, args ...interface{}) error { *dest.(*AreTimeLimitedV2CredsSubmittedResult) = AreTimeLimitedV2CredsSubmittedResult{ AlreadySubmitted: true, + Mismatch: false, + } + return nil + }, + }, + given: uuid.Must(uuid.FromString("8f51f9ca-b593-4200-9bfb-91ac34748e09")), + exp: tcExpected{ + noErr: true, + result: map[string]bool{ + "alreadySubmitted": true, + "mismatch": false, + }, + }, + }, + { + name: "mismatch", + dbi: &mockGetContext{ + getContext: func(ctx context.Context, dest interface{}, query string, args ...interface{}) error { + *dest.(*AreTimeLimitedV2CredsSubmittedResult) = AreTimeLimitedV2CredsSubmittedResult{ + AlreadySubmitted: false, Mismatch: true, } return nil @@ -52,6 +72,7 @@ func TestAreTimeLimitedV2CredsSubmitted(t *testing.T) { exp: tcExpected{ noErr: true, result: map[string]bool{ + "alreadySubmitted": false, "mismatch": true, }, }, @@ -63,6 +84,7 @@ func TestAreTimeLimitedV2CredsSubmitted(t *testing.T) { t.Run(tc.name, func(t *testing.T) { result, err := areTimeLimitedV2CredsSubmitted(context.TODO(), tc.dbi, tc.given, "") + should.Equal(t, tc.exp.result["alreadySubmitted"], result.AlreadySubmitted) should.Equal(t, tc.exp.result["mismatch"], result.Mismatch) should.Equal(t, tc.exp.noErr, err == nil) }) diff --git a/services/skus/datastore_test.go b/services/skus/datastore_test.go index ffcfa0634..236935a0f 100644 --- a/services/skus/datastore_test.go +++ b/services/skus/datastore_test.go @@ -684,6 +684,8 @@ func (suite *PostgresTestSuite) TestAreTimeLimitedV2CredsSubmitted() { exp exp } + sharedId := uuid.NewV4() + tests := []testCases{ { name: "no_creds", @@ -694,7 +696,7 @@ func (suite *PostgresTestSuite) TestAreTimeLimitedV2CredsSubmitted() { }, }, { - name: "already_submitted_true_and_mismatch_true", + name: "already_submitted_true_and_mismatch_false", given: tcGiven{ reqID: uuid.NewV4(), timeAwareCrds: TimeAwareSubIssuedCreds{ @@ -704,7 +706,24 @@ func (suite *PostgresTestSuite) TestAreTimeLimitedV2CredsSubmitted() { blindedCreds: []string{"test-cred"}, }, exp: exp{ - submittedCreds: AreTimeLimitedV2CredsSubmittedResult{AlreadySubmitted: true, Mismatch: true}, + submittedCreds: AreTimeLimitedV2CredsSubmittedResult{AlreadySubmitted: true, Mismatch: false}, + mustErr: func(t must.TestingT, err error, i ...interface{}) { + must.NoError(t, err) + }, + }, + }, + { + name: "already_submitted_false_and_mismatch_true", + given: tcGiven{ + reqID: sharedId, + timeAwareCrds: TimeAwareSubIssuedCreds{ + RequestID: sharedId.String(), + BlindedCreds: []string{"other-cred"}, + }, + blindedCreds: []string{"new-cred"}, + }, + exp: exp{ + submittedCreds: AreTimeLimitedV2CredsSubmittedResult{AlreadySubmitted: false, Mismatch: true}, mustErr: func(t must.TestingT, err error, i ...interface{}) { must.NoError(t, err) }, @@ -752,7 +771,7 @@ func (suite *PostgresTestSuite) TestAreTimeLimitedV2CredsSubmitted() { must.NoError(t, err5) } - actual, err := suite.storage.AreTimeLimitedV2CredsSubmitted(ctx, uuid.NewV4(), tc.given.blindedCreds...) + actual, err := suite.storage.AreTimeLimitedV2CredsSubmitted(ctx, tc.given.reqID, tc.given.blindedCreds...) tc.exp.mustErr(t, err) should.Equal(t, tc.exp.submittedCreds, actual)