github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/securitygroup/unbind_running_security_group_test.go (about)

     1  package securitygroup_test
     2  
     3  import (
     4  	fakeRunningDefaults "github.com/cloudfoundry/cli/cf/api/security_groups/defaults/running/fakes"
     5  	fakeSecurityGroup "github.com/cloudfoundry/cli/cf/api/security_groups/fakes"
     6  	"github.com/cloudfoundry/cli/cf/command_registry"
     7  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     8  	"github.com/cloudfoundry/cli/cf/errors"
     9  	"github.com/cloudfoundry/cli/cf/models"
    10  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    11  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    12  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    13  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    14  
    15  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  var _ = Describe("unbind-running-security-group command", func() {
    21  	var (
    22  		ui                            *testterm.FakeUI
    23  		configRepo                    core_config.Repository
    24  		requirementsFactory           *testreq.FakeReqFactory
    25  		fakeSecurityGroupRepo         *fakeSecurityGroup.FakeSecurityGroupRepo
    26  		fakeRunningSecurityGroupsRepo *fakeRunningDefaults.FakeRunningSecurityGroupsRepo
    27  		deps                          command_registry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.Ui = ui
    32  		deps.RepoLocator = deps.RepoLocator.SetSecurityGroupRepository(fakeSecurityGroupRepo)
    33  		deps.RepoLocator = deps.RepoLocator.SetRunningSecurityGroupRepository(fakeRunningSecurityGroupsRepo)
    34  		deps.Config = configRepo
    35  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("unbind-running-security-group").SetDependency(deps, pluginCall))
    36  	}
    37  
    38  	BeforeEach(func() {
    39  		ui = &testterm.FakeUI{}
    40  		configRepo = testconfig.NewRepositoryWithDefaults()
    41  		requirementsFactory = &testreq.FakeReqFactory{}
    42  		fakeSecurityGroupRepo = &fakeSecurityGroup.FakeSecurityGroupRepo{}
    43  		fakeRunningSecurityGroupsRepo = &fakeRunningDefaults.FakeRunningSecurityGroupsRepo{}
    44  	})
    45  
    46  	runCommand := func(args ...string) bool {
    47  		return testcmd.RunCliCommand("unbind-running-security-group", args, requirementsFactory, updateCommandDependency, false)
    48  	}
    49  
    50  	Describe("requirements", func() {
    51  		It("fails when the user is not logged in", func() {
    52  			Expect(runCommand("name")).To(BeFalse())
    53  		})
    54  
    55  		It("fails with usage when a name is not provided", func() {
    56  			runCommand()
    57  			Expect(ui.Outputs).To(ContainSubstrings(
    58  				[]string{"Incorrect Usage", "Requires", "argument"},
    59  			))
    60  		})
    61  	})
    62  
    63  	Context("when the user is logged in and provides the name of a group", func() {
    64  		BeforeEach(func() {
    65  			requirementsFactory.LoginSuccess = true
    66  		})
    67  
    68  		Context("security group exists", func() {
    69  			BeforeEach(func() {
    70  				group := models.SecurityGroup{}
    71  				group.Guid = "just-pretend-this-is-a-guid"
    72  				group.Name = "a-security-group-name"
    73  				fakeSecurityGroupRepo.ReadReturns(group, nil)
    74  			})
    75  
    76  			JustBeforeEach(func() {
    77  				runCommand("a-security-group-name")
    78  			})
    79  
    80  			It("unbinds the group from the running group set", func() {
    81  				Expect(ui.Outputs).To(ContainSubstrings(
    82  					[]string{"Unbinding", "security group", "a-security-group-name", "my-user"},
    83  					[]string{"TIP: Changes will not apply to existing running applications until they are restarted."},
    84  					[]string{"OK"},
    85  				))
    86  
    87  				Expect(fakeSecurityGroupRepo.ReadArgsForCall(0)).To(Equal("a-security-group-name"))
    88  				Expect(fakeRunningSecurityGroupsRepo.UnbindFromRunningSetArgsForCall(0)).To(Equal("just-pretend-this-is-a-guid"))
    89  			})
    90  		})
    91  
    92  		Context("when the security group does not exist", func() {
    93  			BeforeEach(func() {
    94  				fakeSecurityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.NewModelNotFoundError("security group", "anana-qui-parle"))
    95  			})
    96  
    97  			It("warns the user", func() {
    98  				runCommand("anana-qui-parle")
    99  				Expect(ui.WarnOutputs).To(ContainSubstrings(
   100  					[]string{"Security group", "anana-qui-parle", "does not exist"},
   101  				))
   102  
   103  				Expect(ui.Outputs).To(ContainSubstrings(
   104  					[]string{"OK"},
   105  				))
   106  			})
   107  		})
   108  	})
   109  })