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-23 15:35:30 +00:00
|
|
|
// Define a suite struct that embeds suite.Suite
|
|
|
|
type BenefactorKindBoxReqsTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupTest will run before each test in the suite
|
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() {
|
|
|
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
|
|
|
suite.T().Cleanup(teardown)
|
|
|
|
}
|
|
|
|
|
|
|
|
// loginBenefactor utility function
|
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) loginBenefactor() string {
|
2024-08-23 12:24:27 +00:00
|
|
|
phone := "09384664404"
|
|
|
|
sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{
|
|
|
|
PhoneNumber: phone,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-23 15:35:30 +00:00
|
|
|
suite.T().Fatalf("failed to send OTP: %s", err)
|
2024-08-23 12:24:27 +00:00
|
|
|
}
|
|
|
|
registerRes, err := services.BenefactorSvc.LoginOrRegister(context.Background(), benefactoreparam.LoginOrRegisterRequest{
|
|
|
|
PhoneNumber: phone,
|
|
|
|
VerificationCode: sendOTPRes.Code,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-23 15:35:30 +00:00
|
|
|
suite.T().Fatalf("failed to login or register: %s", err)
|
2024-08-23 12:24:27 +00:00
|
|
|
}
|
|
|
|
return registerRes.Tokens.AccessToken
|
|
|
|
}
|
|
|
|
|
2024-08-23 14:28:44 +00:00
|
|
|
// Utility function to create and send HTTP requests
|
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 {
|
|
|
|
if err := json.NewEncoder(&buf).Encode(body); err != nil {
|
2024-08-23 15:35:30 +00:00
|
|
|
suite.T().Fatalf("could not encode body: %s", err)
|
2024-08-23 14:28:44 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-23 15:35:30 +00:00
|
|
|
token := suite.loginBenefactor()
|
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))
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
testServer.Serve(rec, req)
|
|
|
|
return rec
|
|
|
|
}
|
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
// Test to get 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)
|
|
|
|
var response benefactorkindboxreqparam.GetAllResponse
|
2024-08-23 14:28:44 +00:00
|
|
|
err := json.NewDecoder(rec.Body).Decode(&response)
|
2024-08-23 12:24:27 +00:00
|
|
|
if err != nil {
|
2024-08-23 15:35:30 +00:00
|
|
|
suite.T().Fatalf("failed to decode response body: %s", err)
|
2024-08-23 12:24:27 +00:00
|
|
|
}
|
2024-08-23 15:35:30 +00:00
|
|
|
assert.Equal(suite.T(), 5, len(response.AllKindBoxReq))
|
2024-08-23 12:24:27 +00:00
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
}
|
2024-08-23 12:24:27 +00:00
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
// Test to get a specific kind box request by ID
|
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Success() {
|
2024-08-23 12:24:27 +00:00
|
|
|
|
|
|
|
var kindBoxReqID uint = 1
|
2024-08-23 15:35:30 +00:00
|
|
|
rec := suite.createRequest(http.MethodGet, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), nil)
|
2024-08-23 12:24:27 +00:00
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
assert.Equal(suite.T(), http.StatusOK, rec.Code)
|
2024-08-23 12:24:27 +00:00
|
|
|
|
|
|
|
var response benefactorkindboxreqparam.KindBoxReqGetResponse
|
|
|
|
err := json.NewDecoder(rec.Body).Decode(&response)
|
|
|
|
if err != nil {
|
2024-08-23 15:35:30 +00:00
|
|
|
suite.T().Fatalf("failed to decode response body: %s", err)
|
2024-08-23 12:24:27 +00:00
|
|
|
}
|
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
assert.Equal(suite.T(), kindBoxReqID, response.KindBoxReq.ID)
|
|
|
|
assert.Equal(suite.T(), entity.KindBoxOnTable, response.KindBoxReq.KindBoxType)
|
|
|
|
assert.Equal(suite.T(), uint(1), response.KindBoxReq.DeliverAddressID)
|
2024-08-23 12:24:27 +00:00
|
|
|
expectedDate := time.Now().AddDate(0, 0, 7)
|
2024-08-23 15:35:30 +00:00
|
|
|
assert.Equal(suite.T(), expectedDate.Format("2006-01-02"), response.KindBoxReq.DeliverReferDate.Format("2006-01-02"))
|
|
|
|
assert.Equal(suite.T(), uint(1), response.KindBoxReq.DeliverReferTimeID)
|
2024-08-23 12:24:27 +00:00
|
|
|
}
|
2024-08-23 14:28:44 +00:00
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
// Test to create a kind box request
|
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Success() {
|
|
|
|
// TODO : fix constraint with address_id
|
|
|
|
// panic: can't rollback migrations: Error 1451 (23000):
|
|
|
|
// Cannot delete or update a parent row: a foreign key constraint fails
|
|
|
|
// (mysql.kind_box_reqs, CONSTRAINT kind_box_reqs_ibfk_3 FOREIGN KEY (deliver_address_id) REFERENCES addresses (id))
|
|
|
|
// handling 6_create-address-for-benefactor1.sql
|
2024-08-23 14:28:44 +00:00
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
newKindBox := benefactorkindboxreqparam.KindBoxReqAddRequest{
|
2024-08-23 14:28:44 +00:00
|
|
|
KindBoxType: entity.KindBoxOnTable,
|
2024-08-23 15:35:30 +00:00
|
|
|
DeliverAddressID: 1,
|
2024-08-23 14:28:44 +00:00
|
|
|
DeliverReferDate: time.Date(2025, time.January, 2, 15, 4, 5, 0, time.UTC),
|
|
|
|
DeliverReferTimeID: 1,
|
|
|
|
CountRequested: 2,
|
|
|
|
}
|
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
rec := suite.createRequest(http.MethodPost, "/benefactor/kindboxreqs/", newKindBox)
|
|
|
|
assert.Equal(suite.T(), http.StatusCreated, rec.Code)
|
|
|
|
|
|
|
|
var response benefactorkindboxreqparam.KindBoxReqAddResponse
|
|
|
|
err := json.NewDecoder(rec.Body).Decode(&response)
|
2024-08-23 14:28:44 +00:00
|
|
|
if err != nil {
|
2024-08-23 15:35:30 +00:00
|
|
|
suite.T().Fatalf("failed to decode response body: %s", err)
|
2024-08-23 14:28:44 +00:00
|
|
|
}
|
2024-08-23 15:35:30 +00:00
|
|
|
assert.Equal(suite.T(), newKindBox.KindBoxType, response.KindBoxReq.KindBoxType)
|
|
|
|
assert.Equal(suite.T(), newKindBox.DeliverAddressID, response.KindBoxReq.DeliverAddressID)
|
|
|
|
assert.Equal(suite.T(), newKindBox.DeliverReferDate, response.KindBoxReq.DeliverReferDate)
|
|
|
|
assert.Equal(suite.T(), newKindBox.DeliverReferTimeID, response.KindBoxReq.DeliverReferTimeID)
|
|
|
|
assert.Equal(suite.T(), newKindBox.CountRequested, response.KindBoxReq.CountRequested)
|
|
|
|
}
|
2024-08-23 14:28:44 +00:00
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
// Test to update a kind box request
|
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Success() {
|
2024-08-23 14:28:44 +00:00
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
var kindBoxReqID uint = 1
|
|
|
|
now := time.Now().AddDate(0, 0, 7).UTC()
|
|
|
|
updatedKindBox := benefactorkindboxreqparam.KindBoxReqUpdateRequest{
|
|
|
|
KindBoxType: entity.KindBoxCylindrical,
|
|
|
|
CountRequested: 5,
|
|
|
|
Description: "updated description",
|
|
|
|
DeliverReferTimeID: 1,
|
|
|
|
DeliverReferDate: now,
|
|
|
|
DeliverAddressID: 1,
|
|
|
|
}
|
|
|
|
rec := suite.createRequest(http.MethodPut, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), updatedKindBox)
|
|
|
|
assert.Equal(suite.T(), http.StatusNoContent, rec.Code)
|
|
|
|
|
|
|
|
kindBoxReq, err := services.BenefactorKindBoxReqSvc.Get(context.Background(), benefactorkindboxreqparam.KindBoxReqGetRequest{
|
|
|
|
BenefactorID: 1,
|
|
|
|
KindBoxReqID: kindBoxReqID,
|
|
|
|
})
|
2024-08-23 14:28:44 +00:00
|
|
|
if err != nil {
|
2024-08-23 15:35:30 +00:00
|
|
|
suite.T().Fatalf("failed to get kind box request: %s", err)
|
2024-08-23 14:28:44 +00:00
|
|
|
}
|
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
assert.Equal(suite.T(), updatedKindBox.KindBoxType, kindBoxReq.KindBoxType)
|
|
|
|
assert.Equal(suite.T(), updatedKindBox.CountRequested, kindBoxReq.CountRequested)
|
|
|
|
assert.Equal(suite.T(), updatedKindBox.Description, kindBoxReq.Description)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Success() {
|
|
|
|
var kindBoxReqID uint = 1
|
|
|
|
rec := suite.createRequest(http.MethodDelete, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), nil)
|
|
|
|
assert.Equal(suite.T(), http.StatusOK, rec.Code)
|
|
|
|
|
|
|
|
_, err := services.BenefactorKindBoxReqSvc.Get(context.Background(), benefactorkindboxreqparam.KindBoxReqGetRequest{
|
|
|
|
BenefactorID: 1,
|
|
|
|
KindBoxReqID: kindBoxReqID,
|
|
|
|
})
|
|
|
|
// TODO: Fix to assert equal to errmsg.ErrorMsgNotFound
|
|
|
|
assert.Error(suite.T(), err)
|
|
|
|
}
|
2024-08-23 14:28:44 +00:00
|
|
|
|
2024-08-23 15:35:30 +00:00
|
|
|
// Entry point for the test suite
|
|
|
|
func TestBenefactorKindBoxReqsTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(BenefactorKindBoxReqsTestSuite))
|
2024-08-23 14:28:44 +00:00
|
|
|
}
|