app-cloudep-member-server/pkg/usecase/verify_google_test.go

85 lines
2.1 KiB
Go
Raw Normal View History

2024-12-30 03:58:14 +00:00
package usecase
import (
"app-cloudep-member-server/pkg/domain"
"app-cloudep-member-server/pkg/domain/usecase"
"code.30cm.net/digimon/library-go/errs"
"code.30cm.net/digimon/library-go/errs/code"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
"time"
)
func TestValidateGoogleTokenInfo(t *testing.T) {
expectedClientID := "test-client-id"
tests := []struct {
name string
tokenInfo usecase.GoogleTokenInfo
expectedErr error
}{
{
name: "Valid token",
tokenInfo: usecase.GoogleTokenInfo{
Exp: strconv.FormatInt(time.Now().UTC().Add(10*time.Minute).Unix(), 10),
Aud: expectedClientID,
EmailVerified: "true",
},
expectedErr: nil,
},
{
name: "Token expired",
tokenInfo: usecase.GoogleTokenInfo{
Exp: strconv.FormatInt(time.Now().UTC().Add(-10*time.Minute).Unix(), 10),
Aud: expectedClientID,
EmailVerified: "true",
},
expectedErr: errs.ThirdPartyError(
code.CloudEPMember,
domain.FailedToVerifyGoogleTokenExpired,
"token is expired",
),
},
{
name: "Invalid audience",
tokenInfo: usecase.GoogleTokenInfo{
Exp: strconv.FormatInt(time.Now().UTC().Add(10*time.Minute).Unix(), 10),
Aud: "invalid-client-id",
EmailVerified: "true",
},
expectedErr: errs.ThirdPartyError(
code.CloudEPMember,
domain.FailedToVerifyGoogleInvalidAudience,
"invalid audience",
),
},
{
name: "Email not verified",
tokenInfo: usecase.GoogleTokenInfo{
Exp: strconv.FormatInt(time.Now().UTC().Add(10*time.Minute).Unix(), 10),
Aud: expectedClientID,
EmailVerified: "false",
},
expectedErr: errs.ThirdPartyError(
code.CloudEPMember,
domain.FailedToVerifyGoogle,
"email is not verified",
),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := validateGoogleTokenInfo(test.tokenInfo, expectedClientID)
if test.expectedErr != nil {
assert.NotNil(t, err)
assert.EqualError(t, err, test.expectedErr.Error())
} else {
assert.NoError(t, err)
}
})
}
}