2020-04-08 13:24:06 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-05-04 17:14:04 +02:00
|
|
|
"github.com/tidwall/gjson"
|
2020-04-08 13:24:06 +02:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLinkAlbum(t *testing.T) {
|
|
|
|
t.Run("create share link", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
var link entity.Link
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
CreateAlbumLink(router)
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-06-27 13:08:45 +02:00
|
|
|
resp := PerformRequestWithBody(app, "POST", "/api/v1/albums/at9lxuqxpogaaba7/links", `{"Password": "foobar", "Expires": 0, "CanEdit": true}`)
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
if resp.Code != http.StatusOK {
|
|
|
|
t.Fatal(resp.Body.String())
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
if err := json.Unmarshal(resp.Body.Bytes(), &link); err != nil {
|
|
|
|
t.Fatal(err)
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
assert.NotEmpty(t, link.LinkUID)
|
|
|
|
assert.NotEmpty(t, link.ShareUID)
|
2020-06-28 14:52:26 +02:00
|
|
|
assert.NotEmpty(t, link.LinkToken)
|
2020-05-23 20:58:58 +02:00
|
|
|
assert.Equal(t, true, link.CanEdit)
|
2020-06-28 14:52:26 +02:00
|
|
|
assert.Equal(t, 0, link.LinkExpires)
|
2020-04-08 13:24:06 +02:00
|
|
|
assert.False(t, link.CanComment)
|
|
|
|
assert.True(t, link.CanEdit)
|
2020-06-22 15:16:26 +02:00
|
|
|
})
|
|
|
|
t.Run("album does not exist", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateAlbumLink(router)
|
2020-06-27 13:08:45 +02:00
|
|
|
resp := PerformRequestWithBody(app, "POST", "/api/v1/albums/xxx/links", `{"Password": "foobar", "Expires": 0, "CanEdit": true}`)
|
2020-04-08 13:24:06 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
if resp.Code != http.StatusNotFound {
|
|
|
|
t.Fatal(resp.Body.String())
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
val := gjson.Get(resp.Body.String(), "error")
|
2020-05-04 17:14:04 +02:00
|
|
|
assert.Equal(t, "Album not found", val.String())
|
|
|
|
})
|
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2020-06-22 15:16:26 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
CreateAlbumLink(router)
|
2020-06-22 15:16:26 +02:00
|
|
|
|
2020-06-27 13:08:45 +02:00
|
|
|
resp := PerformRequestWithBody(app, "POST", "/api/v1/albums/at9lxuqxpogaaba7/links", `{"Password": "foobar", "Expires": "abc", "CanEdit": true}`)
|
2020-06-22 15:16:26 +02:00
|
|
|
|
|
|
|
if resp.Code != http.StatusBadRequest {
|
|
|
|
t.Fatal(resp.Body.String())
|
|
|
|
}
|
2020-05-04 17:14:04 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkPhoto(t *testing.T) {
|
|
|
|
t.Run("create share link", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2020-05-04 17:14:04 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
var link entity.Link
|
2020-05-04 17:14:04 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
CreatePhotoLink(router)
|
2020-05-04 17:14:04 +02:00
|
|
|
|
2020-06-27 13:08:45 +02:00
|
|
|
resp := PerformRequestWithBody(app, "POST", "/api/v1/photos/pt9jtdre2lvl0yh7/links", `{"Password": "foobar", "Expires": 0, "CanEdit": true}`)
|
2020-06-22 15:16:26 +02:00
|
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
2020-05-04 17:14:04 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
if err := json.Unmarshal(resp.Body.Bytes(), &link); err != nil {
|
2020-05-04 17:14:04 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
assert.NotEmpty(t, link.LinkUID)
|
|
|
|
assert.NotEmpty(t, link.ShareUID)
|
2020-06-28 14:52:26 +02:00
|
|
|
assert.NotEmpty(t, link.LinkToken)
|
|
|
|
assert.Equal(t, 0, link.LinkExpires)
|
2020-05-04 17:14:04 +02:00
|
|
|
assert.False(t, link.CanComment)
|
|
|
|
assert.True(t, link.CanEdit)
|
2020-06-22 15:16:26 +02:00
|
|
|
})
|
|
|
|
t.Run("photo not found", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2020-05-04 17:14:04 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
CreatePhotoLink(router)
|
2020-05-04 17:14:04 +02:00
|
|
|
|
2020-06-27 13:08:45 +02:00
|
|
|
resp := PerformRequestWithBody(app, "POST", "/api/v1/photos/xxx/link", `{"Password": "foobar", "Expires": 0, "CanEdit": true}`)
|
2020-05-04 17:14:04 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
if resp.Code != http.StatusNotFound {
|
|
|
|
t.Fatal(resp.Body.String())
|
2020-05-04 17:14:04 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreatePhotoLink(router)
|
2020-06-27 13:08:45 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/photos/pt9jtdre2lvl0yh7/links", `{"xxx": 123, "Expires": "abc", "CanEdit": "xxx"}`)
|
2020-05-04 17:14:04 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkLabel(t *testing.T) {
|
|
|
|
t.Run("create share link", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2020-05-04 17:14:04 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
var link entity.Link
|
2020-05-04 17:14:04 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
CreateLabelLink(router)
|
2020-05-04 17:14:04 +02:00
|
|
|
|
2020-06-27 13:08:45 +02:00
|
|
|
resp := PerformRequestWithBody(app, "POST", "/api/v1/labels/lt9k3pw1wowuy3c2/links", `{"Password": "foobar", "Expires": 0, "CanEdit": true}`)
|
2020-06-22 15:16:26 +02:00
|
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
2020-05-04 17:14:04 +02:00
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
if err := json.Unmarshal(resp.Body.Bytes(), &link); err != nil {
|
2020-05-04 17:14:04 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:16:26 +02:00
|
|
|
assert.NotEmpty(t, link.LinkUID)
|
|
|
|
assert.NotEmpty(t, link.ShareUID)
|
2020-06-28 14:52:26 +02:00
|
|
|
assert.NotEmpty(t, link.LinkToken)
|
|
|
|
assert.Equal(t, 0, link.LinkExpires)
|
2020-05-04 17:14:04 +02:00
|
|
|
assert.False(t, link.CanComment)
|
|
|
|
assert.True(t, link.CanEdit)
|
|
|
|
})
|
|
|
|
t.Run("label not found", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateLabelLink(router)
|
2020-06-27 13:08:45 +02:00
|
|
|
resp := PerformRequestWithBody(app, "POST", "/api/v1/labels/xxx/links", `{"Password": "foobar", "Expires": 0, "CanEdit": true}`)
|
2020-06-22 15:16:26 +02:00
|
|
|
|
|
|
|
if resp.Code != http.StatusNotFound {
|
|
|
|
t.Fatal(resp.Body.String())
|
|
|
|
}
|
2020-05-04 17:14:04 +02:00
|
|
|
})
|
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateLabelLink(router)
|
2020-06-27 13:08:45 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/labels/lt9k3pw1wowuy3c2/links", `{"xxx": 123, "Expires": "abc", "CanEdit": "xxx"}`)
|
2020-05-04 17:14:04 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
2020-04-08 13:24:06 +02:00
|
|
|
}
|