2019-11-21 04:03:13 +05:30
|
|
|
package mojangtextures
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
"github.com/elyby/chrly/api/mojang"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mojangUuidToTexturesRequestMock struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *mojangUuidToTexturesRequestMock) UuidToTextures(uuid string, signed bool) (*mojang.SignedTexturesResponse, error) {
|
|
|
|
args := o.Called(uuid, signed)
|
|
|
|
var result *mojang.SignedTexturesResponse
|
|
|
|
if casted, ok := args.Get(0).(*mojang.SignedTexturesResponse); ok {
|
|
|
|
result = casted
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, args.Error(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mojangApiTexturesProviderTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
Provider *MojangApiTexturesProvider
|
2020-02-08 15:58:10 +05:30
|
|
|
Emitter *mockEmitter
|
2019-11-21 04:03:13 +05:30
|
|
|
MojangApi *mojangUuidToTexturesRequestMock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *mojangApiTexturesProviderTestSuite) SetupTest() {
|
2020-02-08 15:58:10 +05:30
|
|
|
suite.Emitter = &mockEmitter{}
|
2019-11-21 04:03:13 +05:30
|
|
|
suite.MojangApi = &mojangUuidToTexturesRequestMock{}
|
|
|
|
|
|
|
|
suite.Provider = &MojangApiTexturesProvider{
|
2020-02-08 15:58:10 +05:30
|
|
|
Emitter: suite.Emitter,
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
uuidToTextures = suite.MojangApi.UuidToTextures
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *mojangApiTexturesProviderTestSuite) TearDownTest() {
|
|
|
|
suite.MojangApi.AssertExpectations(suite.T())
|
2020-02-08 15:58:10 +05:30
|
|
|
suite.Emitter.AssertExpectations(suite.T())
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestMojangApiTexturesProvider(t *testing.T) {
|
|
|
|
suite.Run(t, new(mojangApiTexturesProviderTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *mojangApiTexturesProviderTestSuite) TestGetTextures() {
|
|
|
|
expectedResult := &mojang.SignedTexturesResponse{
|
|
|
|
Id: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
Name: "username",
|
|
|
|
}
|
|
|
|
suite.MojangApi.On("UuidToTextures", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", true).Once().Return(expectedResult, nil)
|
|
|
|
|
2020-02-08 15:58:10 +05:30
|
|
|
suite.Emitter.On("Emit",
|
|
|
|
"mojang_textures:mojang_api_textures_provider:before_request",
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
).Once()
|
|
|
|
suite.Emitter.On("Emit",
|
|
|
|
"mojang_textures:mojang_api_textures_provider:after_request",
|
|
|
|
expectedResult,
|
|
|
|
nil,
|
|
|
|
).Once()
|
2019-11-21 04:03:13 +05:30
|
|
|
|
|
|
|
result, err := suite.Provider.GetTextures("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
|
|
|
|
|
|
|
suite.Assert().Equal(expectedResult, result)
|
|
|
|
suite.Assert().Nil(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *mojangApiTexturesProviderTestSuite) TestGetTexturesWithError() {
|
2020-02-08 15:58:10 +05:30
|
|
|
var expectedResponse *mojang.SignedTexturesResponse
|
2019-11-21 04:03:13 +05:30
|
|
|
expectedError := &mojang.TooManyRequestsError{}
|
|
|
|
suite.MojangApi.On("UuidToTextures", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", true).Once().Return(nil, expectedError)
|
|
|
|
|
2020-02-08 15:58:10 +05:30
|
|
|
suite.Emitter.On("Emit",
|
|
|
|
"mojang_textures:mojang_api_textures_provider:before_request",
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
).Once()
|
|
|
|
suite.Emitter.On("Emit",
|
|
|
|
"mojang_textures:mojang_api_textures_provider:after_request",
|
|
|
|
expectedResponse,
|
|
|
|
expectedError,
|
|
|
|
).Once()
|
2019-11-21 04:03:13 +05:30
|
|
|
|
|
|
|
result, err := suite.Provider.GetTextures("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
|
|
|
|
|
|
|
suite.Assert().Nil(result)
|
|
|
|
suite.Assert().Equal(expectedError, err)
|
|
|
|
}
|