2021-06-02 17:25:04 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
|
2021-06-02 17:25:04 +02:00
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-08-21 16:36:00 +02:00
|
|
|
func TestUpdateMarker(t *testing.T) {
|
2021-06-02 17:25:04 +02:00
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
|
|
|
|
GetPhoto(router)
|
2021-08-21 16:36:00 +02:00
|
|
|
UpdateMarker(router)
|
2021-06-02 17:25:04 +02:00
|
|
|
|
|
|
|
r := PerformRequest(app, "GET", "/api/v1/photos/pt9jtdre2lvl0y11")
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
photoUid := gjson.Get(r.Body.String(), "UID").String()
|
|
|
|
fileUid := gjson.Get(r.Body.String(), "Files.0.UID").String()
|
|
|
|
markerUid := gjson.Get(r.Body.String(), "Files.0.Markers.0.UID").String()
|
2021-06-02 17:25:04 +02:00
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
assert.NotEmpty(t, photoUid)
|
|
|
|
assert.NotEmpty(t, fileUid)
|
|
|
|
assert.NotEmpty(t, markerUid)
|
2021-06-02 17:25:04 +02:00
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
u := fmt.Sprintf("/api/v1/markers/%s", markerUid)
|
2021-06-02 17:25:04 +02:00
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
var m = form.Marker{
|
2021-09-17 14:26:12 +02:00
|
|
|
SubjSrc: "manual",
|
2021-06-02 17:25:04 +02:00
|
|
|
MarkerInvalid: true,
|
2021-09-01 20:46:15 +02:00
|
|
|
MarkerName: "Foo",
|
2021-06-02 17:25:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if b, err := json.Marshal(m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
t.Logf("PUT %s", u)
|
|
|
|
r = PerformRequestWithBody(app, "PUT", u, string(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
2021-08-31 14:59:53 +02:00
|
|
|
t.Run("bad request non- primary file", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
|
|
|
|
UpdateMarker(router)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/markers/mt9k3pw1wowu1000", "test")
|
2021-08-31 14:59:53 +02:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("bad request file and photouid not matching", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
|
|
|
|
UpdateMarker(router)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/markers/mt9k3pw1wowu1000", "test")
|
2021-08-31 14:59:53 +02:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("file not existing", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
|
|
|
|
UpdateMarker(router)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/markers/1112", "test")
|
2021-08-31 14:59:53 +02:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("marker not existing", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
|
|
|
|
UpdateMarker(router)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/markers/1112", "test")
|
2021-08-31 14:59:53 +02:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("empty photouid", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
|
|
|
|
UpdateMarker(router)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/markers/mt9k3pw1wowu1000", "test")
|
2021-08-31 14:59:53 +02:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("update cluster with existing subject", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
|
|
|
|
UpdateMarker(router)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
var m = form.Marker{
|
2021-09-17 14:26:12 +02:00
|
|
|
SubjSrc: "manual",
|
2021-09-01 20:46:15 +02:00
|
|
|
MarkerInvalid: false,
|
|
|
|
MarkerName: "Actress A",
|
|
|
|
}
|
|
|
|
|
2021-08-31 14:59:53 +02:00
|
|
|
if b, err := json.Marshal(m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
2021-09-01 20:46:15 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/markers/mt9k3pw1wowuy666", string(b))
|
2021-08-31 14:59:53 +02:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
|
|
|
|
ClearMarkerSubject(router)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
r = PerformRequestWithBody(app, "DELETE", "/api/v1/markers/mt9k3pw1wowuy666/subject", "")
|
2021-08-31 14:59:53 +02:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
}
|
|
|
|
})
|
2021-09-01 20:46:15 +02:00
|
|
|
t.Run("update cluster with existing subject 2", func(t *testing.T) {
|
2021-08-31 14:59:53 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
|
|
|
|
UpdateMarker(router)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
var m = form.Marker{
|
2021-09-17 14:26:12 +02:00
|
|
|
SubjSrc: "manual",
|
2021-09-01 20:46:15 +02:00
|
|
|
MarkerInvalid: false,
|
|
|
|
MarkerName: "Actress A",
|
|
|
|
}
|
|
|
|
|
2021-08-31 14:59:53 +02:00
|
|
|
if b, err := json.Marshal(m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
2021-09-01 20:46:15 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/markers/mt9k3pw1wowuy666", string(b))
|
2021-08-31 14:59:53 +02:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
|
|
|
|
ClearMarkerSubject(router)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
r = PerformRequestWithBody(app, "DELETE", "/api/v1/markers/mt9k3pw1wowuy666/subject", "")
|
2021-08-31 14:59:53 +02:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("invalid body", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
|
|
|
|
UpdateMarker(router)
|
|
|
|
|
|
|
|
var m = struct {
|
2021-09-17 14:26:12 +02:00
|
|
|
ID int
|
|
|
|
Type string
|
|
|
|
Src int
|
|
|
|
Name int
|
|
|
|
SubjUID string
|
|
|
|
SubjSrc string
|
|
|
|
FaceID string
|
2021-08-31 14:59:53 +02:00
|
|
|
}{ID: 8,
|
2021-09-17 14:26:12 +02:00
|
|
|
Type: "face",
|
|
|
|
Src: 123,
|
|
|
|
Name: 456,
|
|
|
|
SubjUID: "jqy1y111h1njaaac",
|
|
|
|
SubjSrc: "manual",
|
|
|
|
FaceID: "GMH5NISEEULNJL6RATITOA3TMZXMTMCI"}
|
2021-08-31 14:59:53 +02:00
|
|
|
if b, err := json.Marshal(m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
2021-09-01 20:46:15 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/markers/mt9k3pw1wowuy666", string(b))
|
2021-08-31 14:59:53 +02:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
}
|
|
|
|
})
|
2021-06-02 17:25:04 +02:00
|
|
|
}
|
2021-08-21 16:36:00 +02:00
|
|
|
|
|
|
|
func TestClearMarkerSubject(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
|
|
|
|
GetPhoto(router)
|
|
|
|
ClearMarkerSubject(router)
|
|
|
|
|
|
|
|
photoResp := PerformRequest(app, "GET", "/api/v1/photos/pt9jtdre2lvl0y11")
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
if photoResp == nil {
|
|
|
|
t.Fatal("response is nil")
|
|
|
|
}
|
|
|
|
|
2021-08-21 16:36:00 +02:00
|
|
|
assert.Equal(t, http.StatusOK, photoResp.Code)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
if photoResp.Body.String() == "" {
|
|
|
|
t.Fatal("body is empty")
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
photoUid := gjson.Get(photoResp.Body.String(), "UID").String()
|
|
|
|
fileUid := gjson.Get(photoResp.Body.String(), "Files.0.UID").String()
|
|
|
|
markerUid := gjson.Get(photoResp.Body.String(), "Files.0.Markers.0.UID").String()
|
2021-08-21 16:36:00 +02:00
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
assert.NotEmpty(t, photoUid)
|
|
|
|
assert.NotEmpty(t, fileUid)
|
|
|
|
assert.NotEmpty(t, markerUid)
|
2021-08-21 16:36:00 +02:00
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
u := fmt.Sprintf("/api/v1/markers/%s/subject", markerUid)
|
2021-08-21 16:36:00 +02:00
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
// t.Logf("DELETE %s", u)
|
2021-08-21 16:36:00 +02:00
|
|
|
|
|
|
|
resp := PerformRequestWithBody(app, "DELETE", u, "")
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
})
|
2021-11-09 18:02:44 +01:00
|
|
|
t.Run("non-primary file", func(t *testing.T) {
|
2021-08-31 14:59:53 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
|
|
|
|
ClearMarkerSubject(router)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
r := PerformRequestWithBody(app, "DELETE", "/api/v1/markers/mt9k3pw1wowu1000/subject", "")
|
2021-08-31 14:59:53 +02:00
|
|
|
|
2021-11-09 18:02:44 +01:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2021-08-31 14:59:53 +02:00
|
|
|
})
|
2021-08-21 16:36:00 +02:00
|
|
|
}
|