2020-03-28 18:01:50 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2020-11-21 18:08:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
|
2020-03-28 18:01:50 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
func TestSearchAccounts(t *testing.T) {
|
2020-03-28 18:01:50 +01:00
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2021-08-11 12:47:13 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2021-09-18 15:32:39 +02:00
|
|
|
SearchAccounts(router)
|
2021-08-11 12:47:13 +02:00
|
|
|
sess := AuthenticateAdmin(app, router)
|
2020-12-18 13:05:48 +01:00
|
|
|
r := AuthenticatedRequest(app, "GET", "/api/v1/accounts?count=10", sess)
|
2020-05-01 16:38:00 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "#(AccName=\"Test Account\").AccURL")
|
2020-05-22 16:29:12 +02:00
|
|
|
count := gjson.Get(r.Body.String(), "#")
|
|
|
|
assert.LessOrEqual(t, int64(1), count.Int())
|
2021-11-09 16:55:16 +01:00
|
|
|
assert.Equal(t, "http://dummy-webdav/", val.String())
|
2020-05-01 16:38:00 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-03-28 18:01:50 +01:00
|
|
|
})
|
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2021-09-18 15:32:39 +02:00
|
|
|
SearchAccounts(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/accounts?xxx=10")
|
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
2020-03-28 18:01:50 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAccount(t *testing.T) {
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
GetAccount(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/accounts/1000000")
|
|
|
|
val := gjson.Get(r.Body.String(), "AccName")
|
|
|
|
assert.Equal(t, "Test Account", val.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("account not found", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
GetAccount(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/accounts/999000")
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:58:56 +02:00
|
|
|
func TestGetAccountFolders(t *testing.T) {
|
2020-05-01 16:38:00 +02:00
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2020-06-30 12:58:56 +02:00
|
|
|
GetAccountFolders(router)
|
|
|
|
r := PerformRequest(app, "GET", "/api/v1/accounts/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
|
|
|
})
|
2020-05-01 16:38:00 +02:00
|
|
|
t.Run("account not found", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2020-06-30 12:58:56 +02:00
|
|
|
GetAccountFolders(router)
|
|
|
|
r := PerformRequest(app, "GET", "/api/v1/accounts/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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShareWithAccount(t *testing.T) {
|
2020-03-28 18:01:50 +01:00
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
ShareWithAccount(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r := PerformRequest(app, "POST", "/api/v1/accounts/1000000/share")
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
t.Run("account not found", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
ShareWithAccount(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r := PerformRequest(app, "POST", "/api/v1/accounts/999000/share")
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateAccount(t *testing.T) {
|
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateAccount(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r := PerformRequest(app, "POST", "/api/v1/accounts")
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
t.Run("could not connect", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateAccount(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/accounts", `{"AccName": "CreateTest1", "AccOwner": "Test", "AccUrl": "http://webdav123/", "AccType": "webdav",
|
|
|
|
"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)
|
|
|
|
})
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateAccount(router)
|
2021-11-09 16:55:16 +01:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/accounts", `{"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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateAccount(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateAccount(router)
|
2021-11-09 16:55:16 +01:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/accounts", `{"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()
|
|
|
|
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdateAccount(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/accounts/"+id, `{"AccName": "CreateTestUpdated", "AccOwner": "TestUpdated123", "SyncInterval": 9}`)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdateAccount(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/accounts/xxx", `{"AccName": "CreateTestUpdated", "AccOwner": "TestUpdated123", "SyncInterval": 9}`)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("changes could not be saved", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdateAccount(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/accounts/"+id, `{"AccName": 6, "AccOwner": "TestUpdated123", "SyncInterval": 9, "AccUrl": "https:xxx.com"}`)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteAccount(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
CreateAccount(router)
|
2021-11-09 16:55:16 +01:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/accounts", `{"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()
|
|
|
|
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
DeleteAccount(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/accounts/"+id)
|
|
|
|
val := gjson.Get(r.Body.String(), "AccOwner")
|
|
|
|
assert.Equal(t, "TestDelete", val.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-06-25 14:54:04 +02:00
|
|
|
GetAccount(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r2 := PerformRequest(app, "GET", "/api/v1/accounts/"+id)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
DeleteAccount(router)
|
2020-05-01 16:38:00 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/accounts/xxx")
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|