diff --git a/delivery/http_server/end2end/benefactor_kindboxreqs_test.go b/delivery/http_server/end2end/benefactor_kindboxreqs_test.go new file mode 100644 index 0000000..5be7d06 --- /dev/null +++ b/delivery/http_server/end2end/benefactor_kindboxreqs_test.go @@ -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) +}