From 23e4ff445cd0bad11c59a675240ca59fd0713e72 Mon Sep 17 00:00:00 2001 From: PavelBrm Date: Thu, 23 May 2024 18:36:41 +1200 Subject: [PATCH] test: implement tests for issuer keys --- model/issuer_keys_test.go | 199 ++++++++++++++++++++++++++++++++++++++ model/issuer_test.go | 36 ++++--- 2 files changed, 216 insertions(+), 19 deletions(-) diff --git a/model/issuer_keys_test.go b/model/issuer_keys_test.go index 8b537907..fa6b230b 100644 --- a/model/issuer_keys_test.go +++ b/model/issuer_keys_test.go @@ -1 +1,200 @@ package model + +import ( + "testing" + "time" + + should "github.com/stretchr/testify/assert" + must "github.com/stretchr/testify/require" +) + +func TestIssuerKeys_isActiveV3Leeway(t *testing.T) { + type tcGiven struct { + key *IssuerKeys + now time.Time + lw time.Duration + } + + type tcExpected struct { + val bool + err error + } + + type testCase struct { + name string + given tcGiven + exp tcExpected + } + + tests := []testCase{ + { + name: "invalid_v3", + given: tcGiven{ + key: &IssuerKeys{}, + now: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + lw: 1 * time.Hour, + }, + exp: tcExpected{err: ErrInvalidIV3Key}, + }, + + { + name: "zero_leeway", + given: tcGiven{ + key: &IssuerKeys{ + StartAt: ptrTo(time.Date(2023, time.December, 31, 0, 0, 1, 0, time.UTC)), + EndAt: ptrTo(time.Date(2024, time.January, 2, 0, 0, 1, 0, time.UTC)), + }, + now: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + }, + exp: tcExpected{val: true}, + }, + + { + name: "leeway_1hour", + given: tcGiven{ + key: &IssuerKeys{ + StartAt: ptrTo(time.Date(2023, time.December, 31, 0, 0, 1, 0, time.UTC)), + EndAt: ptrTo(time.Date(2024, time.January, 2, 0, 0, 1, 0, time.UTC)), + }, + now: time.Date(2024, time.January, 2, 0, 0, 1, 0, time.UTC), + lw: 1 * time.Hour, + }, + exp: tcExpected{val: true}, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual, err := tc.given.key.isActiveV3Leeway(tc.given.now, tc.given.lw) + must.Equal(t, tc.exp.err, err) + + should.Equal(t, tc.exp.val, actual) + }) + } +} + +func TestIssuerKeys_isValidV3(t *testing.T) { + type testCase struct { + name string + given *IssuerKeys + exp bool + } + + tests := []testCase{ + { + name: "invalid_both", + given: &IssuerKeys{}, + }, + + { + name: "invalid_end", + given: &IssuerKeys{ + StartAt: ptrTo(time.Date(2023, time.December, 31, 0, 0, 1, 0, time.UTC)), + }, + }, + + { + name: "invalid_start", + given: &IssuerKeys{ + EndAt: ptrTo(time.Date(2024, time.January, 2, 0, 0, 1, 0, time.UTC)), + }, + }, + + { + name: "valid", + given: &IssuerKeys{ + StartAt: ptrTo(time.Date(2023, time.December, 31, 0, 0, 1, 0, time.UTC)), + EndAt: ptrTo(time.Date(2024, time.January, 2, 0, 0, 1, 0, time.UTC)), + }, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := tc.given.isValidV3() + should.Equal(t, tc.exp, actual) + }) + } +} + +func TestIsTimeWithin(t *testing.T) { + type tcGiven struct { + start time.Time + end time.Time + now time.Time + } + + type testCase struct { + name string + given tcGiven + exp bool + } + + tests := []testCase{ + { + name: "zero_all", + given: tcGiven{}, + }, + + { + name: "zero_start_end", + given: tcGiven{ + now: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + }, + }, + + { + name: "zero_start_now", + given: tcGiven{ + end: time.Date(2024, time.January, 2, 0, 0, 1, 0, time.UTC), + }, + }, + + { + name: "zero_now_end", + given: tcGiven{ + start: time.Date(2023, time.December, 31, 0, 0, 1, 0, time.UTC), + }, + }, + + { + name: "zero_now", + given: tcGiven{ + start: time.Date(2023, time.December, 31, 0, 0, 1, 0, time.UTC), + end: time.Date(2024, time.January, 2, 0, 0, 1, 0, time.UTC), + }, + }, + + { + name: "invalid_inverse", + given: tcGiven{ + start: time.Date(2024, time.January, 2, 0, 0, 1, 0, time.UTC), + end: time.Date(2023, time.December, 31, 0, 0, 1, 0, time.UTC), + now: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + }, + }, + + { + name: "valid", + given: tcGiven{ + start: time.Date(2023, time.December, 31, 0, 0, 1, 0, time.UTC), + end: time.Date(2024, time.January, 2, 0, 0, 1, 0, time.UTC), + now: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), + }, + exp: true, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := isTimeWithin(tc.given.start, tc.given.end, tc.given.now) + should.Equal(t, tc.exp, actual) + }) + } +} diff --git a/model/issuer_test.go b/model/issuer_test.go index cd669582..a607e77d 100644 --- a/model/issuer_test.go +++ b/model/issuer_test.go @@ -1,4 +1,4 @@ -package model_test +package model import ( "testing" @@ -9,13 +9,11 @@ import ( should "github.com/stretchr/testify/assert" crypto "github.com/brave-intl/challenge-bypass-ristretto-ffi" - - "github.com/brave-intl/challenge-bypass-server/model" ) func TestIssuer_HasExpired(t *testing.T) { type tcGiven struct { - issuer *model.Issuer + issuer *Issuer now time.Time } @@ -29,7 +27,7 @@ func TestIssuer_HasExpired(t *testing.T) { { name: "expires_at_zero", given: tcGiven{ - issuer: &model.Issuer{ + issuer: &Issuer{ ID: ptrTo(uuid.MustParse("f100ded0-0000-4000-a000-000000000000")), }, now: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), @@ -39,7 +37,7 @@ func TestIssuer_HasExpired(t *testing.T) { { name: "expires_at_same", given: tcGiven{ - issuer: &model.Issuer{ + issuer: &Issuer{ ID: ptrTo(uuid.MustParse("f100ded0-0000-4000-a000-000000000000")), ExpiresAt: pq.NullTime{ Time: time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC), @@ -53,7 +51,7 @@ func TestIssuer_HasExpired(t *testing.T) { { name: "expires_at_after", given: tcGiven{ - issuer: &model.Issuer{ + issuer: &Issuer{ ID: ptrTo(uuid.MustParse("f100ded0-0000-4000-a000-000000000000")), ExpiresAt: pq.NullTime{ Time: time.Date(2024, time.January, 2, 0, 0, 1, 0, time.UTC), @@ -67,7 +65,7 @@ func TestIssuer_HasExpired(t *testing.T) { { name: "expires_at_before", given: tcGiven{ - issuer: &model.Issuer{ + issuer: &Issuer{ ID: ptrTo(uuid.MustParse("f100ded0-0000-4000-a000-000000000000")), ExpiresAt: pq.NullTime{ Time: time.Date(2023, time.December, 31, 23, 59, 59, 0, time.UTC), @@ -92,7 +90,7 @@ func TestIssuer_HasExpired(t *testing.T) { func TestFindSigningKey(t *testing.T) { type tcGiven struct { - issuer *model.Issuer + issuer *Issuer now time.Time } @@ -106,30 +104,30 @@ func TestFindSigningKey(t *testing.T) { { name: "not_v3", given: tcGiven{ - issuer: &model.Issuer{Version: 2}, + issuer: &Issuer{Version: 2}, now: time.Date(2024, time.January, 1, 1, 0, 1, 0, time.UTC), }, - exp: model.ErrInvalidIssuerType, + exp: ErrInvalidIssuerType, }, { name: "invalid_key_both_times", given: tcGiven{ - issuer: &model.Issuer{ + issuer: &Issuer{ Version: 3, - Keys: []model.IssuerKeys{{}}, + Keys: []IssuerKeys{{}}, }, now: time.Date(2024, time.January, 1, 1, 0, 1, 0, time.UTC), }, - exp: model.ErrInvalidIV3Key, + exp: ErrInvalidIV3Key, }, { name: "valid_key_inactive", given: tcGiven{ - issuer: &model.Issuer{ + issuer: &Issuer{ Version: 3, - Keys: []model.IssuerKeys{ + Keys: []IssuerKeys{ { StartAt: ptrTo(time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC)), EndAt: ptrTo(time.Date(2024, time.January, 2, 0, 0, 1, 0, time.UTC)), @@ -138,15 +136,15 @@ func TestFindSigningKey(t *testing.T) { }, now: time.Date(2023, time.December, 31, 0, 0, 1, 0, time.UTC), }, - exp: model.ErrIssuerV3NoCryptoKey, + exp: ErrIssuerV3NoCryptoKey, }, { name: "valid_key_active", given: tcGiven{ - issuer: &model.Issuer{ + issuer: &Issuer{ Version: 3, - Keys: []model.IssuerKeys{ + Keys: []IssuerKeys{ { SigningKey: mustRandomSigningKey(), StartAt: ptrTo(time.Date(2024, time.January, 1, 0, 0, 1, 0, time.UTC)),