fakeGoogleOAuth starts a test server that mimics Google's token and userinfo endpoints. Returns the server and an oauth2.Config pointing at it.
(t *testing.T)
| 200 | // fakeGoogleOAuth starts a test server that mimics Google's token and userinfo |
| 201 | // endpoints. Returns the server and an oauth2.Config pointing at it. |
| 202 | func fakeGoogleOAuth(t *testing.T) (*httptest.Server, *oauth2.Config) { |
| 203 | t.Helper() |
| 204 | mux := http.NewServeMux() |
| 205 | mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) { |
| 206 | w.Header().Set("Content-Type", "application/json") |
| 207 | json.NewEncoder(w).Encode(map[string]interface{}{ |
| 208 | "access_token": "fake-access-token", |
| 209 | "token_type": "Bearer", |
| 210 | "expires_in": 3600, |
| 211 | }) |
| 212 | }) |
| 213 | mux.HandleFunc("/userinfo", func(w http.ResponseWriter, r *http.Request) { |
| 214 | w.Header().Set("Content-Type", "application/json") |
| 215 | json.NewEncoder(w).Encode(map[string]interface{}{ |
| 216 | "sub": "google-sub-cli-test", |
| 217 | "email": "cliuser@test.com", |
| 218 | "email_verified": true, |
| 219 | "name": "CLI User", |
| 220 | }) |
| 221 | }) |
| 222 | srv := httptest.NewServer(mux) |
| 223 | t.Cleanup(srv.Close) |
| 224 | |
| 225 | oauthCfg := &oauth2.Config{ |
| 226 | ClientID: "test", |
| 227 | ClientSecret: "test", |
| 228 | RedirectURL: "http://localhost/api/auth/callback", |
| 229 | Endpoint: oauth2.Endpoint{ |
| 230 | TokenURL: srv.URL + "/token", |
| 231 | AuthURL: srv.URL + "/authorize", |
| 232 | }, |
| 233 | Scopes: []string{"openid", "email", "profile"}, |
| 234 | } |
| 235 | return srv, oauthCfg |
| 236 | } |
| 237 | |
| 238 | // setupUserAuthWithFakeOAuth creates a UserAuth backed by a fake Google OAuth |
| 239 | // server so we can test HandleCallback without hitting real Google. |
no test coverage detected