2024-08-23 12:24:27 +00:00
|
|
|
//go:build end2end
|
|
|
|
|
|
|
|
package end2end
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
|
|
benefactoreparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
|
|
|
|
benefactorkindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-08-23 15:35:30 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2024-08-23 12:24:27 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-08-24 10:33:16 +00:00
|
|
|
// BenefactorKindBoxReqsTestSuite defines the suite for testing Benefactor Kind Box Requests
|
2024-08-23 15:35:30 +00:00
|
|
|
type BenefactorKindBoxReqsTestSuite struct {
|
|
|
|
suite.Suite
|
2024-08-24 10:16:44 +00:00
|
|
|
benefactorPhone string
|
|
|
|
benefactorID uint
|
|
|
|
kindBoxReqID uint
|
|
|
|
getAllExpected map[string]interface{}
|
|
|
|
getExpected map[string]interface{}
|
|
|
|
createData benefactorkindboxreqparam.KindBoxReqAddRequest
|
|
|
|
updateData benefactorkindboxreqparam.KindBoxReqUpdateRequest
|
2024-08-23 15:35:30 +00:00
|
|
|
}
|
|
|
|
|
2024-08-24 10:41:08 +00:00
|
|
|
// TestBenefactorKindBoxReqsTestSuite is the entry point for the test suite
|
|
|
|
func TestBenefactorKindBoxReqsTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(BenefactorKindBoxReqsTestSuite))
|
|
|
|
}
|
|
|
|
|
2024-08-24 10:33:16 +00:00
|
|
|
// SetupTest runs before each test in the suite
|
2024-08-23 15:35:30 +00:00
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() {
|
|
|
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
|
|
|
suite.T().Cleanup(teardown)
|
2024-08-24 10:33:16 +00:00
|
|
|
|
2024-08-24 10:16:44 +00:00
|
|
|
suite.benefactorPhone = "09384664404"
|
|
|
|
suite.benefactorID = uint(1)
|
|
|
|
suite.kindBoxReqID = uint(1)
|
2024-08-24 10:33:16 +00:00
|
|
|
|
2024-08-24 10:16:44 +00:00
|
|
|
suite.getAllExpected = map[string]interface{}{
|
|
|
|
"count": 5,
|
|
|
|
}
|
2024-08-24 10:33:16 +00:00
|
|
|
|
2024-08-24 10:16:44 +00:00
|
|
|
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),
|
|
|
|
}
|
2024-08-24 10:33:16 +00:00
|
|
|
|
2024-08-24 10:16:44 +00:00
|
|
|
suite.createData = benefactorkindboxreqparam.KindBoxReqAddRequest{
|
|
|
|
KindBoxType: entity.KindBoxCylindrical,
|
|
|
|
DeliverAddressID: uint(1),
|
|
|
|
DeliverReferDate: time.Now().AddDate(0, 0, 7).UTC(),
|
|
|
|
DeliverReferTimeID: uint(1),
|
|
|
|
CountRequested: uint(5),
|
|
|
|
}
|
2024-08-24 10:33:16 +00:00
|
|
|
|
2024-08-24 10:16:44 +00:00
|
|
|
suite.updateData = benefactorkindboxreqparam.KindBoxReqUpdateRequest{
|
|
|
|
KindBoxType: entity.KindBoxCylindrical,
|
|
|
|
CountRequested: uint(10),
|
|
|
|
Description: "updated description",
|
|
|
|
DeliverReferTimeID: uint(1),
|
|
|
|
DeliverReferDate: time.Now().AddDate(0, 0, 7).UTC(),
|
|
|
|
DeliverAddressID: uint(1),
|
|
|
|
}
|
2024-08-23 15:35:30 +00:00
|
|
|
}
|
|
|
|
|
2024-08-24 10:33:16 +00:00
|
|
|
// loginBenefactor authenticates the benefactor and returns an access token
|
2024-08-24 10:41:08 +00:00
|
|
|
// TODO: Move this to a common utility function
|
2024-08-23 15:35:30 +00:00
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) loginBenefactor() string {
|
2024-08-23 12:24:27 +00:00
|
|
|
sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{
|
2024-08-24 10:16:44 +00:00
|
|
|
PhoneNumber: suite.benefactorPhone,
|
2024-08-23 12:24:27 +00:00
|
|
|
})
|
2024-08-24 10:33:16 +00:00
|
|
|
suite.Require().NoError(err, "failed to send OTP")
|
|
|
|
|
2024-08-23 12:24:27 +00:00
|
|
|
registerRes, err := services.BenefactorSvc.LoginOrRegister(context.Background(), benefactoreparam.LoginOrRegisterRequest{
|
2024-08-24 10:16:44 +00:00
|
|
|
PhoneNumber: suite.benefactorPhone,
|
2024-08-23 12:24:27 +00:00
|
|
|
VerificationCode: sendOTPRes.Code,
|
|
|
|
})
|
2024-08-24 10:33:16 +00:00
|
|
|
suite.Require().NoError(err, "failed to login or register")
|
|
|
|
|
2024-08-23 12:24:27 +00:00
|
|
|
return registerRes.Tokens.AccessToken
|
|
|
|
}
|
|
|
|
|
2024-08-24 10:33:16 +00:00
|
|
|
// createRequest is a utility function to create and send HTTP requests
|
2024-08-24 10:41:08 +00:00
|
|
|
// TODO: Move this to a common utility function
|
2024-08-23 15:35:30 +00:00
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) createRequest(method, url string, body interface{}) *httptest.ResponseRecorder {
|
2024-08-23 14:28:44 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if body != nil {
|
2024-08-24 10:33:16 +00:00
|
|
|
err := json.NewEncoder(&buf).Encode(body)
|
|
|
|
suite.Require().NoError(err, "could not encode body")
|
2024-08-23 14:28:44 +00:00
|
|
|
}
|
2024-08-24 10:33:16 +00:00
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
token := suite.loginBenefactor()
|
2024-08-24 10:33:16 +00:00
|
|
|
|
2024-08-23 14:28:44 +00:00
|
|
|
req := httptest.NewRequest(method, url, &buf)
|
|
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
|
|
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", token))
|
2024-08-24 10:33:16 +00:00
|
|
|
|
2024-08-23 14:28:44 +00:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
testServer.Serve(rec, req)
|
|
|
|
return rec
|
|
|
|
}
|
|
|
|
|
2024-08-24 10:33:16 +00:00
|
|
|
// TestBenefactorKindBoxReqs_GetAll_Success tests retrieving all kind box requests
|
2024-08-23 15:35:30 +00:00
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Success() {
|
|
|
|
rec := suite.createRequest(http.MethodGet, "/benefactor/kindboxreqs/", nil)
|
2024-08-24 10:33:16 +00:00
|
|
|
suite.Require().Equal(http.StatusOK, rec.Code)
|
2024-08-24 10:16:44 +00:00
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
var response benefactorkindboxreqparam.GetAllResponse
|
2024-08-23 14:28:44 +00:00
|
|
|
err := json.NewDecoder(rec.Body).Decode(&response)
|
2024-08-24 10:33:16 +00:00
|
|
|
suite.Require().NoError(err, "failed to decode response body")
|
2024-08-23 12:24:27 +00:00
|
|
|
|
2024-08-24 10:33:16 +00:00
|
|
|
assert.Equal(suite.T(), suite.getAllExpected["count"], len(response.AllKindBoxReq))
|
2024-08-23 15:35:30 +00:00
|
|
|
}
|
2024-08-23 12:24:27 +00:00
|
|
|
|
2024-08-24 10:33:16 +00:00
|
|
|
// TestBenefactorKindBoxReqs_Get_Success tests retrieving a specific kind box request by ID
|
2024-08-23 15:35:30 +00:00
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Success() {
|
2024-08-24 10:33:16 +00:00
|
|
|
rec := suite.createRequest(http.MethodGet, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), nil)
|
|
|
|
suite.Require().Equal(http.StatusOK, rec.Code)
|
2024-08-23 12:24:27 +00:00
|
|
|
|
|
|
|
var response benefactorkindboxreqparam.KindBoxReqGetResponse
|
|
|
|
err := json.NewDecoder(rec.Body).Decode(&response)
|
2024-08-24 10:33:16 +00:00
|
|
|
suite.Require().NoError(err, "failed to decode response body")
|
2024-08-23 12:24:27 +00:00
|
|
|
|
2024-08-24 10:33:16 +00:00
|
|
|
assert.Equal(suite.T(), suite.kindBoxReqID, response.KindBoxReq.ID)
|
2024-08-24 10:16:44 +00:00
|
|
|
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_refer_date"].(time.Time).Format("2006-01-02"),
|
|
|
|
response.KindBoxReq.DeliverReferDate.Format("2006-01-02"),
|
|
|
|
)
|
|
|
|
assert.Equal(suite.T(), suite.getExpected["deliver_refer_time"], response.KindBoxReq.DeliverReferTimeID)
|
2024-08-23 12:24:27 +00:00
|
|
|
}
|
2024-08-23 14:28:44 +00:00
|
|
|
|
2024-08-24 10:33:16 +00:00
|
|
|
// TestBenefactorKindBoxReqs_Create_Success tests creating a kind box request
|
2024-08-23 15:35:30 +00:00
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Success() {
|
2024-08-24 10:16:44 +00:00
|
|
|
rec := suite.createRequest(http.MethodPost, "/benefactor/kindboxreqs/", suite.createData)
|
2024-08-24 10:33:16 +00:00
|
|
|
suite.Require().Equal(http.StatusCreated, rec.Code)
|
2024-08-23 15:35:30 +00:00
|
|
|
|
|
|
|
var response benefactorkindboxreqparam.KindBoxReqAddResponse
|
|
|
|
err := json.NewDecoder(rec.Body).Decode(&response)
|
2024-08-24 10:33:16 +00:00
|
|
|
suite.Require().NoError(err, "failed to decode response body")
|
|
|
|
|
2024-08-24 10:16:44 +00:00
|
|
|
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.createData.CountRequested, response.KindBoxReq.CountRequested)
|
2024-08-23 15:35:30 +00:00
|
|
|
}
|
2024-08-23 14:28:44 +00:00
|
|
|
|
2024-08-24 10:33:16 +00:00
|
|
|
// TestBenefactorKindBoxReqs_Update_Success tests updating a kind box request
|
2024-08-23 15:35:30 +00:00
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Success() {
|
2024-08-24 10:33:16 +00:00
|
|
|
rec := suite.createRequest(http.MethodPut, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), suite.updateData)
|
|
|
|
suite.Require().Equal(http.StatusNoContent, rec.Code)
|
2024-08-23 15:35:30 +00:00
|
|
|
|
2024-08-24 10:16:44 +00:00
|
|
|
kindBoxReq, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
|
|
|
benefactorkindboxreqparam.KindBoxReqGetRequest{
|
|
|
|
BenefactorID: suite.benefactorID,
|
|
|
|
KindBoxReqID: suite.kindBoxReqID,
|
|
|
|
},
|
|
|
|
)
|
2024-08-24 10:33:16 +00:00
|
|
|
suite.Require().NoError(err, "failed to get kind box request")
|
2024-08-23 14:28:44 +00:00
|
|
|
|
2024-08-24 10:16:44 +00:00
|
|
|
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)
|
2024-08-24 10:33:16 +00:00
|
|
|
assert.Equal(
|
|
|
|
suite.T(),
|
|
|
|
suite.updateData.DeliverReferDate.Format("2006-01-02"),
|
|
|
|
kindBoxReq.DeliverReferDate.Format("2006-01-02"),
|
|
|
|
)
|
2024-08-24 10:16:44 +00:00
|
|
|
assert.Equal(suite.T(), suite.updateData.DeliverAddressID, kindBoxReq.DeliverAddressID)
|
2024-08-23 15:35:30 +00:00
|
|
|
}
|
|
|
|
|
2024-08-24 10:33:16 +00:00
|
|
|
// TestBenefactorKindBoxReqs_Delete_Success tests deleting a kind box request
|
2024-08-23 15:35:30 +00:00
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Success() {
|
2024-08-24 10:33:16 +00:00
|
|
|
rec := suite.createRequest(http.MethodDelete, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), nil)
|
|
|
|
suite.Require().Equal(http.StatusOK, rec.Code)
|
2024-08-23 15:35:30 +00:00
|
|
|
|
2024-08-24 10:16:44 +00:00
|
|
|
_, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
|
|
|
benefactorkindboxreqparam.KindBoxReqGetRequest{
|
|
|
|
BenefactorID: suite.benefactorID,
|
|
|
|
KindBoxReqID: suite.kindBoxReqID,
|
|
|
|
},
|
|
|
|
)
|
2024-08-24 10:33:16 +00:00
|
|
|
suite.Require().Error(err)
|
2024-08-23 15:35:30 +00:00
|
|
|
}
|