github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/securitygroup/running_security_groups_test.go (about) 1 package securitygroup_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/commandregistry" 5 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 6 "code.cloudfoundry.org/cli/cf/errors" 7 "code.cloudfoundry.org/cli/cf/models" 8 "code.cloudfoundry.org/cli/cf/requirements" 9 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 10 11 "code.cloudfoundry.org/cli/cf/api/securitygroups/defaults/running/runningfakes" 12 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 13 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 14 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 15 16 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 ) 20 21 var _ = Describe("Running-security-groups command", func() { 22 var ( 23 ui *testterm.FakeUI 24 configRepo coreconfig.Repository 25 fakeRunningSecurityGroupRepo *runningfakes.FakeSecurityGroupsRepo 26 requirementsFactory *requirementsfakes.FakeFactory 27 deps commandregistry.Dependency 28 ) 29 30 updateCommandDependency := func(pluginCall bool) { 31 deps.UI = ui 32 deps.RepoLocator = deps.RepoLocator.SetRunningSecurityGroupRepository(fakeRunningSecurityGroupRepo) 33 deps.Config = configRepo 34 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("running-security-groups").SetDependency(deps, pluginCall)) 35 } 36 37 BeforeEach(func() { 38 ui = &testterm.FakeUI{} 39 configRepo = testconfig.NewRepositoryWithDefaults() 40 fakeRunningSecurityGroupRepo = new(runningfakes.FakeSecurityGroupsRepo) 41 requirementsFactory = new(requirementsfakes.FakeFactory) 42 }) 43 44 runCommand := func(args ...string) bool { 45 return testcmd.RunCLICommand("running-security-groups", args, requirementsFactory, updateCommandDependency, false, ui) 46 } 47 48 Describe("requirements", func() { 49 It("should fail when not logged in", func() { 50 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 51 Expect(runCommand()).ToNot(HavePassedRequirements()) 52 }) 53 }) 54 55 Context("when the user is logged in", func() { 56 BeforeEach(func() { 57 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 58 }) 59 60 Context("when there are some security groups set in the Running group", func() { 61 BeforeEach(func() { 62 fakeRunningSecurityGroupRepo.ListReturns([]models.SecurityGroupFields{ 63 {Name: "hiphopopotamus"}, 64 {Name: "my lyrics are bottomless"}, 65 {Name: "steve"}, 66 }, nil) 67 }) 68 69 It("shows the user the name of the security groups of the Running set", func() { 70 Expect(runCommand()).To(BeTrue()) 71 Expect(ui.Outputs()).To(ContainSubstrings( 72 []string{"Acquiring", "security groups", "my-user"}, 73 []string{"hiphopopotamus"}, 74 []string{"my lyrics are bottomless"}, 75 []string{"steve"}, 76 )) 77 }) 78 }) 79 80 Context("when the API returns an error", func() { 81 BeforeEach(func() { 82 fakeRunningSecurityGroupRepo.ListReturns(nil, errors.New("uh oh")) 83 }) 84 85 It("fails loudly", func() { 86 runCommand() 87 Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"})) 88 }) 89 }) 90 91 Context("when there are no security groups set in the Running group", func() { 92 It("tells the user that there are none", func() { 93 runCommand() 94 Expect(ui.Outputs()).To(ContainSubstrings( 95 []string{"No", "security groups", "set"}, 96 )) 97 }) 98 }) 99 }) 100 })