diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-24 15:38:36 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-24 15:38:36 -0600 |
| commit | 7fb5255aafc435f5b47725716963f3aebfb9feb2 (patch) | |
| tree | 2779faa2ce03296013722086019262bbc390cadf /app | |
| parent | 5f04ff8b3051d176dca586357ed68482d62b8eef (diff) | |
test: Validate Entity ObjectReference
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/sparkles/controller_test.go | 5 | ||||
| -rw-r--r-- | app/domain/entity.go | 6 | ||||
| -rw-r--r-- | app/domain/identifiable.go | 12 | ||||
| -rw-r--r-- | app/domain/sparkle.go | 3 | ||||
| -rw-r--r-- | app/domain/sparkle_test.go | 15 | ||||
| -rw-r--r-- | app/domain/user.go | 5 |
6 files changed, 27 insertions, 19 deletions
diff --git a/app/controllers/sparkles/controller_test.go b/app/controllers/sparkles/controller_test.go index e825343..710118d 100644 --- a/app/controllers/sparkles/controller_test.go +++ b/app/controllers/sparkles/controller_test.go @@ -11,6 +11,7 @@ import ( "github.com/xlgmokha/x/pkg/event" "github.com/xlgmokha/x/pkg/serde" "github.com/xlgmokha/x/pkg/test" + "github.com/xlgmokha/x/pkg/x" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/db" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" @@ -64,7 +65,7 @@ func TestSparkles(t *testing.T) { t.Run("POST /sparkles", func(t *testing.T) { t.Run("when a user is authenticated", func(t *testing.T) { - currentUser := domain.NewUser(domain.WithID[*domain.User](domain.ID("1"))) + currentUser := x.New[*domain.User](domain.WithID[*domain.User](domain.ID("1"))) repository := db.NewRepository[*domain.Sparkle](event.New[*domain.Sparkle]()) t.Run("when the user is authorized", func(t *testing.T) { @@ -106,7 +107,7 @@ func TestSparkles(t *testing.T) { }) t.Run("prevents double WriteHeader when serialization fails", func(t *testing.T) { - currentUser := domain.NewUser(domain.WithID[*domain.User](domain.ID("1"))) + currentUser := x.New[*domain.User](domain.WithID[*domain.User](domain.ID("1"))) sparkle, _ := domain.NewSparkle("@user for testing") request, response := test.RequestResponse( diff --git a/app/domain/entity.go b/app/domain/entity.go index b2c2166..0377c51 100644 --- a/app/domain/entity.go +++ b/app/domain/entity.go @@ -1,12 +1,6 @@ package domain -import "github.com/xlgmokha/x/pkg/x" - type Entity interface { Identifiable Validate() error } - -func New[T Entity](options ...x.Configure[T]) T { - return x.New[T](x.Map[x.Configure[T], x.Option[T]](options, x.With[T])...) -} diff --git a/app/domain/identifiable.go b/app/domain/identifiable.go index 190f20c..3a39cf9 100644 --- a/app/domain/identifiable.go +++ b/app/domain/identifiable.go @@ -3,17 +3,21 @@ package domain import ( v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" "github.com/xlgmokha/x/pkg/x" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" ) type Identifiable interface { GetID() ID SetID(id ID) error - ToGID() string ToObjectReference() *v1.ObjectReference } -func WithID[T Identifiable](id ID) x.Configure[T] { - return func(item T) { +func WithID[T Identifiable](id ID) x.Option[T] { + return x.With(func(item T) { item.SetID(id) - } + }) +} + +func WithULID[T Identifiable]() x.Option[T] { + return WithID[T](ID(pls.GenerateULID())) } diff --git a/app/domain/sparkle.go b/app/domain/sparkle.go index c9ff02a..2ea5a53 100644 --- a/app/domain/sparkle.go +++ b/app/domain/sparkle.go @@ -57,8 +57,7 @@ func (s *Sparkle) ToGID() string { func (self *Sparkle) ToObjectReference() *v1.ObjectReference { return &v1.ObjectReference{ ObjectType: "sparkle", - // ObjectId: self.ID.String(), - ObjectId: "1", + ObjectId: self.ID.String(), } } diff --git a/app/domain/sparkle_test.go b/app/domain/sparkle_test.go index 8d81afd..c040d89 100644 --- a/app/domain/sparkle_test.go +++ b/app/domain/sparkle_test.go @@ -1,9 +1,12 @@ package domain import ( + "fmt" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/xlgmokha/x/pkg/x" ) func TestSparkle(t *testing.T) { @@ -48,4 +51,16 @@ func TestSparkle(t *testing.T) { } }) }) + + t.Run("ToObjectReference", func(t *testing.T) { + t.Run("returns a valid object reference", func(t *testing.T) { + sparkle := x.New[*Sparkle](WithULID[*Sparkle]()) + reference := sparkle.ToObjectReference() + + require.NotNil(t, reference) + require.NoError(t, reference.Validate()) + require.NoError(t, reference.ValidateAll()) + assert.Equal(t, fmt.Sprintf("object_type:\"sparkle\" object_id:\"%s\"", sparkle.ID), reference.String()) + }) + }) } diff --git a/app/domain/user.go b/app/domain/user.go index e618b5f..c9dcfb4 100644 --- a/app/domain/user.go +++ b/app/domain/user.go @@ -2,7 +2,6 @@ package domain import ( v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" - "github.com/xlgmokha/x/pkg/x" ) type User struct { @@ -12,10 +11,6 @@ type User struct { Picture string `json:"picture" jsonapi:"attr,picture"` } -func NewUser(options ...x.Configure[*User]) *User { - return New[*User](options...) -} - func (u *User) GetID() ID { return u.ID } |
