2024-01-10 06:12:10 +05:30
|
|
|
package mojang
|
|
|
|
|
|
|
|
import (
|
2024-02-07 06:06:18 +05:30
|
|
|
"context"
|
2024-01-10 06:12:10 +05:30
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockUuidsProvider struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (m *mockUuidsProvider) GetUuid(ctx context.Context, username string) (*ProfileInfo, error) {
|
|
|
|
args := m.Called(ctx, username)
|
2024-01-10 06:12:10 +05:30
|
|
|
var result *ProfileInfo
|
|
|
|
if casted, ok := args.Get(0).(*ProfileInfo); ok {
|
|
|
|
result = casted
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, args.Error(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TexturesProviderMock struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (m *TexturesProviderMock) GetTextures(ctx context.Context, uuid string) (*ProfileResponse, error) {
|
|
|
|
args := m.Called(ctx, uuid)
|
2024-01-30 13:35:04 +05:30
|
|
|
var result *ProfileResponse
|
|
|
|
if casted, ok := args.Get(0).(*ProfileResponse); ok {
|
2024-01-10 06:12:10 +05:30
|
|
|
result = casted
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, args.Error(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
type providerTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
Provider *MojangTexturesProvider
|
|
|
|
UuidsProvider *mockUuidsProvider
|
|
|
|
TexturesProvider *TexturesProviderMock
|
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (s *providerTestSuite) SetupTest() {
|
|
|
|
s.UuidsProvider = &mockUuidsProvider{}
|
|
|
|
s.TexturesProvider = &TexturesProviderMock{}
|
2024-01-10 06:12:10 +05:30
|
|
|
|
2024-02-19 18:24:12 +05:30
|
|
|
s.Provider, _ = NewMojangTexturesProvider(
|
|
|
|
s.UuidsProvider,
|
|
|
|
s.TexturesProvider,
|
|
|
|
)
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (s *providerTestSuite) TearDownTest() {
|
|
|
|
s.UuidsProvider.AssertExpectations(s.T())
|
|
|
|
s.TexturesProvider.AssertExpectations(s.T())
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (s *providerTestSuite) TestGetForValidUsernameSuccessfully() {
|
2024-01-10 06:12:10 +05:30
|
|
|
expectedProfile := &ProfileInfo{Id: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Name: "username"}
|
2024-01-30 13:35:04 +05:30
|
|
|
expectedResult := &ProfileResponse{Id: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Name: "username"}
|
2024-02-07 06:06:18 +05:30
|
|
|
ctx := context.Background()
|
2024-01-10 06:12:10 +05:30
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
s.UuidsProvider.On("GetUuid", ctx, "username").Once().Return(expectedProfile, nil)
|
|
|
|
s.TexturesProvider.On("GetTextures", ctx, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").Once().Return(expectedResult, nil)
|
2024-01-10 06:12:10 +05:30
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
result, err := s.Provider.GetForUsername(ctx, "username")
|
2024-01-10 06:12:10 +05:30
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
s.NoError(err)
|
|
|
|
s.Same(expectedResult, result)
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (s *providerTestSuite) TestGetForUsernameWhichHasNoMojangAccount() {
|
|
|
|
s.UuidsProvider.On("GetUuid", mock.Anything, "username").Once().Return(nil, nil)
|
2024-01-10 06:12:10 +05:30
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
result, err := s.Provider.GetForUsername(context.Background(), "username")
|
2024-01-10 06:12:10 +05:30
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
s.NoError(err)
|
|
|
|
s.Nil(result)
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (s *providerTestSuite) TestGetForUsernameWhichHasMojangAccountButHasNoMojangSkin() {
|
2024-01-10 06:12:10 +05:30
|
|
|
expectedProfile := &ProfileInfo{Id: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Name: "username"}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
s.UuidsProvider.On("GetUuid", mock.Anything, "username").Once().Return(expectedProfile, nil)
|
|
|
|
s.TexturesProvider.On("GetTextures", mock.Anything, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").Once().Return(nil, nil)
|
2024-01-10 06:12:10 +05:30
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
result, err := s.Provider.GetForUsername(context.Background(), "username")
|
2024-01-10 06:12:10 +05:30
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
s.NoError(err)
|
|
|
|
s.Nil(result)
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (s *providerTestSuite) TestGetForTheSameUsernameInRow() {
|
2024-01-10 06:12:10 +05:30
|
|
|
expectedProfile := &ProfileInfo{Id: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Name: "username"}
|
2024-01-30 13:35:04 +05:30
|
|
|
expectedResult := &ProfileResponse{Id: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Name: "username"}
|
2024-01-10 06:12:10 +05:30
|
|
|
|
|
|
|
awaitChan := make(chan time.Time)
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
s.UuidsProvider.On("GetUuid", mock.Anything, "username").Once().WaitUntil(awaitChan).Return(expectedProfile, nil)
|
|
|
|
s.TexturesProvider.On("GetTextures", mock.Anything, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").Once().Return(expectedResult, nil)
|
2024-01-10 06:12:10 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
results := make([]*ProfileResponse, 2)
|
2024-01-10 06:12:10 +05:30
|
|
|
var wgStarted sync.WaitGroup
|
|
|
|
var wgDone sync.WaitGroup
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
wgStarted.Add(1)
|
|
|
|
wgDone.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
wgStarted.Done()
|
2024-02-07 06:06:18 +05:30
|
|
|
textures, _ := s.Provider.GetForUsername(context.Background(), "username")
|
2024-01-10 06:12:10 +05:30
|
|
|
results[i] = textures
|
|
|
|
wgDone.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wgStarted.Wait()
|
|
|
|
close(awaitChan)
|
|
|
|
wgDone.Wait()
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
s.Same(expectedResult, results[0])
|
|
|
|
s.Same(expectedResult, results[1])
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (s *providerTestSuite) TestGetForTheSameUsernameOneAfterAnother() {
|
|
|
|
expectedProfile := &ProfileInfo{Id: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Name: "username"}
|
|
|
|
expectedResult := &ProfileResponse{Id: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Name: "username"}
|
|
|
|
|
|
|
|
s.UuidsProvider.On("GetUuid", mock.Anything, "username").Times(2).Return(expectedProfile, nil)
|
|
|
|
s.TexturesProvider.On("GetTextures", mock.Anything, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").Times(2).Return(expectedResult, nil)
|
|
|
|
|
|
|
|
// Just ensure that providers will be called twice
|
|
|
|
_, _ = s.Provider.GetForUsername(context.Background(), "username")
|
|
|
|
time.Sleep(time.Millisecond * 20)
|
|
|
|
_, _ = s.Provider.GetForUsername(context.Background(), "username")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *providerTestSuite) TestGetForNotAllowedMojangUsername() {
|
|
|
|
result, err := s.Provider.GetForUsername(context.Background(), "Not allowed")
|
|
|
|
s.ErrorIs(err, InvalidUsername)
|
|
|
|
s.Nil(result)
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (s *providerTestSuite) TestGetErrorFromUuidsProvider() {
|
2024-01-10 06:12:10 +05:30
|
|
|
err := errors.New("mock error")
|
2024-02-07 06:06:18 +05:30
|
|
|
s.UuidsProvider.On("GetUuid", mock.Anything, "username").Once().Return(nil, err)
|
2024-01-10 06:12:10 +05:30
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
result, resErr := s.Provider.GetForUsername(context.Background(), "username")
|
|
|
|
s.Nil(result)
|
|
|
|
s.Equal(err, resErr)
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (s *providerTestSuite) TestGetErrorFromTexturesProvider() {
|
2024-01-10 06:12:10 +05:30
|
|
|
expectedProfile := &ProfileInfo{Id: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Name: "username"}
|
|
|
|
err := errors.New("mock error")
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
s.UuidsProvider.On("GetUuid", mock.Anything, "username").Once().Return(expectedProfile, nil)
|
|
|
|
s.TexturesProvider.On("GetTextures", mock.Anything, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").Once().Return(nil, err)
|
2024-01-10 06:12:10 +05:30
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
result, resErr := s.Provider.GetForUsername(context.Background(), "username")
|
|
|
|
s.Nil(result)
|
|
|
|
s.Same(err, resErr)
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvider(t *testing.T) {
|
|
|
|
suite.Run(t, new(providerTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNilProvider_GetForUsername(t *testing.T) {
|
|
|
|
provider := &NilProvider{}
|
2024-02-07 06:06:18 +05:30
|
|
|
result, err := provider.GetForUsername(context.Background(), "username")
|
2024-01-10 06:12:10 +05:30
|
|
|
require.Nil(t, result)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|