2020-02-26 17:50:28 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-05-04 17:14:04 +02:00
|
|
|
"encoding/json"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-05-01 14:15:15 +02:00
|
|
|
"github.com/tidwall/gjson"
|
2020-02-26 17:50:28 +01:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetFile(t *testing.T) {
|
|
|
|
t.Run("search for existing file", func(t *testing.T) {
|
|
|
|
app, router, ctx := NewApiTest()
|
|
|
|
GetFile(router, ctx)
|
2020-05-04 14:40:58 +02:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/files/2cad9168fa6acc5c5c2965ddf6ec465ca42fd818")
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-05-01 14:15:15 +02:00
|
|
|
|
2020-05-04 14:40:58 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "FileName")
|
2020-05-01 14:15:15 +02:00
|
|
|
assert.Equal(t, "exampleFileName.jpg", val.String())
|
2020-02-26 17:50:28 +01:00
|
|
|
})
|
|
|
|
t.Run("search for not existing file", func(t *testing.T) {
|
|
|
|
app, router, ctx := NewApiTest()
|
|
|
|
GetFile(router, ctx)
|
2020-05-04 14:40:58 +02:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/files/111")
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2020-02-26 17:50:28 +01:00
|
|
|
})
|
|
|
|
}
|
2020-05-04 14:40:58 +02:00
|
|
|
|
|
|
|
func TestLinkFile(t *testing.T) {
|
2020-05-04 17:14:04 +02:00
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-05-04 14:40:58 +02:00
|
|
|
app, router, ctx := NewApiTest()
|
|
|
|
LinkFile(router, ctx)
|
2020-05-04 17:14:04 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/files/ft9es39w45bnlqdw/link", `{"password": "foobar123", "expires": 0, "edit": true}`)
|
|
|
|
|
|
|
|
var label entity.Label
|
|
|
|
|
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), &label); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(label.Links) != 1 {
|
|
|
|
t.Fatalf("one link expected: %d, %+v", len(label.Links), label)
|
|
|
|
}
|
|
|
|
|
|
|
|
link := label.Links[0]
|
|
|
|
|
|
|
|
assert.Equal(t, "foobar123", link.LinkPassword)
|
|
|
|
assert.Nil(t, link.LinkExpires)
|
|
|
|
assert.False(t, link.CanComment)
|
|
|
|
assert.True(t, link.CanEdit)
|
|
|
|
})
|
|
|
|
t.Run("file not found", func(t *testing.T) {
|
|
|
|
app, router, ctx := NewApiTest()
|
|
|
|
LinkFile(router, ctx)
|
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/files/xxx/link", `{"password": "foobar", "expires": 0, "edit": true}`)
|
2020-05-04 14:40:58 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2020-05-04 17:14:04 +02:00
|
|
|
assert.Equal(t, "File not found", val.String())
|
2020-05-04 14:40:58 +02:00
|
|
|
})
|
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
|
|
|
app, router, ctx := NewApiTest()
|
|
|
|
LinkFile(router, ctx)
|
2020-05-04 17:14:04 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/files/ft9es39w45bnlqdw/link", `{"xxx": 123, "expires": 0, "edit": "xxx"}`)
|
2020-05-04 14:40:58 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
}
|