-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Config.LocalServerCallbackPath
(#235)
- Loading branch information
Showing
6 changed files
with
121 additions
and
18 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,86 @@ | ||
package e2e_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http/httptest" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/int128/oauth2cli" | ||
"github.com/int128/oauth2cli/e2e_test/authserver" | ||
"github.com/int128/oauth2cli/e2e_test/client" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
func TestLocalServerCallbackPath(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second) | ||
defer cancel() | ||
openBrowserCh := make(chan string) | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
defer close(openBrowserCh) | ||
// Start a local server and get a token. | ||
testServer := httptest.NewServer(&authserver.Handler{ | ||
TestingT: t, | ||
NewAuthorizationResponse: func(req authserver.AuthorizationRequest) string { | ||
if want := "email profile"; req.Scope != want { | ||
t.Errorf("scope wants %s but %s", want, req.Scope) | ||
return fmt.Sprintf("%s?error=invalid_scope", req.RedirectURI) | ||
} | ||
if !assertRedirectURI(t, req.RedirectURI, "http", "localhost", "/callback") { | ||
return fmt.Sprintf("%s?error=invalid_redirect_uri", req.RedirectURI) | ||
} | ||
return fmt.Sprintf("%s?state=%s&code=%s", req.RedirectURI, req.State, "AUTH_CODE") | ||
}, | ||
NewTokenResponse: func(req authserver.TokenRequest) (int, string) { | ||
if want := "AUTH_CODE"; req.Code != want { | ||
t.Errorf("code wants %s but %s", want, req.Code) | ||
return 400, invalidGrantResponse | ||
} | ||
return 200, validTokenResponse | ||
}, | ||
}) | ||
defer testServer.Close() | ||
cfg := oauth2cli.Config{ | ||
OAuth2Config: oauth2.Config{ | ||
ClientID: "YOUR_CLIENT_ID", | ||
ClientSecret: "YOUR_CLIENT_SECRET", | ||
Scopes: []string{"email", "profile"}, | ||
Endpoint: oauth2.Endpoint{ | ||
AuthURL: testServer.URL + "/auth", | ||
TokenURL: testServer.URL + "/token", | ||
}, | ||
}, | ||
LocalServerCallbackPath: "/callback", | ||
LocalServerReadyChan: openBrowserCh, | ||
LocalServerMiddleware: loggingMiddleware(t), | ||
Logf: t.Logf, | ||
} | ||
token, err := oauth2cli.GetToken(ctx, cfg) | ||
if err != nil { | ||
t.Errorf("could not get a token: %s", err) | ||
return | ||
} | ||
if token.AccessToken != "ACCESS_TOKEN" { | ||
t.Errorf("AccessToken wants %s but %s", "ACCESS_TOKEN", token.AccessToken) | ||
} | ||
if token.RefreshToken != "REFRESH_TOKEN" { | ||
t.Errorf("RefreshToken wants %s but %s", "REFRESH_TOKEN", token.RefreshToken) | ||
} | ||
}() | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
toURL, ok := <-openBrowserCh | ||
if !ok { | ||
t.Errorf("server already closed") | ||
return | ||
} | ||
client.GetAndVerify(t, toURL, 200, oauth2cli.DefaultLocalServerSuccessHTML) | ||
}() | ||
wg.Wait() | ||
} |
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
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