Skip to content

Commit

Permalink
Assert expected errors in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aidantwoods committed Dec 31, 2022
1 parent 012c02d commit 315beca
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func TestFutureIat(t *testing.T) {

_, err := parser.ParseV4Local(key, encrypted, nil)
require.Error(t, err)
require.ErrorIs(t, err, &paseto.RuleError{})
require.NotErrorIs(t, err, &paseto.TokenError{})
}

func TestFutureNbf(t *testing.T) {
Expand All @@ -146,6 +148,8 @@ func TestFutureNbf(t *testing.T) {

_, err := parser.ParseV4Local(key, encrypted, nil)
require.Error(t, err)
require.ErrorIs(t, err, &paseto.RuleError{})
require.NotErrorIs(t, err, &paseto.TokenError{})
}

func TestFutureNbfNotBeforeNbfRule(t *testing.T) {
Expand Down Expand Up @@ -184,6 +188,8 @@ func TestFutureNbfNotBeforeNbfRuleError(t *testing.T) {

_, err := parser.ParseV4Local(key, encrypted, nil)
require.Error(t, err)
require.ErrorIs(t, err, &paseto.RuleError{})
require.NotErrorIs(t, err, &paseto.TokenError{})
}

func TestPastExp(t *testing.T) {
Expand All @@ -203,6 +209,8 @@ func TestPastExp(t *testing.T) {

_, err := parser.ParseV4Local(key, encrypted, nil)
require.Error(t, err)
require.ErrorIs(t, err, &paseto.RuleError{})
require.NotErrorIs(t, err, &paseto.TokenError{})
}

func TestReadMeExample(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ func TestV2AsymmetricSecretKeyImport(t *testing.T) {

_, err := paseto.NewV2AsymmetricSecretKeyFromHex(badKey)
require.Error(t, err)
require.NotErrorIs(t, err, &paseto.RuleError{})
require.NotErrorIs(t, err, &paseto.TokenError{})

goodKey := "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a37741eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2"

Expand All @@ -24,6 +26,8 @@ func TestV4AsymmetricSecretKeyImport(t *testing.T) {

_, err := paseto.NewV4AsymmetricSecretKeyFromHex(badKey)
require.Error(t, err)
require.NotErrorIs(t, err, &paseto.RuleError{})
require.NotErrorIs(t, err, &paseto.TokenError{})

goodKey := "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a37741eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2"

Expand Down
8 changes: 8 additions & 0 deletions token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestSomeInt(t *testing.T) {
var output string
err = token.Get("foo", &output)
require.Error(t, err)
require.NotErrorIs(t, err, &paseto.RuleError{})
require.NotErrorIs(t, err, &paseto.TokenError{})

var intOutput int
err = token.Get("foo", &intOutput)
Expand All @@ -46,6 +48,8 @@ func TestSomeBool(t *testing.T) {
var intOutput int
err = token.Get("foo", &intOutput)
require.Error(t, err)
require.NotErrorIs(t, err, &paseto.RuleError{})
require.NotErrorIs(t, err, &paseto.TokenError{})

var output bool
err = token.Get("foo", &output)
Expand Down Expand Up @@ -92,6 +96,8 @@ func TestSomeWrongType(t *testing.T) {
var output bool
err = token.Get("baz", &output)
require.Error(t, err)
require.NotErrorIs(t, err, &paseto.RuleError{})
require.NotErrorIs(t, err, &paseto.TokenError{})
}

func TestSomeWrongKey(t *testing.T) {
Expand All @@ -103,6 +109,8 @@ func TestSomeWrongKey(t *testing.T) {
var output string
err = token.Get("bar", &output)
require.Error(t, err)
require.NotErrorIs(t, err, &paseto.RuleError{})
require.NotErrorIs(t, err, &paseto.TokenError{})
}

func TestFromMap(t *testing.T) {
Expand Down
24 changes: 24 additions & 0 deletions vectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ func TestV2(t *testing.T) {
message, err := paseto.NewMessage(paseto.V2Local, test.Token)
if test.ExpectFail {
require.Error(t, err)
require.ErrorIs(t, err, &paseto.TokenError{})
require.NotErrorIs(t, err, &paseto.RuleError{})
return
}
require.NoError(t, err)

decoded, err = paseto.V2LocalDecrypt(message, sk)
if test.ExpectFail {
require.Error(t, err)
require.ErrorIs(t, err, &paseto.TokenError{})
require.NotErrorIs(t, err, &paseto.RuleError{})
return
}
require.NoError(t, err)
Expand All @@ -69,13 +73,17 @@ func TestV2(t *testing.T) {
message, err := paseto.NewMessage(paseto.V2Public, test.Token)
if test.ExpectFail {
require.Error(t, err)
require.ErrorIs(t, err, &paseto.TokenError{})
require.NotErrorIs(t, err, &paseto.RuleError{})
return
}
require.NoError(t, err)

decoded, err = paseto.V2PublicVerify(message, pk)
if test.ExpectFail {
require.Error(t, err)
require.ErrorIs(t, err, &paseto.TokenError{})
require.NotErrorIs(t, err, &paseto.RuleError{})
return
}
require.NoError(t, err)
Expand Down Expand Up @@ -135,13 +143,17 @@ func TestV3(t *testing.T) {
message, err := paseto.NewMessage(paseto.V3Local, test.Token)
if test.ExpectFail {
require.Error(t, err)
require.ErrorIs(t, err, &paseto.TokenError{})
require.NotErrorIs(t, err, &paseto.RuleError{})
return
}
require.NoError(t, err)

decoded, err = paseto.V3LocalDecrypt(message, sk, []byte(test.ImplicitAssertation))
if test.ExpectFail {
require.Error(t, err)
require.ErrorIs(t, err, &paseto.TokenError{})
require.NotErrorIs(t, err, &paseto.RuleError{})
return
}
require.NoError(t, err)
Expand All @@ -154,13 +166,17 @@ func TestV3(t *testing.T) {
message, err := paseto.NewMessage(paseto.V3Public, test.Token)
if test.ExpectFail {
require.Error(t, err)
require.ErrorIs(t, err, &paseto.TokenError{})
require.NotErrorIs(t, err, &paseto.RuleError{})
return
}
require.NoError(t, err)

decoded, err = paseto.V3PublicVerify(message, pk, []byte(test.ImplicitAssertation))
if test.ExpectFail {
require.Error(t, err)
require.ErrorIs(t, err, &paseto.TokenError{})
require.NotErrorIs(t, err, &paseto.RuleError{})
return
}
require.NoError(t, err)
Expand Down Expand Up @@ -235,13 +251,17 @@ func TestV4(t *testing.T) {
message, err := paseto.NewMessage(paseto.V4Local, test.Token)
if test.ExpectFail {
require.Error(t, err)
require.ErrorIs(t, err, &paseto.TokenError{})
require.NotErrorIs(t, err, &paseto.RuleError{})
return
}
require.NoError(t, err)

decoded, err = paseto.V4LocalDecrypt(message, sk, []byte(test.ImplicitAssertation))
if test.ExpectFail {
require.Error(t, err)
require.ErrorIs(t, err, &paseto.TokenError{})
require.NotErrorIs(t, err, &paseto.RuleError{})
return
}
require.NoError(t, err)
Expand All @@ -254,13 +274,17 @@ func TestV4(t *testing.T) {
message, err := paseto.NewMessage(paseto.V4Public, test.Token)
if test.ExpectFail {
require.Error(t, err)
require.ErrorIs(t, err, &paseto.TokenError{})
require.NotErrorIs(t, err, &paseto.RuleError{})
return
}
require.NoError(t, err)

decoded, err = paseto.V4PublicVerify(message, pk, []byte(test.ImplicitAssertation))
if test.ExpectFail {
require.Error(t, err)
require.ErrorIs(t, err, &paseto.TokenError{})
require.NotErrorIs(t, err, &paseto.RuleError{})
return
}
require.NoError(t, err)
Expand Down

0 comments on commit 315beca

Please sign in to comment.