diff options
author | feistel <6742251-feistel@users.noreply.gitlab.com> | 2021-07-21 17:10:59 +0300 |
---|---|---|
committer | feistel <6742251-feistel@users.noreply.gitlab.com> | 2021-07-22 15:26:08 +0300 |
commit | 8b78197eeccc03ef7ca7f2d3c15f3781e9a46611 (patch) | |
tree | 6dad68f944095849c90d9a3f7b17a9036e6577c1 /test/acceptance/helpers_test.go | |
parent | 7a9c492b619078aed6f9c3f95cf21640afd63100 (diff) |
Add auth handlers to stub gitlab server
Diffstat (limited to 'test/acceptance/helpers_test.go')
-rw-r--r-- | test/acceptance/helpers_test.go | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/test/acceptance/helpers_test.go b/test/acceptance/helpers_test.go index 2aa7041d..11c0f381 100644 --- a/test/acceptance/helpers_test.go +++ b/test/acceptance/helpers_test.go @@ -273,10 +273,6 @@ func RunPagesProcessWithAuth(t *testing.T, pagesBinary string, listeners []Liste return cleanup2 } -func RunPagesProcessWithGitlabServer(t *testing.T, pagesBinary string, listeners []ListenSpec, promPort string, gitlabServer string) func() { - return runPagesProcessWithGitlabServer(t, pagesBinary, listeners, promPort, nil, gitlabServer) -} - func RunPagesProcessWithGitlabServerWithSSLCertFile(t *testing.T, pagesBinary string, listeners []ListenSpec, promPort string, sslCertFile string, gitlabServer string) func() { return runPagesProcessWithGitlabServer(t, pagesBinary, listeners, promPort, []string{"SSL_CERT_FILE=" + sslCertFile}, gitlabServer) @@ -581,6 +577,8 @@ type stubOpts struct { m sync.RWMutex apiCalled bool statusReadyCount int + authHandler http.HandlerFunc + userHandler http.HandlerFunc statusHandler http.HandlerFunc pagesHandler http.HandlerFunc pagesStatusResponse int @@ -616,6 +614,20 @@ func NewGitlabDomainsSourceStub(t *testing.T, opts *stubOpts) *httptest.Server { mux.HandleFunc("/api/v4/internal/pages", pagesHandler) + authHandler := defaultAuthHandler(t, opts) + if opts.authHandler != nil { + authHandler = opts.authHandler + } + + mux.HandleFunc("/oauth/token", authHandler) + + userHandler := defaultUserHandler(t, opts) + if opts.userHandler != nil { + userHandler = opts.userHandler + } + + mux.HandleFunc("/api/v4/user", userHandler) + return httptest.NewServer(mux) } @@ -679,6 +691,25 @@ func defaultAPIHandler(t *testing.T, opts *stubOpts) http.HandlerFunc { } } +func defaultAuthHandler(t *testing.T, opts *stubOpts) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, "POST", r.Method) + err := json.NewEncoder(w).Encode(struct { + AccessToken string `json:"access_token"` + }{ + AccessToken: "abc", + }) + require.NoError(t, err) + } +} + +func defaultUserHandler(t *testing.T, opts *stubOpts) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, "Bearer abc", r.Header.Get("Authorization")) + w.WriteHeader(http.StatusOK) + } +} + func newConfigFile(t *testing.T, configs ...string) string { t.Helper() |