test: add tests for handling empty UUID in DeleteWhitelist and invalid CIDR in Add method

This commit is contained in:
GitHub Actions
2026-04-19 21:11:14 +00:00
parent db1e77ceb3
commit 2c284bdd49
2 changed files with 24 additions and 0 deletions

View File

@@ -265,3 +265,20 @@ func TestDeleteWhitelist_ReloadFailure(t *testing.T) {
assert.Equal(t, http.StatusNoContent, w.Code)
assert.True(t, mock.reloadCalled)
}
func TestDeleteWhitelist_EmptyUUID(t *testing.T) {
t.Parallel()
h, _, _ := setupWhitelistHandler(t)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodDelete, "/api/v1/admin/crowdsec/whitelist/", nil)
c.Params = gin.Params{{Key: "uuid", Value: ""}}
h.DeleteWhitelist(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
var resp map[string]interface{}
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
assert.Equal(t, "uuid is required", resp["error"])
}

View File

@@ -300,3 +300,10 @@ func TestCrowdSecWhitelistService_WriteYAML_RenameError(t *testing.T) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "rename")
}
func TestCrowdSecWhitelistService_Add_InvalidCIDR(t *testing.T) {
t.Parallel()
svc := services.NewCrowdSecWhitelistService(openWhitelistTestDB(t), "")
_, err := svc.Add(context.Background(), "not-an-ip/24", "invalid cidr with slash")
assert.ErrorIs(t, err, services.ErrInvalidIPOrCIDR)
}