From f38434dfb0d4c6319e300136a93fda9dd9053d36 Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Tue, 20 Oct 2020 11:05:02 +0200 Subject: [PATCH] Backend: Add unit tests --- internal/form/feedback.go | 8 +++++++ internal/pro/config_test.go | 13 ++++++++++ internal/pro/feedback_test.go | 43 ++++++++++++++++++++++++++++++++++ internal/pro/session_test.go | 25 ++++++++++++++++++++ internal/session/store_test.go | 20 ++++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 internal/pro/config_test.go create mode 100644 internal/pro/feedback_test.go create mode 100644 internal/pro/session_test.go diff --git a/internal/form/feedback.go b/internal/form/feedback.go index c2fc49358..15d2cc432 100644 --- a/internal/form/feedback.go +++ b/internal/form/feedback.go @@ -1,5 +1,7 @@ package form +import "github.com/ulule/deepcopier" + // Feedback represents support requests / customer feedback. type Feedback struct { Category string `json:"Category"` @@ -13,3 +15,9 @@ type Feedback struct { func (f Feedback) Empty() bool { return len(f.Category) < 1 || len(f.Message) < 3 || len(f.UserEmail) < 5 } + +func NewFeedback(m interface{}) (f Feedback, err error) { + err = deepcopier.Copy(m).To(&f) + + return f, err +} diff --git a/internal/pro/config_test.go b/internal/pro/config_test.go new file mode 100644 index 000000000..b739465ba --- /dev/null +++ b/internal/pro/config_test.go @@ -0,0 +1,13 @@ +package pro + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestConfig_MapKey(t *testing.T) { + t.Run("success", func(t *testing.T) { + c := NewConfig("develop", "testdata/new.yml") + assert.Equal(t, "", c.MapKey()) + }) +} diff --git a/internal/pro/feedback_test.go b/internal/pro/feedback_test.go new file mode 100644 index 000000000..3ef32ee4f --- /dev/null +++ b/internal/pro/feedback_test.go @@ -0,0 +1,43 @@ +package pro + +import ( + "github.com/photoprism/photoprism/internal/form" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNewFeedback(t *testing.T) { + t.Run("success", func(t *testing.T) { + feedback := NewFeedback("xxx") + assert.Equal(t, "xxx", feedback.ClientVersion) + }) +} + +func TestSendFeedback(t *testing.T) { + t.Run("success", func(t *testing.T) { + c := NewConfig("develop", "testdata/new.yml") + + feedback := Feedback{ + Category: "Bug Report", + Subject: "", + Message: "I found a new bug", + UserName: "Test User", + UserEmail: "test@example.com", + UserAgent: "", + ApiKey: "123456", + ClientVersion: "test", + ClientOS: "linux", + ClientArch: "amd64", + ClientCPU: 2, + } + + feedbackForm, err := form.NewFeedback(feedback) + + if err != nil { + t.Fatal(err) + } + + err2 := c.SendFeedback(feedbackForm) + assert.Contains(t, err2.Error(), "failed") + }) +} diff --git a/internal/pro/session_test.go b/internal/pro/session_test.go new file mode 100644 index 000000000..ac35dc3fa --- /dev/null +++ b/internal/pro/session_test.go @@ -0,0 +1,25 @@ +package pro + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func TestSession_Expired(t *testing.T) { + t.Run("empty", func(t *testing.T) { + session := Session{ + MapKey: "", + ExpiresAt: "", + } + assert.True(t, session.Expired()) + }) + t.Run("true", func(t *testing.T) { + date := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC) + session := Session{ + MapKey: "", + ExpiresAt: date.String(), + } + assert.True(t, session.Expired()) + }) +} diff --git a/internal/session/store_test.go b/internal/session/store_test.go index beb751a4b..deda81767 100644 --- a/internal/session/store_test.go +++ b/internal/session/store_test.go @@ -63,6 +63,24 @@ func TestSession_Update(t *testing.T) { } } +func TestSession_UpdateError(t *testing.T) { + s := New(time.Hour, "testdata") + + data := Data{ + User: entity.Admin, + } + + id := s.Create(data) + t.Logf("id: %s", id) + assert.Equal(t, 48, len(id)) + newData := Data{ + User: entity.Guest, + Shares: UIDs{"a000000000000001"}, + } + err := s.Update("", newData) + assert.Equal(t, "session: empty id", err.Error()) +} + func TestSession_Delete(t *testing.T) { s := New(time.Hour, "testdata") s.Delete("abc") @@ -79,6 +97,8 @@ func TestSession_Get(t *testing.T) { t.Logf("id: %s", id) assert.Equal(t, 48, len(id)) + assert.Empty(t, s.Get("")) + cachedData := s.Get(id) if cachedData.Invalid() {