github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/commands/auth_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/cloudfoundry/cli/cf"
     7  	"github.com/cloudfoundry/cli/cf/api/authentication/authenticationfakes"
     8  	"github.com/cloudfoundry/cli/cf/commandregistry"
     9  	"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
    10  	"github.com/cloudfoundry/cli/cf/models"
    11  	"github.com/cloudfoundry/cli/cf/requirements"
    12  	"github.com/cloudfoundry/cli/cf/requirements/requirementsfakes"
    13  	"github.com/cloudfoundry/cli/cf/trace/tracefakes"
    14  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    15  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    16  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  
    20  	"os"
    21  
    22  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    23  )
    24  
    25  var _ = Describe("auth command", func() {
    26  	var (
    27  		ui                  *testterm.FakeUI
    28  		config              coreconfig.Repository
    29  		authRepo            *authenticationfakes.FakeRepository
    30  		requirementsFactory *requirementsfakes.FakeFactory
    31  		deps                commandregistry.Dependency
    32  		fakeLogger          *tracefakes.FakePrinter
    33  	)
    34  
    35  	updateCommandDependency := func(pluginCall bool) {
    36  		deps.UI = ui
    37  		deps.Config = config
    38  		deps.RepoLocator = deps.RepoLocator.SetAuthenticationRepository(authRepo)
    39  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("auth").SetDependency(deps, pluginCall))
    40  	}
    41  
    42  	BeforeEach(func() {
    43  		ui = &testterm.FakeUI{}
    44  		config = testconfig.NewRepositoryWithDefaults()
    45  		requirementsFactory = new(requirementsfakes.FakeFactory)
    46  		authRepo = new(authenticationfakes.FakeRepository)
    47  		authRepo.AuthenticateStub = func(credentials map[string]string) error {
    48  			config.SetAccessToken("my-access-token")
    49  			config.SetRefreshToken("my-refresh-token")
    50  			return nil
    51  		}
    52  
    53  		fakeLogger = new(tracefakes.FakePrinter)
    54  		deps = commandregistry.NewDependency(os.Stdout, fakeLogger, "")
    55  	})
    56  
    57  	Describe("requirements", func() {
    58  		It("fails with usage when given too few arguments", func() {
    59  			testcmd.RunCLICommand("auth", []string{}, requirementsFactory, updateCommandDependency, false, ui)
    60  
    61  			Expect(ui.Outputs()).To(ContainSubstrings(
    62  				[]string{"Incorrect Usage", "Requires", "arguments"},
    63  			))
    64  		})
    65  
    66  		It("fails if the user has not set an api endpoint", func() {
    67  			requirementsFactory.NewAPIEndpointRequirementReturns(requirements.Failing{Message: "no api set"})
    68  			Expect(testcmd.RunCLICommand("auth", []string{"username", "password"}, requirementsFactory, updateCommandDependency, false, ui)).To(BeFalse())
    69  		})
    70  	})
    71  
    72  	Context("when an api endpoint is targeted", func() {
    73  		BeforeEach(func() {
    74  			requirementsFactory.NewAPIEndpointRequirementReturns(requirements.Passing{})
    75  			config.SetAPIEndpoint("foo.example.org/authenticate")
    76  		})
    77  
    78  		It("authenticates successfully", func() {
    79  			requirementsFactory.NewAPIEndpointRequirementReturns(requirements.Passing{})
    80  			testcmd.RunCLICommand("auth", []string{"foo@example.com", "password"}, requirementsFactory, updateCommandDependency, false, ui)
    81  
    82  			Expect(ui.FailedWithUsage).To(BeFalse())
    83  			Expect(ui.Outputs()).To(ContainSubstrings(
    84  				[]string{"foo.example.org/authenticate"},
    85  				[]string{"OK"},
    86  			))
    87  
    88  			Expect(authRepo.AuthenticateArgsForCall(0)).To(Equal(map[string]string{
    89  				"username": "foo@example.com",
    90  				"password": "password",
    91  			}))
    92  		})
    93  
    94  		It("prompts users to upgrade if CLI version < min cli version requirement", func() {
    95  			config.SetMinCLIVersion("5.0.0")
    96  			config.SetMinRecommendedCLIVersion("5.5.0")
    97  			cf.Version = "4.5.0"
    98  
    99  			testcmd.RunCLICommand("auth", []string{"foo@example.com", "password"}, requirementsFactory, updateCommandDependency, false, ui)
   100  
   101  			Expect(ui.Outputs()).To(ContainSubstrings(
   102  				[]string{"To upgrade your CLI"},
   103  				[]string{"5.0.0"},
   104  			))
   105  		})
   106  
   107  		It("gets the UAA endpoint and saves it to the config file", func() {
   108  			requirementsFactory.NewAPIEndpointRequirementReturns(requirements.Passing{})
   109  			testcmd.RunCLICommand("auth", []string{"foo@example.com", "password"}, requirementsFactory, updateCommandDependency, false, ui)
   110  			Expect(authRepo.GetLoginPromptsAndSaveUAAServerURLCallCount()).To(Equal(1))
   111  		})
   112  
   113  		Describe("when authentication fails", func() {
   114  			BeforeEach(func() {
   115  				authRepo.AuthenticateReturns(errors.New("Error authenticating."))
   116  				testcmd.RunCLICommand("auth", []string{"username", "password"}, requirementsFactory, updateCommandDependency, false, ui)
   117  			})
   118  
   119  			It("does not prompt the user when provided username and password", func() {
   120  				Expect(ui.Outputs()).To(ContainSubstrings(
   121  					[]string{config.APIEndpoint()},
   122  					[]string{"Authenticating..."},
   123  					[]string{"FAILED"},
   124  					[]string{"Error authenticating"},
   125  				))
   126  			})
   127  
   128  			It("clears the user's session", func() {
   129  				Expect(config.AccessToken()).To(BeEmpty())
   130  				Expect(config.RefreshToken()).To(BeEmpty())
   131  				Expect(config.SpaceFields()).To(Equal(models.SpaceFields{}))
   132  				Expect(config.OrganizationFields()).To(Equal(models.OrganizationFields{}))
   133  			})
   134  		})
   135  	})
   136  })