Skip to content

Commit

Permalink
test: implement tests for issuer keys
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbrm committed May 23, 2024
1 parent dc4bcc5 commit 23e4ff4
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 19 deletions.
199 changes: 199 additions & 0 deletions model/issuer_keys_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
36 changes: 17 additions & 19 deletions model/issuer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model_test
package model

import (
"testing"
Expand All @@ -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
}

Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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
}

Expand All @@ -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)),
Expand All @@ -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)),
Expand Down

0 comments on commit 23e4ff4

Please sign in to comment.