chrly/http/textures_test.go

195 lines
5.7 KiB
Go
Raw Normal View History

package http
import (
"bytes"
"io/ioutil"
"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"
)
func TestConfig_Textures(t *testing.T) {
2019-04-28 03:13:22 +05:30
t.Run("Obtain textures for exists user with only default skin", 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)
2019-04-28 03:13:22 +05:30
mocks.Log.EXPECT().IncCounter("textures.request", int64(1))
2019-04-28 03:13:22 +05:30
mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
mocks.Capes.EXPECT().FindByUsername("mock_user").Return(nil, &db.CapeNotFoundError{Who: "mock_user"})
2019-04-28 03:13:22 +05:30
req := httptest.NewRequest("GET", "http://chrly/textures/mock_user", nil)
w := httptest.NewRecorder()
2019-04-28 03:13:22 +05:30
config.CreateHandler().ServeHTTP(w, req)
resp := w.Result()
assert.Equal(200, resp.StatusCode)
assert.Equal("application/json", resp.Header.Get("Content-Type"))
response, _ := ioutil.ReadAll(resp.Body)
assert.JSONEq(`{
"SKIN": {
"url": "http://chrly/skin.png"
}
}`, string(response))
})
t.Run("Obtain textures for exists user with only slim skin", 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)
2019-04-28 03:13:22 +05:30
mocks.Log.EXPECT().IncCounter("textures.request", int64(1))
2019-04-28 03:13:22 +05:30
mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", true), nil)
mocks.Capes.EXPECT().FindByUsername("mock_user").Return(nil, &db.CapeNotFoundError{Who: "mock_user"})
2019-04-28 03:13:22 +05:30
req := httptest.NewRequest("GET", "http://chrly/textures/mock_user", 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(200, resp.StatusCode)
assert.Equal("application/json", resp.Header.Get("Content-Type"))
response, _ := ioutil.ReadAll(resp.Body)
assert.JSONEq(`{
"SKIN": {
"url": "http://chrly/skin.png",
"metadata": {
"model": "slim"
}
}
2019-04-28 03:13:22 +05:30
}`, string(response))
})
2019-04-28 03:13:22 +05:30
t.Run("Obtain textures for exists user with only cape", 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)
2019-04-28 03:13:22 +05:30
mocks.Log.EXPECT().IncCounter("textures.request", int64(1))
2019-04-28 03:13:22 +05:30
mocks.Skins.EXPECT().FindByUsername("mock_user").Return(nil, &db.SkinNotFoundError{Who: "mock_user"})
mocks.Capes.EXPECT().FindByUsername("mock_user").Return(&model.Cape{File: bytes.NewReader(createCape())}, nil)
2019-04-28 03:13:22 +05:30
req := httptest.NewRequest("GET", "http://chrly/textures/mock_user", 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(200, resp.StatusCode)
assert.Equal("application/json", resp.Header.Get("Content-Type"))
response, _ := ioutil.ReadAll(resp.Body)
assert.JSONEq(`{
"CAPE": {
"url": "http://chrly/cloaks/mock_user"
}
}`, string(response))
})
t.Run("Obtain textures for exists user with skin and cape", func(t *testing.T) {
assert := testify.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config, mocks := setupMocks(ctrl)
mocks.Log.EXPECT().IncCounter("textures.request", int64(1))
mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
mocks.Capes.EXPECT().FindByUsername("mock_user").Return(&model.Cape{File: bytes.NewReader(createCape())}, nil)
req := httptest.NewRequest("GET", "http://chrly/textures/mock_user", nil)
w := httptest.NewRecorder()
config.CreateHandler().ServeHTTP(w, req)
2019-04-28 03:13:22 +05:30
resp := w.Result()
assert.Equal(200, resp.StatusCode)
assert.Equal("application/json", resp.Header.Get("Content-Type"))
response, _ := ioutil.ReadAll(resp.Body)
assert.JSONEq(`{
"SKIN": {
"url": "http://chrly/skin.png"
},
"CAPE": {
"url": "http://chrly/cloaks/mock_user"
}
}`, string(response))
})
t.Run("Obtain textures for not exists user that exists in Mojang", func(t *testing.T) {
assert := testify.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config, mocks := setupMocks(ctrl)
2019-04-28 03:13:22 +05:30
mocks.Log.EXPECT().IncCounter("textures.request", int64(1))
2019-04-28 03:13:22 +05:30
mocks.Skins.EXPECT().FindByUsername("mock_username").Return(nil, &db.SkinNotFoundError{})
mocks.Capes.EXPECT().FindByUsername("mock_username").Return(nil, &db.CapeNotFoundError{})
mocks.Queue.On("GetTexturesForUsername", "mock_username").Once().Return(createTexturesResponse(true, true))
req := httptest.NewRequest("GET", "http://chrly/textures/mock_username", nil)
w := httptest.NewRecorder()
config.CreateHandler().ServeHTTP(w, req)
resp := w.Result()
assert.Equal(200, resp.StatusCode)
assert.Equal("application/json", resp.Header.Get("Content-Type"))
response, _ := ioutil.ReadAll(resp.Body)
assert.JSONEq(`{
"SKIN": {
"url": "http://mojang/skin.png"
},
"CAPE": {
"url": "http://mojang/cape.png"
}
}`, string(response))
})
2019-04-28 03:13:22 +05:30
t.Run("Obtain textures for not exists user that not exists in Mojang too", 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("textures.request", int64(1))
mocks.Skins.EXPECT().FindByUsername("mock_username").Return(nil, &db.SkinNotFoundError{})
mocks.Capes.EXPECT().FindByUsername("mock_username").Return(nil, &db.CapeNotFoundError{})
mocks.Queue.On("GetTexturesForUsername", "mock_username").Once().Return(nil)
req := httptest.NewRequest("GET", "http://chrly/textures/mock_username", nil)
w := httptest.NewRecorder()
config.CreateHandler().ServeHTTP(w, req)
resp := w.Result()
assert.Equal(204, resp.StatusCode)
2019-04-28 03:13:22 +05:30
})
}