From cbcb59f92f6d7f8681cdc7e622691f9a26f0e26c Mon Sep 17 00:00:00 2001 From: H1rono Date: Thu, 2 Jan 2025 18:59:40 +0900 Subject: [PATCH] :recycle: Use `httptest.NewRequestWithContext` --- router/admin_test.go | 72 ++--- router/file_test.go | 78 +++--- router/group_test.go | 386 +++++++++++--------------- router/request_test.go | 546 +++++++++++++++---------------------- router/tag_test.go | 124 ++++----- router/transaction_test.go | 109 +++----- router/user_test.go | 45 +-- 7 files changed, 553 insertions(+), 807 deletions(-) diff --git a/router/admin_test.go b/router/admin_test.go index 0ca7a7ce..d611f3b8 100644 --- a/router/admin_test.go +++ b/router/admin_test.go @@ -23,6 +23,7 @@ func TestHandler_GetAdmins(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) admin := &model.Admin{ @@ -34,8 +35,7 @@ func TestHandler_GetAdmins(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/admins", nil) - require.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/admins", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -59,12 +59,12 @@ func TestHandler_GetAdmins(t *testing.T) { t.Run("Success2", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) var admins []*model.Admin e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/admins", nil) - require.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/admins", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -88,11 +88,11 @@ func TestHandler_GetAdmins(t *testing.T) { t.Run("FailedWithError", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/admins", nil) - require.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/admins", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -117,6 +117,7 @@ func TestHandler_PostAdmin(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) admin := uuid.New() @@ -125,11 +126,8 @@ func TestHandler_PostAdmin(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - "/api/admins", - bytes.NewReader(reqBody)) - require.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/admins", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -147,6 +145,7 @@ func TestHandler_PostAdmin(t *testing.T) { t.Run("FailedWithError", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) admin := uuid.New() @@ -155,11 +154,8 @@ func TestHandler_PostAdmin(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - "/api/admins", - bytes.NewReader(reqBody)) - require.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/admins", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -181,6 +177,7 @@ func TestHandler_PostAdmin(t *testing.T) { t.Run("FailedWithEntConstraintError", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) admin := uuid.New() @@ -189,11 +186,8 @@ func TestHandler_PostAdmin(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - "/api/admins", - bytes.NewReader(reqBody)) - require.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/admins", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -220,6 +214,7 @@ func TestHandler_DeleteAdmin(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) admin := uuid.New() @@ -228,11 +223,8 @@ func TestHandler_DeleteAdmin(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - "/api/admins", - bytes.NewReader(reqBody)) - require.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, "/api/admins", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -250,6 +242,7 @@ func TestHandler_DeleteAdmin(t *testing.T) { t.Run("FailedWithError", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) admin := uuid.New() @@ -258,11 +251,8 @@ func TestHandler_DeleteAdmin(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - "/api/admins", - bytes.NewReader(reqBody)) - require.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, "/api/admins", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -284,16 +274,16 @@ func TestHandler_DeleteAdmin(t *testing.T) { t.Run("InvalidAdminID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - body := `["invalid"]` + invalidUUID := "invalid-uuid" + reqBody, err := json.Marshal([]string{invalidUUID}) + require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - "/api/admins", - bytes.NewReader([]byte(body))) - require.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, "/api/admins", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -308,6 +298,7 @@ func TestHandler_DeleteAdmin(t *testing.T) { t.Run("FailedWithEntConstraintError", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) admin := uuid.New() @@ -316,11 +307,8 @@ func TestHandler_DeleteAdmin(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - "/api/admins", - bytes.NewReader(reqBody)) - require.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, "/api/admins", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) diff --git a/router/file_test.go b/router/file_test.go index 2e3358c0..b4f577d9 100644 --- a/router/file_test.go +++ b/router/file_test.go @@ -34,6 +34,7 @@ func TestHandlers_PostFile(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) request := uuid.New() @@ -71,8 +72,7 @@ func TestHandlers_PostFile(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/files", pr) - require.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, "/api/files", pr) req.Header.Set("Content-Type", writer.FormDataContentType()) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -80,7 +80,7 @@ func TestHandlers_PostFile(t *testing.T) { hn := mw(echo.HandlerFunc(func(c echo.Context) error { return c.NoContent(http.StatusOK) })) - err = hn(c) + err := hn(c) require.NoError(t, err) h, err := NewTestHandlers(t, ctrl) @@ -106,6 +106,7 @@ func TestHandlers_PostFile(t *testing.T) { t.Run("FailedToRepositoryCreateFile", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) request := uuid.New() @@ -139,8 +140,7 @@ func TestHandlers_PostFile(t *testing.T) { }() e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/files", pr) - require.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, "/api/files", pr) req.Header.Set("Content-Type", writer.FormDataContentType()) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -148,7 +148,7 @@ func TestHandlers_PostFile(t *testing.T) { hn := mw(echo.HandlerFunc(func(c echo.Context) error { return c.NoContent(http.StatusOK) })) - err = hn(c) + err := hn(c) require.NoError(t, err) h, err := NewTestHandlers(t, ctrl) @@ -174,6 +174,7 @@ func TestHandlers_PostFile(t *testing.T) { t.Run("FailedToServiceCreateFile", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) request := uuid.New() @@ -211,8 +212,7 @@ func TestHandlers_PostFile(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/files", pr) - require.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, "/api/files", pr) req.Header.Set("Content-Type", writer.FormDataContentType()) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -220,7 +220,7 @@ func TestHandlers_PostFile(t *testing.T) { hn := mw(echo.HandlerFunc(func(c echo.Context) error { return c.NoContent(http.StatusOK) })) - err = hn(c) + err := hn(c) require.NoError(t, err) h, err := NewTestHandlers(t, ctrl) @@ -254,6 +254,7 @@ func TestHandlers_GetFile(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) file := &model.File{ @@ -268,8 +269,8 @@ func TestHandlers_GetFile(t *testing.T) { r := io.NopCloser(bytes.NewReader(f)) e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/files/%s", file.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/files/%s", file.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -295,6 +296,7 @@ func TestHandlers_GetFile(t *testing.T) { t.Run("FailedToGetFile", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) file := &model.File{ @@ -305,8 +307,8 @@ func TestHandlers_GetFile(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/files/%s", file.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/files/%s", file.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -331,6 +333,7 @@ func TestHandlers_GetFile(t *testing.T) { t.Run("FailedToOpenFile", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) file := &model.File{ @@ -341,8 +344,8 @@ func TestHandlers_GetFile(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/files/%s", file.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/files/%s", file.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -372,11 +375,11 @@ func TestHandlers_GetFile(t *testing.T) { t.Run("UnknownFile", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/files/po", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/files/po", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -401,6 +404,7 @@ func TestHandlers_GetFileMeta(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) file := &model.File{ @@ -412,8 +416,8 @@ func TestHandlers_GetFileMeta(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/files/%s/meta", file.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/files/%s/meta", file.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -445,6 +449,7 @@ func TestHandlers_GetFileMeta(t *testing.T) { t.Run("FailedToGetFile", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) file := &model.File{ @@ -456,8 +461,8 @@ func TestHandlers_GetFileMeta(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/files/%s/meta", file.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/files/%s/meta", file.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -482,11 +487,11 @@ func TestHandlers_GetFileMeta(t *testing.T) { t.Run("UnknownFile", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/files/po/meta", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/files/po/meta", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -511,6 +516,7 @@ func TestHandlers_DeleteFile(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) file := &model.File{ @@ -521,8 +527,8 @@ func TestHandlers_DeleteFile(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/files/%s", file.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/files/%s", file.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodDelete, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -548,6 +554,7 @@ func TestHandlers_DeleteFile(t *testing.T) { t.Run("FailedToRepositoryDeleteFile", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) file := &model.File{ @@ -558,8 +565,8 @@ func TestHandlers_DeleteFile(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/files/%s", file.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/files/%s", file.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodDelete, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -584,6 +591,7 @@ func TestHandlers_DeleteFile(t *testing.T) { t.Run("FailedToServiceDeleteFile", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) file := &model.File{ @@ -593,8 +601,8 @@ func TestHandlers_DeleteFile(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/files/%s", file.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/files/%s", file.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodDelete, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -624,23 +632,25 @@ func TestHandlers_DeleteFile(t *testing.T) { t.Run("UnknownFile", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) + invalidUUID := "invalid-uuid" + _, mocErr := uuid.Parse(invalidUUID) + e := echo.New() - req, err := http.NewRequest(http.MethodDelete, "/api/files/po", nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/files/%s", invalidUUID) + req := httptest.NewRequestWithContext(ctx, http.MethodDelete, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/files/:fileID") c.SetParamNames("fileID") - c.SetParamValues("po") + c.SetParamValues(invalidUUID) h, err := NewTestHandlers(t, ctrl) assert.NoError(t, err) - _, mocErr := uuid.Parse("po") - err = h.Handlers.DeleteFile(c) assert.Error(t, err) // FIXME: http.StatusBadRequestだけ判定したい; mocErrの内容は関係ない diff --git a/router/group_test.go b/router/group_test.go index bf56d244..c4d6c30b 100644 --- a/router/group_test.go +++ b/router/group_test.go @@ -27,12 +27,12 @@ func TestHandlers_GetGroups(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget1 := random.Numeric(t, 1000000) budget2 := random.Numeric(t, 1000000) - group1 := &model.Group{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -41,7 +41,6 @@ func TestHandlers_GetGroups(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - group2 := &model.Group{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -50,12 +49,10 @@ func TestHandlers_GetGroups(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - groups := []*model.Group{group1, group2} e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/groups", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/groups", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -88,13 +85,13 @@ func TestHandlers_GetGroups(t *testing.T) { t.Run("Success2", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) var groups []*model.Group e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/groups", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/groups", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -118,11 +115,11 @@ func TestHandlers_GetGroups(t *testing.T) { t.Run("FailedToGetGroups", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/groups", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/groups", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -147,11 +144,11 @@ func TestHandlers_PostGroup(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) - group := &model.Group{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -173,11 +170,8 @@ func TestHandlers_PostGroup(t *testing.T) { Budget: *group.Budget, }) require.NoError(t, err) - req, err := http.NewRequest( - http.MethodPost, - "/api/groups", - bytes.NewReader(reqBody)) - require.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/groups", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -208,6 +202,7 @@ func TestHandlers_PostGroup(t *testing.T) { t.Run("FailedWithCreateGroup", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) budget := random.Numeric(t, 1000000) @@ -224,11 +219,8 @@ func TestHandlers_PostGroup(t *testing.T) { Budget: budget, }) require.NoError(t, err) - req, err := http.NewRequest( - http.MethodPost, - "/api/groups", - bytes.NewReader(reqBody)) - require.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/groups", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -253,9 +245,10 @@ func TestHandlers_GetGroupDetail(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) group := &model.Group{ ID: uuid.New(), @@ -265,7 +258,6 @@ func TestHandlers_GetGroupDetail(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - user1 := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -310,8 +302,8 @@ func TestHandlers_GetGroupDetail(t *testing.T) { memberIDs := []*uuid.UUID{&member1.ID, &member2.ID} e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/groups/%s", group.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", group.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) // FIXME: #822 req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -357,9 +349,10 @@ func TestHandlers_GetGroupDetail(t *testing.T) { t.Run("Success2", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) group := &model.Group{ ID: uuid.New(), @@ -371,8 +364,8 @@ func TestHandlers_GetGroupDetail(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/groups/%s", group.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", group.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) // FIXME: #822 req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -418,17 +411,15 @@ func TestHandlers_GetGroupDetail(t *testing.T) { t.Run("FailedWithUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) + ctrl := gomock.NewController(t) invalidUUID := "invalid-uuid" _, resErr := uuid.Parse(invalidUUID) - ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/groups/%s", invalidUUID), - nil) - require.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", invalidUUID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("api/groups/:groupID") @@ -445,14 +436,12 @@ func TestHandlers_GetGroupDetail(t *testing.T) { t.Run("NilGroupID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/groups/%s", uuid.Nil.String()), - nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", uuid.Nil.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -473,17 +462,15 @@ func TestHandlers_GetGroupDetail(t *testing.T) { t.Run("UnknownGroupID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) unknownGroupID := uuid.New() var resErr *ent.NotFoundError errors.As(errors.New("unknown group id"), &resErr) e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/groups/%s", unknownGroupID), - nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", unknownGroupID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -506,6 +493,7 @@ func TestHandlers_GetGroupDetail(t *testing.T) { t.Run("FailedToGetGroup", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) resErr := errors.New("failed to get groups") @@ -522,8 +510,8 @@ func TestHandlers_GetGroupDetail(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/groups/%s", group.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", group.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -546,6 +534,7 @@ func TestHandlers_GetGroupDetail(t *testing.T) { t.Run("FailedToGetOwners", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) resErr := errors.New("failed to get owners") @@ -562,8 +551,8 @@ func TestHandlers_GetGroupDetail(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/groups/%s", group.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", group.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -591,6 +580,7 @@ func TestHandlers_GetGroupDetail(t *testing.T) { t.Run("FailedToGetMembers", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) resErr := errors.New("failed to get members") @@ -618,8 +608,8 @@ func TestHandlers_GetGroupDetail(t *testing.T) { owners := []*model.Owner{&owner} e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/groups/%s", group.ID), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", group.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -656,13 +646,13 @@ func TestHandlers_PutGroup(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) + date := time.Now() date2 := time.Now().Add(time.Hour) - budget := random.Numeric(t, 1000000) budget2 := random.Numeric(t, 1000000) - group := &model.Group{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -671,7 +661,6 @@ func TestHandlers_PutGroup(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - updated := &model.Group{ ID: group.ID, Name: random.AlphaNumeric(t, 20), @@ -693,11 +682,8 @@ func TestHandlers_PutGroup(t *testing.T) { Budget: *updated.Budget, }) require.NoError(t, err) - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/groups/%s", group.ID.String()), - bytes.NewReader(reqBody)) - require.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", group.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -733,13 +719,13 @@ func TestHandlers_PutGroup(t *testing.T) { t.Run("FailedWithUpdateGroup", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) + date := time.Now() date2 := time.Now().Add(time.Hour) - budget := random.Numeric(t, 1000000) budget2 := random.Numeric(t, 1000000) - group := &model.Group{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -748,7 +734,6 @@ func TestHandlers_PutGroup(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - updated := &model.Group{ ID: group.ID, Name: random.AlphaNumeric(t, 20), @@ -770,11 +755,8 @@ func TestHandlers_PutGroup(t *testing.T) { Budget: *updated.Budget, }) require.NoError(t, err) - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/groups/%s", group.ID.String()), - bytes.NewReader(reqBody)) - require.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", group.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -800,7 +782,9 @@ func TestHandlers_PutGroup(t *testing.T) { t.Run("FailedWithUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) + invalidUUID := "invalid-uuid" _, resErr := uuid.Parse(invalidUUID) @@ -816,11 +800,8 @@ func TestHandlers_PutGroup(t *testing.T) { Budget: 1000000, }) require.NoError(t, err) - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/groups/%s", invalidUUID), - bytes.NewReader(reqBody)) - require.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", invalidUUID) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -842,11 +823,11 @@ func TestHandlers_DeleteGroup(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) - group := &model.Group{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -857,11 +838,8 @@ func TestHandlers_DeleteGroup(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s", group.ID.String()), - nil) - require.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", group.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodDelete, path, nil) rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("api/groups/:groupID") @@ -881,11 +859,11 @@ func TestHandlers_DeleteGroup(t *testing.T) { t.Run("FailedWithDeleteGroup", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) - group := &model.Group{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -896,11 +874,8 @@ func TestHandlers_DeleteGroup(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s", group.ID.String()), - nil) - require.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", group.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodDelete, path, nil) rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("api/groups/:groupID") @@ -923,17 +898,15 @@ func TestHandlers_DeleteGroup(t *testing.T) { t.Run("FailedWithUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) + ctrl := gomock.NewController(t) invalidUUID := "invalid-uuid" _, resErr := uuid.Parse(invalidUUID) - ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s", invalidUUID), - nil) - require.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s", invalidUUID) + req := httptest.NewRequestWithContext(ctx, http.MethodDelete, path, nil) rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("api/groups/:groupID") @@ -954,9 +927,10 @@ func TestHandlers_PostMember(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) group := &model.Group{ ID: uuid.New(), @@ -966,7 +940,6 @@ func TestHandlers_PostMember(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -975,17 +948,13 @@ func TestHandlers_PostMember(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - member := []uuid.UUID{user.ID} reqBody, err := json.Marshal(member) require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - fmt.Sprintf("/api/groups/%s/members", group.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/members", group.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1017,6 +986,7 @@ func TestHandlers_PostMember(t *testing.T) { t.Run("InvalidUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) invalidUUID := "invalid-uuid" @@ -1026,11 +996,8 @@ func TestHandlers_PostMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - fmt.Sprintf("/api/groups/%s/members", invalidUUID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/members", invalidUUID) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1049,6 +1016,7 @@ func TestHandlers_PostMember(t *testing.T) { t.Run("NilUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) member := []uuid.UUID{uuid.Nil} @@ -1056,11 +1024,8 @@ func TestHandlers_PostMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - fmt.Sprintf("/api/groups/%s/members", uuid.Nil.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/members", uuid.Nil.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1081,9 +1046,10 @@ func TestHandlers_PostMember(t *testing.T) { t.Run("UnknownGroupID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -1092,7 +1058,6 @@ func TestHandlers_PostMember(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - unknownGroupID := uuid.New() var resErr *ent.ConstraintError errors.As(errors.New("unknown group id"), &resErr) @@ -1102,11 +1067,8 @@ func TestHandlers_PostMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - fmt.Sprintf("/api/groups/%s/members", unknownGroupID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/members", unknownGroupID) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1129,9 +1091,10 @@ func TestHandlers_PostMember(t *testing.T) { t.Run("UnknownUserID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) group := &model.Group{ ID: uuid.New(), @@ -1151,11 +1114,8 @@ func TestHandlers_PostMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - fmt.Sprintf("/api/groups/%s/members", group.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/members", group.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1182,9 +1142,10 @@ func TestHandlers_DeleteMember(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) group := &model.Group{ ID: uuid.New(), @@ -1194,7 +1155,6 @@ func TestHandlers_DeleteMember(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -1203,17 +1163,14 @@ func TestHandlers_DeleteMember(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - member := []uuid.UUID{user.ID} reqBody, err := json.Marshal(member) require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s/members", group.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/members", group.ID.String()) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1234,9 +1191,10 @@ func TestHandlers_DeleteMember(t *testing.T) { t.Run("NilGroupUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -1245,17 +1203,14 @@ func TestHandlers_DeleteMember(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - member := []uuid.UUID{user.ID} reqBody, err := json.Marshal(member) require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s/members", uuid.Nil.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/members", uuid.Nil.String()) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1276,9 +1231,10 @@ func TestHandlers_DeleteMember(t *testing.T) { t.Run("UnknownGroupID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -1287,21 +1243,17 @@ func TestHandlers_DeleteMember(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - unknownGroupID := uuid.New() var resErr *ent.NotFoundError errors.As(errors.New("unknown group id"), &resErr) - member := []uuid.UUID{user.ID} reqBody, err := json.Marshal(member) require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s/members", unknownGroupID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/members", unknownGroupID.String()) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1324,9 +1276,10 @@ func TestHandlers_DeleteMember(t *testing.T) { t.Run("UnknownMemberID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) group := &model.Group{ ID: uuid.New(), @@ -1336,21 +1289,17 @@ func TestHandlers_DeleteMember(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - unknownUserID := uuid.New() var resErr *ent.NotFoundError errors.As(errors.New("unknown member id"), &resErr) - member := []uuid.UUID{unknownUserID} reqBody, err := json.Marshal(member) require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s/members", group.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/members", group.ID.String()) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1373,21 +1322,19 @@ func TestHandlers_DeleteMember(t *testing.T) { t.Run("InvalidGroupUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) invalidUUID := "invalid-uuid" _, resErr := uuid.Parse(invalidUUID) - member := []uuid.UUID{uuid.New()} reqBody, err := json.Marshal(member) require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s/members", invalidUUID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/members", invalidUUID) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1410,9 +1357,10 @@ func TestHandlers_PostOwner(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) group := &model.Group{ ID: uuid.New(), @@ -1422,7 +1370,6 @@ func TestHandlers_PostOwner(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -1431,17 +1378,13 @@ func TestHandlers_PostOwner(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - owner := []uuid.UUID{user.ID} reqBody, err := json.Marshal(owner) require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - fmt.Sprintf("/api/groups/%s/owners", group.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/owners", group.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1460,6 +1403,7 @@ func TestHandlers_PostOwner(t *testing.T) { Return([]*model.Owner{ modelOwner, }, nil) + assert.NoError(t, h.Handlers.PostOwner(c)) assert.Equal(t, http.StatusOK, rec.Code) var got []uuid.UUID @@ -1471,19 +1415,18 @@ func TestHandlers_PostOwner(t *testing.T) { t.Run("InvalidUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) invalidUUID := "invalid-uuid" + _, resErr := uuid.Parse(invalidUUID) owner := []string{invalidUUID} reqBody, err := json.Marshal(owner) require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - fmt.Sprintf("/api/groups/%s/owners", invalidUUID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/owners", invalidUUID) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1494,8 +1437,6 @@ func TestHandlers_PostOwner(t *testing.T) { h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) - _, resErr := uuid.Parse(c.Param("groupID")) - err = h.Handlers.PostOwner(c) assert.Error(t, err) // FIXME: http.StatusBadRequestだけ判定したい; resErrの内容は関係ない @@ -1504,6 +1445,7 @@ func TestHandlers_PostOwner(t *testing.T) { t.Run("NilUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) owner := []uuid.UUID{uuid.New()} @@ -1511,11 +1453,8 @@ func TestHandlers_PostOwner(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - fmt.Sprintf("/api/groups/%s/owners", uuid.Nil.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/owners", uuid.Nil.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1536,9 +1475,10 @@ func TestHandlers_PostOwner(t *testing.T) { t.Run("UnknownGroupID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -1547,21 +1487,16 @@ func TestHandlers_PostOwner(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - unknownGroupID := uuid.New() var resErr *ent.ConstraintError errors.As(errors.New("unknown group id"), &resErr) - owner := []uuid.UUID{user.ID} reqBody, err := json.Marshal(owner) require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - fmt.Sprintf("/api/groups/%s/owners", unknownGroupID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/owners", unknownGroupID) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1584,9 +1519,10 @@ func TestHandlers_PostOwner(t *testing.T) { t.Run("UnknownUserID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) group := &model.Group{ ID: uuid.New(), @@ -1596,21 +1532,16 @@ func TestHandlers_PostOwner(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - unknownUserID := uuid.New() var resErr *ent.ConstraintError errors.As(errors.New("unknown user id"), &resErr) - owner := []uuid.UUID{unknownUserID} reqBody, err := json.Marshal(owner) require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPost, - fmt.Sprintf("/api/groups/%s/owners", group.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/owners", group.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPost, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1637,9 +1568,10 @@ func TestHandlers_DeleteOwner(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) group := &model.Group{ ID: uuid.New(), @@ -1649,7 +1581,6 @@ func TestHandlers_DeleteOwner(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -1658,17 +1589,14 @@ func TestHandlers_DeleteOwner(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - owner := []uuid.UUID{user.ID} reqBody, err := json.Marshal(owner) require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s/owners", group.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/owners", group.ID.String()) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1689,6 +1617,7 @@ func TestHandlers_DeleteOwner(t *testing.T) { t.Run("NilGroupUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) owner := []uuid.UUID{uuid.Nil} @@ -1696,11 +1625,9 @@ func TestHandlers_DeleteOwner(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s/owners", uuid.Nil.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/owners", uuid.Nil.String()) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1721,9 +1648,10 @@ func TestHandlers_DeleteOwner(t *testing.T) { t.Run("UnknownGroupID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -1732,21 +1660,17 @@ func TestHandlers_DeleteOwner(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - unknownGroupID := uuid.New() var resErr *ent.NotFoundError errors.As(errors.New("unknown group id"), &resErr) - owner := []uuid.UUID{user.ID} reqBody, err := json.Marshal(owner) require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s/owners", unknownGroupID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/owners", unknownGroupID) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1768,9 +1692,10 @@ func TestHandlers_DeleteOwner(t *testing.T) { t.Run("UnknownOwnerID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 1000000) group := &model.Group{ ID: uuid.New(), @@ -1780,7 +1705,6 @@ func TestHandlers_DeleteOwner(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - unknownUserID := uuid.New() reqBody, err := json.Marshal([]uuid.UUID{unknownUserID}) require.NoError(t, err) @@ -1788,11 +1712,9 @@ func TestHandlers_DeleteOwner(t *testing.T) { errors.As(errors.New("unknown owner id"), &resErr) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s/owners", group.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/owners", group.ID.String()) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1815,27 +1737,25 @@ func TestHandlers_DeleteOwner(t *testing.T) { t.Run("InvalidGroupUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - invID := "po" + invalidUUID := "invalid-uuid" + _, resErr := uuid.Parse(invalidUUID) owner := []uuid.UUID{uuid.New()} reqBody, err := json.Marshal(owner) require.NoError(t, err) - _, resErr := uuid.Parse(invID) - e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/groups/%s/owners", invID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/groups/%s/owners", invalidUUID) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/groups/:groupID/owners") c.SetParamNames("groupID") - c.SetParamValues(invID) + c.SetParamValues(invalidUUID) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) diff --git a/router/request_test.go b/router/request_test.go index 0de35a90..dfcc06b7 100644 --- a/router/request_test.go +++ b/router/request_test.go @@ -104,11 +104,11 @@ func TestHandlers_GetRequests(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date1 := time.Now() date2 := date1.Add(time.Hour) - request1 := &model.RequestResponse{ ID: uuid.New(), Status: model.Submitted, @@ -130,8 +130,7 @@ func TestHandlers_GetRequests(t *testing.T) { requests := []*model.RequestResponse{request2, request1} e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/requests", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/requests", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -183,13 +182,13 @@ func TestHandlers_GetRequests(t *testing.T) { t.Run("Success2", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) requests := []*model.RequestResponse{} e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/requests", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/requests", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -214,10 +213,10 @@ func TestHandlers_GetRequests(t *testing.T) { t.Run("Success3", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date1 := time.Now() - request1 := &model.RequestResponse{ ID: uuid.New(), Status: model.Submitted, @@ -229,14 +228,10 @@ func TestHandlers_GetRequests(t *testing.T) { } requests := []*model.RequestResponse{request1} - status := "submitted" - e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/requests?status=%s", status), - nil) - assert.NoError(t, err) + status := "submitted" + path := fmt.Sprintf("/api/requests?status=%s", status) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -277,13 +272,13 @@ func TestHandlers_GetRequests(t *testing.T) { t.Run("Success4", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date1 := time.Now() date2str := date1.Add(time.Hour).Format("2006-01-02") date2, err := service.StrToDate(date2str) require.NoError(t, err) - request1 := &model.RequestResponse{ ID: uuid.New(), Status: model.Submitted, @@ -296,11 +291,8 @@ func TestHandlers_GetRequests(t *testing.T) { requests := []*model.RequestResponse{request1} e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/requests?until=%s", date2str), - nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests?until=%s", date2str) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -341,13 +333,13 @@ func TestHandlers_GetRequests(t *testing.T) { t.Run("Success5", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date1 := time.Now() date2str := date1.Add(-time.Hour).Format("2006-01-02") date2, err := service.StrToDate(date2str) require.NoError(t, err) - request1 := &model.RequestResponse{ ID: uuid.New(), Status: model.Submitted, @@ -360,11 +352,8 @@ func TestHandlers_GetRequests(t *testing.T) { requests := []*model.RequestResponse{request1} e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/requests?since=%s", date2str), - nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests?since=%s", date2str) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -404,24 +393,22 @@ func TestHandlers_GetRequests(t *testing.T) { t.Run("Success6", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date1 := time.Now() - tag1 := model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 10), CreatedAt: date1, UpdatedAt: date1, } - tag1ov := TagOverview{ ID: tag1.ID, Name: tag1.Name, CreatedAt: tag1.CreatedAt, UpdatedAt: tag1.UpdatedAt, } - request1 := &model.RequestResponse{ ID: uuid.New(), Status: model.Submitted, @@ -435,11 +422,8 @@ func TestHandlers_GetRequests(t *testing.T) { requests := []*model.RequestResponse{request1} e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/requests?tag=%s", tag1.Name), - nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests?tag=%s", tag1.Name) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -479,6 +463,7 @@ func TestHandlers_GetRequests(t *testing.T) { t.Run("Success7", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date := time.Now() @@ -494,11 +479,8 @@ func TestHandlers_GetRequests(t *testing.T) { modelRequests := []*model.RequestResponse{request} e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/requests?created_by=%s", request.CreatedBy.String()), - nil) - require.NoError(t, err) + path := fmt.Sprintf("/api/requests?created_by=%s", request.CreatedBy.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -512,6 +494,7 @@ func TestHandlers_GetRequests(t *testing.T) { CreatedBy: &request.CreatedBy}, ). Return(modelRequests, nil) + err = h.Handlers.GetRequests(c) if !assert.NoError(t, err) { return @@ -540,11 +523,12 @@ func TestHandlers_GetRequests(t *testing.T) { t.Run("InvaildStatus", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/requests?status=invalid-status", nil) - assert.NoError(t, err) + path := "/api/requests?status=invalid-status" + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -560,11 +544,11 @@ func TestHandlers_GetRequests(t *testing.T) { t.Run("FailedToGetRequests", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/requests", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/requests", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -592,9 +576,10 @@ func TestHandlers_PostRequest(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -610,7 +595,6 @@ func TestHandlers_PostRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - reqRequest := Request{ CreatedBy: request.CreatedBy, Title: request.Title, @@ -623,8 +607,8 @@ func TestHandlers_PostRequest(t *testing.T) { var group *model.Group e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) - assert.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -652,9 +636,10 @@ func TestHandlers_PostRequest(t *testing.T) { t.Run("SuccessWithTags", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -662,7 +647,6 @@ func TestHandlers_PostRequest(t *testing.T) { UpdatedAt: date, } tags := []*model.Tag{tag} - request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -679,7 +663,6 @@ func TestHandlers_PostRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - reqRequest := Request{ CreatedBy: request.CreatedBy, Title: request.Title, @@ -692,8 +675,8 @@ func TestHandlers_PostRequest(t *testing.T) { var group *model.Group e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) - assert.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -712,6 +695,7 @@ func TestHandlers_PostRequest(t *testing.T) { tags, targets, group, reqRequest.CreatedBy). Return(request, nil) + assert.NoError(t, h.Handlers.PostRequest(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *RequestResponse @@ -724,9 +708,10 @@ func TestHandlers_PostRequest(t *testing.T) { t.Run("SuccessWithGroup", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() budget := random.Numeric(t, 100000) group := &model.Group{ ID: uuid.New(), @@ -734,7 +719,6 @@ func TestHandlers_PostRequest(t *testing.T) { Description: random.AlphaNumeric(t, 50), Budget: &budget, } - request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -751,7 +735,6 @@ func TestHandlers_PostRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - reqRequest := Request{ CreatedBy: request.CreatedBy, Title: request.Title, @@ -764,8 +747,8 @@ func TestHandlers_PostRequest(t *testing.T) { targets := []*model.RequestTarget{} e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) - assert.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -784,6 +767,7 @@ func TestHandlers_PostRequest(t *testing.T) { tags, targets, group, reqRequest.CreatedBy). Return(request, nil) + assert.NoError(t, h.Handlers.PostRequest(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *RequestResponse @@ -796,21 +780,20 @@ func TestHandlers_PostRequest(t *testing.T) { t.Run("SuccessWithTarget", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() target := &model.RequestTarget{ Target: uuid.New(), Amount: random.Numeric(t, 1000000), } - tgd := &model.RequestTargetDetail{ ID: uuid.New(), Target: target.Target, Amount: target.Amount, CreatedAt: date, } - request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -827,12 +810,10 @@ func TestHandlers_PostRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - tg := &Target{ Target: target.Target, Amount: target.Amount, } - reqRequest := Request{ CreatedBy: request.CreatedBy, Title: request.Title, @@ -845,8 +826,8 @@ func TestHandlers_PostRequest(t *testing.T) { var group *model.Group e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) - assert.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -861,6 +842,7 @@ func TestHandlers_PostRequest(t *testing.T) { tags, []*model.RequestTarget{target}, group, reqRequest.CreatedBy). Return(request, nil) + assert.NoError(t, h.Handlers.PostRequest(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *RequestResponse @@ -873,9 +855,10 @@ func TestHandlers_PostRequest(t *testing.T) { t.Run("UnknownTagID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -885,9 +868,7 @@ func TestHandlers_PostRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - unknownTagID := uuid.New() - reqRequest := Request{ CreatedBy: request.CreatedBy, Title: request.Title, @@ -898,8 +879,8 @@ func TestHandlers_PostRequest(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) - assert.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -922,9 +903,10 @@ func TestHandlers_PostRequest(t *testing.T) { t.Run("UnknownGroupID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -934,9 +916,7 @@ func TestHandlers_PostRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - unknownGroupID := uuid.New() - reqRequest := Request{ CreatedBy: request.CreatedBy, Title: request.Title, @@ -947,8 +927,8 @@ func TestHandlers_PostRequest(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) - assert.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -971,9 +951,10 @@ func TestHandlers_PostRequest(t *testing.T) { t.Run("UnknownUserID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -983,7 +964,6 @@ func TestHandlers_PostRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - reqRequest := Request{ CreatedBy: request.CreatedBy, Title: request.Title, @@ -996,8 +976,8 @@ func TestHandlers_PostRequest(t *testing.T) { var group *model.Group e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) - assert.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/requests", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1028,9 +1008,10 @@ func TestHandlers_GetRequest(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -1051,11 +1032,8 @@ func TestHandlers_GetRequest(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/requests/%s", request.ID.String()), - nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1073,6 +1051,7 @@ func TestHandlers_GetRequest(t *testing.T) { EXPECT(). GetComments(c.Request().Context(), request.ID). Return(nil, nil) + assert.NoError(t, h.Handlers.GetRequest(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *RequestResponse @@ -1085,6 +1064,7 @@ func TestHandlers_GetRequest(t *testing.T) { t.Run("SuccessWithComments", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date := time.Now() @@ -1106,7 +1086,6 @@ func TestHandlers_GetRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - comment1 := &model.Comment{ ID: uuid.New(), User: request.CreatedBy, @@ -1124,11 +1103,8 @@ func TestHandlers_GetRequest(t *testing.T) { comments := []*model.Comment{comment1, comment2} e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/requests/%s", request.ID.String()), - nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1162,9 +1138,10 @@ func TestHandlers_GetRequest(t *testing.T) { t.Run("SuccessWithTarget", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() target := &model.RequestTargetDetail{ ID: uuid.New(), Target: uuid.New(), @@ -1172,7 +1149,6 @@ func TestHandlers_GetRequest(t *testing.T) { PaidAt: nil, CreatedAt: date, } - request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -1192,12 +1168,10 @@ func TestHandlers_GetRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } + e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/requests/%s", request.ID.String()), - nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1231,16 +1205,15 @@ func TestHandlers_GetRequest(t *testing.T) { t.Run("InvalidUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) + invalidUUID := "invalid-uuid" _, resErr := uuid.Parse(invalidUUID) e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/requests/%s", invalidUUID), - nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", invalidUUID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1259,11 +1232,12 @@ func TestHandlers_GetRequest(t *testing.T) { t.Run("NilUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/requests/%s", uuid.Nil), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", uuid.Nil) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1284,16 +1258,14 @@ func TestHandlers_GetRequest(t *testing.T) { t.Run("UnknownID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) unknownID := uuid.New() e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/requests/%s", unknownID.String()), - nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", unknownID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1323,9 +1295,10 @@ func TestHandlers_PutRequest(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -1342,7 +1315,6 @@ func TestHandlers_PutRequest(t *testing.T) { Tags: []*model.Tag{}, Targets: []*model.RequestTargetDetail{}, } - reqRequest := PutRequest{ Title: random.AlphaNumeric(t, 30), Content: random.AlphaNumeric(t, 50), @@ -1352,7 +1324,6 @@ func TestHandlers_PutRequest(t *testing.T) { tags := []*model.Tag{} targets := []*model.RequestTarget{} var group *model.Group - updateRequest := &model.RequestDetail{ ID: request.ID, Status: request.Status, @@ -1365,11 +1336,8 @@ func TestHandlers_PutRequest(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s", request.ID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", request.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1405,9 +1373,10 @@ func TestHandlers_PutRequest(t *testing.T) { t.Run("SuccessWithTag", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -1423,7 +1392,6 @@ func TestHandlers_PutRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - tag1 := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -1446,7 +1414,6 @@ func TestHandlers_PutRequest(t *testing.T) { require.NoError(t, err) targets := []*model.RequestTarget{} var group *model.Group - updateRequest := &model.RequestDetail{ ID: request.ID, Status: request.Status, @@ -1461,11 +1428,8 @@ func TestHandlers_PutRequest(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1509,9 +1473,10 @@ func TestHandlers_PutRequest(t *testing.T) { t.Run("SuccessWithTarget", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -1545,7 +1510,6 @@ func TestHandlers_PutRequest(t *testing.T) { {Target: target2.Target, Amount: target2.Amount}, } targetDetails := []*model.RequestTargetDetail{target1, target2} - reqRequest := PutRequest{ Title: random.AlphaNumeric(t, 30), Content: random.AlphaNumeric(t, 50), @@ -1558,7 +1522,6 @@ func TestHandlers_PutRequest(t *testing.T) { require.NoError(t, err) tags := []*model.Tag{} var group *model.Group - updateRequest := &model.RequestDetail{ ID: request.ID, Status: request.Status, @@ -1573,11 +1536,8 @@ func TestHandlers_PutRequest(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1601,6 +1561,7 @@ func TestHandlers_PutRequest(t *testing.T) { EXPECT(). GetComments(c.Request().Context(), request.ID). Return([]*model.Comment{}, nil) + assert.NoError(t, h.Handlers.PutRequest(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *RequestResponse @@ -1613,9 +1574,10 @@ func TestHandlers_PutRequest(t *testing.T) { t.Run("SuccessWithGroup", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -1631,7 +1593,6 @@ func TestHandlers_PutRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - budget := random.Numeric(t, 100000) group := &model.Group{ ID: uuid.New(), @@ -1650,7 +1611,6 @@ func TestHandlers_PutRequest(t *testing.T) { require.NoError(t, err) tags := []*model.Tag{} targets := []*model.RequestTarget{} - updateRequest := &model.RequestDetail{ ID: request.ID, Status: request.Status, @@ -1665,11 +1625,8 @@ func TestHandlers_PutRequest(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1697,6 +1654,7 @@ func TestHandlers_PutRequest(t *testing.T) { EXPECT(). GetComments(c.Request().Context(), request.ID). Return([]*model.Comment{}, nil) + assert.NoError(t, h.Handlers.PutRequest(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *RequestResponse @@ -1709,9 +1667,10 @@ func TestHandlers_PutRequest(t *testing.T) { t.Run("SuccessWithComment", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -1727,7 +1686,6 @@ func TestHandlers_PutRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - reqRequest := PutRequest{ Title: random.AlphaNumeric(t, 30), Content: random.AlphaNumeric(t, 50), @@ -1737,7 +1695,6 @@ func TestHandlers_PutRequest(t *testing.T) { tags := []*model.Tag{} targets := []*model.RequestTarget{} var group *model.Group - updateRequest := &model.RequestDetail{ ID: request.ID, Status: request.Status, @@ -1748,7 +1705,6 @@ func TestHandlers_PutRequest(t *testing.T) { Title: reqRequest.Title, Content: reqRequest.Content, } - comment1 := &model.Comment{ ID: uuid.New(), User: request.CreatedBy, @@ -1766,11 +1722,8 @@ func TestHandlers_PutRequest(t *testing.T) { comments := []*model.Comment{comment1, comment2} e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s", request.ID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", request.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1793,6 +1746,7 @@ func TestHandlers_PutRequest(t *testing.T) { EXPECT(). GetComments(c.Request().Context(), request.ID). Return(comments, nil) + assert.NoError(t, h.Handlers.PutRequest(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *RequestResponse @@ -1808,16 +1762,15 @@ func TestHandlers_PutRequest(t *testing.T) { t.Run("InvalidUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) + invalidUUID := "invalid-uuid" _, resErr := uuid.Parse(invalidUUID) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s", invalidUUID), - nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", invalidUUID) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1836,11 +1789,12 @@ func TestHandlers_PutRequest(t *testing.T) { t.Run("NilUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s", uuid.Nil), nil) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", uuid.Nil) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1861,6 +1815,7 @@ func TestHandlers_PutRequest(t *testing.T) { t.Run("UnknownID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) unknownID := uuid.New() @@ -1875,11 +1830,8 @@ func TestHandlers_PutRequest(t *testing.T) { var group *model.Group e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s", unknownID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", unknownID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1910,9 +1862,10 @@ func TestHandlers_PutRequest(t *testing.T) { t.Run("UnknownTagID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -1922,14 +1875,12 @@ func TestHandlers_PutRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - reqRequest := PutRequest{ Title: random.AlphaNumeric(t, 30), Content: random.AlphaNumeric(t, 50), @@ -1939,11 +1890,8 @@ func TestHandlers_PutRequest(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -1969,9 +1917,10 @@ func TestHandlers_PutRequest(t *testing.T) { t.Run("UnknownGroupID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() request := &model.RequestDetail{ ID: uuid.New(), Status: model.Submitted, @@ -1981,7 +1930,6 @@ func TestHandlers_PutRequest(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - budget := random.Numeric(t, 100000) group := &model.Group{ ID: uuid.New(), @@ -1991,7 +1939,6 @@ func TestHandlers_PutRequest(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - reqRequest := PutRequest{ Title: random.AlphaNumeric(t, 30), Content: random.AlphaNumeric(t, 50), @@ -2001,11 +1948,8 @@ func TestHandlers_PutRequest(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -2035,9 +1979,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("SuccessByCreator", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -2054,14 +1999,12 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: user.ID, } - reqStatus := PutStatus{ Status: model.Submitted, Comment: random.AlphaNumeric(t, 20), } reqBody, err := json.Marshal(reqStatus) require.NoError(t, err) - comment := &model.Comment{ ID: uuid.New(), User: user.ID, @@ -2076,11 +2019,8 @@ func TestHandlers_PutStatus(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -2105,7 +2045,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -2122,6 +2062,7 @@ func TestHandlers_PutStatus(t *testing.T) { EXPECT(). CreateComment(ctx, reqStatus.Comment, request.ID, user.ID). Return(comment, nil) + assert.NoError(t, h.Handlers.PutStatus(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *StatusResponse @@ -2145,9 +2086,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("SuccessByAdminFromSubmittedToFixRequired", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -2164,14 +2106,12 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - reqStatus := PutStatus{ Status: model.FixRequired, Comment: random.AlphaNumeric(t, 20), } reqBody, err := json.Marshal(reqStatus) require.NoError(t, err) - comment := &model.Comment{ ID: uuid.New(), User: user.ID, @@ -2186,11 +2126,8 @@ func TestHandlers_PutStatus(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -2215,7 +2152,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -2232,6 +2169,7 @@ func TestHandlers_PutStatus(t *testing.T) { EXPECT(). CreateComment(ctx, reqStatus.Comment, request.ID, user.ID). Return(comment, nil) + assert.NoError(t, h.Handlers.PutStatus(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *StatusResponse @@ -2255,9 +2193,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("SuccessByAdminFromSubmittedToAccepted", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -2274,14 +2213,12 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - reqStatus := PutStatus{ Status: model.Accepted, Comment: random.AlphaNumeric(t, 20), } reqBody, err := json.Marshal(reqStatus) require.NoError(t, err) - comment := &model.Comment{ ID: uuid.New(), User: user.ID, @@ -2296,11 +2233,8 @@ func TestHandlers_PutStatus(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -2325,7 +2259,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -2342,6 +2276,7 @@ func TestHandlers_PutStatus(t *testing.T) { EXPECT(). CreateComment(ctx, reqStatus.Comment, request.ID, user.ID). Return(comment, nil) + assert.NoError(t, h.Handlers.PutStatus(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *StatusResponse @@ -2365,9 +2300,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("SuccessByAdminFromSubmittedToFixRequired", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -2384,14 +2320,12 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - reqStatus := PutStatus{ Status: model.FixRequired, Comment: random.AlphaNumeric(t, 20), } reqBody, err := json.Marshal(reqStatus) require.NoError(t, err) - comment := &model.Comment{ ID: uuid.New(), User: user.ID, @@ -2406,11 +2340,8 @@ func TestHandlers_PutStatus(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -2435,7 +2366,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -2452,6 +2383,7 @@ func TestHandlers_PutStatus(t *testing.T) { EXPECT(). CreateComment(ctx, reqStatus.Comment, request.ID, user.ID). Return(comment, nil) + assert.NoError(t, h.Handlers.PutStatus(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *StatusResponse @@ -2475,9 +2407,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("SuccessByAdminFromFixRequiredToSubmitted", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -2494,14 +2427,12 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - reqStatus := PutStatus{ Status: model.Submitted, Comment: random.AlphaNumeric(t, 20), } reqBody, err := json.Marshal(reqStatus) require.NoError(t, err) - comment := &model.Comment{ ID: uuid.New(), User: user.ID, @@ -2516,11 +2447,8 @@ func TestHandlers_PutStatus(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -2545,7 +2473,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -2562,6 +2490,7 @@ func TestHandlers_PutStatus(t *testing.T) { EXPECT(). CreateComment(ctx, reqStatus.Comment, request.ID, user.ID). Return(comment, nil) + assert.NoError(t, h.Handlers.PutStatus(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *StatusResponse @@ -2585,9 +2514,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("SuccessByAdminFromAcceptedToSubmitted", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -2611,14 +2541,12 @@ func TestHandlers_PutStatus(t *testing.T) { CreatedAt: date, } targets := []*model.RequestTargetDetail{target} - reqStatus := PutStatus{ Status: model.Submitted, Comment: random.AlphaNumeric(t, 20), } reqBody, err := json.Marshal(reqStatus) require.NoError(t, err) - comment := &model.Comment{ ID: uuid.New(), User: user.ID, @@ -2633,11 +2561,8 @@ func TestHandlers_PutStatus(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -2662,7 +2587,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -2683,6 +2608,7 @@ func TestHandlers_PutStatus(t *testing.T) { EXPECT(). CreateComment(ctx, reqStatus.Comment, request.ID, user.ID). Return(comment, nil) + assert.NoError(t, h.Handlers.PutStatus(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *StatusResponse @@ -2706,9 +2632,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("InvalidStatus", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -2725,7 +2652,6 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: user.ID, } - invalidStatus := random.AlphaNumeric(t, 20) reqBody, err := json.Marshal(&struct { Status string `json:"status"` @@ -2737,11 +2663,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -2781,11 +2704,12 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("InvalidUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) + invalidUUID := "invalid-uuid" _, resErr := uuid.Parse(invalidUUID) date := time.Now() - user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -2794,7 +2718,6 @@ func TestHandlers_PutStatus(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - reqStatus := PutStatus{ Status: model.Submitted, Comment: random.AlphaNumeric(t, 20), @@ -2803,11 +2726,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", invalidUUID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", invalidUUID) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -2840,9 +2760,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("NillUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -2851,7 +2772,6 @@ func TestHandlers_PutStatus(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - reqStatus := PutStatus{ Status: model.Submitted, Comment: random.AlphaNumeric(t, 20), @@ -2860,11 +2780,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", uuid.Nil), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", uuid.Nil) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -2899,9 +2816,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("SessionNotFound", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -2918,7 +2836,6 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: user.ID, } - reqStatus := PutStatus{ Status: model.Submitted, Comment: random.AlphaNumeric(t, 20), @@ -2927,11 +2844,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -2958,9 +2872,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("SameStatusError", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -2977,7 +2892,6 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: user.ID, } - reqStatus := PutStatus{ Status: request.Status, Comment: random.AlphaNumeric(t, 20), @@ -2986,11 +2900,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -3015,7 +2926,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -3031,6 +2942,7 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("CommentRequiredErrorFromSubmittedToFixRequired", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date := time.Now() @@ -3058,11 +2970,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -3087,7 +2996,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -3106,9 +3015,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("CommentRequiredErrorFromSubmittedToRejected", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -3125,7 +3035,6 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: user.ID, } - reqStatus := PutStatus{ Status: model.Rejected, } @@ -3133,11 +3042,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -3162,7 +3068,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -3181,9 +3087,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("CommentRequiredErrorFromAcceptedToSubmitted", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -3200,7 +3107,6 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: user.ID, } - reqStatus := PutStatus{ Status: model.Submitted, } @@ -3208,11 +3114,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -3237,7 +3140,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -3256,9 +3159,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("UnknownUser", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -3275,7 +3179,6 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: user.ID, } - reqStatus := PutStatus{ Status: model.Submitted, Comment: random.AlphaNumeric(t, 20), @@ -3284,11 +3187,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -3316,7 +3216,7 @@ func TestHandlers_PutStatus(t *testing.T) { var resErr *ent.NotFoundError errors.As(errors.New("unknown id"), &resErr) - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -3334,9 +3234,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("AdminNoPrivilege", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -3353,7 +3254,6 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - reqStatus := PutStatus{ Status: model.Accepted, Comment: random.AlphaNumeric(t, 20), @@ -3362,11 +3262,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -3391,7 +3288,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -3414,9 +3311,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("AlreadyPaid", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -3440,7 +3338,6 @@ func TestHandlers_PutStatus(t *testing.T) { CreatedAt: date, } targets := []*model.RequestTargetDetail{target} - reqStatus := PutStatus{ Status: model.Submitted, Comment: random.AlphaNumeric(t, 20), @@ -3449,11 +3346,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -3478,7 +3372,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -3502,9 +3396,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("CreatorNoPrivilege", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -3521,7 +3416,6 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: user.ID, } - reqStatus := PutStatus{ Status: model.Accepted, Comment: random.AlphaNumeric(t, 20), @@ -3530,11 +3424,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -3559,7 +3450,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). @@ -3581,9 +3472,10 @@ func TestHandlers_PutStatus(t *testing.T) { t.Run("NoPrivilege", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() user := &model.User{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -3600,7 +3492,6 @@ func TestHandlers_PutStatus(t *testing.T) { UpdatedAt: date, CreatedBy: uuid.New(), } - reqStatus := PutStatus{ Status: model.Submitted, Comment: random.AlphaNumeric(t, 20), @@ -3609,11 +3500,8 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/requests/%s/status", request.ID.String()), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/requests/%s/status", request.ID.String()) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -3638,7 +3526,7 @@ func TestHandlers_PutStatus(t *testing.T) { Admin: user.Admin, } - ctx := c.Request().Context() + ctx = c.Request().Context() h.Repository.MockRequestRepository. EXPECT(). GetRequest(ctx, request.ID). diff --git a/router/tag_test.go b/router/tag_test.go index ea4f75f6..653c5c5e 100644 --- a/router/tag_test.go +++ b/router/tag_test.go @@ -26,10 +26,10 @@ func TestHandlers_GetTags(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date := time.Now() - tag1 := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -45,8 +45,7 @@ func TestHandlers_GetTags(t *testing.T) { tags := []*model.Tag{tag1, tag2} e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/tags", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/tags", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -77,13 +76,13 @@ func TestHandlers_GetTags(t *testing.T) { t.Run("Success2", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) tags := []*model.Tag{} e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/tags", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/tags", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -107,11 +106,11 @@ func TestHandlers_GetTags(t *testing.T) { t.Run("FailedToGetTags", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/tags", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/tags", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -136,16 +135,16 @@ func TestHandlers_PostTag(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - reqTag := Tag{ Name: tag.Name, } @@ -153,8 +152,8 @@ func TestHandlers_PostTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/tags", bytes.NewReader(reqBody)) - assert.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/tags", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -184,16 +183,16 @@ func TestHandlers_PostTag(t *testing.T) { t.Run("MissingName", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() tag := &model.Tag{ ID: uuid.New(), Name: "", CreatedAt: date, UpdatedAt: date, } - reqTag := Tag{ Name: "", } @@ -201,8 +200,8 @@ func TestHandlers_PostTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/tags", bytes.NewReader(reqBody)) - assert.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/tags", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -228,23 +227,22 @@ func TestHandlers_PutTag(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - reqTag := Tag{ Name: tag.Name, Description: random.AlphaNumeric(t, 50), } reqBody, err := json.Marshal(reqTag) require.NoError(t, err) - updateTag := &model.Tag{ ID: tag.ID, Name: reqTag.Name, @@ -253,11 +251,8 @@ func TestHandlers_PutTag(t *testing.T) { } e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/tags/%s", tag.ID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/tags/%s", tag.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -290,16 +285,16 @@ func TestHandlers_PutTag(t *testing.T) { t.Run("MissingName", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - reqTag := Tag{ Name: "", } @@ -307,11 +302,8 @@ func TestHandlers_PutTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/tags/%s", tag.ID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/tags/%s", tag.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -336,18 +328,19 @@ func TestHandlers_PutTag(t *testing.T) { t.Run("InvalidUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) + invalidUUID := "invalid-uuid" _, resErr := uuid.Parse(invalidUUID) - date := time.Now() + date := time.Now() tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - reqTag := Tag{ Name: tag.Name, Description: random.AlphaNumeric(t, 50), @@ -356,11 +349,8 @@ func TestHandlers_PutTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/tags/%s", invalidUUID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/tags/%s", invalidUUID) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -379,16 +369,16 @@ func TestHandlers_PutTag(t *testing.T) { t.Run("NilUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() tag := &model.Tag{ ID: uuid.Nil, Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - reqTag := Tag{ Name: tag.Name, Description: random.AlphaNumeric(t, 50), @@ -397,11 +387,8 @@ func TestHandlers_PutTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/tags/%s", tag.ID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/tags/%s", tag.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -425,16 +412,16 @@ func TestHandlers_DeleteTag(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - reqTag := Tag{ Name: tag.Name, Description: random.AlphaNumeric(t, 50), @@ -443,11 +430,9 @@ func TestHandlers_DeleteTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/tags/%s", tag.ID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/tags/%s", tag.ID) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -469,16 +454,16 @@ func TestHandlers_DeleteTag(t *testing.T) { t.Run("UnknownID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - reqTag := Tag{ Name: tag.Name, Description: random.AlphaNumeric(t, 50), @@ -487,11 +472,9 @@ func TestHandlers_DeleteTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/tags/%s", tag.ID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/tags/%s", tag.ID) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -516,18 +499,19 @@ func TestHandlers_DeleteTag(t *testing.T) { t.Run("InvalidUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) + invalidUUID := "invalid-uuid" _, resErr := uuid.Parse(invalidUUID) - date := time.Now() + date := time.Now() tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - reqTag := Tag{ Name: tag.Name, Description: random.AlphaNumeric(t, 50), @@ -536,11 +520,9 @@ func TestHandlers_DeleteTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/tags/%s", invalidUUID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/tags/%s", invalidUUID) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -559,16 +541,16 @@ func TestHandlers_DeleteTag(t *testing.T) { t.Run("NilUUID", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) - date := time.Now() + date := time.Now() tag := &model.Tag{ ID: uuid.Nil, Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - reqTag := Tag{ Name: tag.Name, Description: random.AlphaNumeric(t, 50), @@ -577,11 +559,9 @@ func TestHandlers_DeleteTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("/api/tags/%s", tag.ID), - bytes.NewReader(reqBody)) - assert.NoError(t, err) + path := fmt.Sprintf("/api/tags/%s", tag.ID) + req := httptest.NewRequestWithContext( + ctx, http.MethodDelete, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) diff --git a/router/transaction_test.go b/router/transaction_test.go index 5f7dac19..e2994fae 100644 --- a/router/transaction_test.go +++ b/router/transaction_test.go @@ -57,12 +57,11 @@ func TestHandlers_GetTransactions(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date := time.Now() - budget := random.Numeric(t, 1000000) - tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -88,7 +87,6 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - tx2 := &model.TransactionResponse{ ID: uuid.New(), Title: random.AlphaNumeric(t, 20), @@ -108,12 +106,10 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - txs := []*model.TransactionResponse{tx1, tx2} e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/transactions", nil) - require.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/transactions", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -133,6 +129,7 @@ func TestHandlers_GetTransactions(t *testing.T) { Group: nil, }). Return(txs, nil) + assert.NoError(t, h.Handlers.GetTransactions(c)) assert.Equal(t, http.StatusOK, rec.Code) var got []*Transaction @@ -147,12 +144,11 @@ func TestHandlers_GetTransactions(t *testing.T) { t.Run("SuccessWithSort", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date := time.Now() - budget := random.Numeric(t, 1000000) - tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -178,7 +174,6 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - tx2 := &model.TransactionResponse{ ID: uuid.New(), Title: random.AlphaNumeric(t, 20), @@ -199,12 +194,11 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: date.Add(time.Hour), UpdatedAt: date.Add(time.Hour), } - txs := []*model.TransactionResponse{tx1, tx2} e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/transactions?sort=created_at", nil) - require.NoError(t, err) + path := "/api/transactions?sort=created_at" + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -221,6 +215,7 @@ func TestHandlers_GetTransactions(t *testing.T) { Offset: 0, }). Return(txs, nil) + assert.NoError(t, h.Handlers.GetTransactions(c)) assert.Equal(t, http.StatusOK, rec.Code) var got []*Transaction @@ -235,12 +230,11 @@ func TestHandlers_GetTransactions(t *testing.T) { t.Run("SuccessWithAscSort", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date := time.Now() - budget := random.Numeric(t, 1000000) - tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), @@ -266,7 +260,6 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - tx2 := &model.TransactionResponse{ ID: uuid.New(), Title: random.AlphaNumeric(t, 20), @@ -287,13 +280,12 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: date.Add(time.Hour), UpdatedAt: date.Add(time.Hour), } - // Reverse txs := []*model.TransactionResponse{tx2, tx1} e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/transactions?sort=-created_at", nil) - require.NoError(t, err) + path := "/api/transactions?sort=-created_at" + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -310,6 +302,7 @@ func TestHandlers_GetTransactions(t *testing.T) { Offset: 0, }). Return(txs, nil) + assert.NoError(t, h.Handlers.GetTransactions(c)) assert.Equal(t, http.StatusOK, rec.Code) var got []*Transaction @@ -324,21 +317,18 @@ func TestHandlers_GetTransactions(t *testing.T) { t.Run("SuccessWithTarget", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date := time.Now() - budget := random.Numeric(t, 1000000) - tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - target1 := random.AlphaNumeric(t, 20) - tx1 := &model.TransactionResponse{ ID: uuid.New(), Title: random.AlphaNumeric(t, 20), @@ -358,7 +348,6 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - tx2 := &model.TransactionResponse{ ID: uuid.New(), Title: random.AlphaNumeric(t, 20), @@ -379,15 +368,11 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: date.Add(time.Hour), UpdatedAt: date.Add(time.Hour), } - txs := []*model.TransactionResponse{tx1, tx2} e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/transactions?target=%s", target1), - nil) - require.NoError(t, err) + path := fmt.Sprintf("/api/transactions?target=%s", target1) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -404,6 +389,7 @@ func TestHandlers_GetTransactions(t *testing.T) { Offset: 0, }). Return(txs, nil) + assert.NoError(t, h.Handlers.GetTransactions(c)) assert.Equal(t, http.StatusOK, rec.Code) var got []*Transaction @@ -418,21 +404,18 @@ func TestHandlers_GetTransactions(t *testing.T) { t.Run("SuccessWithSinceUntil", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date := time.Now() - budget := random.Numeric(t, 1000000) - tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - target1 := random.AlphaNumeric(t, 20) - tx1 := &model.TransactionResponse{ ID: uuid.New(), Title: random.AlphaNumeric(t, 20), @@ -452,7 +435,6 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - txs := []*model.TransactionResponse{tx1} since, err := service.StrToDate("2020-01-01") require.NoError(t, err) @@ -460,18 +442,14 @@ func TestHandlers_GetTransactions(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest( - http.MethodGet, - fmt.Sprintf("/api/transactions?since=%s&until=%s", "2020-01-01", "2020-01-02"), - nil) - require.NoError(t, err) + path := fmt.Sprintf("/api/transactions?since=%s&until=%s", "2020-01-01", "2020-01-02") + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) - h.Repository.MockTransactionRepository. EXPECT(). GetTransactions(c.Request().Context(), model.TransactionQuery{ @@ -481,6 +459,7 @@ func TestHandlers_GetTransactions(t *testing.T) { Offset: 0, }). Return(txs, nil) + assert.NoError(t, h.Handlers.GetTransactions(c)) assert.Equal(t, http.StatusOK, rec.Code) var got []*Transaction @@ -501,21 +480,18 @@ func TestHandlers_PostTransaction(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date := time.Now() - budget := random.Numeric(t, 1000000) - tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - target1 := random.AlphaNumeric(t, 20) - tx1 := &model.TransactionResponse{ ID: uuid.New(), Title: random.AlphaNumeric(t, 20), @@ -535,9 +511,7 @@ func TestHandlers_PostTransaction(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - txs := []*model.TransactionResponse{tx1} - tags := []*uuid.UUID{&tag.ID} group := tx1.Group.ID @@ -556,11 +530,8 @@ func TestHandlers_PostTransaction(t *testing.T) { Group: &group, }) require.NoError(t, err) - req, err := http.NewRequest( - http.MethodPost, - "/api/transactions", - bytes.NewReader(reqBody)) - require.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/transactions", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -574,6 +545,7 @@ func TestHandlers_PostTransaction(t *testing.T) { c.Request().Context(), tx1.Title, tx1.Amount, tx1.Target, tags, &group, nil). Return(tx1, nil) + assert.NoError(t, h.Handlers.PostTransaction(c)) assert.Equal(t, http.StatusOK, rec.Code) var got []*Transaction @@ -588,21 +560,18 @@ func TestHandlers_PostTransaction(t *testing.T) { t.Run("SuccessWithRequest", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) date := time.Now() - budget := random.Numeric(t, 1000000) - tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: date, UpdatedAt: date, } - target1 := random.AlphaNumeric(t, 20) - tx := &model.TransactionResponse{ ID: uuid.New(), Title: random.AlphaNumeric(t, 20), @@ -622,10 +591,8 @@ func TestHandlers_PostTransaction(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - tags := []*uuid.UUID{&tag.ID} group := tx.Group.ID - request := &model.RequestDetail{ ID: uuid.New(), Status: model.Accepted, @@ -651,11 +618,8 @@ func TestHandlers_PostTransaction(t *testing.T) { Request: &request.ID, }) require.NoError(t, err) - req, err := http.NewRequest( - http.MethodPost, - "/api/transactions", - bytes.NewReader(reqBody)) - require.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, "/api/transactions", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -694,17 +658,16 @@ func TestHandlers_GetTransaction(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) budget := random.Numeric(t, 1000000) - tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: time.Now(), UpdatedAt: time.Now(), } - tx := &model.TransactionResponse{ ID: uuid.New(), Title: random.AlphaNumeric(t, 20), @@ -726,8 +689,8 @@ func TestHandlers_GetTransaction(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/transactions/%s", tx.ID), nil) - require.NoError(t, err) + path := fmt.Sprintf("/api/transactions/%s", tx.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, path, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -741,6 +704,7 @@ func TestHandlers_GetTransaction(t *testing.T) { EXPECT(). GetTransaction(c.Request().Context(), tx.ID). Return(tx, nil) + assert.NoError(t, h.Handlers.GetTransaction(c)) assert.Equal(t, http.StatusOK, rec.Code) var got *Transaction @@ -757,24 +721,22 @@ func TestHandlers_PutTransaction(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) budget := random.Numeric(t, 1000000) - tag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: time.Now(), UpdatedAt: time.Now(), } - updatedTag := &model.Tag{ ID: uuid.New(), Name: random.AlphaNumeric(t, 20), CreatedAt: time.Now(), UpdatedAt: time.Now(), } - tx := &model.TransactionResponse{ ID: uuid.New(), Title: random.AlphaNumeric(t, 20), @@ -794,7 +756,6 @@ func TestHandlers_PutTransaction(t *testing.T) { CreatedAt: time.Now(), UpdatedAt: time.Now(), } - updated := &model.TransactionResponse{ ID: tx.ID, Title: random.AlphaNumeric(t, 20), @@ -814,7 +775,6 @@ func TestHandlers_PutTransaction(t *testing.T) { CreatedAt: time.Now(), UpdatedAt: time.Now(), } - updatedTags := lo.Map(updated.Tags, func(modelTag *model.Tag, _ int) *uuid.UUID { return &modelTag.ID }) @@ -832,11 +792,8 @@ func TestHandlers_PutTransaction(t *testing.T) { Tags: []uuid.UUID{updatedTag.ID}, }) require.NoError(t, err) - req, err := http.NewRequest( - http.MethodPut, - fmt.Sprintf("/api/transactions/%s", tx.ID), - bytes.NewReader(reqBody)) - require.NoError(t, err) + path := fmt.Sprintf("/api/transactions/%s", tx.ID) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, path, bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) diff --git a/router/user_test.go b/router/user_test.go index 433b7e83..09f9cb1f 100644 --- a/router/user_test.go +++ b/router/user_test.go @@ -39,6 +39,7 @@ func TestHandlers_GetUsers(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) user1 := makeUser(t, random.Numeric(t, 2) == 1) @@ -46,8 +47,7 @@ func TestHandlers_GetUsers(t *testing.T) { users := []*model.User{user1, user2} e := echo.New() - req, err := http.NewRequest(http.MethodGet, "/api/users", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/users", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -73,12 +73,13 @@ func TestHandlers_GetUsers(t *testing.T) { t.Run("Success2", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) users := []*model.User{} e := echo.New() - req := httptest.NewRequest(http.MethodGet, "/api/users", nil) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/users", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -104,10 +105,11 @@ func TestHandlers_GetUsers(t *testing.T) { t.Run("FailedToGetUsers", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) e := echo.New() - req := httptest.NewRequest(http.MethodGet, "/", nil) + req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -133,10 +135,10 @@ func TestHandlers_UpdateUserInfo(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) user := makeUser(t, random.Numeric(t, 2) == 1) - updateUser := &model.User{ ID: user.ID, Name: user.Name, @@ -155,8 +157,8 @@ func TestHandlers_UpdateUserInfo(t *testing.T) { assert.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, "/api/users", bytes.NewReader(reqBody)) - assert.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPut, "/api/users", bytes.NewReader(reqBody)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -174,6 +176,7 @@ func TestHandlers_UpdateUserInfo(t *testing.T) { c.Request().Context(), user.ID, updateUser.Name, updateUser.DisplayName, updateUser.Admin). Return(updateUser, nil) + assert.NoError(t, h.Handlers.UpdateUserInfo(c)) assert.Equal(t, http.StatusOK, rec.Code) var got User @@ -189,10 +192,10 @@ func TestHandlers_UpdateUserInfo(t *testing.T) { t.Run("FailedToUpdateUser", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) user := makeUser(t, random.Numeric(t, 2) == 1) - updateUser := &model.User{ ID: user.ID, Name: user.Name, @@ -201,7 +204,6 @@ func TestHandlers_UpdateUserInfo(t *testing.T) { CreatedAt: user.CreatedAt, UpdatedAt: time.Now(), } - reqUser := PutUserRequest{ Name: updateUser.Name, DisplayName: updateUser.DisplayName, @@ -211,8 +213,8 @@ func TestHandlers_UpdateUserInfo(t *testing.T) { assert.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, "/api/users", bytes.NewReader(bodyReqUser)) - assert.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPut, "/api/users", bytes.NewReader(bodyReqUser)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -239,10 +241,10 @@ func TestHandlers_UpdateUserInfo(t *testing.T) { t.Run("FailedToGetUser", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) user := makeUser(t, random.Numeric(t, 2) == 1) - updateUser := &model.User{ ID: user.ID, Name: user.Name, @@ -251,7 +253,6 @@ func TestHandlers_UpdateUserInfo(t *testing.T) { CreatedAt: user.CreatedAt, UpdatedAt: time.Now(), } - reqUser := PutUserRequest{ Name: updateUser.Name, DisplayName: updateUser.DisplayName, @@ -261,8 +262,8 @@ func TestHandlers_UpdateUserInfo(t *testing.T) { assert.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, "/api/users", bytes.NewReader(bodyReqUser)) - assert.NoError(t, err) + req := httptest.NewRequestWithContext( + ctx, http.MethodPut, "/api/users", bytes.NewReader(bodyReqUser)) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -287,13 +288,14 @@ func TestHandlers_GetMe(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) + accessUser := makeUser(t, random.Numeric(t, 2) == 1) user := modelUserToUser(accessUser) e := echo.New() - req, err := http.NewRequest(http.MethodPut, "/api/users/me", nil) - assert.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, "/api/users/me", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -301,7 +303,7 @@ func TestHandlers_GetMe(t *testing.T) { hn := mw(echo.HandlerFunc(func(c echo.Context) error { return c.NoContent(http.StatusOK) })) - err = hn(c) + err := hn(c) require.NoError(t, err) h, err := NewTestHandlers(t, ctrl) @@ -328,11 +330,12 @@ func TestHandlers_GetMe(t *testing.T) { t.Run("FailedToGetUser", func(t *testing.T) { t.Parallel() + ctx := testutil.NewContext(t) ctrl := gomock.NewController(t) + e := echo.New() e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret")))) - req, err := http.NewRequest(http.MethodPut, "/api/users/me", nil) - require.NoError(t, err) + req := httptest.NewRequestWithContext(ctx, http.MethodPut, "/api/users/me", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -340,7 +343,7 @@ func TestHandlers_GetMe(t *testing.T) { hn := mw(echo.HandlerFunc(func(c echo.Context) error { return c.NoContent(http.StatusOK) })) - err = hn(c) + err := hn(c) require.NoError(t, err) h, err := NewTestHandlers(t, ctrl)