github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/unbind_security_group_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    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("unbind-security-group Command", func() {
    20  	var (
    21  		cmd             UnbindSecurityGroupCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v7fakes.FakeActor
    26  		binaryName      string
    27  		executeErr      error
    28  		expectedErr     error
    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 = UnbindSecurityGroupCommand{
    38  			BaseCommand: BaseCommand{
    39  				UI:          testUI,
    40  				Config:      fakeConfig,
    41  				SharedActor: fakeSharedActor,
    42  				Actor:       fakeActor,
    43  			},
    44  		}
    45  
    46  		cmd.RequiredArgs.SecurityGroupName = "some-security-group"
    47  		cmd.RequiredArgs.OrganizationName = "some-org"
    48  		cmd.RequiredArgs.SpaceName = "some-space"
    49  		cmd.Lifecycle = "some-lifecycle"
    50  
    51  		binaryName = "faceman"
    52  		fakeConfig.BinaryNameReturns(binaryName)
    53  		fakeConfig.ExperimentalReturns(true)
    54  
    55  		fakeConfig.CurrentUserReturns(
    56  			configv3.User{Name: "some-user"},
    57  			nil)
    58  		fakeActor.UnbindSecurityGroupReturns(
    59  			v7action.Warnings{"unbind warning"},
    60  			nil)
    61  		fakeSharedActor.CheckTargetReturns(nil)
    62  
    63  	})
    64  
    65  	JustBeforeEach(func() {
    66  		executeErr = cmd.Execute(nil)
    67  	})
    68  
    69  	When("checking target fails", func() {
    70  		BeforeEach(func() {
    71  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    72  		})
    73  
    74  		It("returns an error", func() {
    75  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "faceman"}))
    76  
    77  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    78  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    79  			Expect(checkTargetedOrg).To(BeFalse())
    80  			Expect(checkTargetedSpace).To(BeFalse())
    81  		})
    82  	})
    83  
    84  	When("getting the current user fails", func() {
    85  		BeforeEach(func() {
    86  			expectedErr = errors.New("getting user failed")
    87  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    88  		})
    89  
    90  		It("returns an error", func() {
    91  			Expect(executeErr).To(MatchError(expectedErr))
    92  		})
    93  	})
    94  
    95  	It("indicates that it will unbind the security group", func() {
    96  		Expect(testUI.Out).To(Say(`Unbinding some-lifecycle security group %s from org %s / space %s as %s\.\.\.`, "some-security-group", "some-org", "some-space", "some-user"))
    97  	})
    98  
    99  	It("displays the warnings from unbinding the security group", func() {
   100  		Expect(testUI.Err).To(Say("unbind warning"))
   101  	})
   102  
   103  	When("unbinding the security group fails", func() {
   104  		BeforeEach(func() {
   105  			fakeActor.UnbindSecurityGroupReturns(
   106  				v7action.Warnings{"unbind warning"},
   107  				errors.New("unbind-error"),
   108  			)
   109  		})
   110  		When("security group is unbound", func() {
   111  			BeforeEach(func() {
   112  				fakeActor.UnbindSecurityGroupReturns(
   113  					v7action.Warnings{"unbind warning"},
   114  					actionerror.SecurityGroupNotBoundToSpaceError{
   115  						Name:      "some-security-group",
   116  						Space:     "some-space",
   117  						Lifecycle: constant.SecurityGroupLifecycle("some-lifecycle"),
   118  					})
   119  			})
   120  			It("returns the security group not bound error as a warning and succeeds", func() {
   121  				Expect(fakeActor.UnbindSecurityGroupCallCount()).To(Equal(1))
   122  				securityGroupName, orgName, spaceName, lifecycle := fakeActor.UnbindSecurityGroupArgsForCall(0)
   123  				Expect(securityGroupName).To(Equal("some-security-group"))
   124  				Expect(orgName).To(Equal("some-org"))
   125  				Expect(spaceName).To(Equal("some-space"))
   126  				Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycle("some-lifecycle")))
   127  				Expect(testUI.Err).To(Say("Security group some-security-group not bound to space some-space for lifecycle phase 'some-lifecycle'."))
   128  				Expect(testUI.Out).To(Say("OK\n\n"))
   129  				Expect(testUI.Out).To(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`))
   130  				Expect(executeErr).NotTo(HaveOccurred())
   131  			})
   132  		})
   133  		It("returns the error", func() {
   134  			Expect(fakeActor.UnbindSecurityGroupCallCount()).To(Equal(1))
   135  			securityGroupName, orgName, spaceName, lifecycle := fakeActor.UnbindSecurityGroupArgsForCall(0)
   136  			Expect(securityGroupName).To(Equal("some-security-group"))
   137  			Expect(orgName).To(Equal("some-org"))
   138  			Expect(spaceName).To(Equal("some-space"))
   139  			Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycle("some-lifecycle")))
   140  			Expect(executeErr).To(MatchError("unbind-error"))
   141  		})
   142  	})
   143  
   144  	It("indicates it successfully unbound the security group", func() {
   145  		Expect(testUI.Out).To(Say("OK\n\n"))
   146  		Expect(testUI.Out).To(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`))
   147  	})
   148  
   149  })