2020-02-03 15:50:05 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2020-02-03 15:50:05 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-04-26 11:41:54 +02:00
|
|
|
"github.com/tidwall/gjson"
|
2020-02-03 15:50:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetPhoto(t *testing.T) {
|
|
|
|
t.Run("search for existing photo", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
GetPhoto(router)
|
2020-05-04 17:54:42 +02:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/photos/pt9jtdre2lvl0yh7")
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-05-23 20:58:58 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "Lat")
|
2020-04-26 11:41:54 +02:00
|
|
|
assert.Equal(t, "48.519234", val.String())
|
2020-02-03 15:50:05 +01:00
|
|
|
})
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2020-02-03 15:50:05 +01:00
|
|
|
t.Run("search for not existing photo", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
GetPhoto(router)
|
2020-05-04 17:54:42 +02:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/photos/xxx")
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdatePhoto(t *testing.T) {
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdatePhoto(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/photos/pt9jtdre2lvl0y13", `{"Title": "Updated01", "Country": "de"}`)
|
|
|
|
val := gjson.Get(r.Body.String(), "Title")
|
2020-05-04 17:54:42 +02:00
|
|
|
assert.Equal(t, "Updated01", val.String())
|
2020-05-23 20:58:58 +02:00
|
|
|
val2 := gjson.Get(r.Body.String(), "Country")
|
2020-05-04 17:54:42 +02:00
|
|
|
assert.Equal(t, "de", 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()
|
|
|
|
UpdatePhoto(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/photos/pt9jtdre2lvl0y13", `{"Name": "Updated01", "Country": 123}`)
|
2020-05-04 17:54:42 +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()
|
|
|
|
UpdatePhoto(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/photos/xxx", `{"Name": "Updated01", "Country": "de"}`)
|
2020-05-04 17:54:42 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.ErrEntityNotFound), val.String())
|
2020-05-04 17:54:42 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2020-02-03 15:50:05 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPhotoDownload(t *testing.T) {
|
|
|
|
t.Run("could not find original", func(t *testing.T) {
|
2020-05-27 19:38:40 +02:00
|
|
|
app, router, conf := NewApiTest()
|
2020-06-25 14:54:04 +02:00
|
|
|
GetPhotoDownload(router)
|
2020-05-27 19:38:40 +02:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/photos/pt9jtdre2lvl0yh7/dl?t="+conf.DownloadToken())
|
2020-05-04 17:54:42 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2020-02-03 15:50:05 +01:00
|
|
|
})
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2020-02-03 15:50:05 +01:00
|
|
|
t.Run("not existing photo", func(t *testing.T) {
|
2020-05-27 19:38:40 +02:00
|
|
|
app, router, conf := NewApiTest()
|
2020-06-25 14:54:04 +02:00
|
|
|
GetPhotoDownload(router)
|
2020-05-27 19:38:40 +02:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/photos/xxx/dl?t="+conf.DownloadToken())
|
2020-05-04 17:54:42 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2020-02-03 15:50:05 +01:00
|
|
|
})
|
2020-07-14 14:42:23 +02:00
|
|
|
|
|
|
|
t.Run("invalid token", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
GetPhotoDownload(router)
|
|
|
|
r := PerformRequest(app, "GET", "/api/v1/photos/pt9jtdre2lvl0yh7/dl?t=xxx")
|
|
|
|
assert.Equal(t, http.StatusForbidden, r.Code)
|
|
|
|
})
|
2020-02-03 15:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLikePhoto(t *testing.T) {
|
|
|
|
t.Run("existing photo", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
LikePhoto(router)
|
2020-05-04 17:54:42 +02:00
|
|
|
r := PerformRequest(app, "POST", "/api/v1/photos/pt9jtdre2lvl0yh9/like")
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-06-25 14:54:04 +02:00
|
|
|
GetPhoto(router)
|
2020-05-04 17:54:42 +02:00
|
|
|
r2 := PerformRequest(app, "GET", "/api/v1/photos/pt9jtdre2lvl0yh9")
|
2020-05-23 20:58:58 +02:00
|
|
|
val := gjson.Get(r2.Body.String(), "Favorite")
|
2020-05-04 17:54:42 +02:00
|
|
|
assert.Equal(t, "true", val.String())
|
2020-02-03 15:50:05 +01:00
|
|
|
})
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2020-02-03 15:50:05 +01:00
|
|
|
t.Run("not existing photo", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
LikePhoto(router)
|
2020-05-04 17:54:42 +02:00
|
|
|
r := PerformRequest(app, "POST", "/api/v1/photos/xxx/like")
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2020-02-03 15:50:05 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDislikePhoto(t *testing.T) {
|
|
|
|
t.Run("existing photo", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
DislikePhoto(router)
|
2020-05-04 17:54:42 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/photos/pt9jtdre2lvl0yh8/like")
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-06-25 14:54:04 +02:00
|
|
|
GetPhoto(router)
|
2020-05-04 17:54:42 +02:00
|
|
|
r2 := PerformRequest(app, "GET", "/api/v1/photos/pt9jtdre2lvl0yh8")
|
2020-05-23 20:58:58 +02:00
|
|
|
val := gjson.Get(r2.Body.String(), "Favorite")
|
2020-05-04 17:54:42 +02:00
|
|
|
assert.Equal(t, "false", val.String())
|
2020-02-03 15:50:05 +01:00
|
|
|
})
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2020-02-03 15:50:05 +01:00
|
|
|
t.Run("not existing photo", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
DislikePhoto(router)
|
2020-05-04 17:54:42 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/photos/xxx/like")
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-14 11:00:49 +02:00
|
|
|
func TestPhotoPrimary(t *testing.T) {
|
2020-05-04 17:54:42 +02:00
|
|
|
t.Run("existing photo", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2020-07-14 11:00:49 +02:00
|
|
|
PhotoPrimary(router)
|
2020-06-30 16:58:32 +02:00
|
|
|
r := PerformRequest(app, "POST", "/api/v1/photos/pt9jtdre2lvl0yh8/files/ft1es39w45bnlqdw/primary")
|
2020-05-04 17:54:42 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-06-25 14:54:04 +02:00
|
|
|
GetFile(router)
|
2020-05-04 17:54:42 +02:00
|
|
|
r2 := PerformRequest(app, "GET", "/api/v1/files/ocad9168fa6acc5c5c2965ddf6ec465ca42fd818")
|
2020-05-23 20:58:58 +02:00
|
|
|
val := gjson.Get(r2.Body.String(), "Primary")
|
2020-05-04 17:54:42 +02:00
|
|
|
assert.Equal(t, "true", val.String())
|
|
|
|
r3 := PerformRequest(app, "GET", "/api/v1/files/3cad9168fa6acc5c5c2965ddf6ec465ca42fd818")
|
2020-05-23 20:58:58 +02:00
|
|
|
val2 := gjson.Get(r3.Body.String(), "Primary")
|
2020-05-04 17:54:42 +02:00
|
|
|
assert.Equal(t, "false", val2.String())
|
|
|
|
})
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
t.Run("wrong photo uid", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2020-07-14 11:00:49 +02:00
|
|
|
PhotoPrimary(router)
|
2020-06-30 16:58:32 +02:00
|
|
|
r := PerformRequest(app, "POST", "/api/v1/photos/xxx/files/ft1es39w45bnlqdw/primary")
|
2020-05-04 17:54:42 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.ErrEntityNotFound), val.String())
|
2020-05-04 17:54:42 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2020-02-03 15:50:05 +01:00
|
|
|
})
|
|
|
|
}
|
2020-07-14 14:42:23 +02:00
|
|
|
|
|
|
|
func TestGetPhotoYaml(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
GetPhotoYaml(router)
|
|
|
|
r := PerformRequest(app, "GET", "/api/v1/photos/pt9jtdre2lvl0yh7/yaml")
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not existing photo", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
GetPhotoYaml(router)
|
|
|
|
r := PerformRequest(app, "GET", "/api/v1/photos/xxx/yaml")
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApprovePhoto(t *testing.T) {
|
|
|
|
t.Run("existing photo", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
GetPhoto(router)
|
|
|
|
r3 := PerformRequest(app, "GET", "/api/v1/photos/pt9jtxrexxvl0y20")
|
|
|
|
val2 := gjson.Get(r3.Body.String(), "Quality")
|
|
|
|
assert.Equal(t, "1", val2.String())
|
|
|
|
ApprovePhoto(router)
|
|
|
|
r := PerformRequest(app, "POST", "/api/v1/photos/pt9jtxrexxvl0y20/approve")
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
r2 := PerformRequest(app, "GET", "/api/v1/photos/pt9jtxrexxvl0y20")
|
|
|
|
val := gjson.Get(r2.Body.String(), "Quality")
|
|
|
|
assert.Equal(t, "3", val.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not existing photo", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
ApprovePhoto(router)
|
|
|
|
r := PerformRequest(app, "POST", "/api/v1/photos/xxx/approve")
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
}
|