github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/commands/securitygroup/staging_security_groups_test.go (about) 1 package securitygroup_test 2 3 import ( 4 "github.com/cloudfoundry/cli/cf/commandregistry" 5 "github.com/cloudfoundry/cli/cf/configuration/coreconfig" 6 "github.com/cloudfoundry/cli/cf/errors" 7 "github.com/cloudfoundry/cli/cf/models" 8 "github.com/cloudfoundry/cli/cf/requirements" 9 "github.com/cloudfoundry/cli/cf/requirements/requirementsfakes" 10 11 "github.com/cloudfoundry/cli/cf/api/securitygroups/defaults/staging/stagingfakes" 12 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 13 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 14 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 15 16 . "github.com/cloudfoundry/cli/testhelpers/matchers" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 ) 20 21 var _ = Describe("staging-security-groups command", func() { 22 var ( 23 ui *testterm.FakeUI 24 configRepo coreconfig.Repository 25 fakeStagingSecurityGroupRepo *stagingfakes.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.SetStagingSecurityGroupRepository(fakeStagingSecurityGroupRepo) 33 deps.Config = configRepo 34 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("staging-security-groups").SetDependency(deps, pluginCall)) 35 } 36 37 BeforeEach(func() { 38 ui = &testterm.FakeUI{} 39 configRepo = testconfig.NewRepositoryWithDefaults() 40 fakeStagingSecurityGroupRepo = new(stagingfakes.FakeSecurityGroupsRepo) 41 requirementsFactory = new(requirementsfakes.FakeFactory) 42 }) 43 44 runCommand := func(args ...string) bool { 45 return testcmd.RunCLICommand("staging-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 for staging", func() { 61 BeforeEach(func() { 62 fakeStagingSecurityGroupRepo.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 for staging", func() { 70 Expect(runCommand()).To(BeTrue()) 71 Expect(ui.Outputs()).To(ContainSubstrings( 72 []string{"Acquiring", "staging security group", "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 fakeStagingSecurityGroupRepo.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 for staging", func() { 92 It("tells the user that there are none", func() { 93 runCommand() 94 Expect(ui.Outputs()).To(ContainSubstrings( 95 []string{"No", "staging security group", "set"}, 96 )) 97 }) 98 }) 99 }) 100 })