From 7de1ffdbe9aba0a8bf1ead530086c038e85b43be Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Wed, 28 Feb 2024 18:00:27 +0300 Subject: [PATCH] [#306] Fix billing tests Signed-off-by: Denis Kirillov --- api/router_test.go | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/api/router_test.go b/api/router_test.go index 5954311..468d14c 100644 --- a/api/router_test.go +++ b/api/router_test.go @@ -57,7 +57,7 @@ func prepareRouter(t *testing.T) *routerMock { Limit: 10, BacklogTimeout: 30 * time.Second, }, - Handler: &handlerMock{t: t, cfg: middlewareSettings, buckets: map[string]*data.BucketInfo{}}, + Handler: &handlerMock{t: t, cfg: middlewareSettings, buckets: map[string]*data.BucketInfo{}}, Center: ¢erMock{t: t}, Log: logger, Metrics: metrics.NewAppMetrics(metricsConfig), @@ -442,33 +442,24 @@ func putObjectBase(router *routerMock, namespace, bktName, objName string) *http func TestOwnerIDRetrieving(t *testing.T) { chiRouter := prepareRouter(t) - w := httptest.NewRecorder() - r := httptest.NewRequest(http.MethodGet, "/test-bucket", nil) + ns, bktName, objName := "", "test-bucket", "test-object" - chiRouter.ServeHTTP(w, r) - resp := readResponse(t, w) + createBucket(chiRouter, ns, bktName) + + resp := putObject(chiRouter, ns, bktName, objName) require.NotEqual(t, "anon", resp.ReqInfo.User) - w = httptest.NewRecorder() - r = httptest.NewRequest(http.MethodGet, "/test-bucket", nil) - chiRouter.cfg.Center.(*centerMock).anon = true - - chiRouter.ServeHTTP(w, r) - resp = readResponse(t, w) + resp = putObject(chiRouter, ns, bktName, objName) require.Equal(t, "anon", resp.ReqInfo.User) } func TestBillingMetrics(t *testing.T) { chiRouter := prepareRouter(t) - bktName, objName := "test-bucket", "test-object" - target := fmt.Sprintf("/%s/%s", bktName, objName) + ns, bktName, objName := "", "test-bucket", "test-object" - w := httptest.NewRecorder() - r := httptest.NewRequest(http.MethodPut, target, nil) - - chiRouter.ServeHTTP(w, r) + createBucket(chiRouter, ns, bktName) dump := chiRouter.cfg.Metrics.UsersAPIStats().DumpMetrics() require.Len(t, dump.Requests, 1) require.NotEqual(t, "anon", dump.Requests[0].User) @@ -477,11 +468,7 @@ func TestBillingMetrics(t *testing.T) { require.Equal(t, 1, dump.Requests[0].Requests) chiRouter.cfg.Center.(*centerMock).anon = true - - w = httptest.NewRecorder() - r = httptest.NewRequest(http.MethodPut, target, nil) - - chiRouter.ServeHTTP(w, r) + putObject(chiRouter, ns, bktName, objName) dump = chiRouter.cfg.Metrics.UsersAPIStats().DumpMetrics() require.Len(t, dump.Requests, 1) require.Equal(t, "anon", dump.Requests[0].User) -- 2.45.2