summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-04-25 10:16:54 -0600
committermo khan <mo@mokhan.ca>2025-04-25 10:16:54 -0600
commit2cd89b747c7c255df9197132fcdf04d0c8cd2ff3 (patch)
tree53e8dffe9dd73f4471f073bcd0199dc9361fad70 /app
parent00b0381dfccab2ddff7de04933fdb11b32695faf (diff)
feat: record the author of the new sparkle
Diffstat (limited to 'app')
-rw-r--r--app/controllers/dashboard/controller.go6
-rw-r--r--app/controllers/sparkles/controller.go5
-rw-r--r--app/controllers/sparkles/controller_test.go73
-rw-r--r--app/controllers/sparkles/init.go2
4 files changed, 46 insertions, 40 deletions
diff --git a/app/controllers/dashboard/controller.go b/app/controllers/dashboard/controller.go
index 8f5105d..65b2fe5 100644
--- a/app/controllers/dashboard/controller.go
+++ b/app/controllers/dashboard/controller.go
@@ -4,7 +4,6 @@ import (
"net/http"
"github.com/xlgmokha/x/pkg/log"
- "github.com/xlgmokha/x/pkg/x"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/views"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web/middleware"
@@ -26,11 +25,6 @@ func (c *Controller) MountTo(mux *http.ServeMux) {
func (c *Controller) Show(w http.ResponseWriter, r *http.Request) {
currentUser := key.CurrentUser.From(r.Context())
- if x.IsZero(currentUser) {
- http.Redirect(w, r, "/", http.StatusFound)
- return
- }
-
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "text/html")
diff --git a/app/controllers/sparkles/controller.go b/app/controllers/sparkles/controller.go
index 07a21ba..04eee12 100644
--- a/app/controllers/sparkles/controller.go
+++ b/app/controllers/sparkles/controller.go
@@ -9,6 +9,7 @@ import (
"github.com/xlgmokha/x/pkg/x"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/db"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web/middleware"
)
type Controller struct {
@@ -20,8 +21,10 @@ func New(db db.Repository[*domain.Sparkle]) *Controller {
}
func (c *Controller) MountTo(mux *http.ServeMux) {
+ requireUser := middleware.RequireUser(http.StatusFound, "/")
+
mux.HandleFunc("GET /sparkles", c.Index)
- mux.HandleFunc("POST /sparkles", c.Create)
+ mux.Handle("POST /sparkles", requireUser(http.HandlerFunc(c.Create)))
}
func (c *Controller) Index(w http.ResponseWriter, r *http.Request) {
diff --git a/app/controllers/sparkles/controller_test.go b/app/controllers/sparkles/controller_test.go
index 4eadd2c..4ef4d7d 100644
--- a/app/controllers/sparkles/controller_test.go
+++ b/app/controllers/sparkles/controller_test.go
@@ -9,6 +9,7 @@ import (
"github.com/xlgmokha/x/pkg/serde"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/db"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test"
)
@@ -42,39 +43,45 @@ func TestSparkles(t *testing.T) {
})
t.Run("POST /sparkles", func(t *testing.T) {
- t.Run("saves a new sparkle", func(t *testing.T) {
- repository := db.NewRepository[*domain.Sparkle]()
- mux := http.NewServeMux()
- controller := New(repository)
- controller.MountTo(mux)
-
- sparkle, _ := domain.NewSparkle("@tanuki for reviewing my code!")
- request, response := test.RequestResponse(
- "POST",
- "/sparkles",
- test.WithAcceptHeader(serde.JSON),
- test.WithContentType(sparkle, serde.JSON),
- )
-
- mux.ServeHTTP(response, request)
-
- require.Equal(t, http.StatusCreated, response.Code)
-
- t.Run("returns a JSON representation of the sparkle", func(t *testing.T) {
- item, err := serde.FromJSON[*domain.Sparkle](response.Body)
- require.NoError(t, err)
-
- assert.NotEmpty(t, item.ID)
- assert.Equal(t, "@tanuki", item.Sparklee)
- assert.Equal(t, "for reviewing my code!", item.Reason)
- })
-
- t.Run("saves the sparkle to the db", func(t *testing.T) {
- assert.Equal(t, 1, len(repository.All()))
- item := repository.All()[0]
-
- assert.Equal(t, "@tanuki", item.Sparklee)
- assert.Equal(t, "for reviewing my code!", item.Reason)
+ t.Run("when a user is logged in", func(t *testing.T) {
+ currentUser := &domain.User{}
+
+ t.Run("saves a new sparkle", func(t *testing.T) {
+ repository := db.NewRepository[*domain.Sparkle]()
+ mux := http.NewServeMux()
+ controller := New(repository)
+ controller.MountTo(mux)
+
+ sparkle, _ := domain.NewSparkle("@tanuki for reviewing my code!")
+ request, response := test.RequestResponse(
+ "POST",
+ "/sparkles",
+ test.WithAcceptHeader(serde.JSON),
+ test.WithContentType(sparkle, serde.JSON),
+ test.WithContextKeyValue(t.Context(), key.CurrentUser, currentUser),
+ )
+
+ mux.ServeHTTP(response, request)
+
+ require.Equal(t, http.StatusCreated, response.Code)
+
+ t.Run("returns a JSON representation of the sparkle", func(t *testing.T) {
+ item, err := serde.FromJSON[*domain.Sparkle](response.Body)
+ require.NoError(t, err)
+
+ assert.NotEmpty(t, item.ID)
+ assert.Equal(t, "@tanuki", item.Sparklee)
+ assert.Equal(t, "for reviewing my code!", item.Reason)
+ })
+
+ t.Run("saves the sparkle to the db", func(t *testing.T) {
+ assert.Equal(t, 1, len(repository.All()))
+ item := repository.All()[0]
+
+ assert.Equal(t, "@tanuki", item.Sparklee)
+ assert.Equal(t, "for reviewing my code!", item.Reason)
+ assert.Equal(t, currentUser, item.Author)
+ })
})
})
})
diff --git a/app/controllers/sparkles/init.go b/app/controllers/sparkles/init.go
index 9efcac8..98bf43d 100644
--- a/app/controllers/sparkles/init.go
+++ b/app/controllers/sparkles/init.go
@@ -7,6 +7,7 @@ import (
"github.com/xlgmokha/x/pkg/mapper"
"github.com/xlgmokha/x/pkg/serde"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key"
)
func init() {
@@ -15,6 +16,7 @@ func init() {
if err != nil {
log.WithFields(r.Context(), log.Fields{"error": err})
}
+ sparkle.Author = key.CurrentUser.From(r.Context())
return sparkle
})
}