forked from ebhomengo/niki
test(end2end): add subtests
Signed-off-by: Reza Mobaraki <rezam578@gmail.com>
This commit is contained in:
parent
92720e143f
commit
00de075ad3
|
|
@ -76,97 +76,129 @@ 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)
|
||||||
responseRecord := CreateRequest("GET", url, token, nil)
|
|
||||||
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
|
||||||
|
|
||||||
var response addressparam.GetAddressResponse
|
suite.T().Run("Success", func(t *testing.T) {
|
||||||
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
responseRecord := CreateRequest("GET", url, token, nil)
|
||||||
suite.Require().NoError(err, "could not decode response body")
|
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
||||||
|
|
||||||
suite.Require().Equal(suite.addressID, response.Address.ID)
|
var response addressparam.GetAddressResponse
|
||||||
suite.Require().Equal(suite.benefactorID, response.Address.BenefactorID)
|
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
||||||
suite.Require().Equal(suite.getExpected.Address.PostalCode, response.Address.PostalCode)
|
suite.Require().NoError(err, "could not decode response body")
|
||||||
suite.Require().Equal(suite.getExpected.Address.Address, response.Address.Address)
|
|
||||||
// TODO: Fix
|
suite.Require().Equal(suite.addressID, response.Address.ID)
|
||||||
//suite.Require().Equal(suite.getExpected.Address.Lat, response.Address.Lat)
|
suite.Require().Equal(suite.benefactorID, response.Address.BenefactorID)
|
||||||
//suite.Require().Equal(suite.getExpected.Address.Lon, response.Address.Lon)
|
suite.Require().Equal(suite.getExpected.Address.PostalCode, response.Address.PostalCode)
|
||||||
suite.Require().Equal(suite.getExpected.Address.Name, response.Address.Name)
|
suite.Require().Equal(suite.getExpected.Address.Address, response.Address.Address)
|
||||||
suite.Require().Equal(suite.getExpected.Address.CityID, response.Address.CityID)
|
suite.Require().Equal(suite.getExpected.Address.Name, response.Address.Name)
|
||||||
|
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/")
|
||||||
responseRecord := CreateRequest("GET", url, token, nil)
|
|
||||||
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
|
||||||
|
|
||||||
var response addressparam.GetAllAddressesResponse
|
suite.T().Run("Success", func(t *testing.T) {
|
||||||
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
responseRecord := CreateRequest("GET", url, token, nil)
|
||||||
suite.Require().NoError(err, "could not decode response body")
|
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
||||||
|
|
||||||
suite.Require().Equal(suite.getAllExpected["count"], len(response.AllAddresses))
|
var response addressparam.GetAllAddressesResponse
|
||||||
|
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
||||||
|
suite.Require().NoError(err, "could not decode response body")
|
||||||
|
|
||||||
|
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/")
|
||||||
responseRecord := CreateRequest("POST", url, token, suite.createData)
|
|
||||||
suite.Require().Equal(http.StatusCreated, responseRecord.Code)
|
|
||||||
|
|
||||||
var response addressparam.BenefactorAddAddressResponse
|
suite.T().Run("Success", func(t *testing.T) {
|
||||||
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
responseRecord := CreateRequest("POST", url, token, suite.createData)
|
||||||
suite.Require().NoError(err, "could not decode response body")
|
suite.Require().Equal(http.StatusCreated, responseRecord.Code)
|
||||||
|
|
||||||
suite.Require().Equal(suite.benefactorID, response.Address.BenefactorID)
|
var response addressparam.BenefactorAddAddressResponse
|
||||||
suite.Require().Equal(suite.createData.Address, response.Address.Address)
|
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
||||||
suite.Require().Equal(suite.createData.PostalCode, response.Address.PostalCode)
|
suite.Require().NoError(err, "could not decode response body")
|
||||||
suite.Require().Equal(suite.createData.Name, response.Address.Name)
|
|
||||||
suite.Require().Equal(suite.createData.CityID, response.Address.CityID)
|
suite.Require().Equal(suite.benefactorID, response.Address.BenefactorID)
|
||||||
|
suite.Require().Equal(suite.createData.Address, response.Address.Address)
|
||||||
|
suite.Require().Equal(suite.createData.PostalCode, response.Address.PostalCode)
|
||||||
|
suite.Require().Equal(suite.createData.Name, response.Address.Name)
|
||||||
|
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)
|
||||||
responseRecord := CreateRequest("PATCH", url, token, suite.updateData)
|
|
||||||
suite.Require().Equal(http.StatusNoContent, responseRecord.Code)
|
|
||||||
|
|
||||||
updatedAddress, sErr := services.BenefactorAddressSvc.Get(context.Background(),
|
suite.T().Run("Success", func(t *testing.T) {
|
||||||
addressparam.GetAddressRequest{
|
responseRecord := CreateRequest("PATCH", url, token, suite.updateData)
|
||||||
AddressID: suite.addressID,
|
suite.Require().Equal(http.StatusNoContent, responseRecord.Code)
|
||||||
BenefactorID: suite.benefactorID,
|
|
||||||
})
|
|
||||||
suite.Require().NoError(sErr, "failed to get benefactor address")
|
|
||||||
|
|
||||||
suite.Require().Equal(suite.updateData.PostalCode, updatedAddress.Address.PostalCode)
|
updatedAddress, sErr := services.BenefactorAddressSvc.Get(context.Background(),
|
||||||
suite.Require().Equal(suite.updateData.Address, updatedAddress.Address.Address)
|
addressparam.GetAddressRequest{
|
||||||
suite.Require().Equal(suite.updateData.Name, updatedAddress.Address.Name)
|
AddressID: suite.addressID,
|
||||||
suite.Require().Equal(suite.updateData.CityID, updatedAddress.Address.CityID)
|
BenefactorID: suite.benefactorID,
|
||||||
// TODO Fixing floating-point comparison with tolerance
|
})
|
||||||
//suite.Require().Equal(suite.updateData.Lat, updatedAddress.Address.Lat)
|
suite.Require().NoError(sErr, "failed to get benefactor address")
|
||||||
//suite.Require().Equal(suite.updateData.Lon, updatedAddress.Address.Lon)
|
|
||||||
|
suite.Require().Equal(suite.updateData.PostalCode, updatedAddress.Address.PostalCode)
|
||||||
|
suite.Require().Equal(suite.updateData.Address, updatedAddress.Address.Address)
|
||||||
|
suite.Require().Equal(suite.updateData.Name, updatedAddress.Address.Name)
|
||||||
|
suite.Require().Equal(suite.updateData.CityID, updatedAddress.Address.CityID)
|
||||||
|
})
|
||||||
|
|
||||||
|
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.Require().Equal(http.StatusNoContent, responseRecord.Code)
|
suite.T().Run("Success", func(t *testing.T) {
|
||||||
|
responseRecord := CreateRequest("DELETE", url, token, nil)
|
||||||
|
suite.Require().Equal(http.StatusNoContent, responseRecord.Code)
|
||||||
|
|
||||||
_, err := services.BenefactorAddressSvc.Get(context.Background(),
|
_, err := services.BenefactorAddressSvc.Get(context.Background(),
|
||||||
addressparam.GetAddressRequest{
|
addressparam.GetAddressRequest{
|
||||||
AddressID: suite.addressID,
|
AddressID: suite.addressID,
|
||||||
BenefactorID: suite.benefactorID,
|
BenefactorID: suite.benefactorID,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
message, code := httpmsg.Error(err)
|
message, code := httpmsg.Error(err)
|
||||||
suite.Require().Error(err)
|
suite.Require().Error(err)
|
||||||
|
|
||||||
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)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,45 +56,75 @@ 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() {
|
||||||
token := LoginBenefactor(suite.benefactorPhone)
|
suite.Run("Success", func() {
|
||||||
url := fmt.Sprintf("/benefactor/kindboxes")
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
responseRecord := CreateRequest("GET", url, token, nil)
|
url := fmt.Sprintf("/benefactor/kindboxes")
|
||||||
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
responseRecord := CreateRequest("GET", url, token, nil)
|
||||||
|
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
||||||
|
|
||||||
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() {
|
||||||
token := LoginBenefactor(suite.benefactorPhone)
|
suite.Run("Success", func() {
|
||||||
url := fmt.Sprintf("/benefactor/kindboxes/%d", suite.kindBoxID)
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
responseRecord := CreateRequest("GET", url, token, nil)
|
url := fmt.Sprintf("/benefactor/kindboxes/%d", suite.kindBoxID)
|
||||||
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
responseRecord := CreateRequest("GET", url, token, nil)
|
||||||
|
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
||||||
|
|
||||||
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")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,101 +70,143 @@ 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() {
|
||||||
token := LoginBenefactor(suite.benefactorPhone)
|
suite.Run("Success", func() {
|
||||||
url := fmt.Sprintf("/benefactor/kindboxreqs/")
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
responseRecord := CreateRequest(http.MethodGet, url, token, nil)
|
url := fmt.Sprintf("/benefactor/kindboxreqs/")
|
||||||
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
responseRecord := CreateRequest(http.MethodGet, url, token, nil)
|
||||||
|
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
||||||
|
|
||||||
var response benefactorkindboxreqparam.GetAllResponse
|
var response benefactorkindboxreqparam.GetAllResponse
|
||||||
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
||||||
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() {
|
||||||
token := LoginBenefactor(suite.benefactorPhone)
|
suite.Run("Success", func() {
|
||||||
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
responseRecord := CreateRequest(http.MethodGet, url, token, nil)
|
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
|
||||||
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
responseRecord := CreateRequest(http.MethodGet, url, token, nil)
|
||||||
|
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
||||||
|
|
||||||
var response benefactorkindboxreqparam.KindBoxReqGetResponse
|
var response benefactorkindboxreqparam.KindBoxReqGetResponse
|
||||||
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
||||||
suite.Require().NoError(err, "failed to decode response body")
|
suite.Require().NoError(err, "failed to decode response body")
|
||||||
|
|
||||||
assert.Equal(suite.T(), suite.kindBoxReqID, 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["kind_box_type"], response.KindBoxReq.KindBoxType)
|
||||||
assert.Equal(suite.T(), suite.getExpected["deliver_address"], response.KindBoxReq.DeliverAddressID)
|
assert.Equal(suite.T(), suite.getExpected["deliver_address"], response.KindBoxReq.DeliverAddressID)
|
||||||
assert.Equal(
|
assert.Equal(
|
||||||
suite.T(),
|
suite.T(),
|
||||||
suite.getExpected["deliver_refer_date"].(time.Time).Format("2006-01-02"),
|
suite.getExpected["deliver_refer_date"].(time.Time).Format("2006-01-02"),
|
||||||
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() {
|
||||||
token := LoginBenefactor(suite.benefactorPhone)
|
suite.Run("Success", func() {
|
||||||
url := fmt.Sprintf("/benefactor/kindboxreqs/")
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
rec := CreateRequest(http.MethodPost, url, token, suite.createData)
|
url := fmt.Sprintf("/benefactor/kindboxreqs/")
|
||||||
suite.Require().Equal(http.StatusCreated, rec.Code)
|
rec := CreateRequest(http.MethodPost, url, token, suite.createData)
|
||||||
|
suite.Require().Equal(http.StatusCreated, rec.Code)
|
||||||
|
|
||||||
var response benefactorkindboxreqparam.KindBoxReqAddResponse
|
var response benefactorkindboxreqparam.KindBoxReqAddResponse
|
||||||
err := json.NewDecoder(rec.Body).Decode(&response)
|
err := json.NewDecoder(rec.Body).Decode(&response)
|
||||||
suite.Require().NoError(err, "failed to decode response body")
|
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.KindBoxType, response.KindBoxReq.KindBoxType)
|
||||||
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() {
|
||||||
token := LoginBenefactor(suite.benefactorPhone)
|
suite.Run("Success", func() {
|
||||||
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
rec := CreateRequest(http.MethodPut, url, token, suite.updateData)
|
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
|
||||||
suite.Require().Equal(http.StatusNoContent, rec.Code)
|
rec := CreateRequest(http.MethodPut, url, token, suite.updateData)
|
||||||
|
suite.Require().Equal(http.StatusNoContent, rec.Code)
|
||||||
|
|
||||||
kindBoxReq, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
kindBoxReq, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
||||||
benefactorkindboxreqparam.KindBoxReqGetRequest{
|
benefactorkindboxreqparam.KindBoxReqGetRequest{
|
||||||
BenefactorID: suite.benefactorID,
|
BenefactorID: suite.benefactorID,
|
||||||
KindBoxReqID: suite.kindBoxReqID,
|
KindBoxReqID: suite.kindBoxReqID,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
suite.Require().NoError(err, "failed to get kind box request")
|
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.KindBoxType, kindBoxReq.KindBoxType)
|
||||||
assert.Equal(suite.T(), suite.updateData.CountRequested, kindBoxReq.CountRequested)
|
assert.Equal(suite.T(), suite.updateData.CountRequested, kindBoxReq.CountRequested)
|
||||||
assert.Equal(suite.T(), suite.updateData.Description, kindBoxReq.Description)
|
assert.Equal(suite.T(), suite.updateData.Description, kindBoxReq.Description)
|
||||||
assert.Equal(suite.T(), suite.updateData.DeliverReferTimeID, kindBoxReq.DeliverReferTimeID)
|
assert.Equal(suite.T(), suite.updateData.DeliverReferTimeID, kindBoxReq.DeliverReferTimeID)
|
||||||
assert.Equal(
|
assert.Equal(
|
||||||
suite.T(),
|
suite.T(),
|
||||||
suite.updateData.DeliverReferDate.Format("2006-01-02"),
|
suite.updateData.DeliverReferDate.Format("2006-01-02"),
|
||||||
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() {
|
||||||
token := LoginBenefactor(suite.benefactorPhone)
|
suite.Run("Success", func() {
|
||||||
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
rec := CreateRequest(http.MethodDelete, url, token, nil)
|
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
|
||||||
suite.Require().Equal(http.StatusOK, rec.Code)
|
rec := CreateRequest(http.MethodDelete, url, token, nil)
|
||||||
|
suite.Require().Equal(http.StatusOK, rec.Code)
|
||||||
|
|
||||||
_, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
_, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
||||||
benefactorkindboxreqparam.KindBoxReqGetRequest{
|
benefactorkindboxreqparam.KindBoxReqGetRequest{
|
||||||
BenefactorID: suite.benefactorID,
|
BenefactorID: suite.benefactorID,
|
||||||
KindBoxReqID: suite.kindBoxReqID,
|
KindBoxReqID: suite.kindBoxReqID,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
message, code := httpmsg.Error(err)
|
message, code := httpmsg.Error(err)
|
||||||
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)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue