2020-03-28 18:01:50 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-26 14:28:50 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-11-21 18:08:41 +01:00
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
|
2021-11-26 14:28:50 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2020-03-28 18:01:50 +01:00
|
|
|
)
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
func TestGetService(t *testing.T) {
|
|
|
|
t.Run("Ok", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
GetService(router)
|
|
|
|
r := PerformRequest(app, "GET", "/api/v1/services/1000000")
|
2020-05-01 16:38:00 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "AccName")
|
|
|
|
assert.Equal(t, "Test Account", val.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
2022-10-15 21:54:11 +02:00
|
|
|
t.Run("NotFound", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
GetService(router)
|
|
|
|
r := PerformRequest(app, "GET", "/api/v1/services/999000")
|
2020-05-01 16:38:00 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.ErrAccountNotFound), val.String())
|
2020-05-01 16:38:00 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
func TestGetServiceFolders(t *testing.T) {
|
|
|
|
t.Run("Ok", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
GetServiceFolders(router)
|
|
|
|
r := PerformRequest(app, "GET", "/api/v1/services/1000000/folders")
|
2020-05-22 16:29:12 +02:00
|
|
|
count := gjson.Get(r.Body.String(), "#")
|
|
|
|
assert.LessOrEqual(t, int64(2), count.Int())
|
2020-05-01 16:38:00 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "0.abs")
|
|
|
|
assert.Equal(t, "/Photos", val.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-03-28 18:01:50 +01:00
|
|
|
})
|
2022-10-15 21:54:11 +02:00
|
|
|
t.Run("NotFound", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
GetServiceFolders(router)
|
|
|
|
r := PerformRequest(app, "GET", "/api/v1/services/999000/folders")
|
2020-05-01 16:38:00 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.ErrAccountNotFound), val.String())
|
2020-05-01 16:38:00 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
func TestCreateService(t *testing.T) {
|
|
|
|
t.Run("BadRequest", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
AddService(router)
|
|
|
|
r := PerformRequest(app, "POST", "/api/v1/services")
|
2020-05-01 16:38:00 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.ErrBadRequest), val.String())
|
2020-05-01 16:38:00 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
2022-10-15 21:54:11 +02:00
|
|
|
t.Run("ConnectFailed", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
AddService(router)
|
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/services", `{"AccName": "CreateTest1", "AccOwner": "Test", "AccUrl": "http://webdav123/", "AccType": "webdav",
|
2020-05-01 16:38:00 +02:00
|
|
|
"AccKey": "123", "AccUser": "testuser", "AccPass": "testpasswd", "AccError": "", "AccShare": false, "AccSync": false, "RetryLimit": 3, "SharePath": "", "ShareSize": "", "ShareExpires": 0,
|
|
|
|
"SyncPath": "", "SyncInterval": 3, "SyncUpload": false, "SyncDownload": false, "SyncFilenames": false, "SyncRaw": false}`)
|
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.ErrConnectionFailed), val.String())
|
2020-05-01 16:38:00 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
2022-10-15 21:54:11 +02:00
|
|
|
t.Run("Ok", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
AddService(router)
|
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/services", `{"AccName": "CreateTest", "AccOwner": "Test", "AccUrl": "http://dummy-webdav/", "AccType": "webdav",
|
2020-05-01 16:38:00 +02:00
|
|
|
"AccKey": "123", "AccUser": "admin", "AccPass": "photoprism", "AccError": "", "AccShare": false, "AccSync": false, "RetryLimit": 3, "SharePath": "", "ShareSize": "", "ShareExpires": 0,
|
|
|
|
"SyncPath": "", "SyncInterval": 3, "SyncUpload": false, "SyncDownload": false, "SyncFilenames": false, "SyncRaw": false}`)
|
|
|
|
val := gjson.Get(r.Body.String(), "AccOwner")
|
|
|
|
assert.Equal(t, "Test", val.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
func TestUpdateService(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
AddService(router)
|
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/services", `{"AccName": "CreateTest3", "AccOwner": "TestUpdate", "AccUrl": "http://dummy-webdav/", "AccType": "webdav",
|
2020-05-01 16:38:00 +02:00
|
|
|
"AccKey": "123", "AccUser": "admin", "AccPass": "photoprism", "AccError": "", "AccShare": false, "AccSync": false, "RetryLimit": 3, "SharePath": "", "ShareSize": "", "ShareExpires": 0,
|
|
|
|
"SyncPath": "", "SyncInterval": 5, "SyncUpload": false, "SyncDownload": false, "SyncFilenames": false, "SyncRaw": false}`)
|
|
|
|
val := gjson.Get(r.Body.String(), "AccOwner")
|
|
|
|
assert.Equal(t, "TestUpdate", val.String())
|
|
|
|
val2 := gjson.Get(r.Body.String(), "SyncInterval")
|
|
|
|
assert.Equal(t, int64(5), val2.Int())
|
|
|
|
val3 := gjson.Get(r.Body.String(), "AccName")
|
2021-11-09 16:55:16 +01:00
|
|
|
assert.Equal(t, "Dummy-Webdav", val3.String())
|
2020-05-01 16:38:00 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
id := gjson.Get(r.Body.String(), "ID").String()
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
t.Run("Ok", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
UpdateService(router)
|
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/services/"+id, `{"AccName": "CreateTestUpdated", "AccOwner": "TestUpdated123", "SyncInterval": 9}`)
|
2020-05-01 16:38:00 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "AccOwner")
|
|
|
|
assert.Equal(t, "TestUpdated123", val.String())
|
|
|
|
val2 := gjson.Get(r.Body.String(), "SyncInterval")
|
|
|
|
assert.Equal(t, int64(9), val2.Int())
|
|
|
|
val3 := gjson.Get(r.Body.String(), "AccName")
|
|
|
|
assert.Equal(t, "CreateTestUpdated", val3.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
t.Run("NotFound", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
UpdateService(router)
|
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/services/xxx", `{"AccName": "CreateTestUpdated", "AccOwner": "TestUpdated123", "SyncInterval": 9}`)
|
2020-05-01 16:38:00 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.ErrAccountNotFound), val.String())
|
2020-05-01 16:38:00 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
t.Run("SaveFailed", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
UpdateService(router)
|
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/services/"+id, `{"AccName": 6, "AccOwner": "TestUpdated123", "SyncInterval": 9, "AccUrl": "https:xxx.com"}`)
|
2020-05-01 16:38:00 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.ErrBadRequest), val.String())
|
2020-05-01 16:38:00 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
func TestDeleteService(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
AddService(router)
|
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/services", `{"AccName": "DeleteTest", "AccOwner": "TestDelete", "AccUrl": "http://dummy-webdav/", "AccType": "webdav",
|
2020-05-01 16:38:00 +02:00
|
|
|
"AccKey": "123", "AccUser": "admin", "AccPass": "photoprism", "AccError": "", "AccShare": false, "AccSync": false, "RetryLimit": 3, "SharePath": "", "ShareSize": "", "ShareExpires": 0,
|
|
|
|
"SyncPath": "", "SyncInterval": 5, "SyncUpload": false, "SyncDownload": false, "SyncFilenames": false, "SyncRaw": false}`)
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
id := gjson.Get(r.Body.String(), "ID").String()
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
t.Run("Ok", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
DeleteService(router)
|
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/services/"+id)
|
2020-05-01 16:38:00 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "AccOwner")
|
|
|
|
assert.Equal(t, "TestDelete", val.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2022-10-15 21:54:11 +02:00
|
|
|
GetService(router)
|
|
|
|
r2 := PerformRequest(app, "GET", "/api/v1/services/"+id)
|
2020-05-01 16:38:00 +02:00
|
|
|
val2 := gjson.Get(r2.Body.String(), "error")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.ErrAccountNotFound), val2.String())
|
2020-05-01 16:38:00 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r2.Code)
|
|
|
|
})
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
t.Run("NotFound", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2022-10-15 21:54:11 +02:00
|
|
|
DeleteService(router)
|
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/services/xxx")
|
2020-05-01 16:38:00 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
2020-07-07 10:51:55 +02:00
|
|
|
assert.Equal(t, i18n.Msg(i18n.ErrAccountNotFound), val.String())
|
2020-05-01 16:38:00 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2020-03-28 18:01:50 +01:00
|
|
|
})
|
|
|
|
}
|