Skip to content

Commit 0653355

Browse files
committed
fix: check parent for envelope
1 parent 89d7b11 commit 0653355

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

internal/controllers/envelope_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ func TestNonexistingBudgetEnvelopes404(t *testing.T) {
7777
test.AssertHTTPStatus(t, http.StatusNotFound, &recorder)
7878
}
7979

80+
// TestEnvelopeParentChecked is a regression test for https://github.com/envelope-zero/backend/issues/90.
81+
//
82+
// It verifies that the envelope details endpoint for a category only returns envelopes that belong to the
83+
// category.
84+
func TestEnvelopeParentChecked(t *testing.T) {
85+
r := test.Request(t, "POST", "/v1/budgets/1/categories", `{ "name": "Testing category" }`)
86+
test.AssertHTTPStatus(t, http.StatusCreated, &r)
87+
88+
var category CategoryDetailResponse
89+
test.DecodeResponse(t, &r, &category)
90+
91+
path := fmt.Sprintf("/v1/budgets/1/categories/%v", category.Data.ID)
92+
r = test.Request(t, "GET", path+"/envelopes/1", "")
93+
test.AssertHTTPStatus(t, http.StatusNotFound, &r)
94+
95+
r = test.Request(t, "DELETE", path, "")
96+
test.AssertHTTPStatus(t, http.StatusNoContent, &r)
97+
}
98+
8099
func TestCreateEnvelope(t *testing.T) {
81100
recorder := test.Request(t, "POST", "/v1/budgets/1/categories/1/envelopes", `{ "name": "New Envelope", "note": "More tests something something" }`)
82101
test.AssertHTTPStatus(t, http.StatusCreated, &recorder)

internal/controllers/helper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ func getEnvelope(c *gin.Context) (models.Envelope, error) {
148148
return models.Envelope{}, err
149149
}
150150

151-
_, err = getCategory(c)
151+
category, err := getCategory(c)
152152
if err != nil {
153153
return models.Envelope{}, err
154154
}
155155

156156
err = models.DB.Where(&models.Envelope{
157+
CategoryID: category.ID,
157158
Model: models.Model{
158159
ID: envelopeID,
159160
},

0 commit comments

Comments
 (0)