From e9a26b908865587172fbd4be8ad7b047d1bef64f Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 3 Apr 2026 20:50:50 +0200 Subject: [PATCH] feat: add DELETE /test/all endpoint to truncate all tables --- pkg/routes/api/v1/testing.go | 30 ++++++++++++++++++++++++++++++ pkg/routes/routes.go | 1 + 2 files changed, 31 insertions(+) diff --git a/pkg/routes/api/v1/testing.go b/pkg/routes/api/v1/testing.go index 8c624d0ba..1b86ea86f 100644 --- a/pkg/routes/api/v1/testing.go +++ b/pkg/routes/api/v1/testing.go @@ -114,3 +114,33 @@ func HandleTesting(c *echo.Context) error { return c.JSON(http.StatusCreated, data) } + +// HandleTestingTruncateAll truncates all tables in the database +// @Summary Truncate all tables +// @Description Removes all data from every Vikunja table. Used by e2e tests to ensure clean state before each test. Requires the testing token. +// @tags testing +// @Produce json +// @Success 200 {object} map[string]string "All tables truncated." +// @Failure 403 {object} web.HTTPError "Forbidden" +// @Failure 500 {object} models.Message "Internal server error." +// @Router /test/all [delete] +func HandleTestingTruncateAll(c *echo.Context) error { + token := c.Request().Header.Get("Authorization") + if token != config.ServiceTestingtoken.GetString() { + return echo.ErrForbidden + } + + events.WaitForPendingHandlers() + + if err := db.TruncateAllTables(); err != nil { + log.Errorf("Error truncating all tables: %v", err) + return c.JSON(http.StatusInternalServerError, map[string]interface{}{ + "error": true, + "message": err.Error(), + }) + } + + return c.JSON(http.StatusOK, map[string]string{ + "message": "ok", + }) +} diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index 4d0d532e9..49caf4c51 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -393,6 +393,7 @@ func registerAPIRoutes(a *echo.Group) { // Testing if config.ServiceTestingtoken.GetString() != "" { + n.DELETE("/test/all", apiv1.HandleTestingTruncateAll) n.PATCH("/test/:table", apiv1.HandleTesting) }