photoprism/internal/api/album_test.go

302 lines
11 KiB
Go
Raw Normal View History

2019-07-17 16:46:54 +02:00
package api
import (
"net/http"
"testing"
"github.com/photoprism/photoprism/internal/i18n"
"github.com/tidwall/gjson"
"github.com/stretchr/testify/assert"
2019-07-17 16:46:54 +02:00
)
func TestGetAlbums(t *testing.T) {
t.Run("successful request", func(t *testing.T) {
app, router, _ := NewApiTest()
GetAlbums(router)
2020-05-01 19:58:05 +02:00
r := PerformRequest(app, "GET", "/api/v1/albums?count=10")
count := gjson.Get(r.Body.String(), "#")
assert.LessOrEqual(t, int64(3), count.Int())
2020-05-01 19:58:05 +02:00
assert.Equal(t, http.StatusOK, r.Code)
2019-07-17 16:46:54 +02:00
})
t.Run("invalid request", func(t *testing.T) {
app, router, _ := NewApiTest()
GetAlbums(router)
2020-05-01 19:58:05 +02:00
r := PerformRequest(app, "GET", "/api/v1/albums?xxx=10")
assert.Equal(t, http.StatusBadRequest, r.Code)
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) {
app, router, _ := NewApiTest()
GetAlbum(router)
2020-05-01 19:58:05 +02:00
r := PerformRequest(app, "GET", "/api/v1/albums/at9lxuqxpogaaba8")
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) {
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) {
app, router, _ := NewApiTest()
CreateAlbum(router)
r := PerformRequestWithBody(app, "POST", "/api/v1/albums", `{"Title": "New created album", "Notes": "", "Favorite": true}`)
val := gjson.Get(r.Body.String(), "Slug")
2020-05-01 19:58:05 +02:00
assert.Equal(t, "new-created-album", val.String())
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) {
app, router, _ := NewApiTest()
CreateAlbum(router)
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) {
app, router, _ := NewApiTest()
CreateAlbum(router)
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)
uid := gjson.Get(r.Body.String(), "UID").String()
2020-05-01 19:58:05 +02:00
t.Run("successful request", func(t *testing.T) {
app, router, _ := NewApiTest()
UpdateAlbum(router)
r := PerformRequestWithBody(app, "PUT", "/api/v1/albums/"+uid, `{"Title": "Updated01", "Notes": "", "Favorite": false}`)
val := gjson.Get(r.Body.String(), "Slug")
2020-05-01 19:58:05 +02:00
assert.Equal(t, "updated01", val.String())
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) {
app, router, _ := NewApiTest()
UpdateAlbum(router)
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) {
app, router, _ := NewApiTest()
UpdateAlbum(router)
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) {
app, router, _ := NewApiTest()
CreateAlbum(router)
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)
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) {
app, router, _ := NewApiTest()
DeleteAlbum(router)
r := PerformRequest(app, "DELETE", "/api/v1/albums/"+uid)
2020-05-01 19:58:05 +02:00
assert.Equal(t, http.StatusOK, r.Code)
val := gjson.Get(r.Body.String(), "Slug")
2020-05-01 19:58:05 +02:00
assert.Equal(t, "delete", val.String())
GetAlbums(router)
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) {
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) {
app, router, _ := NewApiTest()
2019-07-17 16:46:54 +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) {
app, router, _ := NewApiTest()
2019-07-17 16:46:54 +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)
GetAlbum(router)
2020-05-01 19:58:05 +02:00
r2 := PerformRequest(app, "GET", "/api/v1/albums/at9lxuqxpogaaba7")
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) {
app, router, _ := NewApiTest()
2019-07-17 16:46:54 +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) {
app, router, _ := NewApiTest()
2020-02-02 15:50:33 +01: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)
GetAlbum(router)
2020-05-01 19:58:05 +02:00
r2 := PerformRequest(app, "GET", "/api/v1/albums/at9lxuqxpogaaba8")
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) {
app, router, _ := NewApiTest()
CreateAlbum(router)
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)
uid := gjson.Get(r.Body.String(), "UID").String()
2020-05-01 19:58:05 +02:00
t.Run("successful request", func(t *testing.T) {
app, router, _ := NewApiTest()
AddPhotosToAlbum(router)
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")
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) {
app, router, _ := NewApiTest()
AddPhotosToAlbum(router)
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")
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) {
app, router, _ := NewApiTest()
AddPhotosToAlbum(router)
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) {
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) {
app, router, _ := NewApiTest()
CreateAlbum(router)
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)
uid := gjson.Get(r.Body.String(), "UID").String()
AddPhotosToAlbum(router)
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) {
app, router, _ := NewApiTest()
RemovePhotosFromAlbum(router)
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")
assert.Equal(t, i18n.Msg(i18n.MsgChangesSaved), val.String())
2020-05-01 19:58:05 +02:00
assert.Equal(t, http.StatusOK, r.Code)
})
t.Run("no items selected", func(t *testing.T) {
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")
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) {
app, router, _ := NewApiTest()
RemovePhotosFromAlbum(router)
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) {
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
})
}
func TestDownloadAlbum(t *testing.T) {
t.Run("download not existing album", func(t *testing.T) {
app, router, conf := NewApiTest()
DownloadAlbum(router)
2020-02-02 18:41:03 +01:00
r := PerformRequest(app, "GET", "/api/v1/albums/5678/dl?t="+conf.DownloadToken())
2020-05-01 19:58:05 +02:00
assert.Equal(t, http.StatusNotFound, r.Code)
2019-07-17 16:46:54 +02:00
})
2020-02-02 18:41:03 +01:00
t.Run("download existing album", func(t *testing.T) {
app, router, conf := NewApiTest()
DownloadAlbum(router)
2020-02-02 18:41:03 +01:00
r := PerformRequest(app, "GET", "/api/v1/albums/at9lxuqxpogaaba8/dl?t="+conf.DownloadToken())
2020-05-01 19:58:05 +02:00
assert.Equal(t, http.StatusOK, r.Code)
2020-02-02 18:41:03 +01:00
})
2019-07-17 16:46:54 +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")
assert.Equal(t, "Invalid request", val.String())
assert.Equal(t, http.StatusBadRequest, r.Code)
})
}