2020-05-06 12:56:13 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateSession(t *testing.T) {
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateSession(router)
|
2020-06-25 01:20:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"username": "admin", "password": "photoprism"}`)
|
2020-05-06 12:56:13 +02:00
|
|
|
val2 := gjson.Get(r.Body.String(), "user.Email")
|
2020-06-25 01:20:58 +02:00
|
|
|
assert.Equal(t, "", val2.String())
|
2020-05-06 12:56:13 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("bad request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateSession(router)
|
2020-06-25 01:20:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"username": 123, "password": "xxx"}`)
|
2020-05-06 12:56:13 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
2020-07-14 15:01:11 +02:00
|
|
|
t.Run("invalid token", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateSession(router)
|
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"username": "admin", "password": "photoprism", "token": "xxx"}`)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("valid token", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateSession(router)
|
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"username": "admin", "password": "photoprism", "token": "1jxf3jfn2k"}`)
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
2020-05-06 12:56:13 +02:00
|
|
|
t.Run("invalid password", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateSession(router)
|
2020-06-25 01:20:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"username": "admin", "password": "xxx"}`)
|
2020-05-06 12:56:13 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2020-06-25 01:20:58 +02:00
|
|
|
assert.Equal(t, "Invalid user name or password", val.String())
|
2020-05-06 12:56:13 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteSession(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateSession(router)
|
2020-06-25 01:20:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"username": "admin", "password": "photoprism"}`)
|
2020-06-25 14:54:04 +02:00
|
|
|
id := gjson.Get(r.Body.String(), "id")
|
2020-05-06 12:56:13 +02:00
|
|
|
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
DeleteSession(router)
|
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/session/"+id.String())
|
2020-05-06 12:56:13 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|