github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/serviceauthtoken/update_service_auth_token_test.go (about) 1 package serviceauthtoken_test 2 3 import ( 4 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 5 "github.com/cloudfoundry/cli/cf/command_registry" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 "github.com/cloudfoundry/cli/cf/models" 8 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 9 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 10 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 11 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 12 13 . "github.com/cloudfoundry/cli/testhelpers/matchers" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("update-service-auth-token command", func() { 19 var ( 20 ui *testterm.FakeUI 21 configRepo core_config.Repository 22 authTokenRepo *testapi.FakeAuthTokenRepo 23 requirementsFactory *testreq.FakeReqFactory 24 deps command_registry.Dependency 25 ) 26 27 updateCommandDependency := func(pluginCall bool) { 28 deps.Ui = ui 29 deps.RepoLocator = deps.RepoLocator.SetServiceAuthTokenRepository(authTokenRepo) 30 deps.Config = configRepo 31 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("update-service-auth-token").SetDependency(deps, pluginCall)) 32 } 33 34 BeforeEach(func() { 35 ui = &testterm.FakeUI{Inputs: []string{"y"}} 36 authTokenRepo = &testapi.FakeAuthTokenRepo{} 37 configRepo = testconfig.NewRepositoryWithDefaults() 38 requirementsFactory = &testreq.FakeReqFactory{} 39 }) 40 41 runCommand := func(args ...string) bool { 42 return testcmd.RunCliCommand("update-service-auth-token", args, requirementsFactory, updateCommandDependency, false) 43 } 44 45 Describe("requirements", func() { 46 It("fails with usage when not provided exactly three args", func() { 47 requirementsFactory.LoginSuccess = true 48 runCommand("some-token-label", "a-provider") 49 Expect(ui.Outputs).To(ContainSubstrings( 50 []string{"Incorrect Usage", "Requires", "arguments"}, 51 )) 52 }) 53 54 It("fails when not logged in", func() { 55 Expect(runCommand("label", "provider", "token")).To(BeFalse()) 56 }) 57 }) 58 59 Context("when logged in and the service auth token exists", func() { 60 BeforeEach(func() { 61 requirementsFactory.LoginSuccess = true 62 foundAuthToken := models.ServiceAuthTokenFields{} 63 foundAuthToken.Guid = "found-auth-token-guid" 64 foundAuthToken.Label = "found label" 65 foundAuthToken.Provider = "found provider" 66 authTokenRepo.FindByLabelAndProviderServiceAuthTokenFields = foundAuthToken 67 }) 68 69 It("updates the service auth token with the provided args", func() { 70 runCommand("a label", "a provider", "a value") 71 72 expectedAuthToken := models.ServiceAuthTokenFields{} 73 expectedAuthToken.Guid = "found-auth-token-guid" 74 expectedAuthToken.Label = "found label" 75 expectedAuthToken.Provider = "found provider" 76 expectedAuthToken.Token = "a value" 77 78 Expect(ui.Outputs).To(ContainSubstrings( 79 []string{"Updating service auth token as", "my-user"}, 80 []string{"OK"}, 81 )) 82 83 Expect(authTokenRepo.FindByLabelAndProviderLabel).To(Equal("a label")) 84 Expect(authTokenRepo.FindByLabelAndProviderProvider).To(Equal("a provider")) 85 Expect(authTokenRepo.UpdatedServiceAuthTokenFields).To(Equal(expectedAuthToken)) 86 Expect(authTokenRepo.UpdatedServiceAuthTokenFields).To(Equal(expectedAuthToken)) 87 }) 88 }) 89 })