Auth: added api tests for user sessions #98

This commit is contained in:
Timo Volkmann
2021-08-10 17:22:15 +02:00
parent e5b1b7b5f6
commit dd0ee298cd
2 changed files with 57 additions and 15 deletions

View File

@@ -1,6 +1,7 @@
package api
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
@@ -23,10 +24,15 @@ func NewApiTest() (app *gin.Engine, router *gin.RouterGroup, conf *config.Config
// NewApiTest returns new API test helper with authenticated admin session.
func NewAdminApiTest() (app *gin.Engine, router *gin.RouterGroup, conf *config.Config, sessId string) {
return NewAuthenticatedApiTest("admin", "photoprism")
}
// NewApiTest returns new API test helper with authenticated admin session.
func NewAuthenticatedApiTest(username string, password string) (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"}`)
reader := strings.NewReader(fmt.Sprintf(`{"username": %s, "password": "%s"}`, username, password))
req, _ := http.NewRequest("POST", "/api/v1/session", reader)
w := httptest.NewRecorder()
app.ServeHTTP(w, req)