2018-11-17 08:28:50 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-05-08 18:35:19 +02:00
|
|
|
"os"
|
2020-04-08 13:24:06 +02:00
|
|
|
"strings"
|
2020-05-08 18:35:19 +02:00
|
|
|
"testing"
|
2018-11-17 08:28:50 +01:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-09-02 21:30:50 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-04-30 20:07:03 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2018-11-17 08:28:50 +01:00
|
|
|
)
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
log = logrus.StandardLogger()
|
|
|
|
log.SetLevel(logrus.TraceLevel)
|
|
|
|
event.AuditLog = log
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
c := config.TestConfig()
|
2022-09-28 09:01:17 +02:00
|
|
|
service.SetConfig(c)
|
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
|
|
|
|
_ = c.CloseDb()
|
|
|
|
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:05:48 +01:00
|
|
|
// NewApiTest returns new API test helper.
|
2019-05-06 23:18:10 +02:00
|
|
|
func NewApiTest() (app *gin.Engine, router *gin.RouterGroup, conf *config.Config) {
|
2018-11-17 08:28:50 +01:00
|
|
|
gin.SetMode(gin.TestMode)
|
2022-09-28 09:01:17 +02:00
|
|
|
|
2018-11-17 08:28:50 +01:00
|
|
|
app = gin.New()
|
|
|
|
router = app.Group("/api/v1")
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return app, router, service.Config()
|
2020-12-18 13:05:48 +01:00
|
|
|
}
|
|
|
|
|
2022-03-31 17:55:40 +02:00
|
|
|
// Executes an API request with an empty request body.
|
2018-11-18 19:18:19 +01:00
|
|
|
// See https://medium.com/@craigchilds94/testing-gin-json-responses-1f258ce3b0b1
|
|
|
|
func PerformRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
|
2018-11-17 08:28:50 +01:00
|
|
|
req, _ := http.NewRequest(method, path, nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-12-18 13:05:48 +01:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2022-03-31 17:55:40 +02:00
|
|
|
// Executes an API request with the request body as a string.
|
2020-04-08 13:24:06 +02:00
|
|
|
func PerformRequestWithBody(r http.Handler, method, path, body string) *httptest.ResponseRecorder {
|
|
|
|
reader := strings.NewReader(body)
|
|
|
|
req, _ := http.NewRequest(method, path, reader)
|
|
|
|
w := httptest.NewRecorder()
|
2020-05-08 18:35:19 +02:00
|
|
|
|
2021-08-11 12:47:13 +02:00
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return w
|
2020-05-08 18:35:19 +02:00
|
|
|
}
|