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"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func loginBenefactor(t *testing.T) string {
|
|
|
|
// TODO: Consider mocking the OTP service & fetching the verification code from Redis.
|
|
|
|
phone := "09384664404"
|
|
|
|
sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{
|
|
|
|
PhoneNumber: phone,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to send OTP: %s", err)
|
|
|
|
}
|
|
|
|
registerRes, err := services.BenefactorSvc.LoginOrRegister(context.Background(), benefactoreparam.LoginOrRegisterRequest{
|
|
|
|
PhoneNumber: phone,
|
|
|
|
VerificationCode: sendOTPRes.Code,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to login or register: %s", err)
|
|
|
|
}
|
|
|
|
return registerRes.Tokens.AccessToken
|
|
|
|
}
|
|
|
|
|
2024-08-23 14:28:44 +00:00
|
|
|
// Utility function to create and send HTTP requests
|
|
|
|
func createRequest(t *testing.T, method, url string, body interface{}) *httptest.ResponseRecorder {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if body != nil {
|
|
|
|
if err := json.NewEncoder(&buf).Encode(body); err != nil {
|
|
|
|
t.Fatalf("could not encode body: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
token := loginBenefactor(t)
|
|
|
|
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 12:24:27 +00:00
|
|
|
func TestBenefactorKindBoxReqs_GetAll_Success(t *testing.T) {
|
|
|
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
|
|
|
t.Cleanup(teardown)
|
|
|
|
|
2024-08-23 14:28:44 +00:00
|
|
|
rec := createRequest(t, http.MethodGet, "/benefactor/kindboxreqs/", nil)
|
2024-08-23 12:24:27 +00:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBenefactorKindBoxReqs_Create_Success(t *testing.T) {
|
|
|
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
|
|
|
t.Cleanup(teardown)
|
|
|
|
|
|
|
|
newKindBox := benefactorkindboxreqparam.KindBoxReqAddRequest{
|
|
|
|
KindBoxType: entity.KindBoxOnTable,
|
|
|
|
DeliverAddressID: 1,
|
|
|
|
DeliverReferDate: time.Date(2025, time.January, 2, 15, 4, 5, 0, time.UTC),
|
|
|
|
DeliverReferTimeID: 1,
|
|
|
|
CountRequested: 2,
|
|
|
|
}
|
|
|
|
|
2024-08-23 14:28:44 +00:00
|
|
|
rec := createRequest(t, http.MethodPost, "/benefactor/kindboxreqs/", newKindBox)
|
2024-08-23 12:24:27 +00:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusCreated, rec.Code)
|
|
|
|
|
|
|
|
var response benefactorkindboxreqparam.KindBoxReqAddResponse
|
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 {
|
|
|
|
t.Fatalf("failed to decode response body: %s", err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, newKindBox.KindBoxType, response.KindBoxReq.KindBoxType)
|
|
|
|
assert.Equal(t, newKindBox.DeliverAddressID, response.KindBoxReq.DeliverAddressID)
|
|
|
|
assert.Equal(t, newKindBox.DeliverReferDate, response.KindBoxReq.DeliverReferDate)
|
|
|
|
assert.Equal(t, newKindBox.DeliverReferTimeID, response.KindBoxReq.DeliverReferTimeID)
|
|
|
|
assert.Equal(t, newKindBox.CountRequested, response.KindBoxReq.CountRequested)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBenefactorKindBoxReqs_Get_Success(t *testing.T) {
|
|
|
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
|
|
|
t.Cleanup(teardown)
|
|
|
|
|
|
|
|
token := fmt.Sprintf("Bearer %s", loginBenefactor(t))
|
|
|
|
|
|
|
|
var kindBoxReqID uint = 1
|
|
|
|
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), nil)
|
|
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
|
|
req.Header.Set(echo.HeaderAuthorization, token)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
testServer.Serve(rec, req)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
|
|
|
|
var response benefactorkindboxreqparam.KindBoxReqGetResponse
|
|
|
|
err := json.NewDecoder(rec.Body).Decode(&response)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to decode response body: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, kindBoxReqID, response.KindBoxReq.ID)
|
|
|
|
assert.Equal(t, entity.KindBoxOnTable, response.KindBoxReq.KindBoxType)
|
|
|
|
assert.Equal(t, uint(1), response.KindBoxReq.DeliverAddressID)
|
|
|
|
expectedDate := time.Now().AddDate(0, 0, 7)
|
|
|
|
assert.Equal(t, expectedDate.Format("2006-01-02"), response.KindBoxReq.DeliverReferDate.Format("2006-01-02"))
|
|
|
|
assert.Equal(t, uint(1), response.KindBoxReq.DeliverReferTimeID)
|
|
|
|
}
|
2024-08-23 14:28:44 +00:00
|
|
|
|
|
|
|
func TestBenefactorKindBoxReqs_Update_Success(t *testing.T) {
|
|
|
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
|
|
|
t.Cleanup(teardown)
|
|
|
|
|
|
|
|
token := fmt.Sprintf("Bearer %s", loginBenefactor(t))
|
|
|
|
|
|
|
|
var kindBoxReqID uint = 1
|
|
|
|
updatedKindBox := benefactorkindboxreqparam.KindBoxReqUpdateRequest{
|
|
|
|
KindBoxType: entity.KindBoxOnTable,
|
|
|
|
DeliverAddressID: 2,
|
|
|
|
DeliverReferDate: time.Date(2025, time.January, 2, 15, 4, 5, 0, time.UTC),
|
|
|
|
DeliverReferTimeID: 1,
|
|
|
|
CountRequested: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody, err := json.Marshal(updatedKindBox)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to marshal request body: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodPut, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), bytes.NewBuffer(requestBody))
|
|
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
|
|
req.Header.Set(echo.HeaderAuthorization, token)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
testServer.Serve(rec, req)
|
|
|
|
|
|
|
|
var response benefactorkindboxreqparam.KindBoxReqUpdateResponse
|
|
|
|
err = json.NewDecoder(rec.Body).Decode(&response)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to decode response body: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNoContent, rec.Code)
|
|
|
|
|
|
|
|
}
|