2020-04-19 05:01:09 +05:30
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2024-01-30 13:35:04 +05:30
|
|
|
"strings"
|
2020-04-19 05:01:09 +05:30
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
"github.com/elyby/chrly/db"
|
|
|
|
"github.com/elyby/chrly/internal/profiles"
|
2020-04-19 05:01:09 +05:30
|
|
|
)
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
type ProfilesManagerMock struct {
|
|
|
|
mock.Mock
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
func (m *ProfilesManagerMock) PersistProfile(profile *db.Profile) error {
|
|
|
|
return m.Called(profile).Error(0)
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
func (m *ProfilesManagerMock) RemoveProfileByUuid(uuid string) error {
|
|
|
|
return m.Called(uuid).Error(0)
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
type ApiTestSuite struct {
|
|
|
|
suite.Suite
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
App *Api
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
ProfilesManager *ProfilesManagerMock
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
func (t *ApiTestSuite) SetupSubTest() {
|
|
|
|
t.ProfilesManager = &ProfilesManagerMock{}
|
|
|
|
t.App = &Api{
|
|
|
|
ProfilesManager: t.ProfilesManager,
|
|
|
|
}
|
|
|
|
}
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
func (t *ApiTestSuite) TearDownSubTest() {
|
|
|
|
t.ProfilesManager.AssertExpectations(t.T())
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
func (t *ApiTestSuite) TestPostProfile() {
|
|
|
|
t.Run("successfully post profile", func() {
|
|
|
|
t.ProfilesManager.On("PersistProfile", &db.Profile{
|
|
|
|
Uuid: "0f657aa8-bfbe-415d-b700-5750090d3af3",
|
|
|
|
Username: "mock_username",
|
|
|
|
SkinUrl: "https://example.com/skin.png",
|
|
|
|
SkinModel: "slim",
|
|
|
|
CapeUrl: "https://example.com/cape.png",
|
|
|
|
MojangTextures: "bW9jawo=",
|
|
|
|
MojangSignature: "bW9jawo=",
|
|
|
|
}).Once().Return(nil)
|
|
|
|
|
|
|
|
req := httptest.NewRequest("POST", "http://chrly/profiles", bytes.NewBufferString(url.Values{
|
2023-12-22 06:26:02 +05:30
|
|
|
"uuid": {"0f657aa8-bfbe-415d-b700-5750090d3af3"},
|
2024-01-30 13:35:04 +05:30
|
|
|
"username": {"mock_username"},
|
|
|
|
"skinUrl": {"https://example.com/skin.png"},
|
|
|
|
"skinModel": {"slim"},
|
|
|
|
"capeUrl": {"https://example.com/cape.png"},
|
|
|
|
"mojangTextures": {"bW9jawo="},
|
|
|
|
"mojangSignature": {"bW9jawo="},
|
|
|
|
}.Encode()))
|
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
w := httptest.NewRecorder()
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.App.Handler().ServeHTTP(w, req)
|
|
|
|
result := w.Result()
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.Equal(http.StatusCreated, result.StatusCode)
|
|
|
|
body, _ := io.ReadAll(result.Body)
|
|
|
|
t.Empty(body)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("handle malformed body", func() {
|
|
|
|
req := httptest.NewRequest("POST", "http://chrly/profiles", strings.NewReader("invalid;=url?encoded_string"))
|
2020-04-19 05:01:09 +05:30
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.App.Handler().ServeHTTP(w, req)
|
|
|
|
result := w.Result()
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.Equal(http.StatusBadRequest, result.StatusCode)
|
|
|
|
body, _ := io.ReadAll(result.Body)
|
|
|
|
t.JSONEq(`{
|
2020-04-19 05:01:09 +05:30
|
|
|
"errors": {
|
2024-01-30 13:35:04 +05:30
|
|
|
"body": [
|
|
|
|
"The body of the request must be a valid url-encoded string"
|
2020-04-19 05:01:09 +05:30
|
|
|
]
|
|
|
|
}
|
|
|
|
}`, string(body))
|
|
|
|
})
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.Run("receive validation errors", func() {
|
|
|
|
t.ProfilesManager.On("PersistProfile", mock.Anything).Once().Return(&profiles.ValidationError{
|
|
|
|
Errors: map[string][]string{
|
|
|
|
"mock": {"error1", "error2"},
|
|
|
|
},
|
|
|
|
})
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
req := httptest.NewRequest("POST", "http://chrly/profiles", strings.NewReader(""))
|
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
2020-04-19 05:01:09 +05:30
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.App.Handler().ServeHTTP(w, req)
|
|
|
|
result := w.Result()
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.Equal(http.StatusBadRequest, result.StatusCode)
|
|
|
|
body, _ := io.ReadAll(result.Body)
|
|
|
|
t.JSONEq(`{
|
|
|
|
"errors": {
|
|
|
|
"mock": [
|
|
|
|
"error1",
|
|
|
|
"error2"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`, string(body))
|
2020-04-19 05:01:09 +05:30
|
|
|
})
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.Run("receive other error", func() {
|
|
|
|
t.ProfilesManager.On("PersistProfile", mock.Anything).Once().Return(errors.New("mock error"))
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
req := httptest.NewRequest("POST", "http://chrly/profiles", strings.NewReader(""))
|
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
2020-04-19 05:01:09 +05:30
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.App.Handler().ServeHTTP(w, req)
|
|
|
|
result := w.Result()
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.Equal(http.StatusInternalServerError, result.StatusCode)
|
2020-04-19 05:01:09 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
func (t *ApiTestSuite) TestDeleteProfileByUuid() {
|
|
|
|
t.Run("successfully delete", func() {
|
|
|
|
t.ProfilesManager.On("RemoveProfileByUuid", "0f657aa8-bfbe-415d-b700-5750090d3af3").Once().Return(nil)
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
req := httptest.NewRequest("DELETE", "http://chrly/profiles/0f657aa8-bfbe-415d-b700-5750090d3af3", nil)
|
2020-04-19 05:01:09 +05:30
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.App.Handler().ServeHTTP(w, req)
|
2020-04-19 05:01:09 +05:30
|
|
|
|
|
|
|
resp := w.Result()
|
2024-01-30 13:35:04 +05:30
|
|
|
t.Equal(http.StatusNoContent, resp.StatusCode)
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
t.Empty(body)
|
2020-04-19 05:01:09 +05:30
|
|
|
})
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.Run("error from manager", func() {
|
|
|
|
t.ProfilesManager.On("RemoveProfileByUuid", mock.Anything).Return(errors.New("mock error"))
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
req := httptest.NewRequest("DELETE", "http://chrly/profiles/0f657aa8-bfbe-415d-b700-5750090d3af3", nil)
|
2020-04-19 05:01:09 +05:30
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
t.App.Handler().ServeHTTP(w, req)
|
2020-04-19 05:01:09 +05:30
|
|
|
|
|
|
|
resp := w.Result()
|
2024-01-30 13:35:04 +05:30
|
|
|
t.Equal(http.StatusInternalServerError, resp.StatusCode)
|
2020-04-19 05:01:09 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
func TestApi(t *testing.T) {
|
|
|
|
suite.Run(t, new(ApiTestSuite))
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|