-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
validate blinded tokens submitted for tlv2 (#2158)
* validation of tlv2 time limited v2 blinded tokens submitted, fixes: #2157 * use default of 2 per interval credentials in this test * review feedback * revert back the test to original * adding test to excercise check * Patches For 2158 (#2160) * Refactor code and tests * Remove unnecessary type conversion * Use better names --------- Co-authored-by: Pavel Brm <[email protected]>
- Loading branch information
Showing
3 changed files
with
340 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
package skus | ||
|
||
import ( | ||
"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 TestCheckNumBlindedCreds(t *testing.T) { | ||
type tcGiven struct { | ||
ord *model.Order | ||
item *model.OrderItem | ||
ncreds int | ||
} | ||
|
||
type testCase struct { | ||
name string | ||
given tcGiven | ||
exp error | ||
} | ||
|
||
tests := []testCase{ | ||
{ | ||
name: "irrelevant_credential_type", | ||
given: tcGiven{ | ||
ord: &model.Order{ | ||
ID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
}, | ||
item: &model.OrderItem{ | ||
ID: uuid.Must(uuid.FromString("82514074-c4f5-4515-8d8d-29ab943615b3")), | ||
OrderID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
CredentialType: timeLimited, | ||
}, | ||
}, | ||
}, | ||
|
||
{ | ||
name: "single_use_valid_1", | ||
given: tcGiven{ | ||
ord: &model.Order{ | ||
ID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
}, | ||
item: &model.OrderItem{ | ||
ID: uuid.Must(uuid.FromString("82514074-c4f5-4515-8d8d-29ab943615b3")), | ||
OrderID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
CredentialType: singleUse, | ||
Quantity: 1, | ||
}, | ||
ncreds: 1, | ||
}, | ||
}, | ||
|
||
{ | ||
name: "single_use_valid_2", | ||
given: tcGiven{ | ||
ord: &model.Order{ | ||
ID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
}, | ||
item: &model.OrderItem{ | ||
ID: uuid.Must(uuid.FromString("82514074-c4f5-4515-8d8d-29ab943615b3")), | ||
OrderID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
CredentialType: singleUse, | ||
Quantity: 2, | ||
}, | ||
ncreds: 1, | ||
}, | ||
}, | ||
|
||
{ | ||
name: "single_use_invalid", | ||
given: tcGiven{ | ||
ord: &model.Order{ | ||
ID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
}, | ||
item: &model.OrderItem{ | ||
ID: uuid.Must(uuid.FromString("82514074-c4f5-4515-8d8d-29ab943615b3")), | ||
OrderID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
CredentialType: singleUse, | ||
Quantity: 2, | ||
}, | ||
ncreds: 3, | ||
}, | ||
exp: errInvalidNCredsSingleUse, | ||
}, | ||
|
||
{ | ||
name: "tlv2_invalid_numPerInterval_missing", | ||
given: tcGiven{ | ||
ord: &model.Order{ | ||
ID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
Metadata: datastore.Metadata{}, | ||
}, | ||
item: &model.OrderItem{ | ||
ID: uuid.Must(uuid.FromString("82514074-c4f5-4515-8d8d-29ab943615b3")), | ||
OrderID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
CredentialType: timeLimitedV2, | ||
Quantity: 1, | ||
}, | ||
ncreds: 6, | ||
}, | ||
exp: model.ErrNumPerIntervalNotSet, | ||
}, | ||
|
||
{ | ||
name: "tlv2_invalid_numPerInterval_invalid", | ||
given: tcGiven{ | ||
ord: &model.Order{ | ||
ID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
Metadata: datastore.Metadata{ | ||
"numPerInterval": "NaN", | ||
}, | ||
}, | ||
item: &model.OrderItem{ | ||
ID: uuid.Must(uuid.FromString("82514074-c4f5-4515-8d8d-29ab943615b3")), | ||
OrderID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
CredentialType: timeLimitedV2, | ||
Quantity: 1, | ||
}, | ||
ncreds: 6, | ||
}, | ||
exp: model.ErrInvalidNumPerInterval, | ||
}, | ||
|
||
{ | ||
name: "tlv2_invalid_numIntervals_missing", | ||
given: tcGiven{ | ||
ord: &model.Order{ | ||
ID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
Metadata: datastore.Metadata{ | ||
// We get a float64 upon fetching from the database. | ||
"numPerInterval": float64(2), | ||
}, | ||
}, | ||
item: &model.OrderItem{ | ||
ID: uuid.Must(uuid.FromString("82514074-c4f5-4515-8d8d-29ab943615b3")), | ||
OrderID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
CredentialType: timeLimitedV2, | ||
Quantity: 1, | ||
}, | ||
ncreds: 6, | ||
}, | ||
exp: model.ErrNumIntervalsNotSet, | ||
}, | ||
|
||
{ | ||
name: "tlv2_invalid_numIntervals_invalid", | ||
given: tcGiven{ | ||
ord: &model.Order{ | ||
ID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
Metadata: datastore.Metadata{ | ||
// We get a float64 upon fetching from the database. | ||
"numPerInterval": float64(2), | ||
"numIntervals": "NaN", | ||
}, | ||
}, | ||
item: &model.OrderItem{ | ||
ID: uuid.Must(uuid.FromString("82514074-c4f5-4515-8d8d-29ab943615b3")), | ||
OrderID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
CredentialType: timeLimitedV2, | ||
Quantity: 1, | ||
}, | ||
ncreds: 6, | ||
}, | ||
exp: model.ErrInvalidNumIntervals, | ||
}, | ||
|
||
{ | ||
name: "tlv2_valid_1", | ||
given: tcGiven{ | ||
ord: &model.Order{ | ||
ID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
Metadata: datastore.Metadata{ | ||
// We get a float64 upon fetching from the database. | ||
"numPerInterval": float64(2), | ||
"numIntervals": float64(3), | ||
}, | ||
}, | ||
item: &model.OrderItem{ | ||
ID: uuid.Must(uuid.FromString("82514074-c4f5-4515-8d8d-29ab943615b3")), | ||
OrderID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
CredentialType: timeLimitedV2, | ||
Quantity: 1, | ||
}, | ||
ncreds: 6, | ||
}, | ||
}, | ||
|
||
{ | ||
name: "tlv2_valid_2", | ||
given: tcGiven{ | ||
ord: &model.Order{ | ||
ID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
Metadata: datastore.Metadata{ | ||
// We get a float64 upon fetching from the database. | ||
"numPerInterval": float64(2), | ||
"numIntervals": float64(4), | ||
}, | ||
}, | ||
item: &model.OrderItem{ | ||
ID: uuid.Must(uuid.FromString("82514074-c4f5-4515-8d8d-29ab943615b3")), | ||
OrderID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
CredentialType: timeLimitedV2, | ||
Quantity: 1, | ||
}, | ||
ncreds: 6, | ||
}, | ||
}, | ||
|
||
{ | ||
name: "tlv2_invalid", | ||
given: tcGiven{ | ||
ord: &model.Order{ | ||
ID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
Metadata: datastore.Metadata{ | ||
// We get a float64 upon fetching from the database. | ||
"numPerInterval": float64(2), | ||
"numIntervals": float64(3), | ||
}, | ||
}, | ||
item: &model.OrderItem{ | ||
ID: uuid.Must(uuid.FromString("82514074-c4f5-4515-8d8d-29ab943615b3")), | ||
OrderID: uuid.Must(uuid.FromString("df140c71-740b-46c9-bedd-27be0b1e6354")), | ||
CredentialType: timeLimitedV2, | ||
Quantity: 1, | ||
}, | ||
ncreds: 7, | ||
}, | ||
exp: errInvalidNCredsTlv2, | ||
}, | ||
} | ||
|
||
for i := range tests { | ||
tc := tests[i] | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
actual := checkNumBlindedCreds(tc.given.ord, tc.given.item, tc.given.ncreds) | ||
|
||
should.Equal(t, tc.exp, actual) | ||
}) | ||
} | ||
} |