test(end2end): add subtests

Signed-off-by: Reza Mobaraki <rezam578@gmail.com>
This commit is contained in:
Reza Mobaraki 2024-10-02 01:12:58 +03:30
parent 92720e143f
commit 00de075ad3
No known key found for this signature in database
GPG Key ID: 922CBCF25B541A6F
3 changed files with 269 additions and 187 deletions

View File

@ -76,6 +76,8 @@ func (suite *BenefactorAddressTestSuit) SetupTest() {
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGet() { func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGet() {
token := LoginBenefactor(suite.benefactorPhone) token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/address/%d", suite.addressID) url := fmt.Sprintf("/address/%d", suite.addressID)
suite.T().Run("Success", func(t *testing.T) {
responseRecord := CreateRequest("GET", url, token, nil) responseRecord := CreateRequest("GET", url, token, nil)
suite.Require().Equal(http.StatusOK, responseRecord.Code) suite.Require().Equal(http.StatusOK, responseRecord.Code)
@ -87,17 +89,22 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGet() {
suite.Require().Equal(suite.benefactorID, response.Address.BenefactorID) suite.Require().Equal(suite.benefactorID, response.Address.BenefactorID)
suite.Require().Equal(suite.getExpected.Address.PostalCode, response.Address.PostalCode) suite.Require().Equal(suite.getExpected.Address.PostalCode, response.Address.PostalCode)
suite.Require().Equal(suite.getExpected.Address.Address, response.Address.Address) suite.Require().Equal(suite.getExpected.Address.Address, response.Address.Address)
// TODO: Fix
//suite.Require().Equal(suite.getExpected.Address.Lat, response.Address.Lat)
//suite.Require().Equal(suite.getExpected.Address.Lon, response.Address.Lon)
suite.Require().Equal(suite.getExpected.Address.Name, response.Address.Name) suite.Require().Equal(suite.getExpected.Address.Name, response.Address.Name)
suite.Require().Equal(suite.getExpected.Address.CityID, response.Address.CityID) suite.Require().Equal(suite.getExpected.Address.CityID, response.Address.CityID)
})
suite.T().Run("Failure_Unauthorized", func(t *testing.T) {
responseRecord := CreateRequest("GET", url, "", nil) // No token provided
suite.Require().Equal(http.StatusUnauthorized, responseRecord.Code)
})
} }
// TestBenefactorAddressGetAll tests the GET /address/ endpoint // TestBenefactorAddressGetAll tests the GET /address/ endpoint
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGetAll() { func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGetAll() {
token := LoginBenefactor(suite.benefactorPhone) token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/address/") url := fmt.Sprintf("/address/")
suite.T().Run("Success", func(t *testing.T) {
responseRecord := CreateRequest("GET", url, token, nil) responseRecord := CreateRequest("GET", url, token, nil)
suite.Require().Equal(http.StatusOK, responseRecord.Code) suite.Require().Equal(http.StatusOK, responseRecord.Code)
@ -106,12 +113,20 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGetAll() {
suite.Require().NoError(err, "could not decode response body") suite.Require().NoError(err, "could not decode response body")
suite.Require().Equal(suite.getAllExpected["count"], len(response.AllAddresses)) suite.Require().Equal(suite.getAllExpected["count"], len(response.AllAddresses))
})
suite.T().Run("Failure_Unauthorized", func(t *testing.T) {
responseRecord := CreateRequest("GET", url, "", nil) // No token provided
suite.Require().Equal(http.StatusUnauthorized, responseRecord.Code)
})
} }
// TestBenefactorAddressCreate tests the POST /address/ endpoint // TestBenefactorAddressCreate tests the POST /address/ endpoint
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressCreate() { func (suite *BenefactorAddressTestSuit) TestBenefactorAddressCreate() {
token := LoginBenefactor(suite.benefactorPhone) token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/address/") url := fmt.Sprintf("/address/")
suite.T().Run("Success", func(t *testing.T) {
responseRecord := CreateRequest("POST", url, token, suite.createData) responseRecord := CreateRequest("POST", url, token, suite.createData)
suite.Require().Equal(http.StatusCreated, responseRecord.Code) suite.Require().Equal(http.StatusCreated, responseRecord.Code)
@ -124,13 +139,20 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressCreate() {
suite.Require().Equal(suite.createData.PostalCode, response.Address.PostalCode) suite.Require().Equal(suite.createData.PostalCode, response.Address.PostalCode)
suite.Require().Equal(suite.createData.Name, response.Address.Name) suite.Require().Equal(suite.createData.Name, response.Address.Name)
suite.Require().Equal(suite.createData.CityID, response.Address.CityID) suite.Require().Equal(suite.createData.CityID, response.Address.CityID)
})
suite.T().Run("Failure_BadRequest", func(t *testing.T) {
responseRecord := CreateRequest("POST", url, token, nil) // No data provided
suite.Require().Equal(http.StatusBadRequest, responseRecord.Code)
})
} }
// TestBenefactorAddressUpdate tests the PUT /address/:id endpoint // TestBenefactorAddressUpdate tests the PATCH /address/:id endpoint
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressUpdate() { func (suite *BenefactorAddressTestSuit) TestBenefactorAddressUpdate() {
// TODO: check Method is patch, however, all fields are required
token := LoginBenefactor(suite.benefactorPhone) token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/address/%d", suite.addressID) url := fmt.Sprintf("/address/%d", suite.addressID)
suite.T().Run("Success", func(t *testing.T) {
responseRecord := CreateRequest("PATCH", url, token, suite.updateData) responseRecord := CreateRequest("PATCH", url, token, suite.updateData)
suite.Require().Equal(http.StatusNoContent, responseRecord.Code) suite.Require().Equal(http.StatusNoContent, responseRecord.Code)
@ -145,17 +167,21 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressUpdate() {
suite.Require().Equal(suite.updateData.Address, updatedAddress.Address.Address) suite.Require().Equal(suite.updateData.Address, updatedAddress.Address.Address)
suite.Require().Equal(suite.updateData.Name, updatedAddress.Address.Name) suite.Require().Equal(suite.updateData.Name, updatedAddress.Address.Name)
suite.Require().Equal(suite.updateData.CityID, updatedAddress.Address.CityID) suite.Require().Equal(suite.updateData.CityID, updatedAddress.Address.CityID)
// TODO Fixing floating-point comparison with tolerance })
//suite.Require().Equal(suite.updateData.Lat, updatedAddress.Address.Lat)
//suite.Require().Equal(suite.updateData.Lon, updatedAddress.Address.Lon) suite.T().Run("Failure_Unauthorized", func(t *testing.T) {
responseRecord := CreateRequest("PATCH", url, "", suite.updateData) // No token provided
suite.Require().Equal(http.StatusUnauthorized, responseRecord.Code)
})
} }
// TestBenefactorAddressDelete tests the DELETE /address/:id endpoint // TestBenefactorAddressDelete tests the DELETE /address/:id endpoint
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressDelete() { func (suite *BenefactorAddressTestSuit) TestBenefactorAddressDelete() {
token := LoginBenefactor(suite.benefactorPhone) token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/address/%d", suite.addressID) url := fmt.Sprintf("/address/%d", suite.addressID)
responseRecord := CreateRequest("DELETE", url, token, nil)
suite.T().Run("Success", func(t *testing.T) {
responseRecord := CreateRequest("DELETE", url, token, nil)
suite.Require().Equal(http.StatusNoContent, responseRecord.Code) suite.Require().Equal(http.StatusNoContent, responseRecord.Code)
_, err := services.BenefactorAddressSvc.Get(context.Background(), _, err := services.BenefactorAddressSvc.Get(context.Background(),
@ -169,4 +195,10 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressDelete() {
suite.Equal(http.StatusNotFound, code) suite.Equal(http.StatusNotFound, code)
suite.Equal(errmsg.ErrorMsgNotFound, message) suite.Equal(errmsg.ErrorMsgNotFound, message)
})
suite.T().Run("Failure_Unauthorized", func(t *testing.T) {
responseRecord := CreateRequest("DELETE", url, "", nil) // No token provided
suite.Require().Equal(http.StatusUnauthorized, responseRecord.Code)
})
} }

View File

@ -35,25 +35,6 @@ func (suite *BenefactorKindBoxTestSuite) SetupTest() {
suite.kindBboxGetAllExpected = map[string]interface{}{ suite.kindBboxGetAllExpected = map[string]interface{}{
"count": 1, "count": 1,
} }
/*
"ID" : 1,
"KindBoxReqID" : 1,
"BenefactorID" : 1,
"KindBoxType" : "on-table",
"Amount" : 0,
"SerialNumber" : "serial-1",
"Status" : "delivered",
"DeliverReferTimeID" : 1,
"DeliverReferDate" : "2024-08-26T18:19:26Z",
"DeliverAddressID" : 1,
"SenderAgentID" : 1,
"DeliveredAt" : "2024-09-02T18:19:26Z",
"ReturnReferTimeID" : 0,
"ReturnReferDate" : "0001-01-01T00:00:00Z",
"ReturnAddressID" : 0,
"ReceiverAgentID" : 0,
"ReturnedAt" : "0001-01-01T00:00:00Z"
*/
suite.kindBoxGetExpected = benefactorkindboxparam.KindBoxGetResponse{ suite.kindBoxGetExpected = benefactorkindboxparam.KindBoxGetResponse{
KindBox: entity.KindBox{ KindBox: entity.KindBox{
ID: suite.kindBoxID, ID: suite.kindBoxID,
@ -75,10 +56,11 @@ func (suite *BenefactorKindBoxTestSuite) SetupTest() {
ReturnedAt: time.Time{}, ReturnedAt: time.Time{},
}, },
} }
} }
// Test for GET /benefactor/kindboxes (Get All Kind Boxes)
func (suite *BenefactorKindBoxTestSuite) TestBenefactorKindBoxGetAll() { func (suite *BenefactorKindBoxTestSuite) TestBenefactorKindBoxGetAll() {
suite.Run("Success", func() {
token := LoginBenefactor(suite.benefactorPhone) token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/benefactor/kindboxes") url := fmt.Sprintf("/benefactor/kindboxes")
responseRecord := CreateRequest("GET", url, token, nil) responseRecord := CreateRequest("GET", url, token, nil)
@ -86,12 +68,20 @@ func (suite *BenefactorKindBoxTestSuite) TestBenefactorKindBoxGetAll() {
var response benefactorkindboxparam.KindBoxGetAllResponse var response benefactorkindboxparam.KindBoxGetAllResponse
err := json.NewDecoder(responseRecord.Body).Decode(&response) err := json.NewDecoder(responseRecord.Body).Decode(&response)
suite.Require().NoError(err) suite.Require().NoError(err, "could not decode response")
suite.Require().Equal(suite.kindBboxGetAllExpected["count"], len(response.AllKindBox)) suite.Require().Equal(suite.kindBboxGetAllExpected["count"], len(response.AllKindBox))
})
suite.Run("Failure_Unauthorized", func() {
url := fmt.Sprintf("/benefactor/kindboxes")
responseRecord := CreateRequest("GET", url, "invalid_token", nil)
suite.Require().Equal(http.StatusUnauthorized, responseRecord.Code)
})
} }
// Test for GET /benefactor/kindboxes/:id (Get Single Kind Box)
func (suite *BenefactorKindBoxTestSuite) TestBenefactorKindBoxGet() { func (suite *BenefactorKindBoxTestSuite) TestBenefactorKindBoxGet() {
suite.Run("Success", func() {
token := LoginBenefactor(suite.benefactorPhone) token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/benefactor/kindboxes/%d", suite.kindBoxID) url := fmt.Sprintf("/benefactor/kindboxes/%d", suite.kindBoxID)
responseRecord := CreateRequest("GET", url, token, nil) responseRecord := CreateRequest("GET", url, token, nil)
@ -99,21 +89,42 @@ func (suite *BenefactorKindBoxTestSuite) TestBenefactorKindBoxGet() {
var response benefactorkindboxparam.KindBoxGetResponse var response benefactorkindboxparam.KindBoxGetResponse
err := json.NewDecoder(responseRecord.Body).Decode(&response) err := json.NewDecoder(responseRecord.Body).Decode(&response)
suite.Require().NoError(err) suite.Require().NoError(err, "could not decode response body")
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.ID, response.KindBox.ID)
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.KindBoxReqID, response.KindBox.KindBoxReqID)
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.BenefactorID, response.KindBox.BenefactorID)
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.KindBoxType, response.KindBox.KindBoxType)
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.Amount, response.KindBox.Amount)
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.SerialNumber, response.KindBox.SerialNumber)
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.Status, response.KindBox.Status)
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.DeliverReferTimeID, response.KindBox.DeliverReferTimeID)
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.DeliverReferDate.Format("2006-01-02"), response.KindBox.DeliverReferDate.Format("2006-01-02"))
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.DeliverAddressID, response.KindBox.DeliverAddressID)
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.SenderAgentID, response.KindBox.SenderAgentID)
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.DeliveredAt.Format("2006-01-02"), response.KindBox.DeliveredAt.Format("2006-01-02"))
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.ReturnReferTimeID, response.KindBox.ReturnReferTimeID)
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.ReturnReferDate.Format("2006-01-02"), response.KindBox.ReturnReferDate.Format("2006-01-02"))
suite.Require().Equal(suite.kindBoxGetExpected.KindBox.ReturnAddressID, response.KindBox.ReturnAddressID)
suite.assertKindBoxFields(suite.kindBoxGetExpected.KindBox, response.KindBox)
})
suite.Run("Failure_NotFound", func() {
token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/benefactor/kindboxes/%d", 9999) // Non-existent ID
responseRecord := CreateRequest("GET", url, token, nil)
suite.Require().Equal(http.StatusNotFound, responseRecord.Code)
})
suite.Run("Failure_Unauthorized", func() {
url := fmt.Sprintf("/benefactor/kindboxes/%d", suite.kindBoxID)
responseRecord := CreateRequest("GET", url, "invalid_token", nil)
suite.Require().Equal(http.StatusUnauthorized, responseRecord.Code)
})
}
// Helper method to assert all fields of KindBox
func (suite *BenefactorKindBoxTestSuite) assertKindBoxFields(expected, actual entity.KindBox) {
suite.Require().Equal(expected.ID, actual.ID, "ID should match")
suite.Require().Equal(expected.KindBoxReqID, actual.KindBoxReqID, "KindBoxReqID should match")
suite.Require().Equal(expected.BenefactorID, actual.BenefactorID, "BenefactorID should match")
suite.Require().Equal(expected.KindBoxType, actual.KindBoxType, "KindBoxType should match")
suite.Require().Equal(expected.Amount, actual.Amount, "Amount should match")
suite.Require().Equal(expected.SerialNumber, actual.SerialNumber, "SerialNumber should match")
suite.Require().Equal(expected.Status, actual.Status, "Status should match")
suite.Require().Equal(expected.DeliverReferTimeID, actual.DeliverReferTimeID, "DeliverReferTimeID should match")
suite.Require().Equal(expected.DeliverReferDate.Format("2006-01-02"), actual.DeliverReferDate.Format("2006-01-02"), "DeliverReferDate should match")
suite.Require().Equal(expected.DeliverAddressID, actual.DeliverAddressID, "DeliverAddressID should match")
suite.Require().Equal(expected.SenderAgentID, actual.SenderAgentID, "SenderAgentID should match")
suite.Require().Equal(expected.DeliveredAt.Format("2006-01-02"), actual.DeliveredAt.Format("2006-01-02"), "DeliveredAt should match")
suite.Require().Equal(expected.ReturnReferTimeID, actual.ReturnReferTimeID, "ReturnReferTimeID should match")
suite.Require().Equal(expected.ReturnReferDate.Format("2006-01-02"), actual.ReturnReferDate.Format("2006-01-02"), "ReturnReferDate should match")
suite.Require().Equal(expected.ReturnAddressID, actual.ReturnAddressID, "ReturnAddressID should match")
suite.Require().Equal(expected.ReceiverAgentID, actual.ReceiverAgentID, "ReceiverAgentID should match")
suite.Require().Equal(expected.ReturnedAt.Format("2006-01-02"), actual.ReturnedAt.Format("2006-01-02"), "ReturnedAt should match")
} }

