github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/auth_test.go (about) 1 package commands_test 2 3 import ( 4 "github.com/cloudfoundry/cli/cf" 5 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 6 "github.com/cloudfoundry/cli/cf/command_registry" 7 "github.com/cloudfoundry/cli/cf/configuration/core_config" 8 "github.com/cloudfoundry/cli/cf/models" 9 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 10 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 11 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 12 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 16 . "github.com/cloudfoundry/cli/testhelpers/matchers" 17 ) 18 19 var _ = Describe("auth command", func() { 20 var ( 21 ui *testterm.FakeUI 22 config core_config.Repository 23 repo *testapi.FakeAuthenticationRepository 24 requirementsFactory *testreq.FakeReqFactory 25 deps command_registry.Dependency 26 ) 27 28 updateCommandDependency := func(pluginCall bool) { 29 deps.Ui = ui 30 deps.Config = config 31 deps.RepoLocator = deps.RepoLocator.SetAuthenticationRepository(repo) 32 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("auth").SetDependency(deps, pluginCall)) 33 } 34 35 BeforeEach(func() { 36 ui = &testterm.FakeUI{} 37 config = testconfig.NewRepositoryWithDefaults() 38 requirementsFactory = &testreq.FakeReqFactory{} 39 repo = &testapi.FakeAuthenticationRepository{ 40 Config: config, 41 AccessToken: "my-access-token", 42 RefreshToken: "my-refresh-token", 43 } 44 45 deps = command_registry.NewDependency() 46 }) 47 48 Describe("requirements", func() { 49 It("fails with usage when given too few arguments", func() { 50 testcmd.RunCliCommand("auth", []string{}, requirementsFactory, updateCommandDependency, false) 51 52 Expect(ui.Outputs).To(ContainSubstrings( 53 []string{"Incorrect Usage", "Requires", "arguments"}, 54 )) 55 }) 56 57 It("fails if the user has not set an api endpoint", func() { 58 Expect(testcmd.RunCliCommand("auth", []string{"username", "password"}, requirementsFactory, updateCommandDependency, false)).To(BeFalse()) 59 }) 60 }) 61 62 Context("when an api endpoint is targeted", func() { 63 BeforeEach(func() { 64 requirementsFactory.ApiEndpointSuccess = true 65 config.SetApiEndpoint("foo.example.org/authenticate") 66 }) 67 68 It("authenticates successfully", func() { 69 requirementsFactory.ApiEndpointSuccess = true 70 testcmd.RunCliCommand("auth", []string{"foo@example.com", "password"}, requirementsFactory, updateCommandDependency, false) 71 72 Expect(ui.FailedWithUsage).To(BeFalse()) 73 Expect(ui.Outputs).To(ContainSubstrings( 74 []string{"foo.example.org/authenticate"}, 75 []string{"OK"}, 76 )) 77 78 Expect(repo.AuthenticateArgs.Credentials).To(Equal([]map[string]string{ 79 { 80 "username": "foo@example.com", 81 "password": "password", 82 }, 83 })) 84 }) 85 86 It("prompts users to upgrade if CLI version < min cli version requirement", func() { 87 config.SetMinCliVersion("5.0.0") 88 config.SetMinRecommendedCliVersion("5.5.0") 89 cf.Version = "4.5.0" 90 91 testcmd.RunCliCommand("auth", []string{"foo@example.com", "password"}, requirementsFactory, updateCommandDependency, false) 92 93 Expect(ui.Outputs).To(ContainSubstrings( 94 []string{"To upgrade your CLI"}, 95 []string{"5.0.0"}, 96 )) 97 }) 98 99 It("gets the UAA endpoint and saves it to the config file", func() { 100 requirementsFactory.ApiEndpointSuccess = true 101 testcmd.RunCliCommand("auth", []string{"foo@example.com", "password"}, requirementsFactory, updateCommandDependency, false) 102 Expect(repo.GetLoginPromptsWasCalled).To(BeTrue()) 103 }) 104 105 Describe("when authentication fails", func() { 106 BeforeEach(func() { 107 repo.AuthError = true 108 testcmd.RunCliCommand("auth", []string{"username", "password"}, requirementsFactory, updateCommandDependency, false) 109 }) 110 111 It("does not prompt the user when provided username and password", func() { 112 Expect(ui.Outputs).To(ContainSubstrings( 113 []string{config.ApiEndpoint()}, 114 []string{"Authenticating..."}, 115 []string{"FAILED"}, 116 []string{"Error authenticating"}, 117 )) 118 }) 119 120 It("clears the user's session", func() { 121 Expect(config.AccessToken()).To(BeEmpty()) 122 Expect(config.RefreshToken()).To(BeEmpty()) 123 Expect(config.SpaceFields()).To(Equal(models.SpaceFields{})) 124 Expect(config.OrganizationFields()).To(Equal(models.OrganizationFields{})) 125 }) 126 }) 127 }) 128 })