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:
parent
87048818ce
commit
9334b29366
9 changed files with 216 additions and 2 deletions
|
@ -34,6 +34,11 @@ service:
|
||||||
enabletotp: true
|
enabletotp: true
|
||||||
# If not empty, enables logging of crashes and unhandled errors in sentry.
|
# If not empty, enables logging of crashes and unhandled errors in sentry.
|
||||||
sentrydsn: ''
|
sentrydsn: ''
|
||||||
|
# If not empty, this will enable `/test/{table}` endpoints which allow to put any content in the database.
|
||||||
|
# Used to reset the db before frontend tests. Because this is quite a dangerous feature allowing for lots of harm,
|
||||||
|
# each request made to this endpoint neefs to provide an `Authorization: <token>` header with the token from below. <br/>
|
||||||
|
# **You should never use this unless you know exactly what you're doing**
|
||||||
|
testingtoken: ''
|
||||||
|
|
||||||
database:
|
database:
|
||||||
# Database type to use. Supported types are mysql, postgres and sqlite.
|
# Database type to use. Supported types are mysql, postgres and sqlite.
|
||||||
|
|
|
@ -51,6 +51,7 @@ const (
|
||||||
ServiceEnableTaskComments Key = `service.enabletaskcomments`
|
ServiceEnableTaskComments Key = `service.enabletaskcomments`
|
||||||
ServiceEnableTotp Key = `service.enabletotp`
|
ServiceEnableTotp Key = `service.enabletotp`
|
||||||
ServiceSentryDsn Key = `service.sentrydsn`
|
ServiceSentryDsn Key = `service.sentrydsn`
|
||||||
|
ServiceTestingtoken Key = `service.testingtoken`
|
||||||
|
|
||||||
AuthLocalEnabled Key = `auth.local.enabled`
|
AuthLocalEnabled Key = `auth.local.enabled`
|
||||||
AuthOpenIDEnabled Key = `auth.openid.enabled`
|
AuthOpenIDEnabled Key = `auth.openid.enabled`
|
||||||
|
|
|
@ -16,7 +16,11 @@
|
||||||
|
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import "encoding/json"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"xorm.io/xorm/schemas"
|
||||||
|
)
|
||||||
|
|
||||||
// Dump dumps all database tables
|
// Dump dumps all database tables
|
||||||
func Dump() (data map[string][]byte, err error) {
|
func Dump() (data map[string][]byte, err error) {
|
||||||
|
@ -52,3 +56,22 @@ func Restore(table string, contents []map[string]interface{}) (err error) {
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RestoreAndTruncate removes all content from the table before restoring it from the contents map
|
||||||
|
func RestoreAndTruncate(table string, contents []map[string]interface{}) (err error) {
|
||||||
|
if _, err := x.IsTableExist(table); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if x.Dialect().URI().DBType == schemas.SQLITE {
|
||||||
|
if _, err := x.Query("DELETE FROM " + table); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if _, err := x.Query("TRUNCATE TABLE ?", table); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Restore(table, contents)
|
||||||
|
}
|
||||||
|
|
|
@ -17,9 +17,10 @@
|
||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.vikunja.io/api/pkg/log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"code.vikunja.io/api/pkg/log"
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/config"
|
"code.vikunja.io/api/pkg/config"
|
||||||
"code.vikunja.io/api/pkg/modules/auth/openid"
|
"code.vikunja.io/api/pkg/modules/auth/openid"
|
||||||
"code.vikunja.io/api/pkg/modules/migration/todoist"
|
"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)
|
||||||
|
}
|
|
@ -235,6 +235,11 @@ func registerAPIRoutes(a *echo.Group) {
|
||||||
n.POST("/auth/openid/:provider/callback", openid.HandleCallback)
|
n.POST("/auth/openid/:provider/callback", openid.HandleCallback)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Testing
|
||||||
|
if config.ServiceTestingtoken.GetString() != "" {
|
||||||
|
n.PATCH("/test/:table", apiv1.HandleTesting)
|
||||||
|
}
|
||||||
|
|
||||||
// Info endpoint
|
// Info endpoint
|
||||||
n.GET("/info", apiv1.Info)
|
n.GET("/info", apiv1.Info)
|
||||||
|
|
||||||
|
|
|
@ -5605,6 +5605,47 @@ var doc = `{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/test/{table}": {
|
||||||
|
"patch": {
|
||||||
|
"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: \u003ctoken\u003e` + "`" + ` secret when making requests to this endpoint. See docs for more details.",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"testing"
|
||||||
|
],
|
||||||
|
"summary": "Reset the db to a defined state",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "The table to reset",
|
||||||
|
"name": "table",
|
||||||
|
"in": "path",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"201": {
|
||||||
|
"description": "Everything has been imported successfully.",
|
||||||
|
"schema": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/user.User"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"500": {
|
||||||
|
"description": "Internal server error.",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/models.Message"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/user": {
|
"/user": {
|
||||||
"get": {
|
"get": {
|
||||||
"security": [
|
"security": [
|
||||||
|
|
|
@ -5588,6 +5588,47 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/test/{table}": {
|
||||||
|
"patch": {
|
||||||
|
"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: \u003ctoken\u003e` secret when making requests to this endpoint. See docs for more details.",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"testing"
|
||||||
|
],
|
||||||
|
"summary": "Reset the db to a defined state",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "The table to reset",
|
||||||
|
"name": "table",
|
||||||
|
"in": "path",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"201": {
|
||||||
|
"description": "Everything has been imported successfully.",
|
||||||
|
"schema": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/user.User"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"500": {
|
||||||
|
"description": "Internal server error.",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/models.Message"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/user": {
|
"/user": {
|
||||||
"get": {
|
"get": {
|
||||||
"security": [
|
"security": [
|
||||||
|
|
|
@ -4653,6 +4653,33 @@ paths:
|
||||||
summary: Toggle a team member's admin status
|
summary: Toggle a team member's admin status
|
||||||
tags:
|
tags:
|
||||||
- team
|
- team
|
||||||
|
/test/{table}:
|
||||||
|
patch:
|
||||||
|
consumes:
|
||||||
|
- application/json
|
||||||
|
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.'
|
||||||
|
parameters:
|
||||||
|
- description: The table to reset
|
||||||
|
in: path
|
||||||
|
name: table
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
responses:
|
||||||
|
"201":
|
||||||
|
description: Everything has been imported successfully.
|
||||||
|
schema:
|
||||||
|
items:
|
||||||
|
$ref: '#/definitions/user.User'
|
||||||
|
type: array
|
||||||
|
"500":
|
||||||
|
description: Internal server error.
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/models.Message'
|
||||||
|
summary: Reset the db to a defined state
|
||||||
|
tags:
|
||||||
|
- testing
|
||||||
/user:
|
/user:
|
||||||
get:
|
get:
|
||||||
consumes:
|
consumes:
|
||||||
|
|
Loading…
Reference in a new issue