API: Improve form and tests for POST /batch/photos/edit endpoint #271

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2025-07-08 10:40:45 +02:00
parent 004feeb708
commit 2481de49c4
6 changed files with 77 additions and 20 deletions

View File

@@ -0,0 +1,8 @@
package api
import (
"net/http"
)
// MethodsPutPost defines a string slice that contains the PUT and POST request methods.
var MethodsPutPost = []string{http.MethodPut, http.MethodPost}

View File

@@ -26,7 +26,7 @@ import (
// @Param photos body batch.PhotosRequest true "photos selection and values"
// @Router /api/v1/batch/photos/edit [post]
func BatchPhotosEdit(router *gin.RouterGroup) {
router.POST("/batch/photos/edit", func(c *gin.Context) {
router.Match(MethodsPutPost, "/batch/photos/edit", func(c *gin.Context) {
s := Auth(c, acl.ResourcePhotos, acl.ActionUpdate)
if s.Abort(c) {

View File

@@ -14,28 +14,51 @@ import (
)
func TestBatchPhotosEdit(t *testing.T) {
t.Run("ReturnValues", func(t *testing.T) {
t.Run("Success", func(t *testing.T) {
// Create new API test instance.
app, router, _ := NewApiTest()
// Attach POST /api/v1/batch/photos/edit request handler.
BatchPhotosEdit(router)
response := PerformRequestWithBody(app,
// Specify the unique IDs of the photos used for testing.
photoUIDs := `["ps6sg6be2lvl0yh7", "ps6sg6be2lvl0yh8"]`
// Get the photo models and current values for the batch edit form.
editResponse := PerformRequestWithBody(app,
"POST", "/api/v1/batch/photos/edit",
`{"photos": ["ps6sg6be2lvl0yh7", "ps6sg6be2lvl0yh8"]}`,
fmt.Sprintf(`{"photos": %s}`, photoUIDs),
)
body := response.Body.String()
// Check the edit response status code.
assert.Equal(t, http.StatusOK, editResponse.Code)
assert.NotEmpty(t, body)
assert.True(t, strings.HasPrefix(body, `{"models":[{"ID"`), "unexpected response")
// Check the edit response body.
editBody := editResponse.Body.String()
assert.NotEmpty(t, editBody)
assert.True(t, strings.HasPrefix(editBody, `{"models":[{"ID"`), "unexpected response")
// fmt.Println(body)
/* models := gjson.Get(body, "models")
values := gjson.Get(body, "values")
t.Logf("models: %#v", models)
t.Logf("values: %#v", values) */
// Check the edit response values.
editValues := gjson.Get(editBody, "values").Raw
t.Logf("edit values: %#v", editValues)
assert.Equal(t, http.StatusOK, response.Code)
// Send the edit form values back to the same API endpoint and check for errors.
saveResponse := PerformRequestWithBody(app,
"POST", "/api/v1/batch/photos/edit",
fmt.Sprintf(`{"photos": %s, "values": %s}`, photoUIDs, editValues),
)
// Check the save response status code.
assert.Equal(t, http.StatusOK, saveResponse.Code)
// Check the save response body.
saveBody := saveResponse.Body.String()
assert.NotEmpty(t, saveBody)
// Check the save response values.
saveValues := gjson.Get(saveBody, "values").Raw
t.Logf("save values: %#v", saveValues)
assert.Equal(t, editValues, saveValues)
})
t.Run("ReturnPhotosAndValues", func(t *testing.T) {
app, router, conf := NewApiTest()
@@ -43,6 +66,7 @@ func TestBatchPhotosEdit(t *testing.T) {
defer conf.SetAuthMode(config.AuthModePublic)
authToken := AuthenticateUser(app, router, "alice", "Alice123!")
// Attach POST /api/v1/batch/photos/edit request handler.
BatchPhotosEdit(router)
response := AuthenticatedRequestWithBody(app, http.MethodPost, "/api/v1/batch/photos/edit",
@@ -64,7 +88,10 @@ func TestBatchPhotosEdit(t *testing.T) {
})
t.Run("MissingSelection", func(t *testing.T) {
app, router, _ := NewApiTest()
// Attach POST /api/v1/batch/photos/edit request handler.
BatchPhotosEdit(router)
r := PerformRequestWithBody(app, "POST", "/api/v1/batch/photos/edit", `{"photos": [], "return": true}`)
val := gjson.Get(r.Body.String(), "error")
assert.Equal(t, i18n.Msg(i18n.ErrNoItemsSelected), val.String())
@@ -72,7 +99,10 @@ func TestBatchPhotosEdit(t *testing.T) {
})
t.Run("InvalidRequest", func(t *testing.T) {
app, router, _ := NewApiTest()
// Attach POST /api/v1/batch/photos/edit request handler.
BatchPhotosEdit(router)
r := PerformRequestWithBody(app, "POST", "/api/v1/batch/photos/edit", `{"photos": 123, "return": true}`)
assert.Equal(t, http.StatusBadRequest, r.Code)
})
@@ -82,6 +112,7 @@ func TestBatchPhotosEdit(t *testing.T) {
conf.SetAuthMode(config.AuthModePasswd)
defer conf.SetAuthMode(config.AuthModePublic)
// Attach POST /api/v1/batch/photos/edit request handler.
BatchPhotosEdit(router)
sessId := AuthenticateUser(app, router, "alice", "Alice123!")
@@ -105,6 +136,7 @@ func TestBatchPhotosEdit(t *testing.T) {
conf.SetAuthMode(config.AuthModePasswd)
defer conf.SetAuthMode(config.AuthModePublic)
// Attach POST /api/v1/batch/photos/edit request handler.
BatchPhotosEdit(router)
sessId := AuthenticateUser(app, router, "gandalf", "Gandalf123!")

View File

@@ -8187,6 +8187,14 @@
1000000,
1000000000,
60000000000,
3600000000000,
-9223372036854775808,
9223372036854775807,
1,
1000,
1000000,
1000000000,
60000000000,
3600000000000
],
"x-enum-varnames": [
@@ -8205,6 +8213,14 @@
"Millisecond",
"Second",
"Minute",
"Hour",
"minDuration",
"maxDuration",
"Nanosecond",
"Microsecond",
"Millisecond",
"Second",
"Minute",
"Hour"
]
},

View File

@@ -37,6 +37,7 @@ type PhotosForm struct {
DetailsLicense String `json:"DetailsLicense"`
}
// NewPhotosForm creates and returns a new PhotosForm instance.
func NewPhotosForm(photos search.PhotoResults) *PhotosForm {
frm := &PhotosForm{}

View File

@@ -7,48 +7,48 @@ import (
// String represents batch edit form value.
type String struct {
Value string `json:"value"`
Mixed bool `json:"mixed"`
Mixed bool `json:"mixed,omitempty"`
Action Action `json:"action"`
}
// Bool represents batch edit form value.
type Bool struct {
Value bool `json:"value"`
Mixed bool `json:"mixed"`
Mixed bool `json:"mixed,omitempty"`
Action Action `json:"action"`
}
// Time represents batch edit form value.
type Time struct {
Value time.Time `json:"value"`
Mixed bool `json:"mixed"`
Mixed bool `json:"mixed,omitempty"`
Action Action `json:"action"`
}
// Int represents batch edit form value.
type Int struct {
Value int `json:"value"`
Mixed bool `json:"mixed"`
Mixed bool `json:"mixed,omitempty"`
Action Action `json:"action"`
}
// UInt represents batch edit form value.
type UInt struct {
Value uint `json:"value"`
Mixed bool `json:"mixed"`
Mixed bool `json:"mixed,omitempty"`
Action Action `json:"action"`
}
// Float32 represents batch edit form value.
type Float32 struct {
Value float32 `json:"value"`
Mixed bool `json:"mixed"`
Mixed bool `json:"mixed,omitempty"`
Action Action `json:"action"`
}
// Float64 represents batch edit form value.
type Float64 struct {
Value float64 `json:"value"`
Mixed bool `json:"mixed"`
Mixed bool `json:"mixed,omitempty"`
Action Action `json:"action"`
}