forked from ebhomengo/niki
Compare commits
19 Commits
develop
...
stage/rezo
Author | SHA1 | Date |
---|---|---|
Reza Mobaraki | 92720e143f | |
Reza Mobaraki | a43e973b01 | |
Reza Mobaraki | 73af8d30f9 | |
Reza Mobaraki | 096b4dbf69 | |
Reza Mobaraki | 7c1d93e1da | |
Reza Mobaraki | 68d6aebb3b | |
Reza Mobaraki | d8bf950a89 | |
Reza Mobaraki | 17d3502854 | |
Reza Mobaraki | c58cc2789a | |
Reza Mobaraki | 99f710c41d | |
Reza Mobaraki | 38677a0128 | |
Reza Mobaraki | ceab112d0e | |
Reza Mobaraki | 79b054e09f | |
Reza Mobaraki | ae4854070c | |
Reza Mobaraki | 1c3156fe3a | |
Reza Mobaraki | b132249ffc | |
Reza Mobaraki | 15f37001db | |
Reza Mobaraki | 6c78c8098a | |
Reza Mobaraki | 16f37c0f64 |
4
Makefile
4
Makefile
|
@ -1,7 +1,9 @@
|
|||
// TODO: add commands for build and run in dev/produciton mode
|
||||
# TODO: add commands for build and run in dev/produciton mode
|
||||
|
||||
ROOT=$(realpath $(dir $(lastword $(MAKEFILE_LIST))))
|
||||
|
||||
.PHONY: help confirm lint test format build run docker swagger watch migrate/status migrate/new migrate/up migrate/down
|
||||
|
||||
confirm:
|
||||
@echo -n 'Are you sure? [y/N] ' && read ans && [ $${ans:-N} = y ]
|
||||
|
||||
|
|
|
@ -0,0 +1,172 @@
|
|||
//go:build end2end
|
||||
|
||||
package end2end
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
|
||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||
addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type BenefactorAddressTestSuit struct {
|
||||
suite.Suite
|
||||
benefactorPhone string
|
||||
benefactorID uint
|
||||
addressID uint
|
||||
getAllExpected map[string]interface{}
|
||||
getExpected addressparam.GetAddressResponse
|
||||
createData addressparam.BenefactorAddAddressRequest
|
||||
updateData addressparam.UpdateAddressRequest
|
||||
teardown func()
|
||||
}
|
||||
|
||||
func TestBenefactorAddressTestSuit(t *testing.T) {
|
||||
suite.Run(t, new(BenefactorAddressTestSuit))
|
||||
}
|
||||
|
||||
// SetupTest runs before each test in the suite
|
||||
func (suite *BenefactorAddressTestSuit) SetupTest() {
|
||||
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
||||
suite.T().Cleanup(teardown)
|
||||
suite.benefactorPhone = "09384664404"
|
||||
suite.benefactorID = 1
|
||||
suite.addressID = 1
|
||||
suite.getAllExpected = map[string]interface{}{
|
||||
"count": 1,
|
||||
}
|
||||
suite.getExpected = addressparam.GetAddressResponse{
|
||||
Address: entity.Address{
|
||||
ID: suite.addressID,
|
||||
PostalCode: "3719655861",
|
||||
Address: "tehran sare koche 1",
|
||||
Lat: 35.632508,
|
||||
Lon: 51.452859,
|
||||
Name: "home1",
|
||||
CityID: 8,
|
||||
BenefactorID: suite.benefactorID,
|
||||
},
|
||||
}
|
||||
suite.createData = addressparam.BenefactorAddAddressRequest{
|
||||
PostalCode: "3719655861",
|
||||
Address: "create shiraz kaf sharo",
|
||||
Lat: 29.62949,
|
||||
Lon: 52.497287,
|
||||
CityID: 194,
|
||||
Name: "create shiraz",
|
||||
}
|
||||
suite.updateData = addressparam.UpdateAddressRequest{
|
||||
PostalCode: "3719655861",
|
||||
Address: "update shiraz kaf sharo",
|
||||
Lat: 29.62949,
|
||||
Lon: 52.497287,
|
||||
CityID: 194,
|
||||
Name: "update shiraz",
|
||||
}
|
||||
}
|
||||
|
||||
// TestBenefactorAddressGet tests the GET /address/:id endpoint
|
||||
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGet() {
|
||||
token := LoginBenefactor(suite.benefactorPhone)
|
||||
url := fmt.Sprintf("/address/%d", suite.addressID)
|
||||
responseRecord := CreateRequest("GET", url, token, nil)
|
||||
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
||||
|
||||
var response addressparam.GetAddressResponse
|
||||
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
||||
suite.Require().NoError(err, "could not decode response body")
|
||||
|
||||
suite.Require().Equal(suite.addressID, response.Address.ID)
|
||||
suite.Require().Equal(suite.benefactorID, response.Address.BenefactorID)
|
||||
suite.Require().Equal(suite.getExpected.Address.PostalCode, response.Address.PostalCode)
|
||||
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.CityID, response.Address.CityID)
|
||||
}
|
||||
|
||||
// TestBenefactorAddressGetAll tests the GET /address/ endpoint
|
||||
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGetAll() {
|
||||
token := LoginBenefactor(suite.benefactorPhone)
|
||||
url := fmt.Sprintf("/address/")
|
||||
responseRecord := CreateRequest("GET", url, token, nil)
|
||||
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
// TestBenefactorAddressCreate tests the POST /address/ endpoint
|
||||
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressCreate() {
|
||||
token := LoginBenefactor(suite.benefactorPhone)
|
||||
url := fmt.Sprintf("/address/")
|
||||
responseRecord := CreateRequest("POST", url, token, suite.createData)
|
||||
suite.Require().Equal(http.StatusCreated, responseRecord.Code)
|
||||
|
||||
var response addressparam.BenefactorAddAddressResponse
|
||||
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
||||
suite.Require().NoError(err, "could not decode response body")
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// TestBenefactorAddressUpdate tests the PUT /address/:id endpoint
|
||||
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressUpdate() {
|
||||
// TODO: check Method is patch, however, all fields are required
|
||||
token := LoginBenefactor(suite.benefactorPhone)
|
||||
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(),
|
||||
addressparam.GetAddressRequest{
|
||||
AddressID: suite.addressID,
|
||||
BenefactorID: suite.benefactorID,
|
||||
})
|
||||
suite.Require().NoError(sErr, "failed to get benefactor address")
|
||||
|
||||
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)
|
||||
// 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)
|
||||
}
|
||||
|
||||
// TestBenefactorAddressDelete tests the DELETE /address/:id endpoint
|
||||
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressDelete() {
|
||||
token := LoginBenefactor(suite.benefactorPhone)
|
||||
url := fmt.Sprintf("/address/%d", suite.addressID)
|
||||
responseRecord := CreateRequest("DELETE", url, token, nil)
|
||||
|
||||
suite.Require().Equal(http.StatusNoContent, responseRecord.Code)
|
||||
|
||||
_, err := services.BenefactorAddressSvc.Get(context.Background(),
|
||||
addressparam.GetAddressRequest{
|
||||
AddressID: suite.addressID,
|
||||
BenefactorID: suite.benefactorID,
|
||||
},
|
||||
)
|
||||
message, code := httpmsg.Error(err)
|
||||
suite.Require().Error(err)
|
||||
|
||||
suite.Equal(http.StatusNotFound, code)
|
||||
suite.Equal(errmsg.ErrorMsgNotFound, message)
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
//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,
|
||||
}
|
||||
/*
|
||||
"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{
|
||||
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{},
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (suite *BenefactorKindBoxTestSuite) TestBenefactorKindBoxGetAll() {
|
||||
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)
|
||||
suite.Require().Equal(suite.kindBboxGetAllExpected["count"], len(response.AllKindBox))
|
||||
|
||||
}
|
||||
|
||||
func (suite *BenefactorKindBoxTestSuite) TestBenefactorKindBoxGet() {
|
||||
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)
|
||||
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)
|
||||
|
||||
}
|
|
@ -0,0 +1,173 @@
|
|||
//go:build end2end
|
||||
|
||||
package end2end
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
|
||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||
benefactorkindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// BenefactorKindBoxReqsTestSuite defines the suite for testing Benefactor Kind Box Requests
|
||||
type BenefactorKindBoxReqsTestSuite struct {
|
||||
suite.Suite
|
||||
benefactorPhone string
|
||||
benefactorID uint
|
||||
kindBoxReqID uint
|
||||
getAllExpected map[string]interface{}
|
||||
getExpected map[string]interface{}
|
||||
createData benefactorkindboxreqparam.KindBoxReqAddRequest
|
||||
updateData benefactorkindboxreqparam.KindBoxReqUpdateRequest
|
||||
}
|
||||
|
||||
// TestBenefactorKindBoxReqsTestSuite is the entry point for the test suite
|
||||
func TestBenefactorKindBoxReqsTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(BenefactorKindBoxReqsTestSuite))
|
||||
}
|
||||
|
||||
// SetupTest runs before each test in the suite
|
||||
func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() {
|
||||
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
||||
suite.T().Cleanup(teardown)
|
||||
|
||||
suite.benefactorPhone = "09384664404"
|
||||
suite.benefactorID = uint(1)
|
||||
suite.kindBoxReqID = uint(1)
|
||||
|
||||
suite.getAllExpected = map[string]interface{}{
|
||||
"count": 5,
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
|
||||
suite.createData = benefactorkindboxreqparam.KindBoxReqAddRequest{
|
||||
KindBoxType: entity.KindBoxCylindrical,
|
||||
DeliverAddressID: uint(1),
|
||||
DeliverReferDate: time.Now().AddDate(0, 0, 7).UTC(),
|
||||
DeliverReferTimeID: uint(1),
|
||||
CountRequested: uint(5),
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
// TestBenefactorKindBoxReqs_GetAll_Success tests retrieving all kind box requests
|
||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Success() {
|
||||
token := LoginBenefactor(suite.benefactorPhone)
|
||||
url := fmt.Sprintf("/benefactor/kindboxreqs/")
|
||||
responseRecord := CreateRequest(http.MethodGet, url, token, nil)
|
||||
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
||||
|
||||
var response benefactorkindboxreqparam.GetAllResponse
|
||||
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
||||
suite.Require().NoError(err, "failed to decode response body")
|
||||
|
||||
assert.Equal(suite.T(), suite.getAllExpected["count"], len(response.AllKindBoxReq))
|
||||
}
|
||||
|
||||
// TestBenefactorKindBoxReqs_Get_Success tests retrieving a specific kind box request by ID
|
||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Success() {
|
||||
token := LoginBenefactor(suite.benefactorPhone)
|
||||
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
|
||||
responseRecord := CreateRequest(http.MethodGet, url, token, nil)
|
||||
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
||||
|
||||
var response benefactorkindboxreqparam.KindBoxReqGetResponse
|
||||
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
||||
suite.Require().NoError(err, "failed to decode response body")
|
||||
|
||||
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["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)
|
||||
}
|
||||
|
||||
// TestBenefactorKindBoxReqs_Create_Success tests creating a kind box request
|
||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Success() {
|
||||
token := LoginBenefactor(suite.benefactorPhone)
|
||||
url := fmt.Sprintf("/benefactor/kindboxreqs/")
|
||||
rec := CreateRequest(http.MethodPost, url, token, suite.createData)
|
||||
suite.Require().Equal(http.StatusCreated, rec.Code)
|
||||
|
||||
var response benefactorkindboxreqparam.KindBoxReqAddResponse
|
||||
err := json.NewDecoder(rec.Body).Decode(&response)
|
||||
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.DeliverAddressID, response.KindBoxReq.DeliverAddressID)
|
||||
assert.Equal(suite.T(), suite.createData.DeliverReferDate, response.KindBoxReq.DeliverReferDate)
|
||||
assert.Equal(suite.T(), suite.createData.CountRequested, response.KindBoxReq.CountRequested)
|
||||
}
|
||||
|
||||
// TestBenefactorKindBoxReqs_Update_Success tests updating a kind box request
|
||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Success() {
|
||||
token := LoginBenefactor(suite.benefactorPhone)
|
||||
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
|
||||
rec := CreateRequest(http.MethodPut, url, token, suite.updateData)
|
||||
suite.Require().Equal(http.StatusNoContent, rec.Code)
|
||||
|
||||
kindBoxReq, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
||||
benefactorkindboxreqparam.KindBoxReqGetRequest{
|
||||
BenefactorID: suite.benefactorID,
|
||||
KindBoxReqID: suite.kindBoxReqID,
|
||||
},
|
||||
)
|
||||
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.CountRequested, kindBoxReq.CountRequested)
|
||||
assert.Equal(suite.T(), suite.updateData.Description, kindBoxReq.Description)
|
||||
assert.Equal(suite.T(), suite.updateData.DeliverReferTimeID, kindBoxReq.DeliverReferTimeID)
|
||||
assert.Equal(
|
||||
suite.T(),
|
||||
suite.updateData.DeliverReferDate.Format("2006-01-02"),
|
||||
kindBoxReq.DeliverReferDate.Format("2006-01-02"),
|
||||
)
|
||||
assert.Equal(suite.T(), suite.updateData.DeliverAddressID, kindBoxReq.DeliverAddressID)
|
||||
}
|
||||
|
||||
// TestBenefactorKindBoxReqs_Delete_Success tests deleting a kind box request
|
||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Success() {
|
||||
token := LoginBenefactor(suite.benefactorPhone)
|
||||
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
|
||||
rec := CreateRequest(http.MethodDelete, url, token, nil)
|
||||
suite.Require().Equal(http.StatusOK, rec.Code)
|
||||
|
||||
_, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
||||
benefactorkindboxreqparam.KindBoxReqGetRequest{
|
||||
BenefactorID: suite.benefactorID,
|
||||
KindBoxReqID: suite.kindBoxReqID,
|
||||
},
|
||||
)
|
||||
message, code := httpmsg.Error(err)
|
||||
suite.Require().Error(err)
|
||||
assert.Equal(suite.T(), http.StatusNotFound, code)
|
||||
assert.Equal(suite.T(), errmsg.ErrorMsgNotFound, message)
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
//go:build end2end
|
||||
|
||||
package end2end
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
|
||||
"github.com/labstack/echo/v4"
|
||||
"log"
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
// CreateRequest is a utility function to create and send HTTP requests
|
||||
func CreateRequest(method, url, token string, body interface{}) *httptest.ResponseRecorder {
|
||||
var buf bytes.Buffer
|
||||
if body != nil {
|
||||
err := json.NewEncoder(&buf).Encode(body)
|
||||
if err != nil {
|
||||
log.Fatalf("could not encode body: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// LoginBenefactor is a utility function to login a benefactor and return the access token
|
||||
func LoginBenefactor(phoneNumber string) string {
|
||||
sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), param.SendOtpRequest{
|
||||
PhoneNumber: phoneNumber,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("failed to send OTP: %v", err)
|
||||
}
|
||||
|
||||
registerRes, err := services.BenefactorSvc.LoginOrRegister(context.Background(), param.LoginOrRegisterRequest{
|
||||
PhoneNumber: phoneNumber,
|
||||
VerificationCode: sendOTPRes.Code,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("failed to register: %v", err)
|
||||
}
|
||||
|
||||
return registerRes.Tokens.AccessToken
|
||||
}
|
Loading…
Reference in New Issue