github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/commands/securitygroup/unbind_staging_security_group_test.go (about) 1 package securitygroup_test 2 3 import ( 4 "github.com/cloudfoundry/cli/cf/api/securitygroups/defaults/staging/stagingfakes" 5 "github.com/cloudfoundry/cli/cf/api/securitygroups/securitygroupsfakes" 6 "github.com/cloudfoundry/cli/cf/commandregistry" 7 "github.com/cloudfoundry/cli/cf/configuration/coreconfig" 8 "github.com/cloudfoundry/cli/cf/errors" 9 "github.com/cloudfoundry/cli/cf/models" 10 "github.com/cloudfoundry/cli/cf/requirements" 11 "github.com/cloudfoundry/cli/cf/requirements/requirementsfakes" 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("unbind-staging-security-group command", func() { 22 var ( 23 ui *testterm.FakeUI 24 configRepo coreconfig.Repository 25 requirementsFactory *requirementsfakes.FakeFactory 26 fakeSecurityGroupRepo *securitygroupsfakes.FakeSecurityGroupRepo 27 fakeStagingSecurityGroupsRepo *stagingfakes.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.SetStagingSecurityGroupRepository(fakeStagingSecurityGroupsRepo) 35 deps.Config = configRepo 36 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("unbind-staging-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 fakeStagingSecurityGroupsRepo = new(stagingfakes.FakeSecurityGroupsRepo) 45 }) 46 47 runCommand := func(args ...string) bool { 48 return testcmd.RunCLICommand("unbind-staging-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 default staging group set", func() { 83 Expect(ui.Outputs()).To(ContainSubstrings( 84 []string{"Unbinding", "security group", "a-security-group-name", "my-user"}, 85 []string{"OK"}, 86 )) 87 88 Expect(fakeSecurityGroupRepo.ReadArgsForCall(0)).To(Equal("a-security-group-name")) 89 Expect(fakeStagingSecurityGroupsRepo.UnbindFromStagingSetArgsForCall(0)).To(Equal("just-pretend-this-is-a-guid")) 90 }) 91 }) 92 93 Context("when the security group does not exist", func() { 94 BeforeEach(func() { 95 fakeSecurityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.NewModelNotFoundError("security group", "anana-qui-parle")) 96 }) 97 98 It("warns the user", func() { 99 runCommand("anana-qui-parle") 100 Expect(ui.WarnOutputs).To(ContainSubstrings( 101 []string{"Security group", "anana-qui-parle", "does not exist"}, 102 )) 103 104 Expect(ui.Outputs()).To(ContainSubstrings( 105 []string{"OK"}, 106 )) 107 }) 108 }) 109 }) 110 })