Skip to content

Commit 89d7b11

Browse files
committed
fix: check parent for category
1 parent 031c86d commit 89d7b11

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

internal/controllers/category_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ func TestCategoryInvalidIDs(t *testing.T) {
5555
r := test.Request(t, "GET", "/v1/budgets/1/categories/-557", "")
5656
test.AssertHTTPStatus(t, http.StatusBadRequest, &r)
5757

58-
r = test.Request(t, "GET", "/v1/budgets/1/categories/HanShotFirst", "")
58+
r = test.Request(t, "GET", "/v1/budgets/1/categories/NFTsAreAScam", "")
59+
test.AssertHTTPStatus(t, http.StatusBadRequest, &r)
60+
61+
r = test.Request(t, "GET", "/v1/budgets/-574/categories/56", "")
62+
test.AssertHTTPStatus(t, http.StatusBadRequest, &r)
63+
64+
r = test.Request(t, "GET", "/v1/budgets/NoReallyNFTsAreAScam/categories/1", "")
5965
test.AssertHTTPStatus(t, http.StatusBadRequest, &r)
6066
}
6167

@@ -68,6 +74,25 @@ func TestNonexistingBudgetCategories404(t *testing.T) {
6874
test.AssertHTTPStatus(t, http.StatusNotFound, &recorder)
6975
}
7076

77+
// TestCategoryParentChecked is a regression test for https://github.com/envelope-zero/backend/issues/90.
78+
//
79+
// It verifies that the category details endpoint for a budget only returns categorys that belong to the
80+
// budget.
81+
func TestCategoryParentChecked(t *testing.T) {
82+
r := test.Request(t, "POST", "/v1/budgets", `{ "name": "New Budget", "note": "More tests something something" }`)
83+
test.AssertHTTPStatus(t, http.StatusCreated, &r)
84+
85+
var budget BudgetDetailResponse
86+
test.DecodeResponse(t, &r, &budget)
87+
88+
path := fmt.Sprintf("/v1/budgets/%v", budget.Data.ID)
89+
r = test.Request(t, "GET", path+"/categories/1", "")
90+
test.AssertHTTPStatus(t, http.StatusNotFound, &r)
91+
92+
r = test.Request(t, "DELETE", path, "")
93+
test.AssertHTTPStatus(t, http.StatusNoContent, &r)
94+
}
95+
7196
func TestCreateCategory(t *testing.T) {
7297
recorder := test.Request(t, "POST", "/v1/budgets/1/categories", `{ "name": "New Category", "note": "More tests something something" }`)
7398
test.AssertHTTPStatus(t, http.StatusCreated, &recorder)

internal/controllers/helper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ func getCategory(c *gin.Context) (models.Category, error) {
118118
return models.Category{}, err
119119
}
120120

121-
_, err = getBudget(c)
121+
budget, err := getBudget(c)
122122
if err != nil {
123123
return models.Category{}, err
124124
}
125125

126126
err = models.DB.Where(&models.Category{
127+
BudgetID: budget.ID,
127128
Model: models.Model{
128129
ID: categoryID,
129130
},

0 commit comments

Comments
 (0)