Dawid Wysokiński
e302cb026d
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: twhelp/core#41
172 lines
3.9 KiB
Go
172 lines
3.9 KiB
Go
package rest_test
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/rest"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
"gitea.dwysokinski.me/twhelp/core/internal/rest/internal/mock"
|
|
"gitea.dwysokinski.me/twhelp/core/internal/rest/internal/model"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func TestVersionHandler_list(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
setup func(svc *mock.FakeVersionService)
|
|
expectedStatus int
|
|
target any
|
|
expectedResponse any
|
|
expectedTotalCount string
|
|
}{
|
|
{
|
|
name: "OK: without any params",
|
|
setup: func(svc *mock.FakeVersionService) {
|
|
versions := []domain.Version{
|
|
{
|
|
Code: "pl",
|
|
Name: "Poland",
|
|
Host: "plemiona.pl",
|
|
Timezone: "Europe/Warsaw",
|
|
},
|
|
{
|
|
Code: "uk",
|
|
Name: "United Kingdom",
|
|
Host: "tribalwars.co.uk",
|
|
Timezone: "Europe/London",
|
|
},
|
|
{
|
|
Code: "hu",
|
|
Name: "Hungary",
|
|
Host: "klanhaboru.hu",
|
|
Timezone: "Europe/Budapest",
|
|
},
|
|
}
|
|
svc.ListReturns(versions, int64(len(versions)), nil)
|
|
},
|
|
expectedStatus: http.StatusOK,
|
|
target: &model.ListVersionsResp{},
|
|
expectedResponse: &model.ListVersionsResp{
|
|
Data: []model.Version{
|
|
{
|
|
Code: "pl",
|
|
Name: "Poland",
|
|
Host: "plemiona.pl",
|
|
Timezone: "Europe/Warsaw",
|
|
},
|
|
{
|
|
Code: "uk",
|
|
Name: "United Kingdom",
|
|
Host: "tribalwars.co.uk",
|
|
Timezone: "Europe/London",
|
|
},
|
|
{
|
|
Code: "hu",
|
|
Name: "Hungary",
|
|
Host: "klanhaboru.hu",
|
|
Timezone: "Europe/Budapest",
|
|
},
|
|
},
|
|
},
|
|
expectedTotalCount: "3",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
svc := mock.FakeVersionService{}
|
|
if tt.setup != nil {
|
|
tt.setup(&svc)
|
|
}
|
|
|
|
router := chi.NewRouter()
|
|
rest.NewVersion(&svc).Register(router)
|
|
|
|
resp := doRequest(router, http.MethodGet, "/api/v1/versions", nil)
|
|
defer resp.Body.Close()
|
|
assertTotalCount(t, resp.Header, tt.expectedTotalCount)
|
|
assertJSONResponse(t, resp, tt.expectedStatus, tt.expectedResponse, tt.target)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestVersion_getByCode(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
setup func(svc *mock.FakeVersionService)
|
|
code string
|
|
expectedStatus int
|
|
target any
|
|
expectedResponse any
|
|
}{
|
|
{
|
|
name: "OK",
|
|
setup: func(svc *mock.FakeVersionService) {
|
|
svc.GetByCodeReturns(domain.Version{
|
|
Code: "pl",
|
|
Name: "Poland",
|
|
Host: "plemiona.pl",
|
|
Timezone: "Europe/Warsaw",
|
|
}, nil)
|
|
},
|
|
code: "pl",
|
|
expectedStatus: http.StatusOK,
|
|
target: &model.GetVersionByCodeResp{},
|
|
expectedResponse: &model.GetVersionByCodeResp{
|
|
Data: model.Version{
|
|
Code: "pl",
|
|
Name: "Poland",
|
|
Host: "plemiona.pl",
|
|
Timezone: "Europe/Warsaw",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: version not found",
|
|
setup: func(svc *mock.FakeVersionService) {
|
|
svc.GetByCodeReturns(domain.Version{}, domain.ErrVersionNotFound)
|
|
},
|
|
code: "pl2",
|
|
expectedStatus: http.StatusNotFound,
|
|
target: &model.ErrorResponse{},
|
|
expectedResponse: &model.ErrorResponse{
|
|
Error: model.APIError{
|
|
Code: domain.ErrVersionNotFound.Code().String(),
|
|
Message: domain.ErrVersionNotFound.Message(),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
svc := mock.FakeVersionService{}
|
|
if tt.setup != nil {
|
|
tt.setup(&svc)
|
|
}
|
|
|
|
router := chi.NewRouter()
|
|
rest.NewVersion(&svc).Register(router)
|
|
|
|
resp := doRequest(router, http.MethodGet, "/api/v1/versions/"+tt.code, nil)
|
|
defer resp.Body.Close()
|
|
assertJSONResponse(t, resp, tt.expectedStatus, tt.expectedResponse, tt.target)
|
|
})
|
|
}
|
|
}
|