2017-08-20 03:52:42 +05:30
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2019-04-28 03:13:22 +05:30
|
|
|
"net/http"
|
2017-08-20 03:52:42 +05:30
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
testify "github.com/stretchr/testify/assert"
|
|
|
|
|
2018-02-16 02:43:57 +05:30
|
|
|
"github.com/elyby/chrly/db"
|
|
|
|
"github.com/elyby/chrly/model"
|
2017-08-20 03:52:42 +05:30
|
|
|
)
|
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
type skinsTestCase struct {
|
|
|
|
Name string
|
|
|
|
RequestUrl string
|
|
|
|
ExpectedLogKey string
|
|
|
|
ExistsInLocalStorage bool
|
|
|
|
ExistsInMojang bool
|
|
|
|
HasSkinInMojangResp bool
|
|
|
|
AssertResponse func(assert *testify.Assertions, resp *http.Response)
|
2017-08-20 03:52:42 +05:30
|
|
|
}
|
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
var skinsTestCases = []*skinsTestCase{
|
|
|
|
{
|
2019-05-01 04:21:16 +05:30
|
|
|
Name: "Obtain skin for known username",
|
2019-04-28 03:13:22 +05:30
|
|
|
ExistsInLocalStorage: true,
|
|
|
|
AssertResponse: func(assert *testify.Assertions, resp *http.Response) {
|
|
|
|
assert.Equal(301, resp.StatusCode)
|
|
|
|
assert.Equal("http://chrly/skin.png", resp.Header.Get("Location"))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-05-01 04:21:16 +05:30
|
|
|
Name: "Obtain skin for unknown username that exists in Mojang and has a cape",
|
2019-04-28 03:13:22 +05:30
|
|
|
ExistsInLocalStorage: false,
|
2019-05-01 04:21:16 +05:30
|
|
|
ExistsInMojang: true,
|
|
|
|
HasSkinInMojangResp: true,
|
2019-04-28 03:13:22 +05:30
|
|
|
AssertResponse: func(assert *testify.Assertions, resp *http.Response) {
|
|
|
|
assert.Equal(301, resp.StatusCode)
|
|
|
|
assert.Equal("http://mojang/skin.png", resp.Header.Get("Location"))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-05-01 04:21:16 +05:30
|
|
|
Name: "Obtain skin for unknown username that exists in Mojang, but don't has a cape",
|
2019-04-28 03:13:22 +05:30
|
|
|
ExistsInLocalStorage: false,
|
2019-05-01 04:21:16 +05:30
|
|
|
ExistsInMojang: true,
|
|
|
|
HasSkinInMojangResp: false,
|
2019-04-28 03:13:22 +05:30
|
|
|
AssertResponse: func(assert *testify.Assertions, resp *http.Response) {
|
|
|
|
assert.Equal(404, resp.StatusCode)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-05-01 04:21:16 +05:30
|
|
|
Name: "Obtain skin for unknown username that doesn't exists in Mojang",
|
2019-04-28 03:13:22 +05:30
|
|
|
ExistsInLocalStorage: false,
|
2019-05-01 04:21:16 +05:30
|
|
|
ExistsInMojang: false,
|
2019-04-28 03:13:22 +05:30
|
|
|
AssertResponse: func(assert *testify.Assertions, resp *http.Response) {
|
|
|
|
assert.Equal(404, resp.StatusCode)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
func TestConfig_Skin(t *testing.T) {
|
|
|
|
performTest := func(t *testing.T, testCase *skinsTestCase) {
|
|
|
|
assert := testify.New(t)
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
config, mocks := setupMocks(ctrl)
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
mocks.Log.EXPECT().IncCounter(testCase.ExpectedLogKey, int64(1))
|
|
|
|
if testCase.ExistsInLocalStorage {
|
|
|
|
mocks.Skins.EXPECT().FindByUsername("mock_username").Return(createSkinModel("mock_username", false), nil)
|
|
|
|
} else {
|
|
|
|
mocks.Skins.EXPECT().FindByUsername("mock_username").Return(nil, &db.SkinNotFoundError{Who: "mock_username"})
|
|
|
|
}
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
if testCase.ExistsInMojang {
|
|
|
|
textures := createTexturesResponse(testCase.HasSkinInMojangResp, true)
|
|
|
|
mocks.Queue.On("GetTexturesForUsername", "mock_username").Return(textures)
|
|
|
|
} else {
|
|
|
|
mocks.Queue.On("GetTexturesForUsername", "mock_username").Return(nil)
|
|
|
|
}
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
req := httptest.NewRequest("GET", testCase.RequestUrl, nil)
|
|
|
|
w := httptest.NewRecorder()
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
config.CreateHandler().ServeHTTP(w, req)
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
resp := w.Result()
|
|
|
|
testCase.AssertResponse(assert, resp)
|
|
|
|
}
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
t.Run("Normal API", func(t *testing.T) {
|
|
|
|
for _, testCase := range skinsTestCases {
|
|
|
|
testCase.RequestUrl = "http://chrly/skins/mock_username"
|
|
|
|
testCase.ExpectedLogKey = "skins.request"
|
|
|
|
t.Run(testCase.Name, func(t *testing.T) {
|
|
|
|
performTest(t, testCase)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
t.Run("GET fallback API", func(t *testing.T) {
|
|
|
|
for _, testCase := range skinsTestCases {
|
|
|
|
testCase.RequestUrl = "http://chrly/skins?name=mock_username"
|
|
|
|
testCase.ExpectedLogKey = "skins.get_request"
|
|
|
|
t.Run(testCase.Name, func(t *testing.T) {
|
|
|
|
performTest(t, testCase)
|
|
|
|
})
|
|
|
|
}
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
t.Run("Should trim trailing slash", func(t *testing.T) {
|
|
|
|
assert := testify.New(t)
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
req := httptest.NewRequest("GET", "http://chrly/skins/?name=notch", nil)
|
|
|
|
w := httptest.NewRecorder()
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
(&Config{}).CreateHandler().ServeHTTP(w, req)
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
resp := w.Result()
|
|
|
|
assert.Equal(301, resp.StatusCode)
|
|
|
|
assert.Equal("http://chrly/skins?name=notch", resp.Header.Get("Location"))
|
|
|
|
})
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
t.Run("Return error when name is not provided", func(t *testing.T) {
|
|
|
|
assert := testify.New(t)
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
config, mocks := setupMocks(ctrl)
|
|
|
|
mocks.Log.EXPECT().IncCounter("skins.get_request", int64(1))
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
req := httptest.NewRequest("GET", "http://chrly/skins", nil)
|
|
|
|
w := httptest.NewRecorder()
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
config.CreateHandler().ServeHTTP(w, req)
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2019-04-28 03:13:22 +05:30
|
|
|
resp := w.Result()
|
|
|
|
assert.Equal(400, resp.StatusCode)
|
|
|
|
})
|
|
|
|
})
|
2017-08-20 03:52:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func createSkinModel(username string, isSlim bool) *model.Skin {
|
|
|
|
return &model.Skin{
|
2018-01-23 02:46:42 +05:30
|
|
|
UserId: 1,
|
2017-08-20 03:52:42 +05:30
|
|
|
Username: username,
|
2019-04-28 03:13:22 +05:30
|
|
|
Uuid: "0f657aa8-bfbe-415d-b700-5750090d3af3", // Use non nil UUID to pass validation in api tests
|
2017-08-20 03:52:42 +05:30
|
|
|
SkinId: 1,
|
2019-04-28 03:13:22 +05:30
|
|
|
Url: "http://chrly/skin.png",
|
2017-08-20 03:52:42 +05:30
|
|
|
MojangTextures: "mocked textures base64",
|
|
|
|
MojangSignature: "mocked signature",
|
|
|
|
IsSlim: isSlim,
|
|
|
|
}
|
|
|
|
}
|