Backend: Add unit tests
This commit is contained in:
parent
c72ddbedcb
commit
f38434dfb0
5 changed files with 109 additions and 0 deletions
|
@ -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
|
||||
}
|
||||
|
|
13
internal/pro/config_test.go
Normal file
13
internal/pro/config_test.go
Normal file
|
@ -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())
|
||||
})
|
||||
}
|
43
internal/pro/feedback_test.go
Normal file
43
internal/pro/feedback_test.go
Normal file
|
@ -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")
|
||||
})
|
||||
}
|
25
internal/pro/session_test.go
Normal file
25
internal/pro/session_test.go
Normal file
|
@ -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())
|
||||
})
|
||||
}
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue