2018-11-17 08:28:50 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-08-11 10:47:52 +02:00
|
|
|
"encoding/json"
|
2018-11-17 08:28:50 +01:00
|
|
|
"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
|
|
|
|
2021-08-11 11:01:14 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-11 12:47:13 +02:00
|
|
|
// AuthenticateAdmin Register session routes and returns valid SessionId.
|
|
|
|
// Call this func after registering other routes and before performing other requests.
|
|
|
|
func AuthenticateAdmin(app *gin.Engine, router *gin.RouterGroup) (sessId string) {
|
|
|
|
return AuthenticateUser(app, router, "admin", "photoprism")
|
2021-08-10 17:22:15 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 12:47:13 +02:00
|
|
|
// AuthenticateUser Register session routes and returns valid SessionId.
|
|
|
|
// Call this func after registering other routes and before performing other requests.
|
|
|
|
func AuthenticateUser(app *gin.Engine, router *gin.RouterGroup, username string, password string) (sessId string) {
|
2020-12-18 13:05:48 +01:00
|
|
|
CreateSession(router)
|
2021-08-11 10:47:52 +02:00
|
|
|
f := form.Login{
|
|
|
|
UserName: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
loginStr, err := json.Marshal(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-08-11 12:47:13 +02:00
|
|
|
r0 := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", string(loginStr))
|
|
|
|
sessId = r0.Header().Get("X-Session-ID")
|
|
|
|
return
|
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)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
return w
|
|
|
|
}
|
2020-05-08 18:35:19 +02:00
|
|
|
|
2022-03-31 17:55:40 +02:00
|
|
|
// Performs an authenticated API request containing the request body as a string.
|
2021-08-11 12:47:13 +02:00
|
|
|
func AuthenticatedRequestWithBody(r http.Handler, method, path, body string, sessionId string) *httptest.ResponseRecorder {
|
|
|
|
reader := strings.NewReader(body)
|
|
|
|
req, _ := http.NewRequest(method, path, reader)
|
|
|
|
req.Header.Add("X-Session-ID", sessionId)
|
|
|
|
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()
|
2022-03-02 12:20:02 +01:00
|
|
|
log.SetLevel(logrus.TraceLevel)
|
2020-05-08 18:35:19 +02:00
|
|
|
|
2022-03-31 17:55:40 +02:00
|
|
|
c := config.NewTestConfig("api")
|
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)
|
|
|
|
}
|