Add testing endpoint to reset db tables (#716)
Fix lint Better error messages Add docs Add testing endpoint to reset db Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/716 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
This commit is contained in:
@ -17,9 +17,10 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
"net/http"
|
||||
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
"code.vikunja.io/api/pkg/modules/auth/openid"
|
||||
"code.vikunja.io/api/pkg/modules/migration/todoist"
|
||||
|
70
pkg/routes/api/v1/testing.go
Normal file
70
pkg/routes/api/v1/testing.go
Normal file
@ -0,0 +1,70 @@
|
||||
// Vikunja is a to-do list application to facilitate your life.
|
||||
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package v1
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
"code.vikunja.io/api/pkg/db"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// HandleTesting is the web handler to reset the db
|
||||
// @Summary Reset the db to a defined state
|
||||
// @Description Fills the specified table with the content provided in the payload. You need to enable the testing endpoint before doing this and provide the `Authorization: <token>` secret when making requests to this endpoint. See docs for more details.
|
||||
// @tags testing
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param table path string true "The table to reset"
|
||||
// @Success 201 {array} user.User "Everything has been imported successfully."
|
||||
// @Failure 500 {object} models.Message "Internal server error."
|
||||
// @Router /test/{table} [patch]
|
||||
func HandleTesting(c echo.Context) error {
|
||||
token := c.Request().Header.Get("Authorization")
|
||||
if token != config.ServiceTestingtoken.GetString() {
|
||||
return echo.ErrForbidden
|
||||
}
|
||||
|
||||
table := c.Param("table")
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err := buf.ReadFrom(c.Request().Body); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
content := []map[string]interface{}{}
|
||||
err := json.Unmarshal(buf.Bytes(), &content)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
||||
"error": true,
|
||||
"message": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
err = db.RestoreAndTruncate(table, content)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
||||
"error": true,
|
||||
"message": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusCreated, nil)
|
||||
}
|
Reference in New Issue
Block a user