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"
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-04-30 20:07:03 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-05-08 18:35:19 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-11-17 08:28:50 +01:00
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
app = gin.New()
|
|
|
|
router = app.Group("/api/v1")
|
2020-06-07 10:09:35 +02:00
|
|
|
return app, router, service.Config()
|
2018-11-17 08:28:50 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 13:05:48 +01:00
|
|
|
// NewApiTest returns new API test helper with authenticated admin session.
|
|
|
|
func NewAdminApiTest() (app *gin.Engine, router *gin.RouterGroup, conf *config.Config, sessId string) {
|
|
|
|
app = gin.New()
|
|
|
|
router = app.Group("/api/v1")
|
|
|
|
CreateSession(router)
|
|
|
|
reader := strings.NewReader(`{"username": "admin", "password": "photoprism"}`)
|
|
|
|
req, _ := http.NewRequest("POST", "/api/v1/session", reader)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
app.ServeHTTP(w, req)
|
|
|
|
sessId = w.Header().Get("X-Session-ID")
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
return app, router, service.Config(), sessId
|
|
|
|
}
|
|
|
|
|
2020-04-08 13:24:06 +02:00
|
|
|
// Performs API request with 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)
|
|
|
|
return w
|
2018-11-17 12:29:01 +01:00
|
|
|
}
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-12-18 13:05:48 +01:00
|
|
|
// Performs authenticated API request with empty request body.
|
|
|
|
func AuthenticatedRequest(r http.Handler, method, path, sess string) *httptest.ResponseRecorder {
|
|
|
|
req, _ := http.NewRequest(method, path, nil)
|
|
|
|
req.Header.Add("X-Session-ID", sess)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2020-04-08 13:24:06 +02:00
|
|
|
// Performs API request including request body as string.
|
|
|
|
func PerformRequestWithBody(r http.Handler, method, path, body string) *httptest.ResponseRecorder {
|
|
|
|
reader := strings.NewReader(body)
|
|
|
|
req, _ := http.NewRequest(method, path, reader)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
return w
|
|
|
|
}
|
2020-05-08 18:35:19 +02:00
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
log = logrus.StandardLogger()
|
|
|
|
log.SetLevel(logrus.DebugLevel)
|
|
|
|
|
|
|
|
c := config.TestConfig()
|
2020-06-07 10:09:35 +02:00
|
|
|
service.SetConfig(c)
|
2020-05-08 18:35:19 +02:00
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
|
|
|
|
_ = c.CloseDb()
|
|
|
|
|
|
|
|
os.Exit(code)
|
|
|
|
}
|