github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/securitygroup/unbind_running_security_group_test.go (about) 1 package securitygroup_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/securitygroups/defaults/running/runningfakes" 5 "code.cloudfoundry.org/cli/cf/api/securitygroups/securitygroupsfakes" 6 "code.cloudfoundry.org/cli/cf/commandregistry" 7 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 8 "code.cloudfoundry.org/cli/cf/errors" 9 "code.cloudfoundry.org/cli/cf/models" 10 "code.cloudfoundry.org/cli/cf/requirements" 11 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 12 testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands" 13 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 14 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 15 16 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 ) 20 21 var _ = Describe("unbind-running-security-group command", func() { 22 var ( 23 ui *testterm.FakeUI 24 configRepo coreconfig.Repository 25 requirementsFactory *requirementsfakes.FakeFactory 26 fakeSecurityGroupRepo *securitygroupsfakes.FakeSecurityGroupRepo 27 fakeRunningSecurityGroupsRepo *runningfakes.FakeSecurityGroupsRepo 28 deps commandregistry.Dependency 29 ) 30 31 updateCommandDependency := func(pluginCall bool) { 32 deps.UI = ui 33 deps.RepoLocator = deps.RepoLocator.SetSecurityGroupRepository(fakeSecurityGroupRepo) 34 deps.RepoLocator = deps.RepoLocator.SetRunningSecurityGroupRepository(fakeRunningSecurityGroupsRepo) 35 deps.Config = configRepo 36 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("unbind-running-security-group").SetDependency(deps, pluginCall)) 37 } 38 39 BeforeEach(func() { 40 ui = &testterm.FakeUI{} 41 configRepo = testconfig.NewRepositoryWithDefaults() 42 requirementsFactory = new(requirementsfakes.FakeFactory) 43 fakeSecurityGroupRepo = new(securitygroupsfakes.FakeSecurityGroupRepo) 44 fakeRunningSecurityGroupsRepo = new(runningfakes.FakeSecurityGroupsRepo) 45 }) 46 47 runCommand := func(args ...string) bool { 48 return testcmd.RunCLICommand("unbind-running-security-group", args, requirementsFactory, updateCommandDependency, false, ui) 49 } 50 51 Describe("requirements", func() { 52 It("fails when the user is not logged in", func() { 53 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 54 Expect(runCommand("name")).To(BeFalse()) 55 }) 56 57 It("fails with usage when a name is not provided", func() { 58 runCommand() 59 Expect(ui.Outputs()).To(ContainSubstrings( 60 []string{"Incorrect Usage", "Requires", "argument"}, 61 )) 62 }) 63 }) 64 65 Context("when the user is logged in and provides the name of a group", func() { 66 BeforeEach(func() { 67 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 68 }) 69 70 Context("security group exists", func() { 71 BeforeEach(func() { 72 group := models.SecurityGroup{} 73 group.GUID = "just-pretend-this-is-a-guid" 74 group.Name = "a-security-group-name" 75 fakeSecurityGroupRepo.ReadReturns(group, nil) 76 }) 77 78 JustBeforeEach(func() { 79 runCommand("a-security-group-name") 80 }) 81 82 It("unbinds the group from the running group set", func() { 83 Expect(ui.Outputs()).To(ContainSubstrings( 84 []string{"Unbinding", "security group", "a-security-group-name", "my-user"}, 85 []string{"TIP: Changes will not apply to existing running applications until they are restarted."}, 86 []string{"OK"}, 87 )) 88 89 Expect(fakeSecurityGroupRepo.ReadArgsForCall(0)).To(Equal("a-security-group-name")) 90 Expect(fakeRunningSecurityGroupsRepo.UnbindFromRunningSetArgsForCall(0)).To(Equal("just-pretend-this-is-a-guid")) 91 }) 92 }) 93 94 Context("when the security group does not exist", func() { 95 BeforeEach(func() { 96 fakeSecurityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.NewModelNotFoundError("security group", "anana-qui-parle")) 97 }) 98 99 It("warns the user", func() { 100 runCommand("anana-qui-parle") 101 Expect(ui.WarnOutputs).To(ContainSubstrings( 102 []string{"Security group", "anana-qui-parle", "does not exist"}, 103 )) 104 105 Expect(ui.Outputs()).To(ContainSubstrings( 106 []string{"OK"}, 107 )) 108 }) 109 }) 110 }) 111 })