Skip to content

Commit 86979fb

Browse files
committed
fix: check parent for allocation
1 parent 0653355 commit 86979fb

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

internal/controllers/allocation.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,8 @@ func GetAllocations(c *gin.Context) {
9292

9393
// GetAllocation retrieves a allocation by its ID.
9494
func GetAllocation(c *gin.Context) {
95-
var allocation models.Allocation
96-
err := models.DB.First(&allocation, c.Param("allocationId")).Error
95+
allocation, err := getAllocation(c)
9796
if err != nil {
98-
FetchErrorHandler(c, err)
9997
return
10098
}
10199

internal/controllers/allocation_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ func TestNoAllocationNotFound(t *testing.T) {
5454
test.AssertHTTPStatus(t, http.StatusNotFound, &recorder)
5555
}
5656

57+
// TestAllocationInvalidIDs verifies that on non-number requests for allocation IDs,
58+
// the API returs a Bad Request status code.
59+
func TestAllocationInvalidIDs(t *testing.T) {
60+
r := test.Request(t, "GET", "/v1/budgets/1/categories/1/envelopes/1/allocations/-2", "")
61+
test.AssertHTTPStatus(t, http.StatusBadRequest, &r)
62+
63+
r = test.Request(t, "GET", "/v1/budgets/1/categories/1/envelopes/1/allocations/RoadWorkAhead", "")
64+
test.AssertHTTPStatus(t, http.StatusBadRequest, &r)
65+
66+
r = test.Request(t, "GET", "/v1/budgets/1/categories/1/envelopes/-755/allocations/1", "")
67+
test.AssertHTTPStatus(t, http.StatusBadRequest, &r)
68+
69+
r = test.Request(t, "GET", "/v1/budgets/1/categories/1/envelopes/WhatDoYourElfEyesSee/allocations/1", "")
70+
test.AssertHTTPStatus(t, http.StatusBadRequest, &r)
71+
}
72+
5773
// TestNonexistingEnvelopeAllocations404 is a regression test for https://github.com/envelope-zero/backend/issues/89.
5874
//
5975
// It verifies that for a non-existing envelope, no matter if the category or budget exists,
@@ -81,6 +97,25 @@ func TestNonexistingBudgetAllocations404(t *testing.T) {
8197
test.AssertHTTPStatus(t, http.StatusNotFound, &recorder)
8298
}
8399

100+
// TestAllocationParentChecked is a regression test for https://github.com/envelope-zero/backend/issues/90.
101+
//
102+
// It verifies that the allocations details endpoint for an envelope only returns allocations that belong to the
103+
// envelope.
104+
func TestAllocationParentChecked(t *testing.T) {
105+
r := test.Request(t, "POST", "/v1/budgets/1/categories/1/envelopes", `{ "name": "Testing envelope" }`)
106+
test.AssertHTTPStatus(t, http.StatusCreated, &r)
107+
108+
var envelope EnvelopeDetailResponse
109+
test.DecodeResponse(t, &r, &envelope)
110+
111+
path := fmt.Sprintf("/v1/budgets/1/categories/1/envelopes/%v", envelope.Data.ID)
112+
r = test.Request(t, "GET", path+"/allocations/1", "")
113+
test.AssertHTTPStatus(t, http.StatusNotFound, &r)
114+
115+
r = test.Request(t, "DELETE", path, "")
116+
test.AssertHTTPStatus(t, http.StatusNoContent, &r)
117+
}
118+
84119
func TestCreateAllocation(t *testing.T) {
85120
recorder := test.Request(t, "POST", "/v1/budgets/1/categories/1/envelopes/1/allocations", `{ "month": 10, "year": 2022, "amount": 15.42 }`)
86121
test.AssertHTTPStatus(t, http.StatusCreated, &recorder)

internal/controllers/helper.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,34 @@ func getEnvelope(c *gin.Context) (models.Envelope, error) {
167167
return envelope, nil
168168
}
169169

170+
// getAllocation verifies that the request URI is valid for the transaction and returns it.
171+
func getAllocation(c *gin.Context) (models.Allocation, error) {
172+
var allocation models.Allocation
173+
174+
envelope, err := getEnvelope(c)
175+
if err != nil {
176+
return models.Allocation{}, err
177+
}
178+
179+
allocationID, err := parseID(c, "allocationId")
180+
if err != nil {
181+
return models.Allocation{}, err
182+
}
183+
184+
err = models.DB.First(&allocation, &models.Allocation{
185+
EnvelopeID: envelope.ID,
186+
Model: models.Model{
187+
ID: allocationID,
188+
},
189+
}).Error
190+
if err != nil {
191+
FetchErrorHandler(c, err)
192+
return models.Allocation{}, err
193+
}
194+
195+
return allocation, nil
196+
}
197+
170198
// getTransaction verifies that the request URI is valid for the transaction and returns it.
171199
func getTransaction(c *gin.Context) (models.Transaction, error) {
172200
var transaction models.Transaction

0 commit comments

Comments
 (0)