diff --git a/delivery/http_server/end2end/benefactor_kindboxreqs_test.go b/delivery/http_server/end2end/benefactor_kindboxreqs_test.go index b8375390..cab5876b 100644 --- a/delivery/http_server/end2end/benefactor_kindboxreqs_test.go +++ b/delivery/http_server/end2end/benefactor_kindboxreqs_test.go @@ -20,7 +20,7 @@ import ( "time" ) -// Define a suite struct that embeds suite.Suite +// BenefactorKindBoxReqsTestSuite defines the suite for testing Benefactor Kind Box Requests type BenefactorKindBoxReqsTestSuite struct { suite.Suite benefactorPhone string @@ -32,22 +32,26 @@ type BenefactorKindBoxReqsTestSuite struct { updateData benefactorkindboxreqparam.KindBoxReqUpdateRequest } -// SetupTest will run before each test in the suite +// SetupTest runs before each test in the suite func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() { teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig()) suite.T().Cleanup(teardown) + suite.benefactorPhone = "09384664404" suite.benefactorID = uint(1) suite.kindBoxReqID = uint(1) + suite.getAllExpected = map[string]interface{}{ "count": 5, } + suite.getExpected = map[string]interface{}{ "kind_box_type": entity.KindBoxOnTable, "deliver_address": uint(1), "deliver_refer_date": time.Now().AddDate(0, 0, 7).UTC(), "deliver_refer_time": uint(1), } + suite.createData = benefactorkindboxreqparam.KindBoxReqAddRequest{ KindBoxType: entity.KindBoxCylindrical, DeliverAddressID: uint(1), @@ -55,6 +59,7 @@ func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() { DeliverReferTimeID: uint(1), CountRequested: uint(5), } + suite.updateData = benefactorkindboxreqparam.KindBoxReqUpdateRequest{ KindBoxType: entity.KindBoxCylindrical, CountRequested: uint(10), @@ -65,71 +70,63 @@ func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() { } } -// TODO: find better place -// loginBenefactor utility function +// loginBenefactor authenticates the benefactor and returns an access token func (suite *BenefactorKindBoxReqsTestSuite) loginBenefactor() string { sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{ PhoneNumber: suite.benefactorPhone, }) - if err != nil { - suite.T().Fatalf("failed to send OTP: %s", err) - } + suite.Require().NoError(err, "failed to send OTP") + registerRes, err := services.BenefactorSvc.LoginOrRegister(context.Background(), benefactoreparam.LoginOrRegisterRequest{ PhoneNumber: suite.benefactorPhone, VerificationCode: sendOTPRes.Code, }) - if err != nil { - suite.T().Fatalf("failed to login or register: %s", err) - } + suite.Require().NoError(err, "failed to login or register") + return registerRes.Tokens.AccessToken } -// Utility function to create and send HTTP requests +// createRequest is a utility function to create and send HTTP requests func (suite *BenefactorKindBoxReqsTestSuite) createRequest(method, url string, body interface{}) *httptest.ResponseRecorder { var buf bytes.Buffer if body != nil { - if err := json.NewEncoder(&buf).Encode(body); err != nil { - suite.T().Fatalf("could not encode body: %s", err) - } + err := json.NewEncoder(&buf).Encode(body) + suite.Require().NoError(err, "could not encode body") } + token := suite.loginBenefactor() + 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 } -// Test to get all kind box requests +// TestBenefactorKindBoxReqs_GetAll_Success tests retrieving all kind box requests func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Success() { rec := suite.createRequest(http.MethodGet, "/benefactor/kindboxreqs/", nil) - - assert.Equal(suite.T(), http.StatusOK, rec.Code) + suite.Require().Equal(http.StatusOK, rec.Code) var response benefactorkindboxreqparam.GetAllResponse err := json.NewDecoder(rec.Body).Decode(&response) - if err != nil { - suite.T().Fatalf("failed to decode response body: %s", err) - } - assert.Equal(suite.T(), suite.getAllExpected["count"], len(response.AllKindBoxReq)) + suite.Require().NoError(err, "failed to decode response body") + assert.Equal(suite.T(), suite.getAllExpected["count"], len(response.AllKindBoxReq)) } -// Test to get a specific kind box request by ID +// TestBenefactorKindBoxReqs_Get_Success tests retrieving a specific kind box request by ID func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Success() { - - rec := suite.createRequest(http.MethodGet, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.benefactorID), nil) - - assert.Equal(suite.T(), http.StatusOK, rec.Code) + rec := suite.createRequest(http.MethodGet, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), nil) + suite.Require().Equal(http.StatusOK, rec.Code) var response benefactorkindboxreqparam.KindBoxReqGetResponse err := json.NewDecoder(rec.Body).Decode(&response) - if err != nil { - suite.T().Fatalf("failed to decode response body: %s", err) - } + suite.Require().NoError(err, "failed to decode response body") - assert.Equal(suite.T(), suite.benefactorID, response.KindBoxReq.ID) + assert.Equal(suite.T(), suite.kindBoxReqID, response.KindBoxReq.ID) assert.Equal(suite.T(), suite.getExpected["kind_box_type"], response.KindBoxReq.KindBoxType) assert.Equal(suite.T(), suite.getExpected["deliver_address"], response.KindBoxReq.DeliverAddressID) assert.Equal( @@ -140,35 +137,25 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Succe assert.Equal(suite.T(), suite.getExpected["deliver_refer_time"], response.KindBoxReq.DeliverReferTimeID) } -// Test to create a kind box request +// TestBenefactorKindBoxReqs_Create_Success tests creating a kind box request func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Success() { - rec := suite.createRequest(http.MethodPost, "/benefactor/kindboxreqs/", suite.createData) - assert.Equal(suite.T(), http.StatusCreated, rec.Code) + suite.Require().Equal(http.StatusCreated, rec.Code) var response benefactorkindboxreqparam.KindBoxReqAddResponse err := json.NewDecoder(rec.Body).Decode(&response) - if err != nil { - suite.T().Fatalf("failed to decode response body: %s", err) - } + suite.Require().NoError(err, "failed to decode response body") + assert.Equal(suite.T(), suite.createData.KindBoxType, response.KindBoxReq.KindBoxType) assert.Equal(suite.T(), suite.createData.DeliverAddressID, response.KindBoxReq.DeliverAddressID) assert.Equal(suite.T(), suite.createData.DeliverReferDate, response.KindBoxReq.DeliverReferDate) - assert.Equal( - suite.T(), - suite.getExpected["deliver_refer_date"].(time.Time).Format("2006-01-02"), - response.KindBoxReq.DeliverReferDate.Format("2006-01-02"), - ) assert.Equal(suite.T(), suite.createData.CountRequested, response.KindBoxReq.CountRequested) } -// Test to update a kind box request +// TestBenefactorKindBoxReqs_Update_Success tests updating a kind box request func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Success() { - - rec := suite.createRequest( - http.MethodPut, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), suite.updateData, - ) - assert.Equal(suite.T(), http.StatusNoContent, rec.Code) + rec := suite.createRequest(http.MethodPut, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), suite.updateData) + suite.Require().Equal(http.StatusNoContent, rec.Code) kindBoxReq, err := services.BenefactorKindBoxReqSvc.Get(context.Background(), benefactorkindboxreqparam.KindBoxReqGetRequest{ @@ -176,24 +163,24 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Su KindBoxReqID: suite.kindBoxReqID, }, ) - if err != nil { - suite.T().Fatalf("failed to get kind box request: %s", err) - } + suite.Require().NoError(err, "failed to get kind box request") assert.Equal(suite.T(), suite.updateData.KindBoxType, kindBoxReq.KindBoxType) assert.Equal(suite.T(), suite.updateData.CountRequested, kindBoxReq.CountRequested) assert.Equal(suite.T(), suite.updateData.Description, kindBoxReq.Description) assert.Equal(suite.T(), suite.updateData.DeliverReferTimeID, kindBoxReq.DeliverReferTimeID) - assert.Equal(suite.T(), suite.updateData.DeliverReferDate, kindBoxReq.DeliverReferDate) + assert.Equal( + suite.T(), + suite.updateData.DeliverReferDate.Format("2006-01-02"), + kindBoxReq.DeliverReferDate.Format("2006-01-02"), + ) assert.Equal(suite.T(), suite.updateData.DeliverAddressID, kindBoxReq.DeliverAddressID) - } +// TestBenefactorKindBoxReqs_Delete_Success tests deleting a kind box request func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Success() { - rec := suite.createRequest( - http.MethodDelete, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), nil, - ) - assert.Equal(suite.T(), http.StatusOK, rec.Code) + rec := suite.createRequest(http.MethodDelete, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), nil) + suite.Require().Equal(http.StatusOK, rec.Code) _, err := services.BenefactorKindBoxReqSvc.Get(context.Background(), benefactorkindboxreqparam.KindBoxReqGetRequest{ @@ -201,11 +188,10 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Su KindBoxReqID: suite.kindBoxReqID, }, ) - // TODO: Fix to assert equal to errmsg.ErrorMsgNotFound - assert.Error(suite.T(), err) + suite.Require().Error(err) } -// Entry point for the test suite +// TestBenefactorKindBoxReqsTestSuite is the entry point for the test suite func TestBenefactorKindBoxReqsTestSuite(t *testing.T) { suite.Run(t, new(BenefactorKindBoxReqsTestSuite)) }