github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/securitygroup/delete_security_group_test.go (about) 1 package securitygroup_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/securitygroups/securitygroupsfakes" 5 "code.cloudfoundry.org/cli/cf/commandregistry" 6 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 7 "code.cloudfoundry.org/cli/cf/errors" 8 "code.cloudfoundry.org/cli/cf/models" 9 "code.cloudfoundry.org/cli/cf/requirements" 10 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 11 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 12 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 13 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 14 15 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 ) 19 20 var _ = Describe("delete-security-group command", func() { 21 var ( 22 ui *testterm.FakeUI 23 securityGroupRepo *securitygroupsfakes.FakeSecurityGroupRepo 24 requirementsFactory *requirementsfakes.FakeFactory 25 configRepo coreconfig.Repository 26 deps commandregistry.Dependency 27 ) 28 29 updateCommandDependency := func(pluginCall bool) { 30 deps.UI = ui 31 deps.RepoLocator = deps.RepoLocator.SetSecurityGroupRepository(securityGroupRepo) 32 deps.Config = configRepo 33 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-security-group").SetDependency(deps, pluginCall)) 34 } 35 36 BeforeEach(func() { 37 ui = &testterm.FakeUI{} 38 requirementsFactory = new(requirementsfakes.FakeFactory) 39 securityGroupRepo = new(securitygroupsfakes.FakeSecurityGroupRepo) 40 configRepo = testconfig.NewRepositoryWithDefaults() 41 }) 42 43 runCommand := func(args ...string) bool { 44 return testcmd.RunCLICommand("delete-security-group", args, requirementsFactory, updateCommandDependency, false, ui) 45 } 46 47 Describe("requirements", func() { 48 It("should fail if not logged in", func() { 49 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 50 Expect(runCommand("my-group")).To(BeFalse()) 51 }) 52 53 It("should fail with usage when not provided a single argument", func() { 54 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 55 runCommand("whoops", "I can't believe", "I accidentally", "the whole thing") 56 Expect(ui.Outputs()).To(ContainSubstrings( 57 []string{"Incorrect Usage", "Requires", "argument"}, 58 )) 59 }) 60 }) 61 62 Context("when logged in", func() { 63 BeforeEach(func() { 64 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 65 }) 66 67 Context("when the group with the given name exists", func() { 68 BeforeEach(func() { 69 securityGroupRepo.ReadReturns(models.SecurityGroup{ 70 SecurityGroupFields: models.SecurityGroupFields{ 71 Name: "my-group", 72 GUID: "group-guid", 73 }, 74 }, nil) 75 }) 76 77 Context("delete a security group", func() { 78 It("when passed the -f flag", func() { 79 runCommand("-f", "my-group") 80 Expect(securityGroupRepo.ReadArgsForCall(0)).To(Equal("my-group")) 81 Expect(securityGroupRepo.DeleteArgsForCall(0)).To(Equal("group-guid")) 82 83 Expect(ui.Prompts).To(BeEmpty()) 84 }) 85 86 It("should prompt user when -f flag is not present", func() { 87 ui.Inputs = []string{"y"} 88 89 runCommand("my-group") 90 Expect(securityGroupRepo.ReadArgsForCall(0)).To(Equal("my-group")) 91 Expect(securityGroupRepo.DeleteArgsForCall(0)).To(Equal("group-guid")) 92 93 Expect(ui.Prompts).To(ContainSubstrings( 94 []string{"Really delete the security group", "my-group"}, 95 )) 96 }) 97 98 It("should not delete when user passes 'n' to prompt", func() { 99 ui.Inputs = []string{"n"} 100 101 runCommand("my-group") 102 Expect(securityGroupRepo.ReadCallCount()).To(Equal(0)) 103 Expect(securityGroupRepo.DeleteCallCount()).To(Equal(0)) 104 105 Expect(ui.Prompts).To(ContainSubstrings( 106 []string{"Really delete the security group", "my-group"}, 107 )) 108 }) 109 }) 110 111 It("tells the user what it's about to do", func() { 112 runCommand("-f", "my-group") 113 Expect(ui.Outputs()).To(ContainSubstrings( 114 []string{"Deleting", "security group", "my-group", "my-user"}, 115 []string{"OK"}, 116 )) 117 }) 118 }) 119 120 Context("when finding the group returns an error", func() { 121 BeforeEach(func() { 122 securityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.New("pbbbbbbbbbbt")) 123 }) 124 125 It("fails and tells the user", func() { 126 runCommand("-f", "whoops") 127 128 Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"})) 129 }) 130 }) 131 132 Context("when a group with that name does not exist", func() { 133 BeforeEach(func() { 134 securityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.NewModelNotFoundError("Security group", "uh uh uh -- you didn't sahy the magick word")) 135 }) 136 137 It("fails and tells the user", func() { 138 runCommand("-f", "whoop") 139 140 Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"whoop", "does not exist"})) 141 }) 142 }) 143 144 It("fails and warns the user if deleting fails", func() { 145 securityGroupRepo.DeleteReturns(errors.New("raspberry")) 146 runCommand("-f", "whoops") 147 148 Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"})) 149 }) 150 }) 151 })