forked from ebhomengo/niki
add all test in one branch
Signed-off-by: Reza Mobaraki <rezam578@gmail.com>
This commit is contained in:
parent
096b4dbf69
commit
73af8d30f9
|
@ -3,20 +3,16 @@
|
||||||
package end2end
|
package end2end
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
|
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
|
||||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||||
addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
|
addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
|
||||||
benefactoreparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
|
|
||||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||||
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -76,44 +72,11 @@ func TestBenefactorAddressTestSuit(t *testing.T) {
|
||||||
suite.Run(t, new(BenefactorAddressTestSuit))
|
suite.Run(t, new(BenefactorAddressTestSuit))
|
||||||
}
|
}
|
||||||
|
|
||||||
// loginBenefactor authenticates the benefactor and returns an access token
|
|
||||||
func (suite *BenefactorAddressTestSuit) loginBenefactor() string {
|
|
||||||
sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{
|
|
||||||
PhoneNumber: suite.benefactorPhone,
|
|
||||||
})
|
|
||||||
suite.Require().NoError(err, "failed to send OTP")
|
|
||||||
|
|
||||||
registerRes, err := services.BenefactorSvc.LoginOrRegister(context.Background(), benefactoreparam.LoginOrRegisterRequest{
|
|
||||||
PhoneNumber: suite.benefactorPhone,
|
|
||||||
VerificationCode: sendOTPRes.Code,
|
|
||||||
})
|
|
||||||
suite.Require().NoError(err, "failed to login or register")
|
|
||||||
|
|
||||||
return registerRes.Tokens.AccessToken
|
|
||||||
}
|
|
||||||
|
|
||||||
// createRequest is a utility function to create and send HTTP requests
|
|
||||||
func (suite *BenefactorAddressTestSuit) createRequest(method, url string, body interface{}) *httptest.ResponseRecorder {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if body != nil {
|
|
||||||
err := json.NewEncoder(&buf).Encode(body)
|
|
||||||
suite.Require().NoError(err, "could not encode body")
|
|
||||||
}
|
|
||||||
|
|
||||||
token := suite.loginBenefactor()
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBenefactorAddressGet tests the GET /address/:id endpoint
|
// TestBenefactorAddressGet tests the GET /address/:id endpoint
|
||||||
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGet() {
|
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGet() {
|
||||||
responseRecord := suite.createRequest("GET", fmt.Sprintf("/address/%d", suite.addressID), nil)
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
|
url := fmt.Sprintf("/address/%d", suite.addressID)
|
||||||
|
responseRecord := CreateRequest("GET", url, token, nil)
|
||||||
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
||||||
|
|
||||||
var response addressparam.GetAddressResponse
|
var response addressparam.GetAddressResponse
|
||||||
|
@ -133,7 +96,9 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGet() {
|
||||||
|
|
||||||
// TestBenefactorAddressGetAll tests the GET /address/ endpoint
|
// TestBenefactorAddressGetAll tests the GET /address/ endpoint
|
||||||
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGetAll() {
|
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGetAll() {
|
||||||
responseRecord := suite.createRequest("GET", "/address/", nil)
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
|
url := fmt.Sprintf("/address/")
|
||||||
|
responseRecord := CreateRequest("GET", url, token, nil)
|
||||||
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
||||||
|
|
||||||
var response addressparam.GetAllAddressesResponse
|
var response addressparam.GetAllAddressesResponse
|
||||||
|
@ -145,7 +110,9 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGetAll() {
|
||||||
|
|
||||||
// TestBenefactorAddressCreate tests the POST /address/ endpoint
|
// TestBenefactorAddressCreate tests the POST /address/ endpoint
|
||||||
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressCreate() {
|
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressCreate() {
|
||||||
responseRecord := suite.createRequest("POST", "/address/", suite.createData)
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
|
url := fmt.Sprintf("/address/")
|
||||||
|
responseRecord := CreateRequest("POST", url, token, suite.createData)
|
||||||
suite.Require().Equal(http.StatusCreated, responseRecord.Code)
|
suite.Require().Equal(http.StatusCreated, responseRecord.Code)
|
||||||
|
|
||||||
var response addressparam.BenefactorAddAddressResponse
|
var response addressparam.BenefactorAddAddressResponse
|
||||||
|
@ -162,7 +129,9 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressCreate() {
|
||||||
// TestBenefactorAddressUpdate tests the PUT /address/:id endpoint
|
// TestBenefactorAddressUpdate tests the PUT /address/:id endpoint
|
||||||
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressUpdate() {
|
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressUpdate() {
|
||||||
// TODO: check Method is patch, however, all fields are required
|
// TODO: check Method is patch, however, all fields are required
|
||||||
responseRecord := suite.createRequest("PATCH", fmt.Sprintf("/address/%d", suite.addressID), suite.updateData)
|
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)
|
suite.Require().Equal(http.StatusNoContent, responseRecord.Code)
|
||||||
|
|
||||||
updatedAddress, sErr := services.BenefactorAddressSvc.Get(context.Background(),
|
updatedAddress, sErr := services.BenefactorAddressSvc.Get(context.Background(),
|
||||||
|
@ -183,7 +152,10 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressUpdate() {
|
||||||
|
|
||||||
// TestBenefactorAddressDelete tests the DELETE /address/:id endpoint
|
// TestBenefactorAddressDelete tests the DELETE /address/:id endpoint
|
||||||
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressDelete() {
|
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressDelete() {
|
||||||
responseRecord := suite.createRequest("DELETE", fmt.Sprintf("/address/%d", suite.addressID), nil)
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
|
url := fmt.Sprintf("/address/%d", suite.addressID)
|
||||||
|
responseRecord := CreateRequest("DELETE", url, token, nil)
|
||||||
|
|
||||||
suite.Require().Equal(http.StatusNoContent, responseRecord.Code)
|
suite.Require().Equal(http.StatusNoContent, responseRecord.Code)
|
||||||
|
|
||||||
_, err := services.BenefactorAddressSvc.Get(context.Background(),
|
_, err := services.BenefactorAddressSvc.Get(context.Background(),
|
||||||
|
|
|
@ -3,21 +3,17 @@
|
||||||
package end2end
|
package end2end
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
|
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
|
||||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
"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"
|
benefactorkindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
|
||||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||||
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -77,50 +73,15 @@ func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// loginBenefactor authenticates the benefactor and returns an access token
|
|
||||||
// TODO: Move this to a common utility function
|
|
||||||
func (suite *BenefactorKindBoxReqsTestSuite) loginBenefactor() string {
|
|
||||||
sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{
|
|
||||||
PhoneNumber: suite.benefactorPhone,
|
|
||||||
})
|
|
||||||
suite.Require().NoError(err, "failed to send OTP")
|
|
||||||
|
|
||||||
registerRes, err := services.BenefactorSvc.LoginOrRegister(context.Background(), benefactoreparam.LoginOrRegisterRequest{
|
|
||||||
PhoneNumber: suite.benefactorPhone,
|
|
||||||
VerificationCode: sendOTPRes.Code,
|
|
||||||
})
|
|
||||||
suite.Require().NoError(err, "failed to login or register")
|
|
||||||
|
|
||||||
return registerRes.Tokens.AccessToken
|
|
||||||
}
|
|
||||||
|
|
||||||
// createRequest is a utility function to create and send HTTP requests
|
|
||||||
// TODO: Move this to a common utility function
|
|
||||||
func (suite *BenefactorKindBoxReqsTestSuite) createRequest(method, url string, body interface{}) *httptest.ResponseRecorder {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if body != nil {
|
|
||||||
err := json.NewEncoder(&buf).Encode(body)
|
|
||||||
suite.Require().NoError(err, "could not encode body")
|
|
||||||
}
|
|
||||||
|
|
||||||
token := suite.loginBenefactor()
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBenefactorKindBoxReqs_GetAll_Success tests retrieving all kind box requests
|
// TestBenefactorKindBoxReqs_GetAll_Success tests retrieving all kind box requests
|
||||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Success() {
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Success() {
|
||||||
rec := suite.createRequest(http.MethodGet, "/benefactor/kindboxreqs/", nil)
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
suite.Require().Equal(http.StatusOK, rec.Code)
|
url := fmt.Sprintf("/benefactor/kindboxreqs/")
|
||||||
|
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(rec.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))
|
||||||
|
@ -128,11 +89,13 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Su
|
||||||
|
|
||||||
// TestBenefactorKindBoxReqs_Get_Success tests retrieving a specific kind box request by ID
|
// TestBenefactorKindBoxReqs_Get_Success tests retrieving a specific kind box request by ID
|
||||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Success() {
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Success() {
|
||||||
rec := suite.createRequest(http.MethodGet, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), nil)
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
suite.Require().Equal(http.StatusOK, rec.Code)
|
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
|
var response benefactorkindboxreqparam.KindBoxReqGetResponse
|
||||||
err := json.NewDecoder(rec.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)
|
||||||
|
@ -148,7 +111,9 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Succe
|
||||||
|
|
||||||
// TestBenefactorKindBoxReqs_Create_Success tests creating a kind box request
|
// TestBenefactorKindBoxReqs_Create_Success tests creating a kind box request
|
||||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Success() {
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Success() {
|
||||||
rec := suite.createRequest(http.MethodPost, "/benefactor/kindboxreqs/", suite.createData)
|
token := LoginBenefactor(suite.benefactorPhone)
|
||||||
|
url := fmt.Sprintf("/benefactor/kindboxreqs/")
|
||||||
|
rec := CreateRequest(http.MethodPost, url, token, suite.createData)
|
||||||
suite.Require().Equal(http.StatusCreated, rec.Code)
|
suite.Require().Equal(http.StatusCreated, rec.Code)
|
||||||
|
|
||||||
var response benefactorkindboxreqparam.KindBoxReqAddResponse
|
var response benefactorkindboxreqparam.KindBoxReqAddResponse
|
||||||
|
@ -163,7 +128,9 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Su
|
||||||
|
|
||||||
// TestBenefactorKindBoxReqs_Update_Success tests updating a kind box request
|
// TestBenefactorKindBoxReqs_Update_Success tests updating a kind box request
|
||||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Success() {
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Success() {
|
||||||
rec := suite.createRequest(http.MethodPut, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), suite.updateData)
|
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)
|
suite.Require().Equal(http.StatusNoContent, rec.Code)
|
||||||
|
|
||||||
kindBoxReq, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
kindBoxReq, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
||||||
|
@ -188,7 +155,9 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Su
|
||||||
|
|
||||||
// TestBenefactorKindBoxReqs_Delete_Success tests deleting a kind box request
|
// TestBenefactorKindBoxReqs_Delete_Success tests deleting a kind box request
|
||||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Success() {
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Success() {
|
||||||
rec := suite.createRequest(http.MethodDelete, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), nil)
|
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)
|
suite.Require().Equal(http.StatusOK, rec.Code)
|
||||||
|
|
||||||
_, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
_, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
||||||
|
|
|
@ -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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
Loading…
Reference in New Issue