(t *testing.T)
| 15 | ) |
| 16 | |
| 17 | func TestBitbucketHandler(t *testing.T) { |
| 18 | jsonData := `{"username": "bitster", "display_name": "Atlas Ian"}` |
| 19 | expectedUser := &User{Username: "bitster", DisplayName: "Atlas Ian"} |
| 20 | proxyClient, server := newBitbucketTestServer(jsonData) |
| 21 | defer server.Close() |
| 22 | // oauth2 Client will use the proxy client's base Transport |
| 23 | ctx := context.WithValue(context.Background(), oauth2.HTTPClient, proxyClient) |
| 24 | anyToken := &oauth2.Token{AccessToken: "any-token"} |
| 25 | ctx = oauth2Login.WithToken(ctx, anyToken) |
| 26 | |
| 27 | config := &oauth2.Config{} |
| 28 | success := func(w http.ResponseWriter, req *http.Request) { |
| 29 | ctx := req.Context() |
| 30 | bitbucketUser, err := UserFromContext(ctx) |
| 31 | assert.Nil(t, err) |
| 32 | assert.Equal(t, expectedUser, bitbucketUser) |
| 33 | fmt.Fprintf(w, "success handler called") |
| 34 | } |
| 35 | failure := testutils.AssertFailureNotCalled(t) |
| 36 | |
| 37 | // BitbucketHandler assert that: |
| 38 | // - Token is read from the ctx and passed to the Bitbucket API |
| 39 | // - bitbucket User is obtained from the Bitbucket API |
| 40 | // - success handler is called |
| 41 | // - bitbucket User is added to the ctx of the success handler |
| 42 | bitbucketHandler := bitbucketHandler(config, http.HandlerFunc(success), failure) |
| 43 | w := httptest.NewRecorder() |
| 44 | req, _ := http.NewRequest("GET", "/", nil) |
| 45 | bitbucketHandler.ServeHTTP(w, req.WithContext(ctx)) |
| 46 | assert.Equal(t, "success handler called", w.Body.String()) |
| 47 | } |
| 48 | |
| 49 | func TestBitbucketHandler_MissingCtxToken(t *testing.T) { |
| 50 | config := &oauth2.Config{} |
nothing calls this directly
no test coverage detected