chrly/http/skin_test.go

159 lines
4.6 KiB
Go
Raw Normal View History

package http
import (
2019-04-28 03:13:22 +05:30
"net/http"
"net/http/httptest"
"testing"
"github.com/golang/mock/gomock"
testify "github.com/stretchr/testify/assert"
"github.com/elyby/chrly/db"
"github.com/elyby/chrly/model"
)
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)
}
2019-04-28 03:13:22 +05:30
var skinsTestCases = []*skinsTestCase{
{
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"))
},
},
{
Name: "Obtain skin for unknown username that exists in Mojang and has a cape",
2019-04-28 03:13:22 +05:30
ExistsInLocalStorage: false,
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"))
},
},
{
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,
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)
},
},
{
Name: "Obtain skin for unknown username that doesn't exists in Mojang",
2019-04-28 03:13:22 +05:30
ExistsInLocalStorage: false,
ExistsInMojang: false,
2019-04-28 03:13:22 +05:30
AssertResponse: func(assert *testify.Assertions, resp *http.Response) {
assert.Equal(404, resp.StatusCode)
},
},
}
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)
2019-04-28 03:13:22 +05:30
ctrl := gomock.NewController(t)
defer ctrl.Finish()
2019-04-28 03:13:22 +05:30
config, mocks := setupMocks(ctrl)
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"})
}
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)
}
2019-04-28 03:13:22 +05:30
req := httptest.NewRequest("GET", testCase.RequestUrl, nil)
w := httptest.NewRecorder()
2019-04-28 03:13:22 +05:30
config.CreateHandler().ServeHTTP(w, req)
2019-04-28 03:13:22 +05:30
resp := w.Result()
testCase.AssertResponse(assert, resp)
}
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)
})
}
})
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)
})
}
2019-04-28 03:13:22 +05:30
t.Run("Should trim trailing slash", func(t *testing.T) {
assert := testify.New(t)
2019-04-28 03:13:22 +05:30
req := httptest.NewRequest("GET", "http://chrly/skins/?name=notch", nil)
w := httptest.NewRecorder()
2019-04-28 03:13:22 +05:30
(&Config{}).CreateHandler().ServeHTTP(w, req)
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"))
})
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)
2019-04-28 03:13:22 +05:30
ctrl := gomock.NewController(t)
defer ctrl.Finish()
2019-04-28 03:13:22 +05:30
config, mocks := setupMocks(ctrl)
mocks.Log.EXPECT().IncCounter("skins.get_request", int64(1))
2019-04-28 03:13:22 +05:30
req := httptest.NewRequest("GET", "http://chrly/skins", nil)
w := httptest.NewRecorder()
2019-04-28 03:13:22 +05:30
config.CreateHandler().ServeHTTP(w, req)
2019-04-28 03:13:22 +05:30
resp := w.Result()
assert.Equal(400, resp.StatusCode)
})
})
}
func createSkinModel(username string, isSlim bool) *model.Skin {
return &model.Skin{
2018-01-23 02:46:42 +05:30
UserId: 1,
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
SkinId: 1,
2019-04-28 03:13:22 +05:30
Url: "http://chrly/skin.png",
MojangTextures: "mocked textures base64",
MojangSignature: "mocked signature",
IsSlim: isSlim,
}
}