github.com/niteshexa/cloudfoundry_cli@v7.1.0+incompatible/command/v7/bind_staging_security_group_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/v7action"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	. "code.cloudfoundry.org/cli/command/v7"
    11  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    12  	"code.cloudfoundry.org/cli/util/configv3"
    13  	"code.cloudfoundry.org/cli/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("bind-staging-security-group Command", func() {
    20  	var (
    21  		cmd               BindStagingSecurityGroupCommand
    22  		testUI            *ui.UI
    23  		fakeConfig        *commandfakes.FakeConfig
    24  		fakeSharedActor   *commandfakes.FakeSharedActor
    25  		fakeActor         *v7fakes.FakeActor
    26  		binaryName        string
    27  		executeErr        error
    28  		securityGroupName = "sec-group-name"
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v7fakes.FakeActor)
    36  
    37  		cmd = BindStagingSecurityGroupCommand{
    38  			BaseCommand: BaseCommand{
    39  				UI:          testUI,
    40  				Config:      fakeConfig,
    41  				SharedActor: fakeSharedActor,
    42  				Actor:       fakeActor,
    43  			},
    44  			SecurityGroup: flag.SecurityGroup{SecurityGroup: securityGroupName},
    45  		}
    46  
    47  		binaryName = "faceman"
    48  		fakeConfig.BinaryNameReturns(binaryName)
    49  
    50  		fakeConfig.CurrentUserReturns(
    51  			configv3.User{Name: "some-user"},
    52  			nil)
    53  	})
    54  
    55  	JustBeforeEach(func() {
    56  		executeErr = cmd.Execute(nil)
    57  	})
    58  	BeforeEach(func() {
    59  		fakeActor.UpdateSecurityGroupGloballyEnabledReturns(
    60  			v7action.Warnings{"globally bind security group warning"},
    61  			nil)
    62  	})
    63  
    64  	When("the current user is invalid", func() {
    65  		BeforeEach(func() {
    66  			fakeConfig.CurrentUserReturns(
    67  				configv3.User{},
    68  				errors.New("some-error"))
    69  		})
    70  		It("returns the error", func() {
    71  			Expect(executeErr).To(HaveOccurred())
    72  			Expect(executeErr).To(MatchError("some-error"))
    73  		})
    74  	})
    75  
    76  	It("globally binds the security group displays all warnings", func() {
    77  		Expect(testUI.Out).To(Say(`Binding security group sec-group-name to staging as some-user\.\.\.`))
    78  
    79  		Expect(fakeActor.UpdateSecurityGroupGloballyEnabledCallCount()).To(Equal(1))
    80  		securityGroupName, lifecycle, enabled := fakeActor.UpdateSecurityGroupGloballyEnabledArgsForCall(0)
    81  		Expect(securityGroupName).To(Equal("sec-group-name"))
    82  		Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycleStaging))
    83  		Expect(enabled).To(Equal(true))
    84  	})
    85  
    86  	When("an error is encountered globally binding the security group", func() {
    87  		var expectedErr error
    88  
    89  		BeforeEach(func() {
    90  			expectedErr = errors.New("bind security group error")
    91  			fakeActor.UpdateSecurityGroupGloballyEnabledReturns(
    92  				v7action.Warnings{"globally bind security group warning"},
    93  				expectedErr)
    94  		})
    95  
    96  		It("returns the error and displays all warnings", func() {
    97  			Expect(executeErr).To(MatchError(expectedErr))
    98  
    99  			Expect(testUI.Out).NotTo(Say("OK"))
   100  
   101  			Expect(testUI.Err).To(Say("globally bind security group warning"))
   102  		})
   103  	})
   104  
   105  	It("signals that the security group is bound and provides a helpful tip", func() {
   106  		Expect(testUI.Out).To(Say("OK"))
   107  		Expect(testUI.Out).To(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`))
   108  		Expect(testUI.Err).To(Say("globally bind security group warning"))
   109  		Expect(executeErr).NotTo(HaveOccurred())
   110  	})
   111  })