From 90c910654bd98fe86269dc9fd49eba64a4e97855 Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Mon, 4 May 2020 17:14:04 +0200 Subject: [PATCH] Backend: Add tests to internal/api --- internal/api/file_test.go | 36 ++++++++--- internal/api/link_test.go | 124 +++++++++++++++++++++++++++++++++++++- 2 files changed, 151 insertions(+), 9 deletions(-) diff --git a/internal/api/file_test.go b/internal/api/file_test.go index d2af90630..adf577516 100644 --- a/internal/api/file_test.go +++ b/internal/api/file_test.go @@ -1,6 +1,8 @@ package api import ( + "encoding/json" + "github.com/photoprism/photoprism/internal/entity" "github.com/tidwall/gjson" "net/http" "testing" @@ -27,22 +29,40 @@ func TestGetFile(t *testing.T) { } func TestLinkFile(t *testing.T) { - t.Run("album not found", func(t *testing.T) { + t.Run("successful request", func(t *testing.T) { app, router, ctx := NewApiTest() LinkFile(router, ctx) - r := PerformRequest(app, "POST", "/api/v1/files/3cad9168fa6acc5c5c2965ddf6ec465ca42fd818/link") - t.Log(r.Body.String()) + r := PerformRequestWithBody(app, "POST", "/api/v1/files/ft9es39w45bnlqdw/link", `{"password": "foobar123", "expires": 0, "edit": true}`) + + var label entity.Label + + if err := json.Unmarshal(r.Body.Bytes(), &label); err != nil { + t.Fatal(err) + } + + if len(label.Links) != 1 { + t.Fatalf("one link expected: %d, %+v", len(label.Links), label) + } + + link := label.Links[0] + + assert.Equal(t, "foobar123", link.LinkPassword) + assert.Nil(t, link.LinkExpires) + assert.False(t, link.CanComment) + assert.True(t, link.CanEdit) + }) + t.Run("file not found", func(t *testing.T) { + app, router, ctx := NewApiTest() + LinkFile(router, ctx) + r := PerformRequestWithBody(app, "POST", "/api/v1/files/xxx/link", `{"password": "foobar", "expires": 0, "edit": true}`) assert.Equal(t, http.StatusNotFound, r.Code) val := gjson.Get(r.Body.String(), "error") - assert.Equal(t, "Album not found", val.String()) + assert.Equal(t, "File not found", val.String()) }) t.Run("invalid request", func(t *testing.T) { app, router, ctx := NewApiTest() LinkFile(router, ctx) - r := PerformRequest(app, "POST", "/api/v1/files/ft9es39w45bnlqdw/link") + r := PerformRequestWithBody(app, "POST", "/api/v1/files/ft9es39w45bnlqdw/link", `{"xxx": 123, "expires": 0, "edit": "xxx"}`) assert.Equal(t, http.StatusBadRequest, r.Code) - val := gjson.Get(r.Body.String(), "error") - assert.Equal(t, "Invalid request", val.String()) }) - } diff --git a/internal/api/link_test.go b/internal/api/link_test.go index fd7e39034..e47d757f2 100644 --- a/internal/api/link_test.go +++ b/internal/api/link_test.go @@ -2,6 +2,7 @@ package api import ( "encoding/json" + "github.com/tidwall/gjson" "net/http" "testing" @@ -18,7 +19,6 @@ func TestLinkAlbum(t *testing.T) { LinkAlbum(router, ctx) result1 := PerformRequestWithBody(app, "POST", "/api/v1/albums/at9lxuqxpogaaba7/link", `{"password": "foobar", "expires": 0, "edit": true}`) - assert.Equal(t, http.StatusOK, result1.Code) if err := json.Unmarshal(result1.Body.Bytes(), &album); err != nil { @@ -51,4 +51,126 @@ func TestLinkAlbum(t *testing.T) { t.Fatal("two links expected") } }) + t.Run("album not found", func(t *testing.T) { + app, router, ctx := NewApiTest() + LinkAlbum(router, ctx) + r := PerformRequestWithBody(app, "POST", "/api/v1/albums/xxx/link", `{"password": "foobar", "expires": 0, "edit": true}`) + assert.Equal(t, http.StatusNotFound, r.Code) + val := gjson.Get(r.Body.String(), "error") + assert.Equal(t, "Album not found", val.String()) + }) + t.Run("invalid request", func(t *testing.T) { + app, router, ctx := NewApiTest() + LinkAlbum(router, ctx) + r := PerformRequestWithBody(app, "POST", "/api/v1/albums/at9lxuqxpogaaba7/link", `{"xxx": 123, "expires": 0, "edit": "xxx"}`) + assert.Equal(t, http.StatusBadRequest, r.Code) + }) +} + +func TestLinkPhoto(t *testing.T) { + t.Run("create share link", func(t *testing.T) { + app, router, ctx := NewApiTest() + + var photo entity.Photo + + LinkPhoto(router, ctx) + + result1 := PerformRequestWithBody(app, "POST", "/api/v1/photos/pt9jtdre2lvl0yh7/link", `{"password": "foobar", "expires": 0, "edit": true}`) + assert.Equal(t, http.StatusOK, result1.Code) + + if err := json.Unmarshal(result1.Body.Bytes(), &photo); err != nil { + t.Fatal(err) + } + + if len(photo.Links) != 1 { + t.Fatalf("one link expected: %d, %+v", len(photo.Links), photo) + } + + link := photo.Links[0] + + assert.Equal(t, "foobar", link.LinkPassword) + assert.Nil(t, link.LinkExpires) + assert.False(t, link.CanComment) + assert.True(t, link.CanEdit) + + result2 := PerformRequestWithBody(app, "POST", "/api/v1/photos/pt9jtdre2lvl0yh7/link", `{"password": "", "expires": 3600}`) + + assert.Equal(t, http.StatusOK, result2.Code) + + if err := json.Unmarshal(result2.Body.Bytes(), &photo); err != nil { + t.Fatal(err) + } + + if len(photo.Links) != 2 { + t.Fatal("two links expected") + } + }) + t.Run("photo not found", func(t *testing.T) { + app, router, ctx := NewApiTest() + LinkPhoto(router, ctx) + r := PerformRequestWithBody(app, "POST", "/api/v1/photos/xxx/link", `{"password": "foobar", "expires": 0, "edit": true}`) + assert.Equal(t, http.StatusNotFound, r.Code) + val := gjson.Get(r.Body.String(), "error") + assert.Equal(t, "Photo not found", val.String()) + }) + t.Run("invalid request", func(t *testing.T) { + app, router, ctx := NewApiTest() + LinkPhoto(router, ctx) + r := PerformRequestWithBody(app, "POST", "/api/v1/photos/pt9jtdre2lvl0yh7/link", `{"xxx": 123, "expires": 0, "edit": "xxx"}`) + assert.Equal(t, http.StatusBadRequest, r.Code) + }) +} + +func TestLinkLabel(t *testing.T) { + t.Run("create share link", func(t *testing.T) { + app, router, ctx := NewApiTest() + + var label entity.Label + + LinkLabel(router, ctx) + + result1 := PerformRequestWithBody(app, "POST", "/api/v1/labels/lt9k3pw1wowuy3c2/link", `{"password": "foobar", "expires": 0, "edit": true}`) + assert.Equal(t, http.StatusOK, result1.Code) + + if err := json.Unmarshal(result1.Body.Bytes(), &label); err != nil { + t.Fatal(err) + } + + if len(label.Links) != 1 { + t.Fatalf("one link expected: %d, %+v", len(label.Links), label) + } + + link := label.Links[0] + + assert.Equal(t, "foobar", link.LinkPassword) + assert.Nil(t, link.LinkExpires) + assert.False(t, link.CanComment) + assert.True(t, link.CanEdit) + + result2 := PerformRequestWithBody(app, "POST", "/api/v1/labels/lt9k3pw1wowuy3c2/link", `{"password": "", "expires": 3600}`) + + assert.Equal(t, http.StatusOK, result2.Code) + + if err := json.Unmarshal(result2.Body.Bytes(), &label); err != nil { + t.Fatal(err) + } + + if len(label.Links) != 2 { + t.Fatal("two links expected") + } + }) + t.Run("label not found", func(t *testing.T) { + app, router, ctx := NewApiTest() + LinkLabel(router, ctx) + r := PerformRequestWithBody(app, "POST", "/api/v1/labels/xxx/link", `{"password": "foobar", "expires": 0, "edit": true}`) + assert.Equal(t, http.StatusNotFound, r.Code) + val := gjson.Get(r.Body.String(), "error") + assert.Equal(t, "Label not found", val.String()) + }) + t.Run("invalid request", func(t *testing.T) { + app, router, ctx := NewApiTest() + LinkLabel(router, ctx) + r := PerformRequestWithBody(app, "POST", "/api/v1/labels/lt9k3pw1wowuy3c2/link", `{"xxx": 123, "expires": 0, "edit": "xxx"}`) + assert.Equal(t, http.StatusBadRequest, r.Code) + }) }