Files
online-backend/internal/lending/loanorders/create_bind_test.go
GuwandykA 0d000f9af6
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
Harden loan-order create validation and cover gin JSON binding.
Reject zero loanType/branchId/dates before insert, and assert mobile-style payloads bind through gin required tags.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-26 13:13:38 +05:00

65 lines
2.1 KiB
Go

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) {
var dto loanorders.CreateLoanOrderDTO
if err := json.Unmarshal(mobileCreateBody(), &dto); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if dto.LoanType.Int64() != 1 || dto.BranchId.Int64() != 1 {
t.Fatalf("ids: loanType=%d branchId=%d", dto.LoanType.Int64(), dto.BranchId.Int64())
}
if dto.Region != "Ahal" {
t.Fatalf("region=%q", dto.Region)
}
if dto.BornAt.Time().Format("2006-01-02") != "1989-03-21" {
t.Fatalf("bornAt=%v", dto.BornAt.Time())
}
if dto.PassportGivenAt.Time().Format("2006-01-02") != "2017-01-15" {
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)
}
}