Skip to content

Commit

Permalink
Move tests out of main package
Browse files Browse the repository at this point in the history
This helps clean things up slightly, and also makes the tests slightly
more readily readable for users looking at them as examples.
  • Loading branch information
aidantwoods committed May 20, 2022
1 parent 41dc809 commit a215f71
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 123 deletions.
107 changes: 54 additions & 53 deletions claims_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package paseto
package paseto_test

import (
"testing"
"time"

"aidanwoods.dev/go-paseto"
"github.com/stretchr/testify/require"
)

func TestAllClaimsPassV2(t *testing.T) {
token := NewToken()
token := paseto.NewToken()

token.SetAudience("a")
token.SetJti("b")
Expand All @@ -19,20 +20,20 @@ func TestAllClaimsPassV2(t *testing.T) {
token.SetNotBefore(time.Now().Add(25 * time.Second))
token.SetIssuedAt(time.Now())

key := NewV2SymmetricKey()
secretKey := NewV2AsymmetricSecretKey()
key := paseto.NewV2SymmetricKey()
secretKey := paseto.NewV2AsymmetricSecretKey()

encrypted := token.V2Encrypt(key)

signed := token.V2Sign(secretKey)

parser := NewParser()
parser.AddRule(ForAudience("a"))
parser.AddRule(IdentifiedBy("b"))
parser.AddRule(IssuedBy("c"))
parser.AddRule(NotExpired())
parser.AddRule(Subject("d"))
parser.AddRule(ValidAt(time.Now().Add(30 * time.Second)))
parser := paseto.NewParser()
parser.AddRule(paseto.ForAudience("a"))
parser.AddRule(paseto.IdentifiedBy("b"))
parser.AddRule(paseto.IssuedBy("c"))
parser.AddRule(paseto.NotExpired())
parser.AddRule(paseto.Subject("d"))
parser.AddRule(paseto.ValidAt(time.Now().Add(30 * time.Second)))

_, err := parser.ParseV2Local(key, encrypted)
require.NoError(t, err)
Expand All @@ -42,7 +43,7 @@ func TestAllClaimsPassV2(t *testing.T) {
}

func TestAllClaimsPassV3(t *testing.T) {
token := NewToken()
token := paseto.NewToken()

token.SetAudience("a")
token.SetJti("b")
Expand All @@ -53,20 +54,20 @@ func TestAllClaimsPassV3(t *testing.T) {
token.SetNotBefore(time.Now().Add(25 * time.Second))
token.SetIssuedAt(time.Now())

key := NewV3SymmetricKey()
secretKey := NewV3AsymmetricSecretKey()
key := paseto.NewV3SymmetricKey()
secretKey := paseto.NewV3AsymmetricSecretKey()

encrypted := token.V3Encrypt(key, nil)

signed := token.V3Sign(secretKey, nil)

parser := NewParser()
parser.AddRule(ForAudience("a"))
parser.AddRule(IdentifiedBy("b"))
parser.AddRule(IssuedBy("c"))
parser.AddRule(NotExpired())
parser.AddRule(Subject("d"))
parser.AddRule(ValidAt(time.Now().Add(30 * time.Second)))
parser := paseto.NewParser()
parser.AddRule(paseto.ForAudience("a"))
parser.AddRule(paseto.IdentifiedBy("b"))
parser.AddRule(paseto.IssuedBy("c"))
parser.AddRule(paseto.NotExpired())
parser.AddRule(paseto.Subject("d"))
parser.AddRule(paseto.ValidAt(time.Now().Add(30 * time.Second)))

_, err := parser.ParseV3Local(key, encrypted, nil)
require.NoError(t, err)
Expand All @@ -76,7 +77,7 @@ func TestAllClaimsPassV3(t *testing.T) {
}

func TestAllClaimsPassV4(t *testing.T) {
token := NewToken()
token := paseto.NewToken()

token.SetAudience("a")
token.SetJti("b")
Expand All @@ -87,20 +88,20 @@ func TestAllClaimsPassV4(t *testing.T) {
token.SetNotBefore(time.Now().Add(25 * time.Second))
token.SetIssuedAt(time.Now())

key := NewV4SymmetricKey()
secretKey := NewV4AsymmetricSecretKey()
key := paseto.NewV4SymmetricKey()
secretKey := paseto.NewV4AsymmetricSecretKey()

encrypted := token.V4Encrypt(key, nil)

signed := token.V4Sign(secretKey, nil)

parser := NewParser()
parser.AddRule(ForAudience("a"))
parser.AddRule(IdentifiedBy("b"))
parser.AddRule(IssuedBy("c"))
parser.AddRule(NotExpired())
parser.AddRule(Subject("d"))
parser.AddRule(ValidAt(time.Now().Add(30 * time.Second)))
parser := paseto.NewParser()
parser.AddRule(paseto.ForAudience("a"))
parser.AddRule(paseto.IdentifiedBy("b"))
parser.AddRule(paseto.IssuedBy("c"))
parser.AddRule(paseto.NotExpired())
parser.AddRule(paseto.Subject("d"))
parser.AddRule(paseto.ValidAt(time.Now().Add(30 * time.Second)))

_, err := parser.ParseV4Local(key, encrypted, nil)
require.NoError(t, err)
Expand All @@ -110,64 +111,64 @@ func TestAllClaimsPassV4(t *testing.T) {
}

func TestFutureIat(t *testing.T) {
token := NewToken()
token := paseto.NewToken()

// simulated check will be 30 seconds from now
token.SetExpiration(time.Now().Add(time.Minute))
token.SetNotBefore(time.Now().Add(25 * time.Second))
token.SetIssuedAt(time.Now().Add(35 * time.Second))

key := NewV4SymmetricKey()
key := paseto.NewV4SymmetricKey()

encrypted := token.V4Encrypt(key, nil)

parser := NewParser()
parser.AddRule(ValidAt(time.Now().Add(30 * time.Second)))
parser := paseto.NewParser()
parser.AddRule(paseto.ValidAt(time.Now().Add(30 * time.Second)))

_, err := parser.ParseV4Local(key, encrypted, nil)
require.Error(t, err)
}

func TestFutureNbf(t *testing.T) {
token := NewToken()
token := paseto.NewToken()

// simulated check will be 30 seconds from now
token.SetExpiration(time.Now().Add(time.Minute))
token.SetNotBefore(time.Now().Add(35 * time.Second))
token.SetIssuedAt(time.Now())

key := NewV4SymmetricKey()
key := paseto.NewV4SymmetricKey()

encrypted := token.V4Encrypt(key, nil)

parser := NewParser()
parser.AddRule(ValidAt(time.Now().Add(30 * time.Second)))
parser := paseto.NewParser()
parser.AddRule(paseto.ValidAt(time.Now().Add(30 * time.Second)))

_, err := parser.ParseV4Local(key, encrypted, nil)
require.Error(t, err)
}

func TestPastExp(t *testing.T) {
token := NewToken()
token := paseto.NewToken()

// simulated check will be 30 seconds from now
token.SetExpiration(time.Now().Add(29 * time.Second))
token.SetNotBefore(time.Now().Add(25 * time.Second))
token.SetIssuedAt(time.Now())

key := NewV4SymmetricKey()
key := paseto.NewV4SymmetricKey()

encrypted := token.V4Encrypt(key, nil)

parser := NewParser()
parser.AddRule(ValidAt(time.Now().Add(30 * time.Second)))
parser := paseto.NewParser()
parser.AddRule(paseto.ValidAt(time.Now().Add(30 * time.Second)))

_, err := parser.ParseV4Local(key, encrypted, nil)
require.Error(t, err)
}

func TestReadMeExample(t *testing.T) {
token := NewToken()
token := paseto.NewToken()

token.SetAudience("audience")
token.SetJti("identifier")
Expand All @@ -179,20 +180,20 @@ func TestReadMeExample(t *testing.T) {
token.SetIssuedAt(time.Now())

secretKeyHex := "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a37741eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2"
secretKey, _ := NewV4AsymmetricSecretKeyFromHex(secretKeyHex)
secretKey, _ := paseto.NewV4AsymmetricSecretKeyFromHex(secretKeyHex)

signed := token.V4Sign(secretKey, nil)

parser := NewParser()
parser.AddRule(ForAudience("audience"))
parser.AddRule(IdentifiedBy("identifier"))
parser.AddRule(IssuedBy("issuer"))
parser.AddRule(Subject("subject"))
parser.AddRule(NotExpired())
parser.AddRule(ValidAt(time.Now()))
parser := paseto.NewParser()
parser.AddRule(paseto.ForAudience("audience"))
parser.AddRule(paseto.IdentifiedBy("identifier"))
parser.AddRule(paseto.IssuedBy("issuer"))
parser.AddRule(paseto.Subject("subject"))
parser.AddRule(paseto.NotExpired())
parser.AddRule(paseto.ValidAt(time.Now()))

publicKeyHex := "1eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2"
publicKey, _ := NewV4AsymmetricPublicKeyFromHex(publicKeyHex)
publicKey, _ := paseto.NewV4AsymmetricPublicKeyFromHex(publicKeyHex)

parsedToken, err := parser.ParseV4Public(publicKey, signed, nil)
require.NoError(t, err)
Expand Down
37 changes: 37 additions & 0 deletions export_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package paseto

// This file is only compiled during tests due to the _test suffix. It serves
// the purpose of exporting some internal functions, which are used in
// paseto_test.

type Packet = packet

var NewMessage = newMessage
var NewPacket = newPacket

var V2LocalDecrypt = v2LocalDecrypt
var V2LocalEncrypt = v2LocalEncrypt
var V2PublicVerify = v2PublicVerify
var V2PublicSign = v2PublicSign

var V3LocalDecrypt = v3LocalDecrypt
var V3LocalEncrypt = v3LocalEncrypt
var V3PublicVerify = v3PublicVerify
var V3PublicSign = v3PublicSign

var V4LocalDecrypt = v4LocalDecrypt
var V4LocalEncrypt = v4LocalEncrypt
var V4PublicVerify = v4PublicVerify
var V4PublicSign = v4PublicSign

func (m message) Encoded() string {
return m.encoded()
}

func (p packet) Content() []byte {
return p.content
}

func (p packet) Footer() []byte {
return p.footer
}
Loading

0 comments on commit a215f71

Please sign in to comment.