65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package usecase
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"code.30cm.net/digimon/app-cloudep-permission-server/pkg/domain/token"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAdditional_SetAndGet(t *testing.T) {
|
|
// 初始化 additional
|
|
additional := NewAdditional(map[string]string{})
|
|
|
|
// 測試 Set() 只允許有效 Key
|
|
validCases := map[token.Additional]string{
|
|
token.ID: "12345",
|
|
token.Role: "admin",
|
|
token.Device: "device-001",
|
|
token.UID: "user-999",
|
|
token.Account: "test@example.com",
|
|
token.Scope: "read:write",
|
|
}
|
|
|
|
// 測試有效 Key
|
|
for key, val := range validCases {
|
|
additional.Set(key, val)
|
|
assert.Equal(t, val, additional.Get(key), "Set/Get for key: "+key.String())
|
|
}
|
|
|
|
// 測試 key 未設定時應回傳空字串
|
|
assert.Equal(t, "", additional.Get("non-existent-key"))
|
|
}
|
|
|
|
func TestIsValidAdditional(t *testing.T) {
|
|
// 測試合法的 keys
|
|
assert.True(t, token.IsValidAdditional(token.ID))
|
|
assert.True(t, token.IsValidAdditional(token.Role))
|
|
assert.True(t, token.IsValidAdditional(token.Device))
|
|
assert.True(t, token.IsValidAdditional(token.UID))
|
|
assert.True(t, token.IsValidAdditional(token.Account))
|
|
assert.True(t, token.IsValidAdditional(token.Scope))
|
|
|
|
// 測試不合法的 keys
|
|
assert.False(t, token.IsValidAdditional(token.Additional("unknown")))
|
|
assert.False(t, token.IsValidAdditional(token.Additional("random")))
|
|
assert.False(t, token.IsValidAdditional(token.Additional("invalid-key")))
|
|
}
|
|
|
|
func TestIGetAll(t *testing.T) {
|
|
validCases := map[string]string{
|
|
token.ID.String(): "12345",
|
|
token.Role.String(): "admin",
|
|
token.Device.String(): "device-001",
|
|
token.UID.String(): "user-999",
|
|
token.Account.String(): "test@example.com",
|
|
token.Scope.String(): "read:write",
|
|
}
|
|
|
|
a := NewAdditional(validCases)
|
|
|
|
result := a.GetAll()
|
|
assert.Equal(t, validCases, result)
|
|
|
|
}
|