diff options
| author | mo khan <mo@mokhan.ca> | 2025-05-28 12:14:11 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-05-28 12:14:11 -0600 |
| commit | 591f293c8bcf464ed62701321d3f27de31ceb621 (patch) | |
| tree | 6b9c8c303f9816a3faf7abb9a75c3c59b6a5808a /app/middleware/user_parser_test.go | |
| parent | f76542bc846bc77e825055a1a6ea7cd0cb178844 (diff) | |
refactor: parse headers injected by envoy
Diffstat (limited to 'app/middleware/user_parser_test.go')
| -rw-r--r-- | app/middleware/user_parser_test.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/app/middleware/user_parser_test.go b/app/middleware/user_parser_test.go new file mode 100644 index 0000000..2127a10 --- /dev/null +++ b/app/middleware/user_parser_test.go @@ -0,0 +1,36 @@ +package middleware + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/xlgmokha/x/pkg/test" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" +) + +func TestUserParser(t *testing.T) { + parser := UserParser() + + t.Run("when x-jwt-claim-* headers are not provided", func(t *testing.T) { + t.Run("forwards the request without a current user attached to the request", func(t *testing.T) { + assert.Nil(t, parser(test.Request("GET", "/"))) + }) + }) + + t.Run("when x-jwt-claim-* headers are provided", func(t *testing.T) { + r := test.Request("GET", "/", + test.WithRequestHeader("x-jwt-claim-sub", "1"), + test.WithRequestHeader("x-jwt-claim-username", "root"), + test.WithRequestHeader("x-jwt-claim-profile-url", "https://gitlab.com/tanuki"), + test.WithRequestHeader("x-jwt-claim-picture-url", "https://example.com/profile.png"), + ) + + result := parser(r) + require.NotNil(t, result) + assert.Equal(t, domain.ID("1"), result.ID) + assert.Equal(t, "root", result.Username) + assert.Equal(t, "https://gitlab.com/tanuki", result.ProfileURL) + assert.Equal(t, "https://example.com/profile.png", result.Picture) + }) +} |
