Skip to content

Commit

Permalink
Merge pull request #154 from auth0/test-token-retriever
Browse files Browse the repository at this point in the history
test: token retriever
  • Loading branch information
Widcket authored Mar 16, 2021
2 parents e4e613c + 0b0f457 commit 1ef8cde
Show file tree
Hide file tree
Showing 4 changed files with 452 additions and 26 deletions.
1 change: 1 addition & 0 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:generate mockgen -source auth.go -destination mock/auth.go -package mock
package auth

import (
Expand Down
76 changes: 76 additions & 0 deletions internal/auth/mock/auth.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions internal/auth/token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package auth

import (
"bytes"
"context"
"io/ioutil"
"net/http"
"testing"

"github.com/auth0/auth0-cli/internal/auth/mock"
"github.com/golang/mock/gomock"
)

// HTTPTransport implements an http.RoundTripper for testing purposes only.
type testTransport struct {
withResponse *http.Response
withError error
requests []*http.Request
}

func (t *testTransport) RoundTrip(req *http.Request) (*http.Response, error) {
t.requests = append(t.requests, req)
return t.withResponse, t.withError
}

func TestTokenRetriever_Refresh(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

secretsMock := mock.NewMockSecretStore(ctrl)
secretsMock.EXPECT().Get("auth0-cli", "mytenant").Return("refresh-token-here", nil).Times(1)

transport := &testTransport{
withResponse: &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(`{
"access_token": "access-token-here",
"id_token": "id-token-here",
"token_type": "token-type-here",
"expires_in": 1000
}`))),
},
}

client := &http.Client{Transport: transport}

tr := &TokenRetriever{
Secrets: secretsMock,
Client: client,
}

got, err := tr.Refresh(context.Background(), "mytenant")
if err != nil {
t.Fatal(err)
}

want := TokenResponse{
AccessToken: "access-token-here",
IDToken: "id-token-here",
TokenType: "token-type-here",
ExpiresIn: 1000,
}

if want != got {
t.Fatalf("wanted: %v, got: %v", want, got)
}

req := transport.requests[0]
err = req.ParseForm()
if err != nil {
t.Fatal(err)
}

if want, got := "https://auth0.auth0.com/oauth/token", req.URL.String(); want != got {
t.Fatalf("wanted request URL: %v, got: %v", want, got)
}
if want, got := "refresh_token", req.Form["grant_type"][0]; want != got {
t.Fatalf("wanted grant_type: %v, got: %v", want, got)
}
if want, got := "2iZo3Uczt5LFHacKdM0zzgUO2eG2uDjT", req.Form["client_id"][0]; want != got {
t.Fatalf("wanted grant_type: %v, got: %v", want, got)
}
if want, got := "refresh-token-here", req.Form["refresh_token"][0]; want != got {
t.Fatalf("wanted grant_type: %v, got: %v", want, got)
}
})
}
Loading

0 comments on commit 1ef8cde

Please sign in to comment.