Skip to content

Commit 33a120f

Browse files
committed
Tests, guards before ill trans check
1 parent 307b510 commit 33a120f

2 files changed

Lines changed: 55 additions & 8 deletions

File tree

broker/patron_request/api/api-handler.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,14 @@ func (a *PatronRequestApiHandler) PutPatronRequestsId(w http.ResponseWriter, r *
550550
api.AddBadRequestError(ctx, w, fmt.Errorf("only borrower-side patron requests can be updated"))
551551
return
552552
}
553+
if !existingPr.RequesterSymbol.Valid || existingPr.RequesterSymbol.String == "" {
554+
api.AddInternalError(ctx, w, fmt.Errorf("existing patron request missing requesterSymbol"))
555+
return
556+
}
557+
if newPr.RequesterSymbol != nil && *newPr.RequesterSymbol != "" && *newPr.RequesterSymbol != existingPr.RequesterSymbol.String {
558+
api.AddBadRequestError(ctx, w, fmt.Errorf("requesterSymbol does not match existing patron request"))
559+
return
560+
}
553561
if a.illRepo == nil {
554562
api.AddInternalError(ctx, w, errors.New("illRepo is not configured"))
555563
return
@@ -564,14 +572,6 @@ func (a *PatronRequestApiHandler) PutPatronRequestsId(w http.ResponseWriter, r *
564572
api.AddInternalError(ctx, w, err)
565573
return
566574
}
567-
if !existingPr.RequesterSymbol.Valid || existingPr.RequesterSymbol.String == "" {
568-
api.AddInternalError(ctx, w, fmt.Errorf("existing patron request missing requesterSymbol"))
569-
return
570-
}
571-
if newPr.RequesterSymbol != nil && *newPr.RequesterSymbol != "" && *newPr.RequesterSymbol != existingPr.RequesterSymbol.String {
572-
api.AddBadRequestError(ctx, w, fmt.Errorf("requesterSymbol does not match existing patron request"))
573-
return
574-
}
575575
symbol = existingPr.RequesterSymbol.String
576576
newPr.RequesterSymbol = &symbol
577577
creationTime := time.Now()

broker/patron_request/api/api-handler_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,25 @@ type PrRepoLendingSide struct {
14131413
PrRepoError
14141414
}
14151415

1416+
// PrRepoBranchOwner returns a borrowing-side PR for id "3" whose RequesterSymbol
1417+
// is "ISIL:S1" — the branch symbol that MockIllRepositorySuccess returns for any
1418+
// peer, allowing a test to reach the requesterSymbol mismatch check.
1419+
type PrRepoBranchOwner struct {
1420+
PrRepoError
1421+
}
1422+
1423+
func (r *PrRepoBranchOwner) GetPatronRequestById(ctx common.ExtendedContext, id string) (pr_db.PatronRequest, error) {
1424+
if id == "3" {
1425+
return pr_db.PatronRequest{
1426+
ID: id,
1427+
State: prservice.BorrowerStateNew,
1428+
Side: prservice.SideBorrowing,
1429+
RequesterSymbol: pgtype.Text{String: "ISIL:S1", Valid: true},
1430+
}, nil
1431+
}
1432+
return r.PrRepoError.GetPatronRequestById(ctx, id)
1433+
}
1434+
14161435
func (r *PrRepoWrongOwner) GetPatronRequestById(ctx common.ExtendedContext, id string) (pr_db.PatronRequest, error) {
14171436
if id == "3" {
14181437
return pr_db.PatronRequest{
@@ -1466,6 +1485,17 @@ func TestPutPatronRequestsIdInvalidJson(t *testing.T) {
14661485
assert.Equal(t, http.StatusBadRequest, rr.Code)
14671486
}
14681487

1488+
func TestPutPatronRequestsIdEmptySymbol(t *testing.T) {
1489+
handler := NewPrApiHandler(new(PrRepoError), mockEventBus, mockEventRepo, tenant.NewResolver(), nil, 10)
1490+
body := proapi.CreatePatronRequest{IllRequest: validIllRequest()}
1491+
jsonBytes, _ := json.Marshal(body)
1492+
req, _ := http.NewRequest("PUT", "/", bytes.NewBuffer(jsonBytes))
1493+
rr := httptest.NewRecorder()
1494+
handler.PutPatronRequestsId(rr, req, "3", proapi.PutPatronRequestsIdParams{})
1495+
assert.Equal(t, http.StatusBadRequest, rr.Code)
1496+
assert.Contains(t, rr.Body.String(), "symbol must be specified")
1497+
}
1498+
14691499
func TestPutPatronRequestsIdMissingId(t *testing.T) {
14701500
repo := new(PrRepoUpdateCapture)
14711501
handler := NewPrApiHandler(repo, mockEventBus, mockEventRepo, tenant.NewResolver(), nil, 10)
@@ -1568,6 +1598,23 @@ func TestPutPatronRequestsIdNotBorrowingSide(t *testing.T) {
15681598
assert.Contains(t, rr.Body.String(), "only borrower-side patron requests can be updated")
15691599
}
15701600

1601+
func TestPutPatronRequestsIdRequesterSymbolMismatch(t *testing.T) {
1602+
// "ISIL:DIFFERENT" is the body/request symbol; MockIllRepositorySuccess returns
1603+
// "ISIL:S1" as a branch of that symbol, so ownership passes. The existing PR
1604+
// stores "ISIL:S1" directly, which differs from the body symbol → mismatch error.
1605+
tenantResolver := tenant.NewResolver().WithIllRepo(new(mocks.MockIllRepositorySuccess))
1606+
handler := NewPrApiHandler(new(PrRepoBranchOwner), mockEventBus, mockEventRepo, tenantResolver, nil, 10)
1607+
differentSymbol := "ISIL:DIFFERENT"
1608+
id := "3"
1609+
body := proapi.CreatePatronRequest{Id: &id, RequesterSymbol: &differentSymbol, IllRequest: validIllRequest()}
1610+
jsonBytes, _ := json.Marshal(body)
1611+
req, _ := http.NewRequest("PUT", "/", bytes.NewBuffer(jsonBytes))
1612+
rr := httptest.NewRecorder()
1613+
handler.PutPatronRequestsId(rr, req, "3", proapi.PutPatronRequestsIdParams{})
1614+
assert.Equal(t, http.StatusBadRequest, rr.Code)
1615+
assert.Contains(t, rr.Body.String(), "requesterSymbol does not match existing patron request")
1616+
}
1617+
15711618
func TestPutPatronRequestsIdPreservesCreatedAt(t *testing.T) {
15721619
originalCreatedAt := time.Date(2024, 1, 15, 10, 0, 0, 0, time.UTC)
15731620
repo := &PrRepoUpdateCapturePreset{

0 commit comments

Comments
 (0)