github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/api/fakes/fake_authenticator.go (about)

     1  package fakes
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     5  	"github.com/cloudfoundry/cli/cf/errors"
     6  )
     7  
     8  type FakeAuthenticationRepository struct {
     9  	Config           core_config.ReadWriter
    10  	AuthenticateArgs struct {
    11  		Credentials []map[string]string
    12  	}
    13  	GetLoginPromptsWasCalled bool
    14  	GetLoginPromptsReturns   struct {
    15  		Error   error
    16  		Prompts map[string]core_config.AuthPrompt
    17  	}
    18  
    19  	AuthError          bool
    20  	AccessToken        string
    21  	RefreshToken       string
    22  	RefreshTokenCalled bool
    23  	RefreshTokenError  error
    24  }
    25  
    26  func (auth *FakeAuthenticationRepository) Authenticate(credentials map[string]string) (apiErr error) {
    27  	auth.AuthenticateArgs.Credentials = append(auth.AuthenticateArgs.Credentials, copyMap(credentials))
    28  
    29  	if auth.AuthError {
    30  		apiErr = errors.New("Error authenticating.")
    31  		return
    32  	}
    33  
    34  	if auth.AccessToken == "" {
    35  		auth.AccessToken = "BEARER some_access_token"
    36  	}
    37  
    38  	auth.Config.SetAccessToken(auth.AccessToken)
    39  	auth.Config.SetRefreshToken(auth.RefreshToken)
    40  
    41  	return
    42  }
    43  
    44  func (auth *FakeAuthenticationRepository) RefreshAuthToken() (string, error) {
    45  	auth.RefreshTokenCalled = true
    46  	if auth.RefreshTokenError == nil {
    47  		return auth.RefreshToken, nil
    48  	}
    49  	return "", auth.RefreshTokenError
    50  }
    51  
    52  func (auth *FakeAuthenticationRepository) GetLoginPromptsAndSaveUAAServerURL() (prompts map[string]core_config.AuthPrompt, apiErr error) {
    53  	auth.GetLoginPromptsWasCalled = true
    54  	prompts = auth.GetLoginPromptsReturns.Prompts
    55  	apiErr = auth.GetLoginPromptsReturns.Error
    56  	return
    57  }
    58  
    59  func copyMap(input map[string]string) map[string]string {
    60  	output := map[string]string{}
    61  	for key, val := range input {
    62  		output[key] = val
    63  	}
    64  	return output
    65  }