From ff505579ec01602d442dd72b2c5e70d2b407890c Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Wed, 6 May 2020 12:56:13 +0200 Subject: [PATCH] Backend: Add tests to internal/api --- internal/api/photo_thumbnail_test.go | 13 +++---- internal/api/preview_test.go | 6 +-- internal/api/session_test.go | 47 ++++++++++++++++++++++++ internal/api/settings_test.go | 46 +++++++++++++++++++++++ internal/api/status_test.go | 19 ++++++++++ internal/api/svg_test.go | 50 +++++++++++++++++++++++++ internal/api/zip_test.go | 55 ++++++++++++++++++++++++++++ 7 files changed, 226 insertions(+), 10 deletions(-) create mode 100644 internal/api/session_test.go create mode 100644 internal/api/settings_test.go create mode 100644 internal/api/status_test.go create mode 100644 internal/api/svg_test.go create mode 100644 internal/api/zip_test.go diff --git a/internal/api/photo_thumbnail_test.go b/internal/api/photo_thumbnail_test.go index 0bc75f7c5..613edabd1 100644 --- a/internal/api/photo_thumbnail_test.go +++ b/internal/api/photo_thumbnail_test.go @@ -11,22 +11,21 @@ func TestGetThumbnail(t *testing.T) { t.Run("invalid type", func(t *testing.T) { app, router, ctx := NewApiTest() GetThumbnail(router, ctx) - result := PerformRequest(app, "GET", "/api/v1/thumbnails/1/xxx") + r := PerformRequest(app, "GET", "/api/v1/thumbnails/1/xxx") - assert.Equal(t, http.StatusOK, result.Code) + assert.Equal(t, http.StatusOK, r.Code) }) t.Run("invalid hash", func(t *testing.T) { app, router, ctx := NewApiTest() GetThumbnail(router, ctx) - result := PerformRequest(app, "GET", "/api/v1/thumbnails/1/tile_500") + r := PerformRequest(app, "GET", "/api/v1/thumbnails/1/tile_500") - assert.Equal(t, http.StatusOK, result.Code) + assert.Equal(t, http.StatusOK, r.Code) }) t.Run("could not find original", func(t *testing.T) { app, router, ctx := NewApiTest() GetThumbnail(router, ctx) - result := PerformRequest(app, "GET", "/api/v1/thumbnails/123xxx/tile_500") - - assert.Equal(t, http.StatusOK, result.Code) + r := PerformRequest(app, "GET", "/api/v1/thumbnails/2cad9168fa6acc5c5c2965ddf6ec465ca42fd818/tile_500") + assert.Equal(t, http.StatusOK, r.Code) }) } diff --git a/internal/api/preview_test.go b/internal/api/preview_test.go index e8a3d3161..1c3b17c12 100644 --- a/internal/api/preview_test.go +++ b/internal/api/preview_test.go @@ -8,10 +8,10 @@ import ( ) func TestGetPreview(t *testing.T) { - t.Run("invalid type", func(t *testing.T) { + t.Run("successful request", func(t *testing.T) { app, router, ctx := NewApiTest() GetPreview(router, ctx) - result := PerformRequest(app, "GET", "/api/v1/preview") - assert.Equal(t, http.StatusOK, result.Code) + r := PerformRequest(app, "GET", "/api/v1/preview") + assert.Equal(t, http.StatusOK, r.Code) }) } diff --git a/internal/api/session_test.go b/internal/api/session_test.go new file mode 100644 index 000000000..d94121d3b --- /dev/null +++ b/internal/api/session_test.go @@ -0,0 +1,47 @@ +package api + +import ( + "github.com/stretchr/testify/assert" + "github.com/tidwall/gjson" + "net/http" + "testing" +) + +func TestCreateSession(t *testing.T) { + t.Run("successful request", func(t *testing.T) { + app, router, conf := NewApiTest() + CreateSession(router, conf) + r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"email": "photoprism", "password": "photoprism"}`) + val2 := gjson.Get(r.Body.String(), "user.Email") + assert.Equal(t, "photoprism@localhost", val2.String()) + assert.Equal(t, http.StatusOK, r.Code) + }) + t.Run("bad request", func(t *testing.T) { + app, router, conf := NewApiTest() + CreateSession(router, conf) + r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"email": 123, "password": "xxx"}`) + assert.Equal(t, http.StatusBadRequest, r.Code) + }) + t.Run("invalid password", func(t *testing.T) { + app, router, conf := NewApiTest() + CreateSession(router, conf) + r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"email": "photoprism", "password": "xxx"}`) + val := gjson.Get(r.Body.String(), "error") + assert.Equal(t, "Invalid password", val.String()) + assert.Equal(t, http.StatusBadRequest, r.Code) + }) +} + +func TestDeleteSession(t *testing.T) { + app, router, conf := NewApiTest() + CreateSession(router, conf) + r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"email": "photoprism", "password": "photoprism"}`) + token := gjson.Get(r.Body.String(), "token") + + t.Run("successful request", func(t *testing.T) { + app, router, conf := NewApiTest() + DeleteSession(router, conf) + r := PerformRequest(app, "DELETE", "/api/v1/session/"+token.String()) + assert.Equal(t, http.StatusOK, r.Code) + }) +} diff --git a/internal/api/settings_test.go b/internal/api/settings_test.go new file mode 100644 index 000000000..9120e5287 --- /dev/null +++ b/internal/api/settings_test.go @@ -0,0 +1,46 @@ +package api + +import ( + "github.com/stretchr/testify/assert" + "github.com/tidwall/gjson" + "net/http" + "testing" +) + +func TestGetSettings(t *testing.T) { + t.Run("successful request", func(t *testing.T) { + app, router, conf := NewApiTest() + GetSettings(router, conf) + r := PerformRequest(app, "GET", "/api/v1/settings") + val := gjson.Get(r.Body.String(), "theme") + assert.NotEmpty(t, val.String()) + val2 := gjson.Get(r.Body.String(), "language") + assert.NotEmpty(t, val2.String()) + assert.Equal(t, http.StatusOK, r.Code) + }) +} + +func TestSaveSettings(t *testing.T) { + t.Run("successful request", func(t *testing.T) { + app, router, conf := NewApiTest() + GetSettings(router, conf) + r := PerformRequest(app, "GET", "/api/v1/settings") + val := gjson.Get(r.Body.String(), "language") + assert.Equal(t, "de", val.String()) + assert.Equal(t, http.StatusOK, r.Code) + + SaveSettings(router, conf) + r2 := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"language": "en"}`) + val2 := gjson.Get(r2.Body.String(), "language") + assert.Equal(t, "en", val2.String()) + assert.Equal(t, http.StatusOK, r2.Code) + r3 := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"language": "de"}`) + assert.Equal(t, http.StatusOK, r3.Code) + }) + t.Run("bad request", func(t *testing.T) { + app, router, conf := NewApiTest() + SaveSettings(router, conf) + r := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"language": 123}`) + assert.Equal(t, http.StatusBadRequest, r.Code) + }) +} diff --git a/internal/api/status_test.go b/internal/api/status_test.go new file mode 100644 index 000000000..195e4f599 --- /dev/null +++ b/internal/api/status_test.go @@ -0,0 +1,19 @@ +package api + +import ( + "github.com/stretchr/testify/assert" + "github.com/tidwall/gjson" + "net/http" + "testing" +) + +func TestGetStatus(t *testing.T) { + t.Run("successful request", func(t *testing.T) { + app, router, conf := NewApiTest() + GetStatus(router, conf) + r := PerformRequest(app, "GET", "/api/v1/status") + val := gjson.Get(r.Body.String(), "status") + assert.Equal(t, "operational", val.String()) + assert.Equal(t, http.StatusOK, r.Code) + }) +} diff --git a/internal/api/svg_test.go b/internal/api/svg_test.go new file mode 100644 index 000000000..fce10641e --- /dev/null +++ b/internal/api/svg_test.go @@ -0,0 +1,50 @@ +package api + +import ( + "github.com/stretchr/testify/assert" + "net/http" + "testing" +) + +func TestGetSvg(t *testing.T) { + t.Run("photo", func(t *testing.T) { + app, router, conf := NewApiTest() + t.Log(conf) + GetSvg(router) + r := PerformRequest(app, "GET", "/api/v1/svg/photo") + assert.Equal(t, photoIconSvg, r.Body.Bytes()) + assert.Equal(t, http.StatusOK, r.Code) + }) + t.Run("label", func(t *testing.T) { + app, router, conf := NewApiTest() + t.Log(conf) + GetSvg(router) + r := PerformRequest(app, "GET", "/api/v1/svg/label") + assert.Equal(t, labelIconSvg, r.Body.Bytes()) + assert.Equal(t, http.StatusOK, r.Code) + }) + t.Run("album", func(t *testing.T) { + app, router, conf := NewApiTest() + t.Log(conf) + GetSvg(router) + r := PerformRequest(app, "GET", "/api/v1/svg/album") + assert.Equal(t, albumIconSvg, r.Body.Bytes()) + assert.Equal(t, http.StatusOK, r.Code) + }) + t.Run("broken", func(t *testing.T) { + app, router, conf := NewApiTest() + t.Log(conf) + GetSvg(router) + r := PerformRequest(app, "GET", "/api/v1/svg/broken") + assert.Equal(t, brokenIconSvg, r.Body.Bytes()) + assert.Equal(t, http.StatusOK, r.Code) + }) + t.Run("uncached", func(t *testing.T) { + app, router, conf := NewApiTest() + t.Log(conf) + GetSvg(router) + r := PerformRequest(app, "GET", "/api/v1/svg/uncached") + assert.Equal(t, uncachedIconSvg, r.Body.Bytes()) + assert.Equal(t, http.StatusOK, r.Code) + }) +} diff --git a/internal/api/zip_test.go b/internal/api/zip_test.go new file mode 100644 index 000000000..60cab6c55 --- /dev/null +++ b/internal/api/zip_test.go @@ -0,0 +1,55 @@ +package api + +import ( + "github.com/stretchr/testify/assert" + "github.com/tidwall/gjson" + "net/http" + "testing" +) + +func TestCreateZip(t *testing.T) { + t.Run("successful request", func(t *testing.T) { + app, router, conf := NewApiTest() + CreateZip(router, conf) + r := PerformRequestWithBody(app, "POST", "/api/v1/zip", `{"photos": ["pt9jtdre2lvl0y12", "pt9jtdre2lvl0y11"]}`) + val := gjson.Get(r.Body.String(), "message") + assert.Contains(t, val.String(), "zip created") + assert.Equal(t, http.StatusOK, r.Code) + }) + t.Run("no photos selected", func(t *testing.T) { + app, router, conf := NewApiTest() + CreateZip(router, conf) + r := PerformRequestWithBody(app, "POST", "/api/v1/zip", `{"photos": []}`) + val := gjson.Get(r.Body.String(), "error") + assert.Equal(t, "No photos selected", val.String()) + assert.Equal(t, http.StatusBadRequest, r.Code) + }) + t.Run("invalid request", func(t *testing.T) { + app, router, conf := NewApiTest() + CreateZip(router, conf) + r := PerformRequestWithBody(app, "POST", "/api/v1/zip", `{"photos": [123, "pt9jtdre2lvl0yxx"]}`) + assert.Equal(t, http.StatusBadRequest, r.Code) + }) +} + +func TestDownloadZip(t *testing.T) { + app, router, conf := NewApiTest() + CreateZip(router, conf) + r := PerformRequestWithBody(app, "POST", "/api/v1/zip", `{"photos": ["pt9jtdre2lvl0y12", "pt9jtdre2lvl0y11"]}`) + filename := gjson.Get(r.Body.String(), "filename") + assert.Equal(t, http.StatusOK, r.Code) + + t.Run("successful request", func(t *testing.T) { + app, router, conf := NewApiTest() + DownloadZip(router, conf) + r := PerformRequest(app, "GET", "/api/v1/zip/"+filename.String()) + assert.Equal(t, http.StatusOK, r.Code) + }) + + t.Run("zip not existing", func(t *testing.T) { + app, router, conf := NewApiTest() + DownloadZip(router, conf) + r := PerformRequest(app, "GET", "/api/v1/zip/xxx") + assert.Equal(t, http.StatusNotFound, r.Code) + }) +}