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