diff options
| -rw-r--r-- | app/controllers/sparkles/controller_test.go | 8 | ||||
| -rw-r--r-- | app/db/in_memory_repository_test.go | 4 | ||||
| -rw-r--r-- | app/domain/sparkle.go | 33 | ||||
| -rw-r--r-- | app/domain/sparkle_test.go | 36 |
4 files changed, 32 insertions, 49 deletions
diff --git a/app/controllers/sparkles/controller_test.go b/app/controllers/sparkles/controller_test.go index 710118d..5c37a11 100644 --- a/app/controllers/sparkles/controller_test.go +++ b/app/controllers/sparkles/controller_test.go @@ -36,7 +36,7 @@ func (f *FailingResponseWriter) Write([]byte) (int, error) { func TestSparkles(t *testing.T) { t.Run("GET /sparkles", func(t *testing.T) { - sparkle, _ := domain.NewSparkle("@tanuki for helping me") + sparkle := x.New[*domain.Sparkle](domain.WithText("@tanuki for helping me")) store := db.NewRepository[*domain.Sparkle](event.New[*domain.Sparkle]()) store.Save(t.Context(), sparkle) @@ -74,7 +74,7 @@ func TestSparkles(t *testing.T) { controller.MountTo(mux) t.Run("saves a new sparkle", func(t *testing.T) { - sparkle, _ := domain.NewSparkle("@tanuki for reviewing my code!") + sparkle := x.New[*domain.Sparkle](domain.WithText("@tanuki for reviewing my code!")) request, response := test.RequestResponse( "POST", "/sparkles", @@ -108,7 +108,7 @@ func TestSparkles(t *testing.T) { t.Run("prevents double WriteHeader when serialization fails", func(t *testing.T) { currentUser := x.New[*domain.User](domain.WithID[*domain.User](domain.ID("1"))) - sparkle, _ := domain.NewSparkle("@user for testing") + sparkle := x.New[*domain.Sparkle](domain.WithText("@user for testing")) request, response := test.RequestResponse( "POST", @@ -129,7 +129,7 @@ func TestSparkles(t *testing.T) { controller.MountTo(mux) t.Run("returns an error", func(t *testing.T) { - sparkle, _ := domain.NewSparkle("@tanuki for reviewing my code!") + sparkle := x.New[*domain.Sparkle](domain.WithText("@tanuki for reviewing my code!")) request, response := test.RequestResponse( "POST", "/sparkles", diff --git a/app/db/in_memory_repository_test.go b/app/db/in_memory_repository_test.go index 8e1e017..5bb220e 100644 --- a/app/db/in_memory_repository_test.go +++ b/app/db/in_memory_repository_test.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/xlgmokha/x/pkg/event" + "github.com/xlgmokha/x/pkg/x" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" ) @@ -129,8 +130,7 @@ func TestInMemoryRepository(t *testing.T) { t.Run("Find", func(t *testing.T) { t.Run("when the entity exists", func(t *testing.T) { - sparkle, err := domain.NewSparkle("@tanuki for testing this func") - require.NoError(t, err) + sparkle := x.New[*domain.Sparkle](domain.WithText("@tanuki for testing this func")) require.NoError(t, storage.Save(t.Context(), sparkle)) result := storage.Find(t.Context(), sparkle.ID) diff --git a/app/domain/sparkle.go b/app/domain/sparkle.go index 748d12b..41c46e5 100644 --- a/app/domain/sparkle.go +++ b/app/domain/sparkle.go @@ -5,7 +5,7 @@ import ( "regexp" v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" - "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" + "github.com/xlgmokha/x/pkg/x" ) type Sparkle struct { @@ -20,25 +20,22 @@ var SparkleeIndex = SparkleRegex.SubexpIndex("sparklee") var ReasonIndex = SparkleRegex.SubexpIndex("reason") var ReasonIsRequired = errors.New("Reason is required") -var SparkleIsEmpty = errors.New("Sparkle is empty") -var SparkleIsInvalid = errors.New("Sparkle is invalid") var SparkleeIsRequired = errors.New("Sparklee is required") -func NewSparkle(text string) (*Sparkle, error) { - if len(text) == 0 { - return nil, SparkleIsEmpty - } +func WithText(text string) x.Option[*Sparkle] { + return x.With(func(item *Sparkle) { + if len(text) == 0 { + return + } - matches := SparkleRegex.FindStringSubmatch(text) - if len(matches) == 0 { - return nil, SparkleIsInvalid - } + matches := SparkleRegex.FindStringSubmatch(text) + if len(matches) == 0 { + return + } - return &Sparkle{ - entity: entity{ID: ID(pls.GenerateULID())}, - Sparklee: matches[SparkleeIndex], - Reason: matches[ReasonIndex], - }, nil + item.Sparklee = matches[SparkleeIndex] + item.Reason = matches[ReasonIndex] + }) } func (s *Sparkle) ToGID() string { @@ -53,10 +50,10 @@ func (self *Sparkle) ToObjectReference() *v1.ObjectReference { } func (s *Sparkle) Validate() error { - if s.Sparklee == "" { + if x.IsZero(s.Sparklee) { return SparkleeIsRequired } - if s.Reason == "" { + if x.IsZero(s.Reason) { return ReasonIsRequired } return nil diff --git a/app/domain/sparkle_test.go b/app/domain/sparkle_test.go index a0e5d34..5d3449f 100644 --- a/app/domain/sparkle_test.go +++ b/app/domain/sparkle_test.go @@ -10,45 +10,31 @@ import ( ) func TestSparkle(t *testing.T) { - t.Run("NewSparkle", func(t *testing.T) { + t.Run("New", func(t *testing.T) { t.Run("with a valid body", func(t *testing.T) { - sparkle, err := NewSparkle("@tanuki for helping me with my homework!") + sparkle := x.New[*Sparkle](WithText("@tanuki for helping me with my homework!")) - assert.Nil(t, err) - if err != nil { - assert.Equal(t, "@tanuki", sparkle.Sparklee) - assert.Equal(t, "for helping me with my homework!", sparkle.Reason) - } + require.NoError(t, sparkle.Validate()) + assert.Equal(t, "@tanuki", sparkle.Sparklee) + assert.Equal(t, "for helping me with my homework!", sparkle.Reason) }) t.Run("with an empty body", func(t *testing.T) { - sparkle, err := NewSparkle("") + sparkle := x.New[*Sparkle](WithText("")) - assert.Nil(t, sparkle) - assert.NotNil(t, err) - if err != nil { - assert.Equal(t, "Sparkle is empty", err.Error()) - } + require.Error(t, sparkle.Validate()) }) t.Run("without a reason", func(t *testing.T) { - sparkle, err := NewSparkle("@tanuki") + sparkle := x.New[*Sparkle](WithText("@tanuki")) - assert.Nil(t, sparkle) - assert.NotNil(t, err) - if err != nil { - assert.Equal(t, "Sparkle is invalid", err.Error()) - } + require.Error(t, sparkle.Validate()) }) t.Run("without a username", func(t *testing.T) { - sparkle, err := NewSparkle("for helping me with my homework") + sparkle := x.New[*Sparkle](WithText("for helping me with my homework")) - assert.Nil(t, sparkle) - assert.NotNil(t, err) - if err != nil { - assert.Equal(t, "Sparkle is invalid", err.Error()) - } + require.Error(t, sparkle.Validate()) }) }) |
