-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle failed status code in provider requests (#313)
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package provider_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/netlify/gotrue/api/provider" | ||
"github.com/netlify/gotrue/conf" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
func TestGithubFail(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
t.Cleanup(cancel) | ||
|
||
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
rw.WriteHeader(http.StatusTeapot) | ||
fmt.Fprint(rw, "Something failed") | ||
})) | ||
t.Cleanup(srv.Close) | ||
|
||
gh, err := provider.NewGithubProvider(conf.OAuthProviderConfiguration{ | ||
ClientID: "client-id", | ||
Secret: "secret", | ||
RedirectURI: "https://redirect.example.org/callback", | ||
URL: srv.URL, | ||
Enabled: true, | ||
}) | ||
require.NoError(t, err) | ||
|
||
user, err := gh.GetUserData(ctx, &oauth2.Token{ | ||
AccessToken: "my-token", | ||
Expiry: time.Now().Add(time.Minute), | ||
}) | ||
require.Error(t, err) | ||
require.Nil(t, user) | ||
assert.Equal(t, "Request failed with status 418:\nSomething failed", err.Error()) | ||
} |