diff options
| author | mo khan <mo@mokhan.ca> | 2025-04-21 13:06:56 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-04-21 13:06:56 -0600 |
| commit | 1ece3b42051d26050cd612a3ed9a20122d501746 (patch) | |
| tree | 1e873073ac585efc610fa5e734f3eeeaaa69b01e /pkg/web/middleware/user_test.go | |
| parent | f157746e34f62621d85b2cbda982b90d9af06125 (diff) | |
feat: attach current user if they are in the db
Diffstat (limited to 'pkg/web/middleware/user_test.go')
| -rw-r--r-- | pkg/web/middleware/user_test.go | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/pkg/web/middleware/user_test.go b/pkg/web/middleware/user_test.go index 7119b41..cde7dec 100644 --- a/pkg/web/middleware/user_test.go +++ b/pkg/web/middleware/user_test.go @@ -6,16 +6,36 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "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/oidc" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test" ) func TestUser(t *testing.T) { - middleware := User() + repository := db.NewRepository[*domain.User]() + middleware := User(repository) + + knownUser := &domain.User{ID: "1"} + require.NoError(t, repository.Save(knownUser)) t.Run("when an ID Token is found in the context", func(t *testing.T) { - t.Run("When the user is found in the db", func(t *testing.T) { + t.Run("when the user is found in the db", func(t *testing.T) { + server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + user := key.CurrentUser.From(r.Context()) + require.NotNil(t, user) + + w.WriteHeader(http.StatusTeapot) + })) + + idToken := &oidc.IDToken{Subject: knownUser.ID} + ctx := key.IDToken.With(t.Context(), idToken) + + r, w := test.RequestResponse("GET", "/example", test.WithContext(ctx)) + server.ServeHTTP(w, r) + assert.Equal(t, http.StatusTeapot, w.Code) }) t.Run("when the user is not found in the db", func(t *testing.T) { |
