diff options
| author | Shulhan <ms@kilabit.info> | 2026-03-30 19:23:29 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-03-30 21:07:31 +0700 |
| commit | 61a44bf239f5545080af83f890d488ceaf257a80 (patch) | |
| tree | 26273fdb5618c9623919f6830a24baa5a2c945b3 /lib/paseto/payload_test.go | |
| parent | 6e3d2f12d6617a6caa45b4a2eeec2dc54aa8d17a (diff) | |
| download | pakakeh.go-61a44bf239f5545080af83f890d488ceaf257a80.tar.xz | |
lib/paseto: add unit tests for Payload and Message types
Diffstat (limited to 'lib/paseto/payload_test.go')
| -rw-r--r-- | lib/paseto/payload_test.go | 117 |
1 files changed, 90 insertions, 27 deletions
diff --git a/lib/paseto/payload_test.go b/lib/paseto/payload_test.go index 75f18ba4..e24eb208 100644 --- a/lib/paseto/payload_test.go +++ b/lib/paseto/payload_test.go @@ -5,40 +5,103 @@ package paseto import ( "testing" + "testing/synctest" "time" "git.sr.ht/~shulhan/pakakeh.go/lib/test" ) func TestPayload_Validate(t *testing.T) { - now := time.Now().UTC().Unix() - peer := Peer{} + synctest.Test(t, func(t *testing.T) { + now := time.Now().UTC().Unix() + drifted1s := now + 1 + drifted2x := now + (DriftSeconds * 2) - issued1sAgo := now - 1 - issued6sAgo := now - 6 + listCase := []struct { + desc string + receiverID string + expErr string + sender Peer + pload Payload + }{{ + desc: `ErrPayloadIssuer`, + sender: Peer{ + ID: `me`, + }, + pload: Payload{ + Issuer: `other`, + }, + expErr: `payload: unknown issuer`, + }, { + desc: `ErrPayloadSubject`, + sender: Peer{ + ID: `sender`, + AllowedSubjects: map[string]struct{}{ + `create`: struct{}{}, + `update`: struct{}{}, + }, + }, + pload: Payload{ + Issuer: `sender`, + Subject: `delete`, + }, + expErr: `payload: unknown subject: delete`, + }, { + desc: `ErrPayloadAudience`, + sender: Peer{ + ID: `sender`, + AllowedSubjects: map[string]struct{}{ + `create`: struct{}{}, + `update`: struct{}{}, + }, + }, + receiverID: `receiver`, + pload: Payload{ + Issuer: `sender`, + Audience: `other`, + Subject: `create`, + }, + expErr: `payload: invalid audience: other`, + }, { + desc: `ErrPayloadExpired`, + pload: Payload{ + ExpiredAt: now, + }, + expErr: `payload: expired: exp is 2000-01-01 00:00:00 +0000 UTC`, + }, { + desc: `NotBefore drifted 1s`, + pload: Payload{ + NotBefore: drifted1s, + }, + }, { + desc: `ErrPayloadNotBefore`, + pload: Payload{ + NotBefore: drifted2x, + }, + expErr: `payload: token cannot be used yet: nbf is 2000-01-01 00:00:10 +0000 UTC`, + }, { + desc: `OK`, + sender: Peer{ + ID: `sender`, + AllowedSubjects: map[string]struct{}{ + `create`: struct{}{}, + `update`: struct{}{}, + }, + }, + receiverID: `receiver`, + pload: Payload{ + Issuer: `sender`, + Audience: `receiver`, + Subject: `create`, + }, + }} - listCase := []struct { - desc string - pload *Payload - expErr string - }{{ - desc: `With IssuedAt less than current time`, - pload: &Payload{ - IssuedAt: issued1sAgo, - }, - }, { - desc: `With IssuedAt greater than drift`, - pload: &Payload{ - IssuedAt: issued6sAgo, - }, - expErr: `payload: issued-at is after current time`, - }} - - for _, tc := range listCase { - err := tc.pload.Validate(``, peer) - if err != nil { - test.Assert(t, tc.desc, tc.expErr, err.Error()) - continue + for _, tc := range listCase { + err := tc.pload.Validate(tc.receiverID, tc.sender) + if err != nil { + test.Assert(t, tc.desc, tc.expErr, err.Error()) + continue + } } - } + }) } |
