summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-07-24 15:55:09 -0600
committermo khan <mo@mokhan.ca>2025-07-24 15:55:09 -0600
commitf6ad289b8d27219c47aefec24113ccb02a62dd99 (patch)
tree8febaf35f6686c3f46ed4b8a1990138b6f622300 /app
parent7fb5255aafc435f5b47725716963f3aebfb9feb2 (diff)
refactor: extract defaultEntity type
Diffstat (limited to 'app')
-rw-r--r--app/domain/entity.go34
-rw-r--r--app/domain/entity_test.go54
-rw-r--r--app/domain/sparkle.go17
-rw-r--r--app/domain/sparkle_test.go9
-rw-r--r--app/middleware/require_permission.go3
5 files changed, 103 insertions, 14 deletions
diff --git a/app/domain/entity.go b/app/domain/entity.go
index 0377c51..0dc3995 100644
--- a/app/domain/entity.go
+++ b/app/domain/entity.go
@@ -1,6 +1,40 @@
package domain
+import (
+ "errors"
+
+ v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
+)
+
type Entity interface {
Identifiable
Validate() error
}
+
+type defaultEntity struct {
+ ID ID `json:"id" jsonapi:"primary,entities"`
+}
+
+func (s *defaultEntity) GetID() ID {
+ return s.ID
+}
+
+func (s *defaultEntity) SetID(id ID) error {
+ s.ID = id
+ return nil
+}
+
+func (s *defaultEntity) ToGID() string {
+ return "gid://sparkle/Entity/" + s.ID.String()
+}
+
+func (self *defaultEntity) ToObjectReference() *v1.ObjectReference {
+ return &v1.ObjectReference{
+ ObjectType: "entity",
+ ObjectId: self.ID.String(),
+ }
+}
+
+func (s *defaultEntity) Validate() error {
+ return errors.New("method Validate not implemented")
+}
diff --git a/app/domain/entity_test.go b/app/domain/entity_test.go
new file mode 100644
index 0000000..ee3fbae
--- /dev/null
+++ b/app/domain/entity_test.go
@@ -0,0 +1,54 @@
+package domain
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+ "github.com/xlgmokha/x/pkg/x"
+)
+
+func TestDefaultEntity(t *testing.T) {
+ type example struct {
+ defaultEntity
+ }
+
+ t.Run("GetID", func(t *testing.T) {
+ t.Run("returns the assigned ID", func(t *testing.T) {
+ example := x.New[*example](WithULID[*example]())
+
+ assert.Equal(t, example.ID, example.GetID())
+ })
+ })
+
+ t.Run("SetID", func(t *testing.T) {
+ t.Run("assigns a new ID", func(t *testing.T) {
+ id := ID("1")
+ example := x.New[*example]()
+ example.SetID(id)
+
+ assert.Equal(t, id, example.GetID())
+ })
+
+ })
+
+ t.Run("ToGID", func(t *testing.T) {
+ example := x.New[*example](WithULID[*example]())
+ gid := example.ToGID()
+
+ assert.Equal(t, fmt.Sprintf("gid://sparkle/Entity/%s", example.ID), gid)
+ })
+
+ t.Run("ToObjectReference", func(t *testing.T) {
+ t.Run("returns a valid object reference", func(t *testing.T) {
+ example := x.New[*example](WithULID[*example]())
+ reference := example.ToObjectReference()
+
+ require.NotNil(t, reference)
+ require.NoError(t, reference.Validate())
+ require.NoError(t, reference.ValidateAll())
+ assert.Equal(t, fmt.Sprintf("object_type:\"entity\" object_id:\"%s\"", example.ID), reference.String())
+ })
+ })
+}
diff --git a/app/domain/sparkle.go b/app/domain/sparkle.go
index 2ea5a53..10e24de 100644
--- a/app/domain/sparkle.go
+++ b/app/domain/sparkle.go
@@ -9,10 +9,10 @@ import (
)
type Sparkle struct {
- ID ID `json:"id" jsonapi:"primary,sparkles"`
Sparklee string `json:"sparklee" jsonapi:"attr,sparklee"`
Author *User `json:"author" jsonapi:"attr,author"`
Reason string `json:"reason" jsonapi:"attr,reason"`
+ defaultEntity
}
var SparkleRegex = regexp.MustCompile(`\A\s*(?P<sparklee>@\w+)\s+(?P<reason>.+)\z`)
@@ -35,21 +35,12 @@ func NewSparkle(text string) (*Sparkle, error) {
}
return &Sparkle{
- ID: ID(pls.GenerateULID()),
- Sparklee: matches[SparkleeIndex],
- Reason: matches[ReasonIndex],
+ defaultEntity: defaultEntity{ID: ID(pls.GenerateULID())},
+ Sparklee: matches[SparkleeIndex],
+ Reason: matches[ReasonIndex],
}, nil
}
-func (s *Sparkle) GetID() ID {
- return s.ID
-}
-
-func (s *Sparkle) SetID(id ID) error {
- s.ID = id
- return nil
-}
-
func (s *Sparkle) ToGID() string {
return "gid://sparkle/Sparkle/" + s.ID.String()
}
diff --git a/app/domain/sparkle_test.go b/app/domain/sparkle_test.go
index c040d89..101d271 100644
--- a/app/domain/sparkle_test.go
+++ b/app/domain/sparkle_test.go
@@ -52,6 +52,15 @@ func TestSparkle(t *testing.T) {
})
})
+ t.Run("ToGID", func(t *testing.T) {
+ t.Run("returns a valid global id", func(t *testing.T) {
+ sparkle := x.New[*Sparkle](WithULID[*Sparkle]())
+ gid := sparkle.ToGID()
+
+ assert.Equal(t, fmt.Sprintf("gid://sparkle/Sparkle/%s", sparkle.ID), gid)
+ })
+ })
+
t.Run("ToObjectReference", func(t *testing.T) {
t.Run("returns a valid object reference", func(t *testing.T) {
sparkle := x.New[*Sparkle](WithULID[*Sparkle]())
diff --git a/app/middleware/require_permission.go b/app/middleware/require_permission.go
index 399602f..91e2b72 100644
--- a/app/middleware/require_permission.go
+++ b/app/middleware/require_permission.go
@@ -4,6 +4,7 @@ import (
"net/http"
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/app/cfg"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/authz"
@@ -17,7 +18,7 @@ func RequirePermission(permission domain.Permission, client authz.CheckPermissio
reply, err := client.CheckPermission(r.Context(), permission.RequestFor(
user,
- &domain.Sparkle{ID: "*"},
+ x.New[*domain.Sparkle](domain.WithID[*domain.Sparkle](domain.ID("*"))),
))
if err != nil {
pls.LogError(r.Context(), err)