github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/serviceauthtoken/service_auth_tokens_test.go (about) 1 package serviceauthtoken_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/apifakes" 5 "code.cloudfoundry.org/cli/cf/commandregistry" 6 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 7 "code.cloudfoundry.org/cli/cf/flags" 8 "code.cloudfoundry.org/cli/cf/models" 9 "code.cloudfoundry.org/cli/cf/requirements" 10 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 11 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 12 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 13 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 14 15 "code.cloudfoundry.org/cli/cf/commands/serviceauthtoken" 16 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 ) 20 21 var _ = Describe("service-auth-tokens command", func() { 22 var ( 23 ui *testterm.FakeUI 24 configRepo coreconfig.Repository 25 authTokenRepo *apifakes.OldFakeAuthTokenRepo 26 requirementsFactory *requirementsfakes.FakeFactory 27 deps commandregistry.Dependency 28 ) 29 30 updateCommandDependency := func(pluginCall bool) { 31 deps.UI = ui 32 deps.RepoLocator = deps.RepoLocator.SetServiceAuthTokenRepository(authTokenRepo) 33 deps.Config = configRepo 34 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("service-auth-tokens").SetDependency(deps, pluginCall)) 35 } 36 37 BeforeEach(func() { 38 ui = &testterm.FakeUI{Inputs: []string{"y"}} 39 authTokenRepo = new(apifakes.OldFakeAuthTokenRepo) 40 configRepo = testconfig.NewRepositoryWithDefaults() 41 requirementsFactory = new(requirementsfakes.FakeFactory) 42 }) 43 44 runCommand := func(args ...string) bool { 45 return testcmd.RunCLICommand("service-auth-tokens", args, requirementsFactory, updateCommandDependency, false, ui) 46 } 47 48 Describe("requirements", func() { 49 It("fails when not logged in", func() { 50 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 51 Expect(runCommand()).To(BeFalse()) 52 }) 53 54 Context("when arguments are provided", func() { 55 var cmd commandregistry.Command 56 var flagContext flags.FlagContext 57 58 BeforeEach(func() { 59 cmd = &serviceauthtoken.ListServiceAuthTokens{} 60 cmd.SetDependency(deps, false) 61 flagContext = flags.NewFlagContext(cmd.MetaData().Flags) 62 }) 63 64 It("should fail with usage", func() { 65 flagContext.Parse("blahblah") 66 67 reqs, err := cmd.Requirements(requirementsFactory, flagContext) 68 Expect(err).NotTo(HaveOccurred()) 69 70 err = testcmd.RunRequirements(reqs) 71 Expect(err).To(HaveOccurred()) 72 Expect(err.Error()).To(ContainSubstring("Incorrect Usage")) 73 Expect(err.Error()).To(ContainSubstring("No argument required")) 74 }) 75 }) 76 77 It("requires CC API version 2.47 or greater", func() { 78 requirementsFactory.NewMaxAPIVersionRequirementReturns(requirements.Failing{Message: "max api 2.47"}) 79 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 80 Expect(runCommand()).To(BeFalse()) 81 }) 82 }) 83 84 Context("when logged in and some service auth tokens exist", func() { 85 BeforeEach(func() { 86 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 87 requirementsFactory.NewMaxAPIVersionRequirementReturns(requirements.Passing{}) 88 89 authTokenRepo.FindAllAuthTokens = []models.ServiceAuthTokenFields{ 90 {Label: "a label", Provider: "a provider"}, 91 {Label: "a second label", Provider: "a second provider"}, 92 } 93 }) 94 95 It("shows you the service auth tokens", func() { 96 runCommand() 97 98 Expect(ui.Outputs()).To(ContainSubstrings( 99 []string{"Getting service auth tokens as", "my-user"}, 100 []string{"OK"}, 101 []string{"label", "provider"}, 102 []string{"a label", "a provider"}, 103 []string{"a second label", "a second provider"}, 104 )) 105 }) 106 }) 107 })