library-go/jwt/claims_test.go

73 lines
2.0 KiB
Go
Raw Normal View History

2024-08-27 05:46:58 +00:00
package jwt
import (
"testing"
2024-08-27 05:49:17 +00:00
"github.com/stretchr/testify/require"
2024-08-27 05:46:58 +00:00
)
func TestDataClaimsSettersAndGetters(t *testing.T) {
tests := []struct {
name string
setterFunc func(c DataClaims, value string)
getterFunc func(c DataClaims) string
value string
expectedVal string
}{
{
name: "Set and Get ID",
setterFunc: func(c DataClaims, value string) { c.SetID(value) },
getterFunc: func(c DataClaims) string { return c.ID() },
value: "12345",
expectedVal: "12345",
},
{
name: "Set and Get Role",
setterFunc: func(c DataClaims, value string) { c.SetRole(value) },
getterFunc: func(c DataClaims) string { return c.Role() },
value: "admin",
expectedVal: "admin",
},
{
name: "Set and Get Device ID",
setterFunc: func(c DataClaims, value string) { c.SetDeviceID(value) },
getterFunc: func(c DataClaims) string { return c.DeviceID() },
value: "device123",
expectedVal: "device123",
},
{
name: "Set and Get Scope",
setterFunc: func(c DataClaims, value string) { c.SetScope(value) },
getterFunc: func(c DataClaims) string { return c.Get(scopeCode) },
value: "read",
expectedVal: "read",
},
{
name: "Set and Get UID",
setterFunc: func(c DataClaims, value string) { c.SetUID(value) },
getterFunc: func(c DataClaims) string { return c.UID() },
value: "user123",
expectedVal: "user123",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
claims := DataClaims{}
// Call the setter function
tt.setterFunc(claims, tt.value)
// Call the getter function and verify the result
require.Equal(t, tt.expectedVal, tt.getterFunc(claims), "Expected value does not match")
})
}
}
func TestDataClaimsGetNonExistentKey(t *testing.T) {
claims := DataClaims{}
// 對於不存在的鍵,應返回空字串
require.Equal(t, "", claims.Get("nonexistent_key"), "Should return empty string for non-existent key")
}