github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/securitygroup/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/configuration/coreconfig" 6 "code.cloudfoundry.org/cli/cf/errors" 7 "code.cloudfoundry.org/cli/cf/models" 8 "code.cloudfoundry.org/cli/cf/requirements" 9 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 10 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 11 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 12 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 13 14 "code.cloudfoundry.org/cli/cf/commandregistry" 15 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 ) 19 20 var _ = Describe("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("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("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 an 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 rulesMap := []map[string]interface{}{{"just-pretend": "that-this-is-correct"}} 70 securityGroup := models.SecurityGroup{ 71 SecurityGroupFields: models.SecurityGroupFields{ 72 Name: "my-group", 73 GUID: "group-guid", 74 Rules: rulesMap, 75 }, 76 Spaces: []models.Space{ 77 { 78 SpaceFields: models.SpaceFields{GUID: "my-space-guid-1", Name: "space-1"}, 79 Organization: models.OrganizationFields{GUID: "my-org-guid-1", Name: "org-1"}, 80 }, 81 { 82 SpaceFields: models.SpaceFields{GUID: "my-space-guid", Name: "space-2"}, 83 Organization: models.OrganizationFields{GUID: "my-org-guid-1", Name: "org-2"}, 84 }, 85 }, 86 } 87 88 securityGroupRepo.ReadReturns(securityGroup, nil) 89 }) 90 91 It("should fetch the security group from its repo", func() { 92 runCommand("my-group") 93 Expect(securityGroupRepo.ReadArgsForCall(0)).To(Equal("my-group")) 94 }) 95 96 It("tells the user what it's about to do and then shows the group", func() { 97 runCommand("my-group") 98 Expect(ui.Outputs()).To(ContainSubstrings( 99 []string{"Getting", "security group", "my-group", "my-user"}, 100 []string{"OK"}, 101 []string{"Name", "my-group"}, 102 []string{"Rules"}, 103 []string{"["}, 104 []string{"{"}, 105 []string{"just-pretend", "that-this-is-correct"}, 106 []string{"}"}, 107 []string{"]"}, 108 []string{"#0", "org-1", "space-1"}, 109 []string{"#1", "org-2", "space-2"}, 110 )) 111 }) 112 113 It("tells the user if no spaces are assigned", func() { 114 securityGroup := models.SecurityGroup{ 115 SecurityGroupFields: models.SecurityGroupFields{ 116 Name: "my-group", 117 GUID: "group-guid", 118 Rules: []map[string]interface{}{}, 119 }, 120 Spaces: []models.Space{}, 121 } 122 123 securityGroupRepo.ReadReturns(securityGroup, nil) 124 125 runCommand("my-group") 126 127 Expect(ui.Outputs()).To(ContainSubstrings( 128 []string{"No spaces assigned"}, 129 )) 130 }) 131 }) 132 133 It("fails and warns the user if a group with that name could not be found", func() { 134 securityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.New("half-past-tea-time")) 135 runCommand("im-late!") 136 137 Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"})) 138 }) 139 }) 140 })