2019-07-17 16:46:54 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2019-12-11 14:10:20 +01:00
|
|
|
|
2021-11-26 14:28:50 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-05-22 16:29:12 +02:00
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
|
2021-11-26 14:28:50 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2019-07-17 16:46:54 +02:00
|
|
|
)
|
|
|
|
|
2020-02-02 15:50:33 +01:00
|
|
|
func TestGetAlbum(t *testing.T) {
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
GetAlbum(router)
|
2020-05-01 19:58:05 +02:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/albums/at9lxuqxpogaaba8")
|
2020-05-23 20:58:58 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "Slug")
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, "holiday-2030", val.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-02-02 15:50:33 +01:00
|
|
|
})
|
2019-07-17 16:46:54 +02:00
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
GetAlbum(router)
|
2020-05-01 19:58:05 +02:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/albums/999000")
|
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
|
|
|
assert.Equal(t, "Album not found", val.String())
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateAlbum(t *testing.T) {
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateAlbum(router)
|
2020-05-26 09:02:19 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums", `{"Title": "New created album", "Notes": "", "Favorite": true}`)
|
2020-05-23 20:58:58 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "Slug")
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, "new-created-album", val.String())
|
2020-05-23 20:58:58 +02:00
|
|
|
val2 := gjson.Get(r.Body.String(), "Favorite")
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, "true", val2.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateAlbum(router)
|
2020-05-26 09:02:19 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums", `{"Title": 333, "Description": "Created via unit test", "Notes": "", "Favorite": true}`)
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
2020-02-02 15:50:33 +01:00
|
|
|
})
|
|
|
|
}
|
2020-05-01 19:58:05 +02:00
|
|
|
func TestUpdateAlbum(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateAlbum(router)
|
2020-05-26 09:02:19 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums", `{"Title": "Update", "Description": "To be updated", "Notes": "", "Favorite": true}`)
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-05-23 20:58:58 +02:00
|
|
|
uid := gjson.Get(r.Body.String(), "UID").String()
|
2020-05-01 19:58:05 +02:00
|
|
|
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdateAlbum(router)
|
2020-05-26 09:02:19 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/albums/"+uid, `{"Title": "Updated01", "Notes": "", "Favorite": false}`)
|
2020-05-23 20:58:58 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "Slug")
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, "updated01", val.String())
|
2020-05-23 20:58:58 +02:00
|
|
|
val2 := gjson.Get(r.Body.String(), "Favorite")
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, "false", val2.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdateAlbum(router)
|
2020-05-26 09:02:19 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/albums"+uid, `{"Title": 333, "Description": "Created via unit test", "Notes": "", "Favorite": true}`)
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
2019-07-17 16:46:54 +02:00
|
|
|
|
2020-05-01 19:58:05 +02:00
|
|
|
t.Run("not found", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdateAlbum(router)
|
2020-05-26 09:02:19 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/albums/xxx", `{"Title": "Update03", "Description": "Created via unit test", "Notes": "", "Favorite": true}`)
|
2020-05-01 19:58:05 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
|
|
|
assert.Equal(t, "Album not found", val.String())
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
}
|
2020-02-02 15:50:33 +01:00
|
|
|
func TestDeleteAlbum(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateAlbum(router)
|
2020-05-26 09:02:19 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums", `{"Title": "Delete", "Description": "To be deleted", "Notes": "", "Favorite": true}`)
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-05-23 20:58:58 +02:00
|
|
|
uid := gjson.Get(r.Body.String(), "UID").String()
|
2020-05-01 19:58:05 +02:00
|
|
|
|
2020-02-02 15:50:33 +01:00
|
|
|
t.Run("delete existing album", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
DeleteAlbum(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/albums/"+uid)
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-05-23 20:58:58 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "Slug")
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, "delete", val.String())
|
2021-09-18 15:32:39 +02:00
|
|
|
SearchAlbums(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r2 := PerformRequest(app, "GET", "/api/v1/albums/"+uid)
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r2.Code)
|
2020-02-02 15:50:33 +01:00
|
|
|
})
|
|
|
|
t.Run("delete not existing album", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
DeleteAlbum(router)
|
2020-05-01 19:58:05 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/albums/999000")
|
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
|
|
|
assert.Equal(t, "Album not found", val.String())
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2019-07-17 16:46:54 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLikeAlbum(t *testing.T) {
|
|
|
|
t.Run("like not existing album", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2019-07-17 16:46:54 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
LikeAlbum(router)
|
2019-07-17 16:46:54 +02:00
|
|
|
|
2020-05-01 19:58:05 +02:00
|
|
|
r := PerformRequest(app, "POST", "/api/v1/albums/xxx/like")
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2019-07-17 16:46:54 +02:00
|
|
|
})
|
2020-02-02 15:50:33 +01:00
|
|
|
t.Run("like existing album", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2019-07-17 16:46:54 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
LikeAlbum(router)
|
2020-05-01 19:58:05 +02:00
|
|
|
r := PerformRequest(app, "POST", "/api/v1/albums/at9lxuqxpogaaba7/like")
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-06-25 14:54:04 +02:00
|
|
|
GetAlbum(router)
|
2020-05-01 19:58:05 +02:00
|
|
|
r2 := PerformRequest(app, "GET", "/api/v1/albums/at9lxuqxpogaaba7")
|
2020-05-23 20:58:58 +02:00
|
|
|
val := gjson.Get(r2.Body.String(), "Favorite")
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, "true", val.String())
|
2020-02-02 15:50:33 +01:00
|
|
|
})
|
2019-07-17 16:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDislikeAlbum(t *testing.T) {
|
|
|
|
t.Run("dislike not existing album", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2019-07-17 16:46:54 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
DislikeAlbum(router)
|
2019-07-17 16:46:54 +02:00
|
|
|
|
2020-05-01 19:58:05 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/albums/5678/like")
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2020-02-02 15:50:33 +01:00
|
|
|
})
|
|
|
|
t.Run("dislike existing album", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2020-02-02 15:50:33 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
DislikeAlbum(router)
|
2020-02-02 15:50:33 +01:00
|
|
|
|
2020-05-01 19:58:05 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/albums/at9lxuqxpogaaba8/like")
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-06-25 14:54:04 +02:00
|
|
|
GetAlbum(router)
|
2020-05-01 19:58:05 +02:00
|
|
|
r2 := PerformRequest(app, "GET", "/api/v1/albums/at9lxuqxpogaaba8")
|
2020-05-23 20:58:58 +02:00
|
|
|
val := gjson.Get(r2.Body.String(), "Favorite")
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, "false", val.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddPhotosToAlbum(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateAlbum(router)
|
2020-05-26 09:02:19 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums", `{"Title": "Add photos", "Description": "", "Notes": "", "Favorite": true}`)
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-05-23 20:58:58 +02:00
|
|
|
uid := gjson.Get(r.Body.String(), "UID").String()
|
2020-05-01 19:58:05 +02:00
|
|
|
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
AddPhotosToAlbum(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums/"+uid+"/photos", `{"photos": ["pt9jtdre2lvl0y12", "pt9jtdre2lvl0y11"]}`)
|
2020-05-01 19:58:05 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "message")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.MsgChangesSaved), val.String())
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
2020-05-03 15:55:38 +02:00
|
|
|
t.Run("add one photo to album", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
AddPhotosToAlbum(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums/"+uid+"/photos", `{"photos": ["pt9jtdre2lvl0y12"]}`)
|
2020-05-03 15:55:38 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "message")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.MsgChangesSaved), val.String())
|
2020-05-03 15:55:38 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
2020-05-01 19:58:05 +02:00
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
AddPhotosToAlbum(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums/"+uid+"/photos", `{"photos": [123, "pt9jtdre2lvl0yxx"]}`)
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
AddPhotosToAlbum(router)
|
2020-05-01 19:58:05 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums/xxx/photos", `{"photos": ["pt9jtdre2lvl0yxx"]}`)
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemovePhotosFromAlbum(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2021-04-19 12:24:14 +02:00
|
|
|
|
|
|
|
// Register routes.
|
2020-06-25 14:54:04 +02:00
|
|
|
CreateAlbum(router)
|
2021-04-19 12:24:14 +02:00
|
|
|
AddPhotosToAlbum(router)
|
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums", `{"Title": "Remove photos", "Description": "", "Notes": "", "Favorite": true}`)
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-05-23 20:58:58 +02:00
|
|
|
uid := gjson.Get(r.Body.String(), "UID").String()
|
2021-04-19 12:24:14 +02:00
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
r2 := PerformRequestWithBody(app, "POST", "/api/v1/albums/"+uid+"/photos", `{"photos": ["pt9jtdre2lvl0y12", "pt9jtdre2lvl0y11"]}`)
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r2.Code)
|
|
|
|
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
RemovePhotosFromAlbum(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "DELETE", "/api/v1/albums/"+uid+"/photos", `{"photos": ["pt9jtdre2lvl0y12", "pt9jtdre2lvl0y11"]}`)
|
2020-05-01 19:58:05 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "message")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.MsgChangesSaved), val.String())
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
2020-06-24 12:01:25 +02:00
|
|
|
t.Run("no items selected", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
RemovePhotosFromAlbum(router)
|
2020-05-01 19:58:05 +02:00
|
|
|
r := PerformRequestWithBody(app, "DELETE", "/api/v1/albums/at9lxuqxpogaaba7/photos", `{"photos": []}`)
|
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2020-06-24 12:01:25 +02:00
|
|
|
assert.Equal(t, "No items selected", val.String())
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
RemovePhotosFromAlbum(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "DELETE", "/api/v1/albums/"+uid+"/photos", `{"photos": [123, "pt9jtdre2lvl0yxx"]}`)
|
2020-05-01 19:58:05 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("album not found", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
RemovePhotosFromAlbum(router)
|
2020-05-01 19:58:05 +02:00
|
|
|
r := PerformRequestWithBody(app, "DELETE", "/api/v1/albums/xxx/photos", `{"photos": ["pt9jtdre2lvl0yxx"]}`)
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2020-02-02 18:41:03 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-14 10:31:37 +02:00
|
|
|
func TestCloneAlbums(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateAlbum(router)
|
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums", `{"Title": "Update", "Description": "To be updated", "Notes": "", "Favorite": true}`)
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
uid := gjson.Get(r.Body.String(), "UID").String()
|
|
|
|
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CloneAlbums(router)
|
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums/"+uid+"/clone", `{"albums": ["`+uid+`"]}`)
|
|
|
|
val := gjson.Get(r.Body.String(), "message")
|
|
|
|
assert.Equal(t, "Album contents cloned", val.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CloneAlbums(router)
|
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums/123/clone", `{albums: ["123"]}`)
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad request", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CloneAlbums(router)
|
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/albums/"+uid+"/clone", `{albums: ["`+uid+`"]}`)
|
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2023-02-09 14:06:01 +01:00
|
|
|
assert.Equal(t, "Unable to do that", val.String())
|
2020-07-14 10:31:37 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
}
|