github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/securitygroup/security_groups_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/flags" 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/cf/util/testhelpers/commands" 13 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 14 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 15 16 "code.cloudfoundry.org/cli/cf/commands/securitygroup" 17 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 ) 21 22 var _ = Describe("list-security-groups command", func() { 23 var ( 24 ui *testterm.FakeUI 25 repo *securitygroupsfakes.FakeSecurityGroupRepo 26 requirementsFactory *requirementsfakes.FakeFactory 27 configRepo coreconfig.Repository 28 deps commandregistry.Dependency 29 ) 30 31 updateCommandDependency := func(pluginCall bool) { 32 deps.UI = ui 33 deps.RepoLocator = deps.RepoLocator.SetSecurityGroupRepository(repo) 34 deps.Config = configRepo 35 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("security-groups").SetDependency(deps, pluginCall)) 36 } 37 38 BeforeEach(func() { 39 ui = &testterm.FakeUI{} 40 requirementsFactory = new(requirementsfakes.FakeFactory) 41 repo = new(securitygroupsfakes.FakeSecurityGroupRepo) 42 configRepo = testconfig.NewRepositoryWithDefaults() 43 }) 44 45 runCommand := func(args ...string) bool { 46 return testcmd.RunCLICommand("security-groups", args, requirementsFactory, updateCommandDependency, false, ui) 47 } 48 49 Describe("requirements", func() { 50 It("should fail if not logged in", func() { 51 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 52 Expect(runCommand()).To(BeFalse()) 53 }) 54 55 Context("when arguments are provided", func() { 56 var cmd commandregistry.Command 57 var flagContext flags.FlagContext 58 59 BeforeEach(func() { 60 cmd = &securitygroup.SecurityGroups{} 61 cmd.SetDependency(deps, false) 62 flagContext = flags.NewFlagContext(cmd.MetaData().Flags) 63 }) 64 65 It("should fail with usage", func() { 66 flagContext.Parse("blahblah") 67 68 reqs, err := cmd.Requirements(requirementsFactory, flagContext) 69 Expect(err).NotTo(HaveOccurred()) 70 71 err = testcmd.RunRequirements(reqs) 72 Expect(err).To(HaveOccurred()) 73 Expect(err.Error()).To(ContainSubstring("Incorrect Usage")) 74 Expect(err.Error()).To(ContainSubstring("No argument required")) 75 }) 76 }) 77 }) 78 79 Context("when logged in", func() { 80 BeforeEach(func() { 81 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 82 }) 83 84 It("tells the user what it's about to do", func() { 85 runCommand() 86 Expect(ui.Outputs()).To(ContainSubstrings( 87 []string{"Getting", "security groups", "my-user"}, 88 )) 89 }) 90 91 It("handles api errors with an error message", func() { 92 repo.FindAllReturns([]models.SecurityGroup{}, errors.New("YO YO YO, ERROR YO")) 93 94 runCommand() 95 Expect(ui.Outputs()).To(ContainSubstrings( 96 []string{"FAILED"}, 97 )) 98 }) 99 100 Context("when there are no security groups", func() { 101 It("Should tell the user that there are no security groups", func() { 102 repo.FindAllReturns([]models.SecurityGroup{}, nil) 103 104 runCommand() 105 Expect(ui.Outputs()).To(ContainSubstrings([]string{"No security groups"})) 106 }) 107 }) 108 109 Context("when there is at least one security group", func() { 110 BeforeEach(func() { 111 securityGroup := models.SecurityGroup{} 112 securityGroup.Name = "my-group" 113 securityGroup.GUID = "group-guid" 114 115 repo.FindAllReturns([]models.SecurityGroup{securityGroup}, nil) 116 }) 117 118 Describe("Where there are spaces assigned", func() { 119 BeforeEach(func() { 120 securityGroups := []models.SecurityGroup{ 121 { 122 SecurityGroupFields: models.SecurityGroupFields{ 123 Name: "my-group", 124 GUID: "group-guid", 125 }, 126 Spaces: []models.Space{ 127 { 128 SpaceFields: models.SpaceFields{GUID: "my-space-guid-1", Name: "space-1"}, 129 Organization: models.OrganizationFields{GUID: "my-org-guid-1", Name: "org-1"}, 130 }, 131 { 132 SpaceFields: models.SpaceFields{GUID: "my-space-guid", Name: "space-2"}, 133 Organization: models.OrganizationFields{GUID: "my-org-guid-2", Name: "org-2"}, 134 }, 135 }, 136 }, 137 } 138 139 repo.FindAllReturns(securityGroups, nil) 140 }) 141 142 It("lists out the security group's: name, organization and space", func() { 143 runCommand() 144 Expect(ui.Outputs()).To(ContainSubstrings( 145 []string{"Getting", "security group", "my-user"}, 146 []string{"OK"}, 147 []string{"#0", "my-group", "org-1", "space-1"}, 148 )) 149 150 Expect(ui.Outputs()).ToNot(ContainSubstrings( 151 []string{"#0", "my-group", "org-2", "space-2"}, 152 )) 153 }) 154 }) 155 156 Describe("Where there are no spaces assigned", func() { 157 It("lists out the security group's: name", func() { 158 runCommand() 159 Expect(ui.Outputs()).To(ContainSubstrings( 160 []string{"Getting", "security group", "my-user"}, 161 []string{"OK"}, 162 []string{"#0", "my-group"}, 163 )) 164 }) 165 }) 166 }) 167 }) 168 })