Skip to content

Commit

Permalink
Add parallel testing for sign_test.go and signer_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrymomot committed Mar 10, 2024
1 parent 2cba487 commit c987b4f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
36 changes: 36 additions & 0 deletions sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import (
)

func TestSha1(t *testing.T) {
t.Parallel()

signingKey := []byte("signing-key")

t.Run("success: string", func(t *testing.T) {
t.Parallel()

testData := "test123"

token, err := signature.NewToken(signingKey, testData, 0, signature.CalculateHmac)
Expand All @@ -24,6 +28,8 @@ func TestSha1(t *testing.T) {
})

t.Run("success: int", func(t *testing.T) {
t.Parallel()

testData := 123

token, err := signature.NewToken(signingKey, testData, 0, signature.CalculateHmac)
Expand All @@ -36,6 +42,8 @@ func TestSha1(t *testing.T) {
})

t.Run("success: struct", func(t *testing.T) {
t.Parallel()

type example struct {
ID uint64
Text string
Expand All @@ -53,6 +61,8 @@ func TestSha1(t *testing.T) {
})

t.Run("invalid signature", func(t *testing.T) {
t.Parallel()

testData := "test123"

token, err := signature.NewToken(signingKey, testData, 0, signature.CalculateHmac)
Expand All @@ -66,12 +76,16 @@ func TestSha1(t *testing.T) {
})

t.Run("invalid token format", func(t *testing.T) {
t.Parallel()

token := "invalid-token"
_, err := signature.ParseToken[string](signingKey, token, signature.CalculateHmac, signature.ValidateHmac)
require.ErrorIs(t, err, signature.ErrInvalidTokenFormat)
})

t.Run("invalid base64 string", func(t *testing.T) {
t.Parallel()

token := "invalid==.token"
_, err := signature.ParseToken[string](signingKey, token, signature.CalculateHmac, signature.ValidateHmac)
require.ErrorIs(t, err, signature.ErrInvalidToken)
Expand All @@ -83,9 +97,13 @@ func TestSha1(t *testing.T) {
}

func TestSha256(t *testing.T) {
t.Parallel()

signingKey := []byte("signing-key")

t.Run("success", func(t *testing.T) {
t.Parallel()

testData := "test123"

token, err := signature.NewToken(signingKey, testData, 0, signature.CalculateHmac256)
Expand All @@ -98,6 +116,8 @@ func TestSha256(t *testing.T) {
})

t.Run("invalid signature", func(t *testing.T) {
t.Parallel()

testData := "test123"

token, err := signature.NewToken(signingKey, testData, 0, signature.CalculateHmac256)
Expand All @@ -112,9 +132,13 @@ func TestSha256(t *testing.T) {
}

func TestSha1Temp(t *testing.T) {
t.Parallel()

signingKey := []byte("signing-key")

t.Run("success", func(t *testing.T) {
t.Parallel()

testData := "test123"
ttl := time.Second * 5

Expand All @@ -128,6 +152,8 @@ func TestSha1Temp(t *testing.T) {
})

t.Run("invalid signature", func(t *testing.T) {
t.Parallel()

testData := "test123"
ttl := time.Second * 5

Expand All @@ -142,6 +168,8 @@ func TestSha1Temp(t *testing.T) {
})

t.Run("expired token", func(t *testing.T) {
t.Parallel()

testData := "test123"

token, err := signature.NewToken(signingKey, testData, 1, signature.CalculateHmac)
Expand All @@ -156,9 +184,13 @@ func TestSha1Temp(t *testing.T) {
}

func TestSha256Temp(t *testing.T) {
t.Parallel()

signingKey := []byte("signing-key")

t.Run("success", func(t *testing.T) {
t.Parallel()

testData := "test123"
ttl := time.Second * 5

Expand All @@ -172,6 +204,8 @@ func TestSha256Temp(t *testing.T) {
})

t.Run("invalid signature", func(t *testing.T) {
t.Parallel()

testData := "test123"
ttl := time.Second * 5

Expand All @@ -186,6 +220,8 @@ func TestSha256Temp(t *testing.T) {
})

t.Run("expired token", func(t *testing.T) {
t.Parallel()

testData := "test123"

token, err := signature.NewToken(signingKey, testData, 1, signature.CalculateHmac256)
Expand Down
18 changes: 18 additions & 0 deletions signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import (
)

func TestSigner_String(t *testing.T) {
t.Parallel()

s := signature.NewSigner[string]([]byte("signing-key"))
require.NotNil(t, s)

t.Run("success", func(t *testing.T) {
t.Parallel()

testData := "test123"

token, err := s.Sign(testData)
Expand All @@ -25,6 +29,8 @@ func TestSigner_String(t *testing.T) {
})

t.Run("success: temporary", func(t *testing.T) {
t.Parallel()

testData := "test123"

token, err := s.SignTemporary(testData, time.Second*5)
Expand All @@ -38,6 +44,8 @@ func TestSigner_String(t *testing.T) {
}

func TestSigner_Struct(t *testing.T) {
t.Parallel()

type example struct {
ID uint64
Text string
Expand All @@ -47,6 +55,8 @@ func TestSigner_Struct(t *testing.T) {
require.NotNil(t, s)

t.Run("success", func(t *testing.T) {
t.Parallel()

testData := example{
ID: 123,
Text: "test123",
Expand All @@ -62,6 +72,8 @@ func TestSigner_Struct(t *testing.T) {
})

t.Run("success: temporary", func(t *testing.T) {
t.Parallel()

testData := example{
ID: 123,
Text: "test123",
Expand All @@ -78,10 +90,14 @@ func TestSigner_Struct(t *testing.T) {
}

func TestSigner256_String(t *testing.T) {
t.Parallel()

s := signature.NewSigner256[string]([]byte("signing-key"))
require.NotNil(t, s)

t.Run("success", func(t *testing.T) {
t.Parallel()

testData := "test123"

token, err := s.Sign(testData)
Expand All @@ -94,6 +110,8 @@ func TestSigner256_String(t *testing.T) {
})

t.Run("success: temporary", func(t *testing.T) {
t.Parallel()

testData := "test123"

token, err := s.SignTemporary(testData, time.Second*5)
Expand Down

0 comments on commit c987b4f

Please sign in to comment.