(t *testing.T)
| 100 | } |
| 101 | |
| 102 | func TestGithubHandler_Enterprise(t *testing.T) { |
| 103 | jsonData := `{"id": 917408, "name": "Alyssa Hacker"}` |
| 104 | expectedUser := &github.User{ID: github.Int64(917408), Name: github.String("Alyssa Hacker")} |
| 105 | proxyClient, server := newGithubTestServer("/api/v3", jsonData) |
| 106 | defer server.Close() |
| 107 | |
| 108 | // oauth2 Client will use the proxy client's base Transport |
| 109 | ctx := context.WithValue(context.Background(), oauth2.HTTPClient, proxyClient) |
| 110 | anyToken := &oauth2.Token{AccessToken: "any-token"} |
| 111 | ctx = oauth2Login.WithToken(ctx, anyToken) |
| 112 | |
| 113 | config := &oauth2.Config{} |
| 114 | config.Endpoint.AuthURL = "https://github.mycompany.com/login/oauth/authorize" |
| 115 | success := func(w http.ResponseWriter, req *http.Request) { |
| 116 | ctx := req.Context() |
| 117 | githubUser, err := UserFromContext(ctx) |
| 118 | assert.Nil(t, err) |
| 119 | assert.Equal(t, expectedUser, githubUser) |
| 120 | fmt.Fprintf(w, "success handler called") |
| 121 | } |
| 122 | failure := testutils.AssertFailureNotCalled(t) |
| 123 | |
| 124 | // GithubHandler assert that: |
| 125 | // - Token is read from the ctx and passed to the GitHub API |
| 126 | // - github User is obtained from the GitHub API |
| 127 | // - success handler is called |
| 128 | // - github User is added to the ctx of the success handler |
| 129 | githubHandler := githubHandler(config, true, http.HandlerFunc(success), failure) |
| 130 | w := httptest.NewRecorder() |
| 131 | req, _ := http.NewRequest("GET", "/", nil) |
| 132 | githubHandler.ServeHTTP(w, req.WithContext(ctx)) |
| 133 | assert.Equal(t, "success handler called", w.Body.String()) |
| 134 | } |
| 135 | |
| 136 | func TestValidateResponse(t *testing.T) { |
| 137 | validUser := &github.User{ID: github.Int64(123)} |
nothing calls this directly
no test coverage detected