go.uber.org/cadence@v1.2.9/internal/oauth_authorization_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"golang.org/x/oauth2"
     9  )
    10  
    11  type fakeOKTokenSource struct{}
    12  type fakeFailingTokenSource struct{}
    13  
    14  func (f *fakeOKTokenSource) Token() (*oauth2.Token, error) {
    15  	return &oauth2.Token{AccessToken: "token"}, nil
    16  }
    17  
    18  func (f *fakeFailingTokenSource) Token() (*oauth2.Token, error) { return nil, errors.New("error") }
    19  
    20  func TestNewOAuthAuthorizationProviderEmptyConfigFails(t *testing.T) {
    21  	p := NewOAuthAuthorizationProvider(OAuthAuthorizerConfig{})
    22  	_, err := p.GetAuthToken()
    23  	assert.Error(t, err, "empty config will always fail")
    24  }
    25  
    26  func TestNewOAuthAuthorizationProviderExtraParams(t *testing.T) {
    27  	p := NewOAuthAuthorizationProvider(OAuthAuthorizerConfig{
    28  		EndpointParams: map[string]string{"test1": "test2"},
    29  	})
    30  	assert.NotEmpty(t, p.config.EndpointParams)
    31  }
    32  
    33  func TestNewOAuthAuthorizationProviderFailingTokenSourceResultsInError(t *testing.T) {
    34  	p := &OAuthProvider{tokenSource: &fakeFailingTokenSource{}}
    35  	token, err := p.GetAuthToken()
    36  	assert.Nil(t, token)
    37  	assert.Error(t, err, "error from tokensource will result in error")
    38  }
    39  
    40  func TestNewOAuthAuthorizationProviderTokenSourceReturnsToken(t *testing.T) {
    41  	p := &OAuthProvider{tokenSource: &fakeOKTokenSource{}}
    42  	token, err := p.GetAuthToken()
    43  	assert.Equal(t, []byte("token"), token)
    44  	assert.NoError(t, err)
    45  }