From 15f37001db9bf7a764cd636e4011d5b72da14fbe Mon Sep 17 00:00:00 2001 From: Reza Mobaraki Date: Fri, 23 Aug 2024 17:58:44 +0330 Subject: [PATCH] Chore(benefactor-kindBoxReqs-test): make it cleaner Signed-off-by: Reza Mobaraki --- .../end2end/benefactor_kindboxreqs_test.go | 78 +++++++++++++------ 1 file changed, 56 insertions(+), 22 deletions(-) diff --git a/delivery/http_server/end2end/benefactor_kindboxreqs_test.go b/delivery/http_server/end2end/benefactor_kindboxreqs_test.go index 5be7d06..b053017 100644 --- a/delivery/http_server/end2end/benefactor_kindboxreqs_test.go +++ b/delivery/http_server/end2end/benefactor_kindboxreqs_test.go @@ -38,28 +38,36 @@ func loginBenefactor(t *testing.T) string { return registerRes.Tokens.AccessToken } +// Utility function to create and send HTTP requests +func createRequest(t *testing.T, method, url string, body interface{}) *httptest.ResponseRecorder { + var buf bytes.Buffer + if body != nil { + if err := json.NewEncoder(&buf).Encode(body); err != nil { + t.Fatalf("could not encode body: %s", err) + } + } + token := loginBenefactor(t) + req := httptest.NewRequest(method, url, &buf) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", token)) + rec := httptest.NewRecorder() + testServer.Serve(rec, req) + return rec +} + func TestBenefactorKindBoxReqs_GetAll_Success(t *testing.T) { teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig()) t.Cleanup(teardown) - token := fmt.Sprintf("Bearer %s", loginBenefactor(t)) - - req := httptest.NewRequest(http.MethodGet, "/benefactor/kindboxreqs/", nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - req.Header.Set(echo.HeaderAuthorization, token) - rec := httptest.NewRecorder() - testServer.Serve(rec, req) + rec := createRequest(t, http.MethodGet, "/benefactor/kindboxreqs/", nil) assert.Equal(t, http.StatusOK, rec.Code) - } func TestBenefactorKindBoxReqs_Create_Success(t *testing.T) { teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig()) t.Cleanup(teardown) - token := fmt.Sprintf("Bearer %s", loginBenefactor(t)) - newKindBox := benefactorkindboxreqparam.KindBoxReqAddRequest{ KindBoxType: entity.KindBoxOnTable, DeliverAddressID: 1, @@ -68,22 +76,12 @@ func TestBenefactorKindBoxReqs_Create_Success(t *testing.T) { CountRequested: 2, } - requestBody, err := json.Marshal(newKindBox) - if err != nil { - t.Fatalf("failed to marshal request body: %s", err) - } - - req := httptest.NewRequest(http.MethodPost, "/benefactor/kindboxreqs/", bytes.NewBuffer(requestBody)) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - req.Header.Set(echo.HeaderAuthorization, token) - - rec := httptest.NewRecorder() - testServer.Serve(rec, req) + rec := createRequest(t, http.MethodPost, "/benefactor/kindboxreqs/", newKindBox) assert.Equal(t, http.StatusCreated, rec.Code) var response benefactorkindboxreqparam.KindBoxReqAddResponse - err = json.NewDecoder(rec.Body).Decode(&response) + err := json.NewDecoder(rec.Body).Decode(&response) if err != nil { t.Fatalf("failed to decode response body: %s", err) } @@ -122,3 +120,39 @@ func TestBenefactorKindBoxReqs_Get_Success(t *testing.T) { assert.Equal(t, expectedDate.Format("2006-01-02"), response.KindBoxReq.DeliverReferDate.Format("2006-01-02")) assert.Equal(t, uint(1), response.KindBoxReq.DeliverReferTimeID) } + +func TestBenefactorKindBoxReqs_Update_Success(t *testing.T) { + teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig()) + t.Cleanup(teardown) + + token := fmt.Sprintf("Bearer %s", loginBenefactor(t)) + + var kindBoxReqID uint = 1 + updatedKindBox := benefactorkindboxreqparam.KindBoxReqUpdateRequest{ + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 2, + DeliverReferDate: time.Date(2025, time.January, 2, 15, 4, 5, 0, time.UTC), + DeliverReferTimeID: 1, + CountRequested: 2, + } + + requestBody, err := json.Marshal(updatedKindBox) + if err != nil { + t.Fatalf("failed to marshal request body: %s", err) + } + + req := httptest.NewRequest(http.MethodPut, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), bytes.NewBuffer(requestBody)) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + req.Header.Set(echo.HeaderAuthorization, token) + rec := httptest.NewRecorder() + testServer.Serve(rec, req) + + var response benefactorkindboxreqparam.KindBoxReqUpdateResponse + err = json.NewDecoder(rec.Body).Decode(&response) + if err != nil { + t.Fatalf("failed to decode response body: %s", err) + } + + assert.Equal(t, http.StatusNoContent, rec.Code) + +}