MCPcopy Create free account
hub / github.com/GoogleChrome/webstatus.dev / testAuthScope

Function testAuthScope

backend/pkg/httpserver/middlewares_test.go:108–134  ·  view source on GitHub ↗
(t *testing.T, path string, method string, shouldBePresent bool, expectedUserInCtx *auth.User)

Source from the content-addressed store, hash-verified

106}
107
108func testAuthScope(t *testing.T, path string, method string, shouldBePresent bool, expectedUserInCtx *auth.User) {
109 authMiddleware := func(next http.Handler) http.Handler {
110 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
111 value := r.Context().Value(backend.BearerAuthScopes)
112 if shouldBePresent && value == nil {
113 t.Error("did not find bearer auth scope, expected it")
114 }
115 if !shouldBePresent && value != nil {
116 t.Error("found bearer auth scope, did not expect it")
117 }
118 if shouldBePresent && expectedUserInCtx != nil {
119 ctx := r.Context()
120 ctx = httpmiddlewares.AuthenticatedUserToContext(ctx, expectedUserInCtx)
121 r = r.WithContext(ctx)
122 }
123 next.ServeHTTP(w, r)
124 })
125 }
126 mockServer := &mockServerInterface{t: t, expectedUserInCtx: expectedUserInCtx, callCount: 0}
127 srv := createOpenAPIServerServer("", mockServer, []func(http.Handler) http.Handler{
128 recoveryMiddleware}, authMiddleware)
129 s := httptest.NewServer(srv.Handler)
130 defer s.Close()
131
132 submitRequest(t, s.URL+path, method)
133 mockServer.assertCallCount(1)
134}
135
136// This test ensures that the third-party OpenAPI library continues to add
137// Bearer authentication scopes to the request context when the route has

Calls 5

assertCallCountMethod · 0.95
submitRequestFunction · 0.85
ErrorMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected