github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/securitygroup/bind_security_group_test.go (about)

     1  package securitygroup_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/organizations/organizationsfakes"
     5  	"code.cloudfoundry.org/cli/cf/api/securitygroups/securitygroupsfakes"
     6  	"code.cloudfoundry.org/cli/cf/api/securitygroups/spaces/spacesfakes"
     7  	spacesapifakes "code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    13  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    14  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    15  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    16  
    17  	"code.cloudfoundry.org/cli/cf/commandregistry"
    18  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("bind-security-group command", func() {
    24  	var (
    25  		ui                    *testterm.FakeUI
    26  		configRepo            coreconfig.Repository
    27  		fakeSecurityGroupRepo *securitygroupsfakes.FakeSecurityGroupRepo
    28  		requirementsFactory   *requirementsfakes.FakeFactory
    29  		fakeSpaceRepo         *spacesapifakes.FakeSpaceRepository
    30  		fakeOrgRepo           *organizationsfakes.FakeOrganizationRepository
    31  		fakeSpaceBinder       *spacesfakes.FakeSecurityGroupSpaceBinder
    32  		deps                  commandregistry.Dependency
    33  	)
    34  
    35  	updateCommandDependency := func(pluginCall bool) {
    36  		deps.UI = ui
    37  		deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(fakeSpaceRepo)
    38  		deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(fakeOrgRepo)
    39  		deps.RepoLocator = deps.RepoLocator.SetSecurityGroupRepository(fakeSecurityGroupRepo)
    40  		deps.RepoLocator = deps.RepoLocator.SetSecurityGroupSpaceBinder(fakeSpaceBinder)
    41  		deps.Config = configRepo
    42  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("bind-security-group").SetDependency(deps, pluginCall))
    43  	}
    44  
    45  	BeforeEach(func() {
    46  		ui = &testterm.FakeUI{}
    47  		fakeOrgRepo = new(organizationsfakes.FakeOrganizationRepository)
    48  		fakeSpaceRepo = new(spacesapifakes.FakeSpaceRepository)
    49  		requirementsFactory = new(requirementsfakes.FakeFactory)
    50  		fakeSecurityGroupRepo = new(securitygroupsfakes.FakeSecurityGroupRepo)
    51  		configRepo = testconfig.NewRepositoryWithDefaults()
    52  		fakeSpaceBinder = new(spacesfakes.FakeSecurityGroupSpaceBinder)
    53  	})
    54  
    55  	runCommand := func(args ...string) bool {
    56  		return testcmd.RunCLICommand("bind-security-group", args, requirementsFactory, updateCommandDependency, false, ui)
    57  	}
    58  
    59  	Describe("requirements", func() {
    60  		It("fails when the user is not logged in", func() {
    61  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    62  			Expect(runCommand("my-craaaaaazy-security-group", "my-org", "my-space")).To(BeFalse())
    63  		})
    64  
    65  		It("succeeds when the user is logged in", func() {
    66  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    67  
    68  			Expect(runCommand("my-craaaaaazy-security-group", "my-org", "my-space")).To(BeTrue())
    69  		})
    70  
    71  		Describe("number of arguments", func() {
    72  			Context("wrong number of arguments", func() {
    73  				It("fails with usage when not provided the name of a security group, org, and space", func() {
    74  					requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    75  					runCommand("one fish", "two fish", "three fish", "purple fish")
    76  
    77  					Expect(ui.Outputs()).To(ContainSubstrings(
    78  						[]string{"Incorrect Usage", "Requires", "arguments"},
    79  					))
    80  				})
    81  			})
    82  
    83  			Context("providing securitygroup and org", func() {
    84  				It("should not fail", func() {
    85  					requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    86  
    87  					Expect(runCommand("my-craaaaaazy-security-group", "my-org")).To(BeTrue())
    88  				})
    89  			})
    90  		})
    91  	})
    92  
    93  	Context("when the user is logged in and provides the name of a security group", func() {
    94  		BeforeEach(func() {
    95  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    96  		})
    97  
    98  		Context("when a security group with that name does not exist", func() {
    99  			BeforeEach(func() {
   100  				fakeSecurityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.NewModelNotFoundError("security group", "my-nonexistent-security-group"))
   101  			})
   102  
   103  			It("fails and tells the user", func() {
   104  				runCommand("my-nonexistent-security-group", "my-org", "my-space")
   105  
   106  				Expect(fakeSecurityGroupRepo.ReadArgsForCall(0)).To(Equal("my-nonexistent-security-group"))
   107  				Expect(ui.Outputs()).To(ContainSubstrings(
   108  					[]string{"FAILED"},
   109  					[]string{"security group", "my-nonexistent-security-group", "not found"},
   110  				))
   111  			})
   112  		})
   113  
   114  		Context("when the org does not exist", func() {
   115  			BeforeEach(func() {
   116  				fakeOrgRepo.FindByNameReturns(models.Organization{}, errors.New("Org org not found"))
   117  			})
   118  
   119  			It("fails and tells the user", func() {
   120  				runCommand("sec group", "org", "space")
   121  
   122  				Expect(fakeOrgRepo.FindByNameArgsForCall(0)).To(Equal("org"))
   123  				Expect(ui.Outputs()).To(ContainSubstrings(
   124  					[]string{"FAILED"},
   125  					[]string{"Org", "org", "not found"},
   126  				))
   127  			})
   128  		})
   129  
   130  		Context("when the space does not exist", func() {
   131  			BeforeEach(func() {
   132  				org := models.Organization{}
   133  				org.Name = "org-name"
   134  				org.GUID = "org-guid"
   135  				fakeOrgRepo.ListOrgsReturns([]models.Organization{org}, nil)
   136  				fakeOrgRepo.FindByNameReturns(org, nil)
   137  				fakeSpaceRepo.FindByNameInOrgReturns(models.Space{}, errors.NewModelNotFoundError("Space", "space-name"))
   138  			})
   139  
   140  			It("fails and tells the user", func() {
   141  				runCommand("sec group", "org-name", "space-name")
   142  
   143  				name, orgGUID := fakeSpaceRepo.FindByNameInOrgArgsForCall(0)
   144  				Expect(name).To(Equal("space-name"))
   145  				Expect(orgGUID).To(Equal("org-guid"))
   146  				Expect(ui.Outputs()).To(ContainSubstrings(
   147  					[]string{"FAILED"},
   148  					[]string{"Space", "space-name", "not found"},
   149  				))
   150  			})
   151  		})
   152  
   153  		Context("everything is hunky dory", func() {
   154  			var securityGroup models.SecurityGroup
   155  			var org models.Organization
   156  
   157  			BeforeEach(func() {
   158  				org = models.Organization{}
   159  				org.Name = "org-name"
   160  				org.GUID = "org-guid"
   161  				fakeOrgRepo.FindByNameReturns(org, nil)
   162  
   163  				space := models.Space{}
   164  				space.Name = "space-name"
   165  				space.GUID = "space-guid"
   166  				fakeSpaceRepo.FindByNameInOrgReturns(space, nil)
   167  
   168  				securityGroup = models.SecurityGroup{}
   169  				securityGroup.Name = "security-group"
   170  				securityGroup.GUID = "security-group-guid"
   171  				fakeSecurityGroupRepo.ReadReturns(securityGroup, nil)
   172  			})
   173  
   174  			Context("when space is provided", func() {
   175  				JustBeforeEach(func() {
   176  					runCommand("security-group", "org-name", "space-name")
   177  				})
   178  
   179  				It("assigns the security group to the space", func() {
   180  					secGroupGUID, spaceGUID := fakeSpaceBinder.BindSpaceArgsForCall(0)
   181  					Expect(secGroupGUID).To(Equal("security-group-guid"))
   182  					Expect(spaceGUID).To(Equal("space-guid"))
   183  				})
   184  
   185  				It("describes what it is doing for the user's benefit", func() {
   186  					Expect(ui.Outputs()).To(ContainSubstrings(
   187  						[]string{"Assigning security group security-group to space space-name in org org-name as my-user"},
   188  						[]string{"OK"},
   189  						[]string{"TIP: Changes will not apply to existing running applications until they are restarted."},
   190  					))
   191  				})
   192  			})
   193  
   194  			Context("when no space is provided", func() {
   195  				var spaces []models.Space
   196  				BeforeEach(func() {
   197  					spaces := []models.Space{
   198  						{
   199  							SpaceFields: models.SpaceFields{
   200  								GUID: "space-guid-1",
   201  								Name: "space-name-1",
   202  							},
   203  						},
   204  						{
   205  							SpaceFields: models.SpaceFields{
   206  								GUID: "space-guid-2",
   207  								Name: "space-name-2",
   208  							},
   209  						},
   210  					}
   211  
   212  					fakeSpaceRepo.ListSpacesFromOrgStub = func(orgGUID string, callback func(models.Space) bool) error {
   213  						Expect(orgGUID).To(Equal(org.GUID))
   214  
   215  						for _, space := range spaces {
   216  							Expect(callback(space)).To(BeTrue())
   217  						}
   218  
   219  						return nil
   220  					}
   221  				})
   222  
   223  				It("binds the security group to all of the org's spaces", func() {
   224  					runCommand("sec group", "org")
   225  
   226  					Expect(fakeSpaceRepo.ListSpacesFromOrgCallCount()).Should(Equal(1))
   227  					Expect(fakeSpaceBinder.BindSpaceCallCount()).Should(Equal(2))
   228  
   229  					for i, space := range spaces {
   230  						securityGroupGUID, spaceGUID := fakeSpaceBinder.BindSpaceArgsForCall(i)
   231  						Expect(securityGroupGUID).To(Equal(securityGroup.GUID))
   232  						Expect(spaceGUID).To(Equal(space.GUID))
   233  					}
   234  				})
   235  			})
   236  		})
   237  	})
   238  })