mjwt/auth/pair_test.go

36 lines
942 B
Go
Raw Normal View History

package auth
import (
2023-10-29 12:28:21 +00:00
"github.com/1f349/mjwt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestCreateTokenPair(t *testing.T) {
2023-10-29 12:28:21 +00:00
t.Parallel()
ps := NewPermStorage()
ps.Set("mjwt:test")
ps.Set("mjwt:test2")
kStore := mjwt.NewKeyStore()
s, err := mjwt.NewIssuerWithKeyStore("mjwt.test", "key2", kStore)
2024-06-09 20:40:13 +01:00
assert.NoError(t, err)
accessToken, refreshToken, err := CreateTokenPair(s, "1", "test", "test2", nil, nil, ps)
2024-06-09 20:40:13 +01:00
assert.NoError(t, err)
_, b, err := mjwt.ExtractClaims[AccessTokenClaims](kStore, accessToken)
2024-06-09 20:40:13 +01:00
assert.NoError(t, err)
assert.Equal(t, "1", b.Subject)
assert.Equal(t, "test", b.ID)
assert.True(t, b.Claims.Perms.Has("mjwt:test"))
assert.True(t, b.Claims.Perms.Has("mjwt:test2"))
assert.False(t, b.Claims.Perms.Has("mjwt:test3"))
_, b2, err := mjwt.ExtractClaims[RefreshTokenClaims](kStore, refreshToken)
2024-06-09 20:40:13 +01:00
assert.NoError(t, err)
assert.Equal(t, "1", b2.Subject)
assert.Equal(t, "test2", b2.ID)
}