forked from ebhomengo/niki
1
0
Fork 0

Chore(benefactor-kindBoxReqs-test): add tests

- Get_Success
- GetAll_Success
- Create_Success

Signed-off-by: Reza Mobaraki <rezam578@gmail.com>
This commit is contained in:
Reza Mobaraki 2024-08-23 15:54:27 +03:30
parent 16f37c0f64
commit 6c78c8098a
No known key found for this signature in database
GPG Key ID: 922CBCF25B541A6F
1 changed files with 124 additions and 0 deletions

View File

@ -0,0 +1,124 @@
//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
}
func TestBenefactorKindBoxReqs_GetAll_Success(t *testing.T) {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
token := fmt.Sprintf("Bearer %s", loginBenefactor(t))
req := httptest.NewRequest(http.MethodGet, "/benefactor/kindboxreqs/", 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)
}
func TestBenefactorKindBoxReqs_Create_Success(t *testing.T) {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
token := fmt.Sprintf("Bearer %s", loginBenefactor(t))
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,
}
requestBody, err := json.Marshal(newKindBox)
if err != nil {
t.Fatalf("failed to marshal request body: %s", err)
}
req := httptest.NewRequest(http.MethodPost, "/benefactor/kindboxreqs/", bytes.NewBuffer(requestBody))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, token)
rec := httptest.NewRecorder()
testServer.Serve(rec, req)
assert.Equal(t, http.StatusCreated, rec.Code)
var response benefactorkindboxreqparam.KindBoxReqAddResponse
err = json.NewDecoder(rec.Body).Decode(&response)
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)
}