Harden loan-order create validation and cover gin JSON binding.
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled

Reject zero loanType/branchId/dates before insert, and assert mobile-style payloads bind through gin required tags.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-26 13:13:38 +05:00
parent 6683826662
commit 0d000f9af6
2 changed files with 42 additions and 12 deletions

View File

@@ -1,26 +1,36 @@
package loanorders_test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"online-back/internal/lending/loanorders"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
func mobileCreateBody() []byte {
body, _ := json.Marshal(map[string]any{
"source": "mobile", "loanType": "1", "region": "Ahal", "branchId": "1",
"customerName": "Kerim", "customerSurname": "Atayew",
"passportAddress": "Ashgabat", "realAddress": "Ashgabat",
"passportSerie": "I-AG", "passportId": "101010",
"passportGivenAt": "2017-01-15", "passportGivenBy": "Police",
"bornPlace": "Ashgabat", "bornAt": "1989-03-21",
"phone": "+993 61 92 92 48", "education": "higher", "marriageStatus": "married",
"passportOne": "a.jpg", "passportTwo": "b.jpg", "passportThree": "c.jpg", "passportFour": "d.jpg",
"satisfiable": "yes", "guarantorNote": "Brother", "loanAmount": "20000",
})
return body
}
func TestCreateLoanOrderDTOBindMobilePayload(t *testing.T) {
raw := []byte(`{
"source":"mobile","loanType":"1","region":"Ahal","branchId":"1",
"customerName":"Kerim","customerSurname":"Atayew",
"passportAddress":"Ashgabat","realAddress":"Ashgabat",
"passportSerie":"I-AG","passportId":"101010",
"passportGivenAt":"2017-01-15","passportGivenBy":"Police",
"bornPlace":"Ashgabat","bornAt":"1989-03-21",
"phone":"+993 61 92 92 48","education":"higher","marriageStatus":"married",
"passportOne":"a.jpg","passportTwo":"b.jpg","passportThree":"c.jpg","passportFour":"d.jpg",
"satisfiable":"yes","guarantorNote":"Brother","loanAmount":"20000"
}`)
var dto loanorders.CreateLoanOrderDTO
if err := json.Unmarshal(raw, &dto); err != nil {
if err := json.Unmarshal(mobileCreateBody(), &dto); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if dto.LoanType.Int64() != 1 || dto.BranchId.Int64() != 1 {
@@ -36,3 +46,19 @@ func TestCreateLoanOrderDTOBindMobilePayload(t *testing.T) {
t.Fatalf("passportGivenAt=%v", dto.PassportGivenAt.Time())
}
}
func TestCreateLoanOrderGinBindRequired(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(mobileCreateBody()))
c.Request.Header.Set("Content-Type", "application/json")
var dto loanorders.CreateLoanOrderDTO
if err := c.ShouldBindBodyWith(&dto, binding.JSON); err != nil {
t.Fatalf("ShouldBindJSON: %v", err)
}
if dto.LoanType.Int64() != 1 || dto.Region != "Ahal" {
t.Fatalf("unexpected dto loanType=%d region=%q", dto.LoanType.Int64(), dto.Region)
}
}

View File

@@ -152,6 +152,10 @@ func (h *handler) create(c *gin.Context) {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
if dto.LoanType.Int64() <= 0 || dto.BranchId.Int64() <= 0 || dto.BornAt.IsZero() || dto.PassportGivenAt.IsZero() {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
item, err := h.repository.Create(c.Request.Context(), dto)
if err != nil {