niki/delivery/http_server/end2end/benefactor_kindboxes_test.go

131 lines
5.6 KiB
Go

//go:build end2end
package end2end
import (
"encoding/json"
"fmt"
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
"git.gocasts.ir/ebhomengo/niki/entity"
benefactorkindboxparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
"github.com/stretchr/testify/suite"
"net/http"
"testing"
"time"
)
type BenefactorKindBoxTestSuite struct {
suite.Suite
benefactorPhone string
benefactorID uint
kindBoxID uint
kindBboxGetAllExpected map[string]interface{}
kindBoxGetExpected benefactorkindboxparam.KindBoxGetResponse
}
func TestBenefactorKindBoxTestSuite(t *testing.T) {
suite.Run(t, new(BenefactorKindBoxTestSuite))
}
func (suite *BenefactorKindBoxTestSuite) SetupTest() {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
suite.T().Cleanup(teardown)
suite.benefactorPhone = "09384664404"
suite.kindBoxID = 1
suite.kindBboxGetAllExpected = map[string]interface{}{
"count": 1,
}
suite.kindBoxGetExpected = benefactorkindboxparam.KindBoxGetResponse{
KindBox: entity.KindBox{
ID: suite.kindBoxID,
KindBoxReqID: 1,
BenefactorID: 1,
KindBoxType: "on-table",
Amount: 0,
SerialNumber: "serial-1",
Status: "delivered",
DeliverReferTimeID: 1,
DeliverReferDate: time.Now().AddDate(0, 0, 7).UTC(),
DeliverAddressID: 1,
SenderAgentID: 1,
DeliveredAt: time.Now().AddDate(0, 0, 7).UTC(),
ReturnReferTimeID: 0,
ReturnReferDate: time.Time{},
ReturnAddressID: 0,
ReceiverAgentID: 0,
ReturnedAt: time.Time{},
},
}
}
// Test for GET /benefactor/kindboxes (Get All Kind Boxes)
func (suite *BenefactorKindBoxTestSuite) TestBenefactorKindBoxGetAll() {
suite.Run("Success", func() {
token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/benefactor/kindboxes")
responseRecord := CreateRequest("GET", url, token, nil)
suite.Require().Equal(http.StatusOK, responseRecord.Code)
var response benefactorkindboxparam.KindBoxGetAllResponse
err := json.NewDecoder(responseRecord.Body).Decode(&response)
suite.Require().NoError(err, "could not decode response")
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() {
suite.Run("Success", func() {
token := LoginBenefactor(suite.benefactorPhone)
url := fmt.Sprintf("/benefactor/kindboxes/%d", suite.kindBoxID)
responseRecord := CreateRequest("GET", url, token, nil)
suite.Require().Equal(http.StatusOK, responseRecord.Code)
var response benefactorkindboxparam.KindBoxGetResponse
err := json.NewDecoder(responseRecord.Body).Decode(&response)
suite.Require().NoError(err, "could not decode response body")
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.StatusUnprocessableEntity, 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")
}