Skip to content

Commit

Permalink
add unit tests for proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Ilario <[email protected]>
  • Loading branch information
filariow committed Jul 12, 2024
1 parent fc99e4e commit 962c87c
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion pkg/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/codeready-toolchain/registration-service/pkg/signup"
"github.com/codeready-toolchain/registration-service/test"
"github.com/codeready-toolchain/registration-service/test/fake"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"

Expand Down Expand Up @@ -423,6 +424,9 @@ func (s *TestProxySuite) checkProxyOK(fakeApp *fake.ProxyFakeApp, p *Proxy, publ
ExpectedProxyResponseHeaders http.Header
ExpectedProxyResponseStatus int
Standalone bool // If true then the request is not expected to be forwarded to the kube api server

OverrideGetSignupFunc func(ctx *gin.Context, userID, username string, checkUserSignupCompleted bool) (*signup.Signup, error)
ExpectedResponse *string
}{
"plain http cors preflight request with no request method": {
ProxyRequestMethod: "OPTIONS",
Expand Down Expand Up @@ -569,6 +573,19 @@ func (s *TestProxySuite) checkProxyOK(fakeApp *fake.ProxyFakeApp, p *Proxy, publ
},
ExpectedProxyResponseStatus: http.StatusOK,
},
"error retrieving user workspaces": {
ProxyRequestMethod: "GET",
ProxyRequestHeaders: map[string][]string{"Authorization": {"Bearer " + s.token(userID)}},
ExpectedAPIServerRequestHeaders: map[string][]string{
"Authorization": {"Bearer clusterSAToken"},
},
ExpectedProxyResponseStatus: http.StatusInternalServerError,
// ExpectedResponse: "invalid workspace request: access to workspace 'alice-private' is forbidden",
OverrideGetSignupFunc: func(ctx *gin.Context, userID, username string, checkUserSignupCompleted bool) (*signup.Signup, error) {

Check failure on line 584 in pkg/proxy/proxy_test.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
return nil, fmt.Errorf("test error")
},
ExpectedResponse: ptr("unable to retrieve user workspaces: test error"),
},
}

rejectedHeaders := []headerToAdd{
Expand Down Expand Up @@ -720,6 +737,9 @@ func (s *TestProxySuite) checkProxyOK(fakeApp *fake.ProxyFakeApp, p *Proxy, publ
},
ProxyMetrics: p.metrics,
}
if tc.OverrideGetSignupFunc != nil {
p.spaceLister.GetSignupFunc = tc.OverrideGetSignupFunc
}
}

// when
Expand All @@ -731,7 +751,9 @@ func (s *TestProxySuite) checkProxyOK(fakeApp *fake.ProxyFakeApp, p *Proxy, publ
require.NotNil(s.T(), resp)
defer resp.Body.Close()
assert.Equal(s.T(), tc.ExpectedProxyResponseStatus, resp.StatusCode)
if !tc.Standalone {
if tc.ExpectedResponse != nil {
s.assertResponseBody(resp, *tc.ExpectedResponse)
} else if !tc.Standalone {
s.assertResponseBody(resp, "my response")
}
for hk, hv := range tc.ExpectedProxyResponseHeaders {
Expand Down Expand Up @@ -817,6 +839,10 @@ var noCORSHeaders = map[string][]string{
"Vary": {},
}

func ptr[T any](t T) *T {
return &t
}

func upgradeToWebsocket(req *http.Request) {
req.Header.Set("Connection", "upgrade")
req.Header.Set("Upgrade", "websocket")
Expand Down

0 comments on commit 962c87c

Please sign in to comment.