View File

@ -18,7 +18,6 @@ import (
"time" "time"
) )
// BenefactorKindBoxReqsTestSuite defines the suite for testing Benefactor Kind Box Requests
type BenefactorKindBoxReqsTestSuite struct { type BenefactorKindBoxReqsTestSuite struct {
suite.Suite suite.Suite
benefactorPhone string benefactorPhone string
@ -30,12 +29,10 @@ type BenefactorKindBoxReqsTestSuite struct {
updateData benefactorkindboxreqparam.KindBoxReqUpdateRequest updateData benefactorkindboxreqparam.KindBoxReqUpdateRequest
} }
// TestBenefactorKindBoxReqsTestSuite is the entry point for the test suite
func TestBenefactorKindBoxReqsTestSuite(t *testing.T) { func TestBenefactorKindBoxReqsTestSuite(t *testing.T) {
suite.Run(t, new(BenefactorKindBoxReqsTestSuite)) suite.Run(t, new(BenefactorKindBoxReqsTestSuite))
} }
// SetupTest runs before each test in the suite
func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() { func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig()) teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
suite.T().Cleanup(teardown) suite.T().Cleanup(teardown)
@ -73,8 +70,8 @@ func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() {
} }
} }
// TestBenefactorKindBoxReqs_GetAll_Success tests retrieving all kind box requests
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Success() { func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Success() {
suite.Run("Success", func() {
token := LoginBenefactor(suite.benefactorPhone) token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/benefactor/kindboxreqs/") url := fmt.Sprintf("/benefactor/kindboxreqs/")
responseRecord := CreateRequest(http.MethodGet, url, token, nil) responseRecord := CreateRequest(http.MethodGet, url, token, nil)
@ -85,10 +82,17 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Su
suite.Require().NoError(err, "failed to decode response body") suite.Require().NoError(err, "failed to decode response body")
assert.Equal(suite.T(), suite.getAllExpected["count"], len(response.AllKindBoxReq)) assert.Equal(suite.T(), suite.getAllExpected["count"], len(response.AllKindBoxReq))
})
suite.Run("Failure_Unauthorized", func() {
url := fmt.Sprintf("/benefactor/kindboxreqs/")
responseRecord := CreateRequest(http.MethodGet, url, "invalid_token", nil)
suite.Require().Equal(http.StatusUnauthorized, responseRecord.Code)
})
} }
// TestBenefactorKindBoxReqs_Get_Success tests retrieving a specific kind box request by ID
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Success() { func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Success() {
suite.Run("Success", func() {
token := LoginBenefactor(suite.benefactorPhone) token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID) url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
responseRecord := CreateRequest(http.MethodGet, url, token, nil) responseRecord := CreateRequest(http.MethodGet, url, token, nil)
@ -107,10 +111,24 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Succe
response.KindBoxReq.DeliverReferDate.Format("2006-01-02"), response.KindBoxReq.DeliverReferDate.Format("2006-01-02"),
) )
assert.Equal(suite.T(), suite.getExpected["deliver_refer_time"], response.KindBoxReq.DeliverReferTimeID) assert.Equal(suite.T(), suite.getExpected["deliver_refer_time"], response.KindBoxReq.DeliverReferTimeID)
})
suite.Run("Failure_NotFound", func() {
token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", 9999) // Non-existent ID
responseRecord := CreateRequest(http.MethodGet, url, token, nil)
suite.Require().Equal(http.StatusNotFound, responseRecord.Code)
})
suite.Run("Failure_Unauthorized", func() {
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
responseRecord := CreateRequest(http.MethodGet, url, "invalid_token", nil)
suite.Require().Equal(http.StatusUnauthorized, responseRecord.Code)
})
} }
// TestBenefactorKindBoxReqs_Create_Success tests creating a kind box request
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Success() { func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Success() {
suite.Run("Success", func() {
token := LoginBenefactor(suite.benefactorPhone) token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/benefactor/kindboxreqs/") url := fmt.Sprintf("/benefactor/kindboxreqs/")
rec := CreateRequest(http.MethodPost, url, token, suite.createData) rec := CreateRequest(http.MethodPost, url, token, suite.createData)
@ -124,10 +142,17 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Su
assert.Equal(suite.T(), suite.createData.DeliverAddressID, response.KindBoxReq.DeliverAddressID) 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.createData.DeliverReferDate, response.KindBoxReq.DeliverReferDate)
assert.Equal(suite.T(), suite.createData.CountRequested, response.KindBoxReq.CountRequested) assert.Equal(suite.T(), suite.createData.CountRequested, response.KindBoxReq.CountRequested)
})
suite.Run("Failure_Unauthorized", func() {
url := fmt.Sprintf("/benefactor/kindboxreqs/")
rec := CreateRequest(http.MethodPost, url, "invalid_token", suite.createData)
suite.Require().Equal(http.StatusUnauthorized, rec.Code)
})
} }
// TestBenefactorKindBoxReqs_Update_Success tests updating a kind box request
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Success() { func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Success() {
suite.Run("Success", func() {
token := LoginBenefactor(suite.benefactorPhone) token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID) url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
rec := CreateRequest(http.MethodPut, url, token, suite.updateData) rec := CreateRequest(http.MethodPut, url, token, suite.updateData)
@ -151,10 +176,17 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Su
kindBoxReq.DeliverReferDate.Format("2006-01-02"), kindBoxReq.DeliverReferDate.Format("2006-01-02"),
) )
assert.Equal(suite.T(), suite.updateData.DeliverAddressID, kindBoxReq.DeliverAddressID) assert.Equal(suite.T(), suite.updateData.DeliverAddressID, kindBoxReq.DeliverAddressID)
})
suite.Run("Failure_Unauthorized", func() {
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
rec := CreateRequest(http.MethodPut, url, "invalid_token", suite.updateData)
suite.Require().Equal(http.StatusUnauthorized, rec.Code)
})
} }
// TestBenefactorKindBoxReqs_Delete_Success tests deleting a kind box request
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Success() { func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Success() {
suite.Run("Success", func() {
token := LoginBenefactor(suite.benefactorPhone) token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID) url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
rec := CreateRequest(http.MethodDelete, url, token, nil) rec := CreateRequest(http.MethodDelete, url, token, nil)
@ -170,4 +202,11 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Su
suite.Require().Error(err) suite.Require().Error(err)
assert.Equal(suite.T(), http.StatusNotFound, code) assert.Equal(suite.T(), http.StatusNotFound, code)
assert.Equal(suite.T(), errmsg.ErrorMsgNotFound, message) assert.Equal(suite.T(), errmsg.ErrorMsgNotFound, message)
})
suite.Run("Failure_Unauthorized", func() {
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
rec := CreateRequest(http.MethodDelete, url, "invalid_token", nil)
suite.Require().Equal(http.StatusUnauthorized, rec.Code)
})
} }