diff --git a/openapi.yaml b/openapi.yaml index fcb5a70..9276854 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -33,7 +33,6 @@ components: description: > A unique object identifier string. Automatically generated by the API on creation (in the form "---" where all letters are lowercase, max 63 characters in total). - readOnly: true OIDCConfig: type: object required: diff --git a/pkg/api/openapi.go b/pkg/api/openapi.go index 5de3142..fac8481 100644 --- a/pkg/api/openapi.go +++ b/pkg/api/openapi.go @@ -3299,46 +3299,46 @@ var swaggerSpec = []string{ "h41SJR6lmMAl4tGljOCUgmigHVfi8CF6ETN3+AUKATOJiHohjVcKMcvByL4vyE7nlFVUx4JWpP8xpUhH", "/ZzaaMpnVXy72RwR9Z8RUePYiK6J/RC0n9fPeSTr4TSpvkdErRaXbEY/m2LHjHzTnDDHEAmAKc1WgSyo", "BtqJFQ/TuqTKe+jA6Quwlv6S2CiqvRn66tlcgYJSIGyaULePcKARkgkUnjIes8KDgIyIkvbRVw/djkEA", - "0mmSBSkRFoAovwXhYwk1FOI7tNlG/hgL7JsDmh6uMH3VMOq1NEXpsgkw7Z0qQnTyCunyVwsR59Luogf1", - "szq1IifMoZiR00zYTsBUxe33SR1eyjFwRHKhsoSfk8BflpSfdPf3bEAp0ZSDXkVW7ssSYT4lYBovVWQF", - "RPo6QZueW9vRKoR1sI4FqSsIdZCEpYlOAUotw1hFquuwVJiF7aMYx531eGZlLOZ83jcJpgEQgpR4BAUD", - "3wUf64yWD90pufRSDlP1HbIsepa8efn1WVVujWeT6NXcSgILglxislprJiv8ljXC3EVMhyVLrFfDkvQP", - "l6KxB2cbOunnT9oOKeGu0ElXFwQwJOwp2iE7lKLshgjufIgUYjgE42W5oQTTvLtvPHdfhOX6Iq6kyZvW", - "jh8C2uMialTF5BX7EWWNfsbOxIpthQI5lc2FUq6RR57k5T+fgFhwq2YfDzVPgh8LoqY9zWWrK7uABYid", - "WI1NRWf+eps4/T8/9j3Xw9aQ7NsM11ipyLbGCRvypOeOfeMYIMSEetvm1X9MS8PPdf8veofH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6l", + "UoVIlDHZdl8+5UqGpj895FK/fcg1M5eyYknHUo7Y3O8cdMyVEfYBvUw2hkxnFIeQe2dXLZM7i5jJV441", + "fiwVD3UNb2DOLiKdnx1p1iW+fN5kq5+Md58iBiVKvGSupfI0/OpYK/eLhXmG/igGnTqUX+aFM+BnH6Ml", + "eFedornzTzxES5Wh5ERyEa35Pfnt8IoDtHkbNeZAagM/1uUs/yJ6telZP1kofObhWR7v083O5svvBydn", + "c0R2AOop5bX2DM7PCWLZ0MzFxN87M1skzqUTszkitAceWYpPOy4rLynPnTfkxfZ3G5Yt1clnH5UV8D7R", + "pGyhDq88J5ujzKex+g2a/Fhir5iRrZyN/R61/P+UBf4zDLDaisrpXbHjUdxT/3KpbcP+trOqN3rEfUxR", + "ABOgPArBYLCb302vvDub3yzP7RufCh7EvsmNbNOjuDte+nH+6pC7TMHILf/PAV0nTP0s+H2YzAUbwGQW", + "7GXK/Vn4uTX7QkFf3FMu01X8LrfwWvy/EFV8mXT+i2329MPi4/mfZx1CRUJwo1nXLHSgsl5hGYyurV3n", + "LFuUtn8/XD78XwAAAP//zGbqLLdJAAA=", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/pkg/service/api_service.go b/pkg/service/api_service.go index 99fd717..ce44abd 100644 --- a/pkg/service/api_service.go +++ b/pkg/service/api_service.go @@ -54,6 +54,11 @@ func NewAPIServer(conf APIConfig, k8sMiddleware ...KubernetesAuth) (*echo.Echo, if err != nil { return nil, fmt.Errorf("error loading swagger spec: %w", err) } + + // Clear out the servers array in the swagger spec, that skips validating + // that server names match. We don't know how/where this thing will be run. + swagger.Servers = nil + swaggerJSON, err = swagger.MarshalJSON() if err != nil { return nil, fmt.Errorf("error marshalling swagger spec: %w", err) diff --git a/pkg/service/api_service_test.go b/pkg/service/api_service_test.go index 607948d..fd45268 100644 --- a/pkg/service/api_service_test.go +++ b/pkg/service/api_service_test.go @@ -257,7 +257,7 @@ func TestHealthz(t *testing.T) { e, _ := setupTest(t) result := testutil.NewRequest().Get("/healthz").Go(t, e) - assert.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) assert.Equal(t, "ok", string(result.Recorder.Body.String())) } @@ -265,7 +265,7 @@ func TestOpenAPI(t *testing.T) { e, _ := setupTest(t) result := testutil.NewRequest().Get("/openapi.json").Go(t, e) - assert.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) swaggerSpec := &openapi3.T{} err := json.Unmarshal(result.Recorder.Body.Bytes(), swaggerSpec) assert.NoError(t, err) @@ -277,7 +277,7 @@ func TestSwaggerUI(t *testing.T) { e, _ := setupTest(t) result := testutil.NewRequest().Get("/docs").Go(t, e) - assert.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) assert.NotEmpty(t, result.Recorder.Body.Bytes) } @@ -285,7 +285,7 @@ func TestDiscovery(t *testing.T) { e, _ := setupTest(t) result := testutil.NewRequest().Get("/").Go(t, e) - assert.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) assert.NotEmpty(t, result.Recorder.Body.Bytes) metadata := api.Metadata{} err := json.Unmarshal(result.Recorder.Body.Bytes(), &metadata) diff --git a/pkg/service/cluster_test.go b/pkg/service/cluster_test.go index 387e998..9ce7ebe 100644 --- a/pkg/service/cluster_test.go +++ b/pkg/service/cluster_test.go @@ -26,7 +26,7 @@ func TestListCluster(t *testing.T) { Get("/clusters"). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - require.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) clusters := make([]api.Cluster, 0) err := result.UnmarshalJsonToObject(&clusters) assert.NoError(t, err) @@ -47,7 +47,7 @@ func TestListCluster_FilteredByTenant(t *testing.T) { Get("/clusters?tenant="+tenantA.Name). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - require.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) clusters := make([]api.Cluster, 0) err := result.UnmarshalJsonToObject(&clusters) assert.NoError(t, err) @@ -98,7 +98,7 @@ func TestListCluster_Sort(t *testing.T) { Get(fmt.Sprintf("/clusters?sort_by=%s", tc.sortBy)). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - require.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) clusters := make([]api.Cluster, 0) err := result.UnmarshalJsonToObject(&clusters) assert.NoError(t, err) @@ -116,7 +116,7 @@ func TestListClusterMissingBearer(t *testing.T) { result := testutil.NewRequest(). Get("/clusters"). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) } func TestListClusterWrongToken(t *testing.T) { @@ -126,7 +126,7 @@ func TestListClusterWrongToken(t *testing.T) { Get("/clusters"). WithHeader(echo.HeaderAuthorization, "asdf"). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) } func TestCreateCluster(t *testing.T) { @@ -158,7 +158,7 @@ func TestCreateCluster(t *testing.T) { WithJsonBody(newCluster). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusCreated, result.Code()) + requireHTTPCode(t, http.StatusCreated, result) cluster := &api.Cluster{} err = result.UnmarshalJsonToObject(cluster) @@ -206,7 +206,7 @@ func TestCreateClusterWithId(t *testing.T) { WithJsonBody(request). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusCreated, result.Code()) + requireHTTPCode(t, http.StatusCreated, result) cluster := &api.Cluster{} err := result.UnmarshalJsonToObject(cluster) assert.NoError(t, err) @@ -238,7 +238,7 @@ func TestCreateClusterInstanceFact(t *testing.T) { WithJsonBody(newCluster). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusCreated, result.Code()) + requireHTTPCode(t, http.StatusCreated, result) cluster := &api.Cluster{} err = result.UnmarshalJsonToObject(cluster) @@ -252,7 +252,7 @@ func TestCreateClusterInstanceFact(t *testing.T) { WithJsonBody(newCluster). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusCreated, result.Code()) + requireHTTPCode(t, http.StatusCreated, result) cluster = &api.Cluster{} err = result.UnmarshalJsonToObject(cluster) assert.NoError(t, err) @@ -269,7 +269,7 @@ func TestCreateClusterNoJSON(t *testing.T) { WithBody([]byte("invalid-body")). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -287,7 +287,7 @@ func TestCreateClusterNoTenant(t *testing.T) { WithJsonBody(createCluster). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -302,7 +302,7 @@ func TestCreateClusterEmpty(t *testing.T) { WithJsonContentType(). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -316,7 +316,7 @@ func TestClusterDelete(t *testing.T) { Delete("/clusters/"+clusterA.Name). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusNoContent, result.Code()) + requireHTTPCode(t, http.StatusNoContent, result) } func TestClusterGet(t *testing.T) { @@ -326,7 +326,7 @@ func TestClusterGet(t *testing.T) { Get("/clusters/"+clusterA.Name). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) cluster := &api.Cluster{} err := result.UnmarshalJsonToObject(cluster) assert.NoError(t, err) @@ -345,7 +345,7 @@ func TestClusterGetNoToken(t *testing.T) { Get("/clusters/"+clusterB.Name). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) cluster := &api.Cluster{} err := result.UnmarshalJsonToObject(cluster) assert.NoError(t, err) @@ -362,7 +362,7 @@ func TestClusterGetNotFound(t *testing.T) { Get("/clusters/not-existing"). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusNotFound, result.Code()) + requireHTTPCode(t, http.StatusNotFound, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -376,7 +376,7 @@ func TestClusterUpdateEmpty(t *testing.T) { Patch("/clusters/1"). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -396,7 +396,7 @@ func TestClusterUpdateTenant(t *testing.T) { WithContentType(api.ContentJSONPatch). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -418,7 +418,7 @@ func TestClusterUpdateUnknown(t *testing.T) { WithContentType(api.ContentJSONPatch). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -440,7 +440,7 @@ func TestClusterUpdateIllegalDeployKey(t *testing.T) { WithContentType(api.ContentJSONPatch). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -462,7 +462,7 @@ func TestClusterUpdateNotManagedDeployKey(t *testing.T) { WithContentType(api.ContentJSONPatch). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -499,7 +499,7 @@ func TestClusterUpdate(t *testing.T) { WithContentType(api.ContentJSONPatch). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - require.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) cluster := &api.Cluster{} err := result.UnmarshalJsonToObject(cluster) require.NoError(t, err) @@ -532,7 +532,7 @@ func TestClusterUpdateDisplayName(t *testing.T) { WithContentType(api.ContentJSONPatch). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - require.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) cluster := &api.Cluster{} err := result.UnmarshalJsonToObject(cluster) require.NoError(t, err) @@ -613,7 +613,7 @@ func TestClusterPut(t *testing.T) { WithJsonBody(tc.cluster). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - require.Equal(t, tc.code, result.Code()) + requireHTTPCode(t, tc.code, result) res := &api.Cluster{} err := result.UnmarshalJsonToObject(res) @@ -649,7 +649,7 @@ func TestClusterPutCreateNameMissmatch(t *testing.T) { WithJsonBody(cluster). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - require.Equal(t, http.StatusCreated, result.Code()) + requireHTTPCode(t, http.StatusCreated, result) res := &api.Cluster{} err := result.UnmarshalJsonToObject(res) @@ -664,3 +664,9 @@ func TestClusterPutCreateNameMissmatch(t *testing.T) { }, clusterObj) require.NotNil(t, clusterObj) } + +// requireHTTPCode is a helper function to check the HTTP status code of a response and log the response body if the code is not as expected. +func requireHTTPCode(t *testing.T, expected int, result *testutil.CompletedRequest) { + t.Helper() + require.Equalf(t, expected, result.Code(), "Unexpected response code: %d, body: %s", result.Code(), string(result.Recorder.Body.String())) +} diff --git a/pkg/service/inventory_test.go b/pkg/service/inventory_test.go index ac1cc51..e6586f5 100644 --- a/pkg/service/inventory_test.go +++ b/pkg/service/inventory_test.go @@ -7,7 +7,6 @@ import ( "github.com/deepmap/oapi-codegen/pkg/testutil" "github.com/labstack/echo/v4" - "github.com/stretchr/testify/assert" "github.com/projectsyn/lieutenant-api/pkg/api" ) @@ -20,7 +19,7 @@ func TestQueryInventory(t *testing.T) { WithHeader(echo.HeaderAuthorization, bearerToken). Get("/inventory?q="+url.QueryEscape(query)). Go(t, e) - assert.Equal(t, http.StatusInternalServerError, result.Code()) + requireHTTPCode(t, http.StatusInternalServerError, result) } func TestUpdateInventory(t *testing.T) { @@ -38,5 +37,5 @@ func TestUpdateInventory(t *testing.T) { WithJsonBody(updateInventory). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusInternalServerError, result.Code()) + requireHTTPCode(t, http.StatusInternalServerError, result) } diff --git a/pkg/service/steward_test.go b/pkg/service/steward_test.go index 0f9f1b8..10ce6c5 100644 --- a/pkg/service/steward_test.go +++ b/pkg/service/steward_test.go @@ -7,7 +7,6 @@ import ( "github.com/deepmap/oapi-codegen/pkg/testutil" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -117,7 +116,7 @@ func TestInstallSteward(t *testing.T) { WithHeader("X-Forwarded-Proto", "https"). Get("/install/steward.json?token="+tc.bootstrapToken). Go(t, e) - require.Equalf(t, http.StatusOK, result.Code(), "Unexpected response code: %d, body: %s", result.Code(), string(result.Recorder.Body.String())) + requireHTTPCode(t, http.StatusOK, result) manifests := &corev1.List{} err := result.UnmarshalJsonToObject(&manifests) assert.NoError(t, err) @@ -154,7 +153,7 @@ func TestInstallStewardNoToken(t *testing.T) { result := testutil.NewRequest(). Get("/install/steward.json"). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -167,7 +166,7 @@ func TestInstallStewardInvalidToken(t *testing.T) { result := testutil.NewRequest(). Get("/install/steward.json?token=NonExistentToken"). Go(t, e) - assert.Equal(t, http.StatusUnauthorized, result.Code()) + requireHTTPCode(t, http.StatusUnauthorized, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -180,7 +179,7 @@ func TestInstallStewardUsedToken(t *testing.T) { result := testutil.NewRequest(). Get("/install/steward.json?token="+clusterB.Status.BootstrapToken.Token). Go(t, e) - assert.Equal(t, http.StatusUnauthorized, result.Code()) + requireHTTPCode(t, http.StatusUnauthorized, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) diff --git a/pkg/service/tenant_test.go b/pkg/service/tenant_test.go index c491d66..94436de 100644 --- a/pkg/service/tenant_test.go +++ b/pkg/service/tenant_test.go @@ -24,7 +24,7 @@ func TestListTenants(t *testing.T) { Get("/tenants/"). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) tenants := []api.Tenant{} err := result.UnmarshalJsonToObject(&tenants) assert.NoError(t, err) @@ -57,7 +57,7 @@ func TestCreateTenant(t *testing.T) { WithJsonBody(newTenant). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusCreated, result.Code()) + requireHTTPCode(t, http.StatusCreated, result) tenant := &api.Tenant{} err := result.UnmarshalJsonToObject(tenant) assert.NoError(t, err) @@ -133,7 +133,7 @@ func TestCreateTenantFail(t *testing.T) { WithBody([]byte("invalid-body")). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -147,7 +147,7 @@ func TestCreateTenantEmpty(t *testing.T) { Post("/tenants/"). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -166,7 +166,8 @@ func TestCreateTenantNoGitURL(t *testing.T) { WithHeader(echo.HeaderAuthorization, bearerToken). WithJsonBody(newTenant). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) + reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -180,7 +181,7 @@ func TestTenantDelete(t *testing.T) { Delete("/tenants/"+tenantA.Name). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusNoContent, result.Code()) + requireHTTPCode(t, http.StatusNoContent, result) } func TestTenantGet(t *testing.T) { @@ -190,7 +191,7 @@ func TestTenantGet(t *testing.T) { Get("/tenants/"+tenantA.Name). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) tenant := &api.Tenant{} err := result.UnmarshalJsonToObject(tenant) assert.NoError(t, err) @@ -208,7 +209,7 @@ func TestTenantUpdateEmpty(t *testing.T) { Patch("/tenants/1"). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -230,7 +231,7 @@ func TestTenantUpdateUnknown(t *testing.T) { WithContentType(api.ContentJSONPatch). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusBadRequest, result.Code()) + requireHTTPCode(t, http.StatusBadRequest, result) reason := &api.Reason{} err := result.UnmarshalJsonToObject(reason) assert.NoError(t, err) @@ -259,7 +260,7 @@ func TestTenantUpdate(t *testing.T) { WithContentType(api.ContentJSONPatch). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) tenant := &api.Tenant{} err := result.UnmarshalJsonToObject(tenant) assert.NoError(t, err) @@ -287,7 +288,7 @@ func TestTenantUpdateDisplayName(t *testing.T) { WithContentType(api.ContentJSONPatch). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - assert.Equal(t, http.StatusOK, result.Code()) + requireHTTPCode(t, http.StatusOK, result) tenant := &api.Tenant{} err := result.UnmarshalJsonToObject(tenant) assert.NoError(t, err) @@ -360,7 +361,7 @@ func TestTenantPut(t *testing.T) { WithJsonBody(tc.tenant). WithHeader(echo.HeaderAuthorization, bearerToken). Go(t, e) - require.Equal(t, tc.code, result.Code()) + requireHTTPCode(t, tc.code, result) res := &api.Tenant{} err := result.UnmarshalJsonToObject(res)