github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/securitygroup/bind_security_group_test.go (about) 1 package securitygroup_test 2 3 import ( 4 "github.com/cloudfoundry/cli/cf/api/fakes" 5 test_org "github.com/cloudfoundry/cli/cf/api/organizations/fakes" 6 testapi "github.com/cloudfoundry/cli/cf/api/security_groups/fakes" 7 zoidberg "github.com/cloudfoundry/cli/cf/api/security_groups/spaces/fakes" 8 "github.com/cloudfoundry/cli/cf/configuration/core_config" 9 "github.com/cloudfoundry/cli/cf/errors" 10 "github.com/cloudfoundry/cli/cf/models" 11 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 12 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 13 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 14 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 15 16 . "github.com/cloudfoundry/cli/cf/commands/securitygroup" 17 . "github.com/cloudfoundry/cli/testhelpers/matchers" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 ) 21 22 var _ = Describe("bind-security-group command", func() { 23 var ( 24 ui *testterm.FakeUI 25 cmd BindSecurityGroup 26 configRepo core_config.ReadWriter 27 fakeSecurityGroupRepo *testapi.FakeSecurityGroupRepo 28 requirementsFactory *testreq.FakeReqFactory 29 fakeSpaceRepo *fakes.FakeSpaceRepository 30 fakeOrgRepo *test_org.FakeOrganizationRepository 31 fakeSpaceBinder *zoidberg.FakeSecurityGroupSpaceBinder 32 ) 33 34 BeforeEach(func() { 35 ui = &testterm.FakeUI{} 36 fakeOrgRepo = &test_org.FakeOrganizationRepository{} 37 fakeSpaceRepo = &fakes.FakeSpaceRepository{} 38 requirementsFactory = &testreq.FakeReqFactory{} 39 fakeSecurityGroupRepo = &testapi.FakeSecurityGroupRepo{} 40 configRepo = testconfig.NewRepositoryWithDefaults() 41 fakeSpaceBinder = &zoidberg.FakeSecurityGroupSpaceBinder{} 42 cmd = NewBindSecurityGroup(ui, configRepo, fakeSecurityGroupRepo, fakeSpaceRepo, fakeOrgRepo, fakeSpaceBinder) 43 }) 44 45 runCommand := func(args ...string) bool { 46 return testcmd.RunCommand(cmd, args, requirementsFactory) 47 } 48 49 Describe("requirements", func() { 50 It("fails when the user is not logged in", func() { 51 Expect(runCommand("my-craaaaaazy-security-group", "my-org", "my-space")).To(BeFalse()) 52 }) 53 54 It("succeeds when the user is logged in", func() { 55 requirementsFactory.LoginSuccess = true 56 57 Expect(runCommand("my-craaaaaazy-security-group", "my-org", "my-space")).To(BeTrue()) 58 }) 59 60 It("fails with usage when not provided the name of a security group, org, and space", func() { 61 requirementsFactory.LoginSuccess = true 62 runCommand("one fish", "two fish", "three fish", "purple fish") 63 64 Expect(ui.FailedWithUsage).To(BeTrue()) 65 }) 66 }) 67 68 Context("when the user is logged in and provides the name of a security group", func() { 69 BeforeEach(func() { 70 requirementsFactory.LoginSuccess = true 71 }) 72 73 Context("when a security group with that name does not exist", func() { 74 BeforeEach(func() { 75 fakeSecurityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.NewModelNotFoundError("security group", "my-nonexistent-security-group")) 76 }) 77 78 It("fails and tells the user", func() { 79 runCommand("my-nonexistent-security-group", "my-org", "my-space") 80 81 Expect(fakeSecurityGroupRepo.ReadArgsForCall(0)).To(Equal("my-nonexistent-security-group")) 82 Expect(ui.Outputs).To(ContainSubstrings( 83 []string{"FAILED"}, 84 []string{"security group", "my-nonexistent-security-group", "not found"}, 85 )) 86 }) 87 }) 88 89 Context("when the org does not exist", func() { 90 BeforeEach(func() { 91 fakeOrgRepo.FindByNameReturns(models.Organization{}, errors.New("Org org not found")) 92 }) 93 94 It("fails and tells the user", func() { 95 runCommand("sec group", "org", "space") 96 97 Expect(fakeOrgRepo.FindByNameArgsForCall(0)).To(Equal("org")) 98 Expect(ui.Outputs).To(ContainSubstrings( 99 []string{"FAILED"}, 100 []string{"Org", "org", "not found"}, 101 )) 102 }) 103 }) 104 105 Context("when the space does not exist", func() { 106 BeforeEach(func() { 107 org := models.Organization{} 108 org.Name = "org-name" 109 org.Guid = "org-guid" 110 fakeOrgRepo.ListOrgsReturns([]models.Organization{org}, nil) 111 fakeOrgRepo.FindByNameReturns(org, nil) 112 fakeSpaceRepo.FindByNameInOrgError = errors.NewModelNotFoundError("Space", "space-name") 113 }) 114 115 It("fails and tells the user", func() { 116 runCommand("sec group", "org-name", "space-name") 117 118 Expect(fakeSpaceRepo.FindByNameInOrgName).To(Equal("space-name")) 119 Expect(fakeSpaceRepo.FindByNameInOrgOrgGuid).To(Equal("org-guid")) 120 Expect(ui.Outputs).To(ContainSubstrings( 121 []string{"FAILED"}, 122 []string{"Space", "space-name", "not found"}, 123 )) 124 }) 125 }) 126 127 Context("everything is hunky dory", func() { 128 BeforeEach(func() { 129 org := models.Organization{} 130 org.Name = "org-name" 131 org.Guid = "org-guid" 132 fakeOrgRepo.ListOrgsReturns([]models.Organization{org}, nil) 133 134 space := models.Space{} 135 space.Name = "space-name" 136 space.Guid = "space-guid" 137 fakeSpaceRepo.FindByNameInOrgSpace = space 138 139 securityGroup := models.SecurityGroup{} 140 securityGroup.Name = "security-group" 141 securityGroup.Guid = "security-group-guid" 142 fakeSecurityGroupRepo.ReadReturns(securityGroup, nil) 143 }) 144 145 JustBeforeEach(func() { 146 runCommand("security-group", "org-name", "space-name") 147 }) 148 149 It("assigns the security group to the space", func() { 150 secGroupGuid, spaceGuid := fakeSpaceBinder.BindSpaceArgsForCall(0) 151 Expect(secGroupGuid).To(Equal("security-group-guid")) 152 Expect(spaceGuid).To(Equal("space-guid")) 153 }) 154 155 It("describes what it is doing for the user's benefit", func() { 156 Expect(ui.Outputs).To(ContainSubstrings( 157 []string{"Assigning security group security-group to space space-name in org org-name as my-user"}, 158 []string{"OK"}, 159 []string{"TIP: Changes will not apply to existing running applications until they are restarted."}, 160 )) 161 }) 162 }) 163 }) 164 })