photoprism/internal/api/link_test.go

143 lines
4.2 KiB
Go
Raw Normal View History

package api
import (
"encoding/json"
2020-05-04 15:14:04 +00:00
"github.com/tidwall/gjson"
"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) {
app, router, _ := NewApiTest()
var link entity.Link
CreateAlbumLink(router)
resp := PerformRequestWithBody(app, "POST", "/api/v1/albums/at9lxuqxpogaaba7/links", `{"Password": "foobar", "ShareExpires": 0, "CanEdit": true}`)
if resp.Code != http.StatusOK {
t.Fatal(resp.Body.String())
}
if err := json.Unmarshal(resp.Body.Bytes(), &link); err != nil {
t.Fatal(err)
}
assert.NotEmpty(t, link.LinkUID)
assert.NotEmpty(t, link.ShareUID)
assert.NotEmpty(t, link.ShareToken)
assert.Equal(t, true, link.CanEdit)
assert.Equal(t, 0, link.ShareExpires)
assert.False(t, link.CanComment)
assert.True(t, link.CanEdit)
})
t.Run("album does not exist", func(t *testing.T) {
app, router, _ := NewApiTest()
CreateAlbumLink(router)
resp := PerformRequestWithBody(app, "POST", "/api/v1/albums/xxx/links", `{"Password": "foobar", "ShareExpires": 0, "CanEdit": true}`)
if resp.Code != http.StatusNotFound {
t.Fatal(resp.Body.String())
}
val := gjson.Get(resp.Body.String(), "error")
2020-05-04 15:14:04 +00:00
assert.Equal(t, "Album not found", val.String())
})
t.Run("invalid request", func(t *testing.T) {
app, router, _ := NewApiTest()
CreateAlbumLink(router)
resp := PerformRequestWithBody(app, "POST", "/api/v1/albums/at9lxuqxpogaaba7/links", `{"Password": "foobar", "ShareExpires": "abc", "CanEdit": true}`)
if resp.Code != http.StatusBadRequest {
t.Fatal(resp.Body.String())
}
2020-05-04 15:14:04 +00:00
})
}
func TestLinkPhoto(t *testing.T) {
t.Run("create share link", func(t *testing.T) {
app, router, _ := NewApiTest()
2020-05-04 15:14:04 +00:00
var link entity.Link
2020-05-04 15:14:04 +00:00
CreatePhotoLink(router)
2020-05-04 15:14:04 +00:00
resp := PerformRequestWithBody(app, "POST", "/api/v1/photos/pt9jtdre2lvl0yh7/links", `{"Password": "foobar", "ShareExpires": 0, "CanEdit": true}`)
assert.Equal(t, http.StatusOK, resp.Code)
2020-05-04 15:14:04 +00:00
if err := json.Unmarshal(resp.Body.Bytes(), &link); err != nil {
2020-05-04 15:14:04 +00:00
t.Fatal(err)
}
assert.NotEmpty(t, link.LinkUID)
assert.NotEmpty(t, link.ShareUID)
assert.NotEmpty(t, link.ShareToken)
assert.Equal(t, 0, link.ShareExpires)
2020-05-04 15:14:04 +00:00
assert.False(t, link.CanComment)
assert.True(t, link.CanEdit)
})
t.Run("photo not found", func(t *testing.T) {
app, router, _ := NewApiTest()
2020-05-04 15:14:04 +00:00
CreatePhotoLink(router)
2020-05-04 15:14:04 +00:00
resp := PerformRequestWithBody(app, "POST", "/api/v1/photos/xxx/link", `{"Password": "foobar", "ShareExpires": 0, "CanEdit": true}`)
2020-05-04 15:14:04 +00:00
if resp.Code != http.StatusNotFound {
t.Fatal(resp.Body.String())
2020-05-04 15:14:04 +00:00
}
})
t.Run("invalid request", func(t *testing.T) {
app, router, _ := NewApiTest()
CreatePhotoLink(router)
r := PerformRequestWithBody(app, "POST", "/api/v1/photos/pt9jtdre2lvl0yh7/links", `{"xxx": 123, "ShareExpires": "abc", "CanEdit": "xxx"}`)
2020-05-04 15:14:04 +00:00
assert.Equal(t, http.StatusBadRequest, r.Code)
})
}
func TestLinkLabel(t *testing.T) {
t.Run("create share link", func(t *testing.T) {
app, router, _ := NewApiTest()
2020-05-04 15:14:04 +00:00
var link entity.Link
2020-05-04 15:14:04 +00:00
CreateLabelLink(router)
2020-05-04 15:14:04 +00:00
resp := PerformRequestWithBody(app, "POST", "/api/v1/labels/lt9k3pw1wowuy3c2/links", `{"Password": "foobar", "ShareExpires": 0, "CanEdit": true}`)
assert.Equal(t, http.StatusOK, resp.Code)
2020-05-04 15:14:04 +00:00
if err := json.Unmarshal(resp.Body.Bytes(), &link); err != nil {
2020-05-04 15:14:04 +00:00
t.Fatal(err)
}
assert.NotEmpty(t, link.LinkUID)
assert.NotEmpty(t, link.ShareUID)
assert.NotEmpty(t, link.ShareToken)
assert.Equal(t, 0, link.ShareExpires)
2020-05-04 15:14:04 +00:00
assert.False(t, link.CanComment)
assert.True(t, link.CanEdit)
})
t.Run("label not found", func(t *testing.T) {
app, router, _ := NewApiTest()
CreateLabelLink(router)
resp := PerformRequestWithBody(app, "POST", "/api/v1/labels/xxx/links", `{"Password": "foobar", "ShareExpires": 0, "CanEdit": true}`)
if resp.Code != http.StatusNotFound {
t.Fatal(resp.Body.String())
}
2020-05-04 15:14:04 +00:00
})
t.Run("invalid request", func(t *testing.T) {
app, router, _ := NewApiTest()
CreateLabelLink(router)
r := PerformRequestWithBody(app, "POST", "/api/v1/labels/lt9k3pw1wowuy3c2/links", `{"xxx": 123, "ShareExpires": "abc", "CanEdit": "xxx"}`)
2020-05-04 15:14:04 +00:00
assert.Equal(t, http.StatusBadRequest, r.Code)
})
}