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

     1  package securitygroup_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/securitygroups/defaults/staging/stagingfakes"
     7  	"code.cloudfoundry.org/cli/cf/api/securitygroups/securitygroupsfakes"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     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/commandregistry"
    17  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("bind-staging-security-group command", func() {
    23  	var (
    24  		ui                           *testterm.FakeUI
    25  		configRepo                   coreconfig.Repository
    26  		requirementsFactory          *requirementsfakes.FakeFactory
    27  		fakeSecurityGroupRepo        *securitygroupsfakes.FakeSecurityGroupRepo
    28  		fakeStagingSecurityGroupRepo *stagingfakes.FakeSecurityGroupsRepo
    29  		deps                         commandregistry.Dependency
    30  	)
    31  
    32  	updateCommandDependency := func(pluginCall bool) {
    33  		deps.UI = ui
    34  		deps.RepoLocator = deps.RepoLocator.SetSecurityGroupRepository(fakeSecurityGroupRepo)
    35  		deps.RepoLocator = deps.RepoLocator.SetStagingSecurityGroupRepository(fakeStagingSecurityGroupRepo)
    36  		deps.Config = configRepo
    37  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("bind-staging-security-group").SetDependency(deps, pluginCall))
    38  	}
    39  
    40  	BeforeEach(func() {
    41  		ui = &testterm.FakeUI{}
    42  		configRepo = testconfig.NewRepositoryWithDefaults()
    43  		requirementsFactory = new(requirementsfakes.FakeFactory)
    44  		fakeSecurityGroupRepo = new(securitygroupsfakes.FakeSecurityGroupRepo)
    45  		fakeStagingSecurityGroupRepo = new(stagingfakes.FakeSecurityGroupsRepo)
    46  	})
    47  
    48  	runCommand := func(args ...string) bool {
    49  		return testcmd.RunCLICommand("bind-staging-security-group", args, requirementsFactory, updateCommandDependency, false, ui)
    50  	}
    51  
    52  	Describe("requirements", func() {
    53  		It("fails when the user is not logged in", func() {
    54  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    55  			Expect(runCommand("name")).To(BeFalse())
    56  		})
    57  
    58  		It("fails with usage when a name is not provided", func() {
    59  			runCommand()
    60  			Expect(ui.Outputs()).To(ContainSubstrings(
    61  				[]string{"Incorrect Usage", "Requires", "argument"},
    62  			))
    63  		})
    64  	})
    65  
    66  	Context("when the user is logged in and provides the name of a group", func() {
    67  		BeforeEach(func() {
    68  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    69  			group := models.SecurityGroup{}
    70  			group.GUID = "just-pretend-this-is-a-guid"
    71  			group.Name = "a-security-group-name"
    72  			fakeSecurityGroupRepo.ReadReturns(group, nil)
    73  		})
    74  
    75  		JustBeforeEach(func() {
    76  			runCommand("a-security-group-name")
    77  		})
    78  
    79  		It("binds the group to the default staging group set", func() {
    80  			Expect(fakeSecurityGroupRepo.ReadArgsForCall(0)).To(Equal("a-security-group-name"))
    81  			Expect(fakeStagingSecurityGroupRepo.BindToStagingSetArgsForCall(0)).To(Equal("just-pretend-this-is-a-guid"))
    82  		})
    83  
    84  		It("describes what it's doing to the user", func() {
    85  			Expect(ui.Outputs()).To(ContainSubstrings(
    86  				[]string{"Binding", "a-security-group-name", "as", "my-user"},
    87  				[]string{"OK"},
    88  			))
    89  		})
    90  
    91  		Context("when binding the security group to the default set fails", func() {
    92  			BeforeEach(func() {
    93  				fakeStagingSecurityGroupRepo.BindToStagingSetReturns(errors.New("WOAH. I know kung fu"))
    94  			})
    95  
    96  			It("fails and describes the failure to the user", func() {
    97  				Expect(ui.Outputs()).To(ContainSubstrings(
    98  					[]string{"FAILED"},
    99  					[]string{"WOAH. I know kung fu"},
   100  				))
   101  			})
   102  		})
   103  
   104  		Context("when the security group with the given name cannot be found", func() {
   105  			BeforeEach(func() {
   106  				fakeSecurityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.New("Crème insufficiently brûlée'd"))
   107  			})
   108  
   109  			It("fails and tells the user that the security group does not exist", func() {
   110  				Expect(fakeStagingSecurityGroupRepo.BindToStagingSetCallCount()).To(Equal(0))
   111  				Expect(ui.Outputs()).To(ContainSubstrings(
   112  					[]string{"FAILED"},
   113  				))
   114  			})
   115  		})
   116  	})
   117  })