customer/internal/service/oauth/oauth_test.go
2023-05-16 04:12:34 +03:00

168 lines
5.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package oauth_test
import (
"context"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"golang.org/x/oauth2"
"penahub.gitlab.yandexcloud.net/pena-services/pena-social-auth/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/pena-social-auth/internal/service/oauth"
"penahub.gitlab.yandexcloud.net/pena-services/pena-social-auth/internal/service/oauth/mocks"
)
type SocialUserInformation struct {
ID string
}
var (
code = "code"
socialUserInformation = SocialUserInformation{
ID: "userid1",
}
oauthTokens = oauth2.Token{
AccessToken: "access-token-auth",
RefreshToken: "refresh-token-auth",
}
)
func TestGetUserInformationByCode(t *testing.T) {
t.Run("Успешное получение информации о пользователе oauth через код", func(t *testing.T) {
serviceClient := mocks.NewServiceClient[SocialUserInformation](t)
oauthClient := mocks.NewOauthClient(t)
oauthService := oauth.New(&oauth.Deps[SocialUserInformation]{
Logger: logrus.New(),
ServiceClient: serviceClient,
OAuthClient: oauthClient,
})
oauthCodeExchangeCall := oauthClient.EXPECT().
Exchange(mock.Anything, code).
Return(&oauthTokens, nil).
Once()
serviceClient.EXPECT().
GetUserInformation(mock.Anything, oauthTokens.AccessToken).
Return(&socialUserInformation, nil).
NotBefore(oauthCodeExchangeCall).
Once()
information, err := oauthService.GetUserInformationByCode(context.Background(), code)
assert.NoError(t, err)
assert.Equal(t, &socialUserInformation, information)
})
t.Run("Ошибка при запросе за информацией о пользователе oauth системы", func(t *testing.T) {
serviceClient := mocks.NewServiceClient[SocialUserInformation](t)
oauthClient := mocks.NewOauthClient(t)
oauthService := oauth.New(&oauth.Deps[SocialUserInformation]{
Logger: logrus.New(),
ServiceClient: serviceClient,
OAuthClient: oauthClient,
})
oauthCodeExchangeCall := oauthClient.EXPECT().
Exchange(mock.Anything, code).
Return(&oauthTokens, nil).
Once()
serviceClient.EXPECT().
GetUserInformation(mock.Anything, oauthTokens.AccessToken).
Return(nil, errors.ErrEmptyArgs).
NotBefore(oauthCodeExchangeCall).
Once()
information, err := oauthService.GetUserInformationByCode(context.Background(), code)
assert.Error(t, err)
assert.EqualError(t, err, errors.ErrEmptyArgs.Error())
assert.Nil(t, information)
})
t.Run("Ошибка при обмене ключа на токены при получении информации об oauth пользователе", func(t *testing.T) {
serviceClient := mocks.NewServiceClient[SocialUserInformation](t)
oauthClient := mocks.NewOauthClient(t)
oauthService := oauth.New(&oauth.Deps[SocialUserInformation]{
Logger: logrus.New(),
ServiceClient: serviceClient,
OAuthClient: oauthClient,
})
oauthClient.EXPECT().
Exchange(mock.Anything, code).
Return(nil, errors.ErrEmptyArgs).
Once()
serviceClient.AssertNotCalled(t, "GetUserInformation")
information, err := oauthService.GetUserInformationByCode(context.Background(), code)
assert.Error(t, err)
assert.EqualError(t, err, errors.ErrEmptyArgs.Error())
assert.Nil(t, information)
})
}
func TestGenerateURL(t *testing.T) {
generatedLink := "https://link"
accessToken := "access-token"
t.Run("Успешная генерация ссылки авторизации", func(t *testing.T) {
oauthClient := mocks.NewOauthClient(t)
oauthService := oauth.New(&oauth.Deps[any]{
OAuthClient: oauthClient,
})
oauthClient.EXPECT().AuthCodeURL(oauthService.GetState(), oauth2.AccessTypeOffline).Return(generatedLink).Once()
assert.Equal(t, generatedLink, oauthService.GenerateAuthURL())
})
t.Run("Успешная генерация ссылки привязки аккаунта", func(t *testing.T) {
oauthClient := mocks.NewOauthClient(t)
oauthService := oauth.New(&oauth.Deps[any]{
OAuthClient: oauthClient,
})
oauthClient.EXPECT().
AuthCodeURL(
oauthService.GetState(),
oauth2.AccessTypeOffline,
oauth2.SetAuthURLParam("accessToken", accessToken),
).
Return(generatedLink).
Once()
assert.Equal(t, generatedLink, oauthService.GenerateLinkURL(accessToken))
})
}
func TestGetState(t *testing.T) {
t.Run("Успешное получение state", func(t *testing.T) {
oauthService := oauth.New(&oauth.Deps[any]{})
assert.NotEmpty(t, oauthService.GetState())
assert.NotNil(t, oauthService.GetState())
assert.NotZero(t, oauthService.GetState())
})
}
func TestValidateState(t *testing.T) {
t.Run("Невалидный state", func(t *testing.T) {
oauthService := oauth.New(&oauth.Deps[any]{})
assert.Equal(t, false, oauthService.ValidateState(""))
})
t.Run("Валидный state", func(t *testing.T) {
oauthService := oauth.New(&oauth.Deps[any]{})
assert.Equal(t, true, oauthService.ValidateState(oauthService.GetState()))
})
}