2024-02-25 07:31:13 +00:00
|
|
|
package port_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cmp"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"slices"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
|
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/domain/domaintest"
|
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/port/internal/apimodel"
|
2024-03-01 07:15:06 +00:00
|
|
|
"github.com/brianvoe/gofakeit/v7"
|
2024-02-25 07:31:13 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const endpointListServers = "/v2/versions/%s/servers"
|
|
|
|
|
|
|
|
func TestListServers(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
handler := newAPIHTTPHandler(t)
|
|
|
|
servers := getAllServers(t, handler)
|
|
|
|
version := servers[0].Version
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
reqModifier func(t *testing.T, req *http.Request)
|
|
|
|
assertResp func(t *testing.T, req *http.Request, resp *http.Response)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OK: without params",
|
|
|
|
assertResp: func(t *testing.T, _ *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ListServersResponse](t, resp.Body)
|
|
|
|
assert.Zero(t, body.Cursor.Next)
|
|
|
|
assert.NotZero(t, body.Cursor.Self)
|
|
|
|
assert.NotZero(t, body.Data)
|
|
|
|
assert.True(t, slices.IsSortedFunc(body.Data, func(a, b apimodel.Server) int {
|
|
|
|
if a.Open && !b.Open {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.Open && b.Open {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmp.Compare(a.Key, b.Key)
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OK: limit=1",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Set("limit", "1")
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ListServersResponse](t, resp.Body)
|
|
|
|
assert.NotZero(t, body.Cursor.Next)
|
|
|
|
assert.NotZero(t, body.Cursor.Self)
|
|
|
|
assert.NotZero(t, body.Data)
|
|
|
|
limit, err := strconv.Atoi(req.URL.Query().Get("limit"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, body.Data, limit)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OK: limit=1 cursor",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Set("limit", "1")
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
resp := doCustomRequest(handler, req.Clone(req.Context()))
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body := decodeJSON[apimodel.ListServersResponse](t, resp.Body)
|
|
|
|
require.NotEmpty(t, body.Cursor.Next)
|
|
|
|
|
|
|
|
q.Set("cursor", body.Cursor.Next)
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ListVersionsResponse](t, resp.Body)
|
|
|
|
assert.Equal(t, req.URL.Query().Get("cursor"), body.Cursor.Self)
|
|
|
|
limit, err := strconv.Atoi(req.URL.Query().Get("limit"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, body.Data, limit)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-02-25 09:21:33 +00:00
|
|
|
name: "OK: open=false",
|
2024-02-25 07:31:13 +00:00
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
q := req.URL.Query()
|
2024-02-25 09:21:33 +00:00
|
|
|
q.Set("open", "false")
|
2024-02-25 07:31:13 +00:00
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, _ *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ListServersResponse](t, resp.Body)
|
|
|
|
assert.NotZero(t, body.Data)
|
|
|
|
for _, s := range body.Data {
|
2024-02-25 09:21:33 +00:00
|
|
|
assert.False(t, s.Open)
|
2024-02-25 07:31:13 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-02-25 09:21:33 +00:00
|
|
|
name: "OK: open=true",
|
2024-02-25 07:31:13 +00:00
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
q := req.URL.Query()
|
2024-02-25 09:21:33 +00:00
|
|
|
q.Set("open", "true")
|
2024-02-25 07:31:13 +00:00
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, _ *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ListServersResponse](t, resp.Body)
|
|
|
|
assert.NotZero(t, body.Data)
|
|
|
|
for _, s := range body.Data {
|
2024-02-25 09:21:33 +00:00
|
|
|
assert.True(t, s.Open)
|
2024-02-25 07:31:13 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: limit is not a string",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Set("limit", "asd")
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
|
|
|
Code: "invalid-param-format",
|
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"error binding string parameter: strconv.ParseInt: parsing \"%s\": invalid syntax",
|
|
|
|
req.URL.Query().Get("limit"),
|
|
|
|
),
|
|
|
|
Path: []string{"$query", "limit"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: limit < 1",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Set("limit", "0")
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
limit, err := strconv.Atoi(req.URL.Query().Get("limit"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
domainErr := domain.MinGreaterEqualError{
|
|
|
|
Min: 1,
|
|
|
|
Current: limit,
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"current": float64(domainErr.Current),
|
|
|
|
"min": float64(domainErr.Min),
|
|
|
|
},
|
|
|
|
Path: []string{"$query", "limit"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-03-11 07:16:29 +00:00
|
|
|
name: fmt.Sprintf("ERR: limit > %d", domain.ServerListMaxLimit),
|
2024-02-25 07:31:13 +00:00
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
q := req.URL.Query()
|
2024-03-11 07:16:29 +00:00
|
|
|
q.Set("limit", strconv.Itoa(domain.ServerListMaxLimit+1))
|
2024-02-25 07:31:13 +00:00
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
limit, err := strconv.Atoi(req.URL.Query().Get("limit"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
domainErr := domain.MaxLessEqualError{
|
2024-03-11 07:16:29 +00:00
|
|
|
Max: domain.ServerListMaxLimit,
|
2024-02-25 07:31:13 +00:00
|
|
|
Current: limit,
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"current": float64(domainErr.Current),
|
|
|
|
"max": float64(domainErr.Max),
|
|
|
|
},
|
|
|
|
Path: []string{"$query", "limit"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: len(cursor) < 1",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Set("cursor", "")
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
domainErr := domain.LenOutOfRangeError{
|
|
|
|
Min: 1,
|
|
|
|
Max: 1000,
|
|
|
|
Current: len(req.URL.Query().Get("cursor")),
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"current": float64(domainErr.Current),
|
|
|
|
"max": float64(domainErr.Max),
|
|
|
|
"min": float64(domainErr.Min),
|
|
|
|
},
|
|
|
|
Path: []string{"$query", "cursor"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: len(cursor) > 1000",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Set("cursor", gofakeit.LetterN(1001))
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
domainErr := domain.LenOutOfRangeError{
|
|
|
|
Min: 1,
|
|
|
|
Max: 1000,
|
|
|
|
Current: len(req.URL.Query().Get("cursor")),
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"current": float64(domainErr.Current),
|
|
|
|
"max": float64(domainErr.Max),
|
|
|
|
"min": float64(domainErr.Min),
|
|
|
|
},
|
|
|
|
Path: []string{"$query", "cursor"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: invalid cursor",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Set("cursor", gofakeit.LetterN(100))
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, _ *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
var domainErr domain.Error
|
|
|
|
require.ErrorAs(t, domain.ErrInvalidCursor, &domainErr)
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Path: []string{"$query", "cursor"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: open is not a valid boolean",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Set("open", "asd")
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
|
|
|
Code: "invalid-param-format",
|
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"error binding string parameter: strconv.ParseBool: parsing \"%s\": invalid syntax",
|
|
|
|
req.URL.Query().Get("open"),
|
|
|
|
),
|
|
|
|
Path: []string{"$query", "open"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: version not found",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
2024-03-15 06:38:38 +00:00
|
|
|
req.URL.Path = fmt.Sprintf(endpointListServers, randInvalidVersionCode(t, handler))
|
2024-02-25 07:31:13 +00:00
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
pathSegments := strings.Split(req.URL.Path, "/")
|
|
|
|
require.Len(t, pathSegments, 5)
|
|
|
|
domainErr := domain.VersionNotFoundError{
|
|
|
|
VersionCode: pathSegments[3],
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"code": domainErr.VersionCode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := httptest.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
fmt.Sprintf(endpointListServers, version.Code),
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if tt.reqModifier != nil {
|
|
|
|
tt.reqModifier(t, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := doCustomRequest(handler, req)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
tt.assertResp(t, req, resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpointGetServer = "/v2/versions/%s/servers/%s"
|
|
|
|
|
|
|
|
func TestGetServer(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
handler := newAPIHTTPHandler(t)
|
2024-03-07 06:01:52 +00:00
|
|
|
server := randSliceElement(getAllServers(t, handler))
|
2024-02-25 07:31:13 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
reqModifier func(t *testing.T, req *http.Request)
|
|
|
|
assertResp func(t *testing.T, req *http.Request, resp *http.Response)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OK",
|
|
|
|
assertResp: func(t *testing.T, _ *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.GetServerResponse](t, resp.Body)
|
|
|
|
assert.Equal(t, server.Server, body.Data)
|
|
|
|
},
|
|
|
|
},
|
2024-03-01 06:33:48 +00:00
|
|
|
{
|
|
|
|
name: "ERR: len(serverKey) > 10",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
req.URL.Path = fmt.Sprintf(endpointGetServer, server.Version.Code, gofakeit.LetterN(11))
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
pathSegments := strings.Split(req.URL.Path, "/")
|
|
|
|
require.Len(t, pathSegments, 6)
|
|
|
|
domainErr := domain.LenOutOfRangeError{
|
|
|
|
Min: 1,
|
|
|
|
Max: 10,
|
|
|
|
Current: len(pathSegments[5]),
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-03-01 06:33:48 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"min": float64(domainErr.Min),
|
|
|
|
"max": float64(domainErr.Max),
|
|
|
|
"current": float64(domainErr.Current),
|
|
|
|
},
|
|
|
|
Path: []string{"$path", "serverKey"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
2024-02-25 07:31:13 +00:00
|
|
|
{
|
|
|
|
name: "ERR: version not found",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
2024-03-15 06:38:38 +00:00
|
|
|
req.URL.Path = fmt.Sprintf(endpointGetServer, randInvalidVersionCode(t, handler), server.Key)
|
2024-02-25 07:31:13 +00:00
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
pathSegments := strings.Split(req.URL.Path, "/")
|
|
|
|
require.Len(t, pathSegments, 6)
|
|
|
|
domainErr := domain.VersionNotFoundError{
|
|
|
|
VersionCode: pathSegments[3],
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"code": domainErr.VersionCode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: server not found",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
req.URL.Path = fmt.Sprintf(endpointGetServer, server.Version.Code, domaintest.RandServerKey())
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
pathSegments := strings.Split(req.URL.Path, "/")
|
|
|
|
require.Len(t, pathSegments, 6)
|
|
|
|
domainErr := domain.ServerNotFoundError{
|
|
|
|
Key: pathSegments[5],
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"key": domainErr.Key,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf(endpointGetServer, server.Version.Code, server.Key), nil)
|
|
|
|
if tt.reqModifier != nil {
|
|
|
|
tt.reqModifier(t, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := doCustomRequest(handler, req)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
tt.assertResp(t, req, resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpointGetServerConfig = "/v2/versions/%s/servers/%s/config"
|
|
|
|
|
|
|
|
func TestGetServerConfig(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
handler := newAPIHTTPHandler(t)
|
2024-03-07 06:01:52 +00:00
|
|
|
server := randSliceElement(getAllServers(t, handler))
|
2024-02-25 07:31:13 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
reqModifier func(t *testing.T, req *http.Request)
|
|
|
|
assertResp func(t *testing.T, req *http.Request, resp *http.Response)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OK",
|
|
|
|
assertResp: func(t *testing.T, _ *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
body := decodeJSON[apimodel.GetServerConfigResponse](t, resp.Body)
|
|
|
|
assert.Empty(t, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: version not found",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
2024-03-15 06:38:38 +00:00
|
|
|
req.URL.Path = fmt.Sprintf(endpointGetServerConfig, randInvalidVersionCode(t, handler), server.Key)
|
2024-02-25 07:31:13 +00:00
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
pathSegments := strings.Split(req.URL.Path, "/")
|
|
|
|
require.Len(t, pathSegments, 7)
|
|
|
|
domainErr := domain.VersionNotFoundError{
|
|
|
|
VersionCode: pathSegments[3],
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"code": domainErr.VersionCode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: server not found",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
req.URL.Path = fmt.Sprintf(endpointGetServerConfig, server.Version.Code, domaintest.RandServerKey())
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
pathSegments := strings.Split(req.URL.Path, "/")
|
|
|
|
require.Len(t, pathSegments, 7)
|
|
|
|
domainErr := domain.ServerNotFoundError{
|
|
|
|
Key: pathSegments[5],
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"key": domainErr.Key,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := httptest.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
fmt.Sprintf(endpointGetServerConfig, server.Version.Code, server.Key),
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if tt.reqModifier != nil {
|
|
|
|
tt.reqModifier(t, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := doCustomRequest(handler, req)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
tt.assertResp(t, req, resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpointGetServerUnitInfo = "/v2/versions/%s/servers/%s/unit-info"
|
|
|
|
|
|
|
|
func TestGetServerUnitInfo(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
handler := newAPIHTTPHandler(t)
|
2024-03-07 06:01:52 +00:00
|
|
|
server := randSliceElement(getAllServers(t, handler))
|
2024-02-25 07:31:13 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
reqModifier func(t *testing.T, req *http.Request)
|
|
|
|
assertResp func(t *testing.T, req *http.Request, resp *http.Response)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OK",
|
|
|
|
assertResp: func(t *testing.T, _ *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
body := decodeJSON[apimodel.GetUnitInfoResponse](t, resp.Body)
|
|
|
|
assert.Empty(t, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: version not found",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
2024-03-15 06:38:38 +00:00
|
|
|
req.URL.Path = fmt.Sprintf(endpointGetServerUnitInfo, randInvalidVersionCode(t, handler), server.Key)
|
2024-02-25 07:31:13 +00:00
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
pathSegments := strings.Split(req.URL.Path, "/")
|
|
|
|
require.Len(t, pathSegments, 7)
|
|
|
|
domainErr := domain.VersionNotFoundError{
|
|
|
|
VersionCode: pathSegments[3],
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"code": domainErr.VersionCode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: server not found",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
req.URL.Path = fmt.Sprintf(endpointGetServerUnitInfo, server.Version.Code, domaintest.RandServerKey())
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
pathSegments := strings.Split(req.URL.Path, "/")
|
|
|
|
require.Len(t, pathSegments, 7)
|
|
|
|
domainErr := domain.ServerNotFoundError{
|
|
|
|
Key: pathSegments[5],
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"key": domainErr.Key,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := httptest.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
fmt.Sprintf(endpointGetServerUnitInfo, server.Version.Code, server.Key),
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if tt.reqModifier != nil {
|
|
|
|
tt.reqModifier(t, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := doCustomRequest(handler, req)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
tt.assertResp(t, req, resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpointGetServerBuildingInfo = "/v2/versions/%s/servers/%s/building-info"
|
|
|
|
|
|
|
|
func TestGetServerBuildingInfo(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
handler := newAPIHTTPHandler(t)
|
2024-03-07 06:01:52 +00:00
|
|
|
server := randSliceElement(getAllServers(t, handler))
|
2024-02-25 07:31:13 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
reqModifier func(t *testing.T, req *http.Request)
|
|
|
|
assertResp func(t *testing.T, req *http.Request, resp *http.Response)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OK",
|
|
|
|
assertResp: func(t *testing.T, _ *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
body := decodeJSON[apimodel.GetBuildingInfoResponse](t, resp.Body)
|
|
|
|
assert.Empty(t, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: version not found",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
2024-03-15 06:38:38 +00:00
|
|
|
req.URL.Path = fmt.Sprintf(endpointGetServerBuildingInfo, randInvalidVersionCode(t, handler), server.Key)
|
2024-02-25 07:31:13 +00:00
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
pathSegments := strings.Split(req.URL.Path, "/")
|
|
|
|
require.Len(t, pathSegments, 7)
|
|
|
|
domainErr := domain.VersionNotFoundError{
|
|
|
|
VersionCode: pathSegments[3],
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"code": domainErr.VersionCode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: server not found",
|
|
|
|
reqModifier: func(t *testing.T, req *http.Request) {
|
|
|
|
t.Helper()
|
|
|
|
req.URL.Path = fmt.Sprintf(endpointGetServerBuildingInfo, server.Version.Code, domaintest.RandServerKey())
|
|
|
|
},
|
|
|
|
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
|
|
|
|
|
|
// body
|
|
|
|
body := decodeJSON[apimodel.ErrorResponse](t, resp.Body)
|
|
|
|
pathSegments := strings.Split(req.URL.Path, "/")
|
|
|
|
require.Len(t, pathSegments, 7)
|
|
|
|
domainErr := domain.ServerNotFoundError{
|
|
|
|
Key: pathSegments[5],
|
|
|
|
}
|
|
|
|
assert.Equal(t, apimodel.ErrorResponse{
|
|
|
|
Errors: []apimodel.Error{
|
|
|
|
{
|
2024-03-18 07:01:47 +00:00
|
|
|
Code: apimodel.ErrorCode(domainErr.Code()),
|
2024-02-25 07:31:13 +00:00
|
|
|
Message: domainErr.Error(),
|
|
|
|
Params: map[string]any{
|
|
|
|
"key": domainErr.Key,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := httptest.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
fmt.Sprintf(endpointGetServerBuildingInfo, server.Version.Code, server.Key),
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if tt.reqModifier != nil {
|
|
|
|
tt.reqModifier(t, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := doCustomRequest(handler, req)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
tt.assertResp(t, req, resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverWithVersion struct {
|
|
|
|
apimodel.Server
|
|
|
|
Version apimodel.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAllServers(tb testing.TB, h http.Handler) []serverWithVersion {
|
|
|
|
tb.Helper()
|
|
|
|
|
2024-03-07 06:01:52 +00:00
|
|
|
versions := getAllVersions(tb, h)
|
2024-02-25 07:31:13 +00:00
|
|
|
|
|
|
|
var servers []serverWithVersion
|
|
|
|
|
2024-03-07 06:01:52 +00:00
|
|
|
for _, v := range versions {
|
2024-02-25 07:31:13 +00:00
|
|
|
respServers := doRequest(h, http.MethodGet, fmt.Sprintf(endpointListServers, v.Code), nil)
|
|
|
|
require.Equal(tb, http.StatusOK, respServers.StatusCode)
|
|
|
|
|
|
|
|
bodyServers := decodeJSON[apimodel.ListServersResponse](tb, respServers.Body)
|
|
|
|
for _, s := range bodyServers.Data {
|
|
|
|
servers = append(servers, serverWithVersion{
|
|
|
|
Server: s,
|
|
|
|
Version: v,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = respServers.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NotZero(tb, servers)
|
|
|
|
|
|
|
|
return servers
|
|
|
|
}
|