github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/securitygroup/unbind_security_group_test.go (about) 1 package securitygroup_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/cf/commandregistry" 7 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 8 "code.cloudfoundry.org/cli/cf/models" 9 "code.cloudfoundry.org/cli/cf/requirements" 10 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 11 12 "code.cloudfoundry.org/cli/cf/api/organizations/organizationsfakes" 13 "code.cloudfoundry.org/cli/cf/api/securitygroups/securitygroupsfakes" 14 "code.cloudfoundry.org/cli/cf/api/securitygroups/spaces/spacesfakes" 15 spacesapifakes "code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes" 16 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 17 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 18 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 19 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 20 . "github.com/onsi/ginkgo" 21 . "github.com/onsi/gomega" 22 ) 23 24 var _ = Describe("unbind-security-group command", func() { 25 var ( 26 ui *testterm.FakeUI 27 securityGroupRepo *securitygroupsfakes.FakeSecurityGroupRepo 28 orgRepo *organizationsfakes.FakeOrganizationRepository 29 spaceRepo *spacesapifakes.FakeSpaceRepository 30 secBinder *spacesfakes.FakeSecurityGroupSpaceBinder 31 requirementsFactory *requirementsfakes.FakeFactory 32 configRepo coreconfig.Repository 33 deps commandregistry.Dependency 34 ) 35 36 updateCommandDependency := func(pluginCall bool) { 37 deps.UI = ui 38 deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo) 39 deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo) 40 deps.RepoLocator = deps.RepoLocator.SetSecurityGroupRepository(securityGroupRepo) 41 deps.RepoLocator = deps.RepoLocator.SetSecurityGroupSpaceBinder(secBinder) 42 deps.Config = configRepo 43 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("unbind-security-group").SetDependency(deps, pluginCall)) 44 } 45 46 BeforeEach(func() { 47 ui = &testterm.FakeUI{} 48 requirementsFactory = new(requirementsfakes.FakeFactory) 49 securityGroupRepo = new(securitygroupsfakes.FakeSecurityGroupRepo) 50 orgRepo = new(organizationsfakes.FakeOrganizationRepository) 51 spaceRepo = new(spacesapifakes.FakeSpaceRepository) 52 secBinder = new(spacesfakes.FakeSecurityGroupSpaceBinder) 53 configRepo = testconfig.NewRepositoryWithDefaults() 54 }) 55 56 runCommand := func(args ...string) bool { 57 return testcmd.RunCLICommand("unbind-security-group", args, requirementsFactory, updateCommandDependency, false, ui) 58 } 59 60 Describe("requirements", func() { 61 It("should fail if not logged in", func() { 62 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 63 Expect(runCommand("my-group")).To(BeFalse()) 64 }) 65 66 It("should fail with usage when not provided with any arguments", func() { 67 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 68 runCommand() 69 Expect(ui.Outputs()).To(ContainSubstrings( 70 []string{"Incorrect Usage", "Requires", "arguments"}, 71 )) 72 }) 73 74 It("should fail with usage when provided with a number of arguments that is either 2 or 4 or a number larger than 4", func() { 75 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 76 runCommand("I", "like") 77 Expect(ui.Outputs()).To(ContainSubstrings( 78 []string{"Incorrect Usage", "Requires", "arguments"}, 79 )) 80 runCommand("Turn", "down", "for", "what") 81 Expect(ui.Outputs()).To(ContainSubstrings( 82 []string{"Incorrect Usage", "Requires", "arguments"}, 83 )) 84 runCommand("My", "Very", "Excellent", "Mother", "Just", "Sat", "Under", "Nine", "ThingsThatArentPlanets") 85 Expect(ui.Outputs()).To(ContainSubstrings( 86 []string{"Incorrect Usage", "Requires", "arguments"}, 87 )) 88 }) 89 }) 90 91 Context("when logged in", func() { 92 BeforeEach(func() { 93 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 94 }) 95 96 Context("when everything exists", func() { 97 BeforeEach(func() { 98 securityGroup := models.SecurityGroup{ 99 SecurityGroupFields: models.SecurityGroupFields{ 100 Name: "my-group", 101 GUID: "my-group-guid", 102 Rules: []map[string]interface{}{}, 103 }, 104 } 105 106 securityGroupRepo.ReadReturns(securityGroup, nil) 107 108 orgRepo.ListOrgsReturns([]models.Organization{{ 109 OrganizationFields: models.OrganizationFields{ 110 Name: "my-org", 111 GUID: "my-org-guid", 112 }}, 113 }, nil) 114 115 space := models.Space{SpaceFields: models.SpaceFields{Name: "my-space", GUID: "my-space-guid"}} 116 spaceRepo.FindByNameInOrgReturns(space, nil) 117 }) 118 119 It("removes the security group when we only pass the security group name (using the targeted org and space)", func() { 120 runCommand("my-group") 121 122 Expect(ui.Outputs()).To(ContainSubstrings( 123 []string{"Unbinding security group", "my-org", "my-space", "my-user"}, 124 []string{"OK"}, 125 )) 126 securityGroupGUID, spaceGUID := secBinder.UnbindSpaceArgsForCall(0) 127 Expect(securityGroupGUID).To(Equal("my-group-guid")) 128 Expect(spaceGUID).To(Equal("my-space-guid")) 129 }) 130 131 It("removes the security group when we pass the org and space", func() { 132 runCommand("my-group", "my-org", "my-space") 133 134 Expect(ui.Outputs()).To(ContainSubstrings( 135 []string{"Unbinding security group", "my-org", "my-space", "my-user"}, 136 []string{"OK"}, 137 )) 138 securityGroupGUID, spaceGUID := secBinder.UnbindSpaceArgsForCall(0) 139 Expect(securityGroupGUID).To(Equal("my-group-guid")) 140 Expect(spaceGUID).To(Equal("my-space-guid")) 141 }) 142 }) 143 144 Context("when one of the things does not exist", func() { 145 BeforeEach(func() { 146 securityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.New("I accidentally the")) 147 }) 148 149 It("fails with an error", func() { 150 runCommand("my-group", "my-org", "my-space") 151 Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"})) 152 }) 153 }) 154 }) 155 })