File: authorize_proxy_user_test.go

package info (click to toggle)
gitlab-agent 16.1.3-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 6,324 kB
  • sloc: makefile: 175; sh: 52; ruby: 3
file content (75 lines) | stat: -rw-r--r-- 2,069 bytes parent folder | download
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package api

import (
	"io"
	"net/http"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/mock_gitlab"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/testhelpers"
	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/testing/protocmp"
)

func TestAuthorizeProxyUser(t *testing.T) {
	const (
		configProjectId int64  = 21
		agentId         int64  = 42
		accessType      string = "session_cookie"
		accessKey       string = "damndeliciouscookie"
		csrfToken       string = "token"
	)
	ctx, _ := testhelpers.CtxWithSpanContext(t)
	response := &AuthorizeProxyUserResponse{
		Agent: &AuthorizedAgentForUser{
			Id:            agentId,
			ConfigProject: &ConfigProject{Id: configProjectId},
		},
		User: &User{
			Id:       1234,
			Username: "any-user",
		},
		AccessAs: &AccessAsProxyAuthorization{
			AccessAs: &AccessAsProxyAuthorization_User{
				User: &AccessAsUserAuthorization{
					Projects: []*ProjectAccessCF{
						{
							Id:    configProjectId,
							Roles: []string{"Developer"},
						},
					},
				},
			},
		},
	}
	gitLabClient := mock_gitlab.SetupClient(t, AuthorizeProxyUserApiPath, func(w http.ResponseWriter, r *http.Request) {
		testhelpers.AssertRequestMethod(t, r, http.MethodPost)

		data, err := io.ReadAll(r.Body)
		if !assert.NoError(t, err) {
			return
		}
		actual := &AuthorizeProxyUserRequest{}
		err = protojson.Unmarshal(data, actual)
		if !assert.NoError(t, err) {
			return
		}
		expected := &AuthorizeProxyUserRequest{
			AgentId:    agentId,
			AccessType: accessType,
			AccessKey:  accessKey,
			CsrfToken:  csrfToken,
		}

		assert.Empty(t, cmp.Diff(expected, actual, protocmp.Transform()))
		testhelpers.RespondWithJSON(t, w, response)
	})

	auth, err := AuthorizeProxyUser(ctx, gitLabClient, agentId, accessType, accessKey, csrfToken)
	require.NoError(t, err)

	assert.Equal(t, response.Agent.Id, auth.Agent.Id)
}