-
Notifications
You must be signed in to change notification settings - Fork 999
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
oauth2/google/stsexchange: implement basic test
- Loading branch information
1 parent
8591d35
commit 880daa6
Showing
1 changed file
with
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package oauth2_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"golang.org/x/oauth2" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"golang.org/x/oauth2/google/externalaccount" | ||
) | ||
|
||
var _ externalaccount.SubjectTokenSupplier = fakeSupplier{} | ||
|
||
type fakeSupplier struct{} | ||
|
||
func (f fakeSupplier) SubjectToken(_ context.Context, _ externalaccount.SupplierOptions) (string, error) { | ||
return "test-token", nil | ||
} | ||
|
||
var _ http.RoundTripper = fakeRT{} | ||
|
||
type fakeRT struct { | ||
body string | ||
} | ||
|
||
func (f fakeRT) RoundTrip(_ *http.Request) (*http.Response, error) { | ||
status := http.StatusUnauthorized | ||
return &http.Response{ | ||
StatusCode: status, | ||
Body: io.NopCloser(bytes.NewReader([]byte(f.body))), | ||
}, nil | ||
} | ||
|
||
func TestSTSExchange_error_handling(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Arrange | ||
body := `{"reason": "client does not exist"}` | ||
client := &http.Client{Transport: fakeRT{body: body}} | ||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, client) | ||
|
||
source, err := externalaccount.NewTokenSource(ctx, externalaccount.Config{ | ||
Audience: "aud", | ||
SubjectTokenType: "test-token", | ||
TokenURL: "url", | ||
Scopes: []string{}, | ||
SubjectTokenSupplier: fakeSupplier{}, | ||
}) | ||
if err != nil { | ||
t.Errorf("got unexpected error while token source building: %s", err) | ||
} | ||
|
||
// Act | ||
_, err = source.Token() | ||
|
||
// Assert | ||
if err == nil { | ||
t.Errorf("expected token issuance error") | ||
} | ||
var retrieveErr *oauth2.RetrieveError | ||
if !errors.As(err, &retrieveErr) { | ||
t.Errorf("expected an instance of RetrieveError, got error: %s", err) | ||
} | ||
|
||
if string(retrieveErr.Body) != body { | ||
t.Errorf("expected body content `%s`, got: `%s`", body, retrieveErr.Body) | ||
} | ||
|
||
if retrieveErr.Response.StatusCode != http.StatusUnauthorized { | ||
t.Errorf("expected unathorized status code, got: %s", retrieveErr.ErrorCode) | ||
} | ||
} |