Auth: fixed typo in tests

This commit is contained in:
Timo Volkmann 2021-08-11 10:47:52 +02:00
parent e509afa6eb
commit 3829377824
2 changed files with 44 additions and 8 deletions

View file

@ -1,7 +1,9 @@
package api
import (
"fmt"
"bytes"
"encoding/json"
"github.com/photoprism/photoprism/internal/form"
"net/http"
"net/http/httptest"
"os"
@ -22,17 +24,25 @@ func NewApiTest() (app *gin.Engine, router *gin.RouterGroup, conf *config.Config
return app, router, service.Config()
}
// NewApiTest returns new API test helper with authenticated admin session.
// NewAdminApiTest 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")
return NewAuthApiTest("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) {
// NewAuthApiTest returns new API test helper with authenticated admin session.
func NewAuthApiTest(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(fmt.Sprintf(`{"username": %s, "password": "%s"}`, username, password))
f := form.Login{
UserName: username,
Password: password,
}
loginStr, err := json.Marshal(f)
if err != nil {
log.Fatal(err)
}
reader := bytes.NewReader(loginStr)
req, _ := http.NewRequest("POST", "/api/v1/session", reader)
w := httptest.NewRecorder()
app.ServeHTTP(w, req)

View file

@ -1,6 +1,8 @@
package api
import (
"encoding/json"
"github.com/photoprism/photoprism/internal/form"
"net/http"
"testing"
@ -76,14 +78,38 @@ func TestCreateSession(t *testing.T) {
func TestDeleteSession(t *testing.T) {
t.Run("delete admin session", func(t *testing.T) {
app, router, _, sessId := NewAdminApiTest()
app, router, _ := NewApiTest()
CreateSession(router)
DeleteSession(router)
f := form.Login{
UserName: "admin",
Password: "photoprism",
}
loginStr, err := json.Marshal(f)
if err != nil {
log.Fatal(err)
}
r0 := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", string(loginStr))
sessId := r0.Header().Get("X-Session-ID")
r := PerformRequest(app, http.MethodDelete, "/api/v1/session/"+sessId)
assert.Equal(t, http.StatusOK, r.Code)
})
t.Run("delete user session", func(t *testing.T) {
app, router, _, sessId := NewAuthenticatedApiTest("alice", "Alice123!")
app, router, _ := NewApiTest()
CreateSession(router)
DeleteSession(router)
f := form.Login{
UserName: "alice",
Password: "Alice123!",
}
loginStr, err := json.Marshal(f)
if err != nil {
log.Fatal(err)
}
r0 := PerformRequestWithBody(app, http.MethodPost, "/api/v1/session", string(loginStr))
sessId := r0.Header().Get("X-Session-ID")
r := PerformRequest(app, http.MethodDelete, "/api/v1/session/"+sessId)
assert.Equal(t, http.StatusOK, r.Code)
})