summaryrefslogtreecommitdiff
path: root/app/middleware/user_parser_test.go
blob: 2127a10b63fd1e342f95cec379484fed2b1172c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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)
	})
}