From 02108fcb00fe4b9633e663d4dcb6a77e7d5e4e96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Fri, 15 Mar 2024 07:38:38 +0100 Subject: [PATCH] refactor: port - http - improve tests --- cmd/twhelp/cmd_serve.go | 2 +- .../port/handler_http_api_ennoblement_test.go | 18 ++++++++++++++---- internal/port/handler_http_api_player_test.go | 6 +++--- internal/port/handler_http_api_server_test.go | 10 +++++----- .../handler_http_api_tribe_change_test.go | 9 +++++++-- internal/port/handler_http_api_tribe_test.go | 4 ++-- .../port/handler_http_api_version_test.go | 19 ++++++++++++++++++- .../port/handler_http_api_village_test.go | 8 ++++---- 8 files changed, 54 insertions(+), 22 deletions(-) diff --git a/cmd/twhelp/cmd_serve.go b/cmd/twhelp/cmd_serve.go index 333ab81..6d019ee 100644 --- a/cmd/twhelp/cmd_serve.go +++ b/cmd/twhelp/cmd_serve.go @@ -31,7 +31,7 @@ var ( apiServerHandlerTimeoutFlag = &cli.DurationFlag{ Name: "api.handlerTimeout", EnvVars: []string{"API_HANDLER_TIMEOUT"}, - Value: 5 * time.Second, //nolint:gomnd, + Value: 5 * time.Second, //nolint:gomnd Usage: "https://pkg.go.dev/net/http#TimeoutHandler", } apiServerReadTimeoutFlag = &cli.DurationFlag{ diff --git a/internal/port/handler_http_api_ennoblement_test.go b/internal/port/handler_http_api_ennoblement_test.go index 41663cc..c0b4ce6 100644 --- a/internal/port/handler_http_api_ennoblement_test.go +++ b/internal/port/handler_http_api_ennoblement_test.go @@ -585,7 +585,7 @@ func TestListEnnoblements(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListEnnoblements, domaintest.RandVersionCode(), server.Key) + req.URL.Path = fmt.Sprintf(endpointListEnnoblements, randInvalidVersionCode(t, handler), server.Key) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -1232,7 +1232,12 @@ func TestListPlayerEnnoblements(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListPlayerEnnoblements, domaintest.RandVersionCode(), server.Key, player.Id) + req.URL.Path = fmt.Sprintf( + endpointListPlayerEnnoblements, + randInvalidVersionCode(t, handler), + server.Key, + player.Id, + ) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -1932,7 +1937,7 @@ func TestListTribeEnnoblements(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListTribeEnnoblements, domaintest.RandVersionCode(), server.Key, tribe.Id) + req.URL.Path = fmt.Sprintf(endpointListTribeEnnoblements, randInvalidVersionCode(t, handler), server.Key, tribe.Id) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -2624,7 +2629,12 @@ func TestListVillageEnnoblements(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListVillageEnnoblements, domaintest.RandVersionCode(), server.Key, village.Id) + req.URL.Path = fmt.Sprintf( + endpointListVillageEnnoblements, + randInvalidVersionCode(t, handler), + server.Key, + village.Id, + ) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() diff --git a/internal/port/handler_http_api_player_test.go b/internal/port/handler_http_api_player_test.go index b5c4a17..28649bd 100644 --- a/internal/port/handler_http_api_player_test.go +++ b/internal/port/handler_http_api_player_test.go @@ -710,7 +710,7 @@ func TestListPlayers(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListPlayers, domaintest.RandVersionCode(), server.Key) + req.URL.Path = fmt.Sprintf(endpointListPlayers, randInvalidVersionCode(t, handler), server.Key) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -1228,7 +1228,7 @@ func TestListTribeMembers(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListTribeMembers, domaintest.RandVersionCode(), server.Key, tribeID) + req.URL.Path = fmt.Sprintf(endpointListTribeMembers, randInvalidVersionCode(t, handler), server.Key, tribeID) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -1413,7 +1413,7 @@ func TestGetPlayer(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointGetPlayer, domaintest.RandVersionCode(), player.Server.Key, player.Id) + req.URL.Path = fmt.Sprintf(endpointGetPlayer, randInvalidVersionCode(t, handler), player.Server.Key, player.Id) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() diff --git a/internal/port/handler_http_api_server_test.go b/internal/port/handler_http_api_server_test.go index c22e595..bdfd8b3 100644 --- a/internal/port/handler_http_api_server_test.go +++ b/internal/port/handler_http_api_server_test.go @@ -386,7 +386,7 @@ func TestListServers(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListServers, domaintest.RandVersionCode()) + req.URL.Path = fmt.Sprintf(endpointListServers, randInvalidVersionCode(t, handler)) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -500,7 +500,7 @@ func TestGetServer(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointGetServer, domaintest.RandVersionCode(), server.Key) + req.URL.Path = fmt.Sprintf(endpointGetServer, randInvalidVersionCode(t, handler), server.Key) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -603,7 +603,7 @@ func TestGetServerConfig(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointGetServerConfig, domaintest.RandVersionCode(), server.Key) + req.URL.Path = fmt.Sprintf(endpointGetServerConfig, randInvalidVersionCode(t, handler), server.Key) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -710,7 +710,7 @@ func TestGetServerUnitInfo(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointGetServerUnitInfo, domaintest.RandVersionCode(), server.Key) + req.URL.Path = fmt.Sprintf(endpointGetServerUnitInfo, randInvalidVersionCode(t, handler), server.Key) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -817,7 +817,7 @@ func TestGetServerBuildingInfo(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointGetServerBuildingInfo, domaintest.RandVersionCode(), server.Key) + req.URL.Path = fmt.Sprintf(endpointGetServerBuildingInfo, randInvalidVersionCode(t, handler), server.Key) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() diff --git a/internal/port/handler_http_api_tribe_change_test.go b/internal/port/handler_http_api_tribe_change_test.go index 5dba4b7..7ebee84 100644 --- a/internal/port/handler_http_api_tribe_change_test.go +++ b/internal/port/handler_http_api_tribe_change_test.go @@ -600,7 +600,12 @@ func TestListPlayerTribeChanges(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListPlayerTribeChanges, domaintest.RandVersionCode(), server.Key, player.Id) + req.URL.Path = fmt.Sprintf( + endpointListPlayerTribeChanges, + randInvalidVersionCode(t, handler), + server.Key, + player.Id, + ) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -1326,7 +1331,7 @@ func TestListTribeTribeChanges(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListTribeTribeChanges, domaintest.RandVersionCode(), server.Key, tribe.Id) + req.URL.Path = fmt.Sprintf(endpointListTribeTribeChanges, randInvalidVersionCode(t, handler), server.Key, tribe.Id) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() diff --git a/internal/port/handler_http_api_tribe_test.go b/internal/port/handler_http_api_tribe_test.go index f96d124..967ee85 100644 --- a/internal/port/handler_http_api_tribe_test.go +++ b/internal/port/handler_http_api_tribe_test.go @@ -636,7 +636,7 @@ func TestListTribes(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListTribes, domaintest.RandVersionCode(), server.Key) + req.URL.Path = fmt.Sprintf(endpointListTribes, randInvalidVersionCode(t, handler), server.Key) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -786,7 +786,7 @@ func TestGetTribe(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointGetTribe, domaintest.RandVersionCode(), tribe.Server.Key, tribe.Id) + req.URL.Path = fmt.Sprintf(endpointGetTribe, randInvalidVersionCode(t, handler), tribe.Server.Key, tribe.Id) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() diff --git a/internal/port/handler_http_api_version_test.go b/internal/port/handler_http_api_version_test.go index 3db3c5b..4ce325a 100644 --- a/internal/port/handler_http_api_version_test.go +++ b/internal/port/handler_http_api_version_test.go @@ -384,7 +384,7 @@ func TestGetVersion(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointGetVersion, domaintest.RandVersionCode()) + req.URL.Path = fmt.Sprintf(endpointGetVersion, randInvalidVersionCode(t, handler)) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -438,3 +438,20 @@ func getAllVersions(tb testing.TB, h http.Handler) []apimodel.Version { require.NotEmpty(tb, bodyVersions.Data) return bodyVersions.Data } + +func randInvalidVersionCode(tb testing.TB, h http.Handler) string { + tb.Helper() + + versions := getAllVersions(tb, h) + + for range 3 { + code := domaintest.RandVersionCode() + if !slices.ContainsFunc(versions, func(version apimodel.Version) bool { + return version.Code == code + }) { + return code + } + } + + return domaintest.RandVersionCode() +} diff --git a/internal/port/handler_http_api_village_test.go b/internal/port/handler_http_api_village_test.go index 5c0cacd..cf3bbea 100644 --- a/internal/port/handler_http_api_village_test.go +++ b/internal/port/handler_http_api_village_test.go @@ -410,7 +410,7 @@ func TestListVillages(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListVillages, domaintest.RandVersionCode(), server.Key) + req.URL.Path = fmt.Sprintf(endpointListVillages, randInvalidVersionCode(t, handler), server.Key) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -788,7 +788,7 @@ func TestListPlayerVillages(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListPlayerVillages, domaintest.RandVersionCode(), server.Key, player.Id) + req.URL.Path = fmt.Sprintf(endpointListPlayerVillages, randInvalidVersionCode(t, handler), server.Key, player.Id) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -1201,7 +1201,7 @@ func TestListTribeVillages(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointListTribeVillages, domaintest.RandVersionCode(), server.Key, tribe.Id) + req.URL.Path = fmt.Sprintf(endpointListTribeVillages, randInvalidVersionCode(t, handler), server.Key, tribe.Id) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper() @@ -1386,7 +1386,7 @@ func TestGetVillage(t *testing.T) { name: "ERR: version not found", reqModifier: func(t *testing.T, req *http.Request) { t.Helper() - req.URL.Path = fmt.Sprintf(endpointGetVillage, domaintest.RandVersionCode(), village.Server.Key, village.Id) + req.URL.Path = fmt.Sprintf(endpointGetVillage, randInvalidVersionCode(t, handler), village.Server.Key, village.Id) }, assertResp: func(t *testing.T, req *http.Request, resp *http.Response) { t.Helper()