github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/unbind_security_group_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	"code.cloudfoundry.org/cli/command/translatableerror"
    12  	. "code.cloudfoundry.org/cli/command/v6"
    13  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("unbind-security-group Command", func() {
    22  	var (
    23  		cmd             UnbindSecurityGroupCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v6fakes.FakeUnbindSecurityGroupActor
    28  		binaryName      string
    29  		executeErr      error
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v6fakes.FakeUnbindSecurityGroupActor)
    37  
    38  		cmd = UnbindSecurityGroupCommand{
    39  			UI:          testUI,
    40  			Config:      fakeConfig,
    41  			SharedActor: fakeSharedActor,
    42  			Actor:       fakeActor,
    43  		}
    44  
    45  		binaryName = "faceman"
    46  		fakeConfig.BinaryNameReturns(binaryName)
    47  		fakeConfig.ExperimentalReturns(true)
    48  
    49  		fakeConfig.CurrentUserReturns(
    50  			configv3.User{Name: "some-user"},
    51  			nil)
    52  	})
    53  
    54  	JustBeforeEach(func() {
    55  		executeErr = cmd.Execute(nil)
    56  	})
    57  
    58  	When("getting the current user fails", func() {
    59  		var expectedErr error
    60  
    61  		BeforeEach(func() {
    62  			expectedErr = errors.New("getting user failed")
    63  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    64  		})
    65  
    66  		It("returns an error", func() {
    67  			Expect(executeErr).To(MatchError(expectedErr))
    68  		})
    69  	})
    70  
    71  	When("checking target fails", func() {
    72  		BeforeEach(func() {
    73  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    74  		})
    75  
    76  		It("returns an error", func() {
    77  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: "faceman"}))
    78  
    79  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    80  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    81  			Expect(checkTargetedOrg).To(BeTrue())
    82  			Expect(checkTargetedSpace).To(BeTrue())
    83  		})
    84  	})
    85  
    86  	When("lifecycle is 'some-lifecycle'", func() {
    87  		// By this point in execution, Goflags will have filtered any invalid
    88  		// lifecycle phase.  We use 'some-lifecycle' to test that the command
    89  		// merely passes the value presented by Goflags.
    90  		BeforeEach(func() {
    91  			cmd.Lifecycle = flag.SecurityGroupLifecycle("some-lifecycle")
    92  		})
    93  
    94  		When("only the security group is provided", func() {
    95  			BeforeEach(func() {
    96  				cmd.RequiredArgs.SecurityGroupName = "some-security-group"
    97  			})
    98  
    99  			When("org and space are targeted", func() {
   100  				BeforeEach(func() {
   101  					fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
   102  					fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"})
   103  					fakeActor.UnbindSecurityGroupByNameAndSpaceReturns(
   104  						v2action.Warnings{"unbind warning"},
   105  						nil)
   106  				})
   107  
   108  				It("unbinds the security group from the targeted space", func() {
   109  					Expect(executeErr).ToNot(HaveOccurred())
   110  
   111  					Expect(testUI.Out).To(Say(`Unbinding security group %s from org %s / space %s as %s\.\.\.`, "some-security-group", "some-org", "some-space", "some-user"))
   112  					Expect(testUI.Out).To(Say("OK\n\n"))
   113  					Expect(testUI.Out).To(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`))
   114  					Expect(testUI.Err).To(Say("unbind warning"))
   115  
   116  					Expect(fakeConfig.TargetedOrganizationCallCount()).To(Equal(1))
   117  					Expect(fakeConfig.TargetedSpaceCallCount()).To(Equal(1))
   118  					Expect(fakeActor.UnbindSecurityGroupByNameAndSpaceCallCount()).To(Equal(1))
   119  					securityGroupName, spaceGUID, lifecycle := fakeActor.UnbindSecurityGroupByNameAndSpaceArgsForCall(0)
   120  					Expect(securityGroupName).To(Equal("some-security-group"))
   121  					Expect(spaceGUID).To(Equal("some-space-guid"))
   122  					Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycle("some-lifecycle")))
   123  				})
   124  
   125  				When("the actor returns a security group not found error", func() {
   126  					BeforeEach(func() {
   127  						fakeActor.UnbindSecurityGroupByNameAndSpaceReturns(
   128  							v2action.Warnings{"unbind warning"},
   129  							actionerror.SecurityGroupNotFoundError{Name: "some-security-group"},
   130  						)
   131  					})
   132  
   133  					It("returns a translated security group not found error", func() {
   134  						Expect(testUI.Err).To(Say("unbind warning"))
   135  
   136  						Expect(executeErr).To(MatchError(actionerror.SecurityGroupNotFoundError{Name: "some-security-group"}))
   137  					})
   138  				})
   139  
   140  				When("the actor returns a security group not bound error", func() {
   141  					BeforeEach(func() {
   142  						fakeActor.UnbindSecurityGroupByNameAndSpaceReturns(
   143  							v2action.Warnings{"unbind warning"},
   144  							actionerror.SecurityGroupNotBoundError{
   145  								Name:      "some-security-group",
   146  								Lifecycle: "some-lifecycle",
   147  							})
   148  					})
   149  
   150  					It("returns a translated security group not bound warning but has no error", func() {
   151  						Expect(testUI.Err).To(Say("unbind warning"))
   152  						Expect(testUI.Err).To(Say("Security group some-security-group not bound to this space for lifecycle phase 'some-lifecycle'."))
   153  
   154  						Expect(testUI.Out).To(Say("OK"))
   155  						Expect(testUI.Out).NotTo(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`))
   156  
   157  						Expect(executeErr).NotTo(HaveOccurred())
   158  					})
   159  				})
   160  
   161  				When("the actor returns an error", func() {
   162  					var expectedErr error
   163  
   164  					BeforeEach(func() {
   165  						expectedErr = errors.New("some unbind security group error")
   166  						fakeActor.UnbindSecurityGroupByNameAndSpaceReturns(
   167  							v2action.Warnings{"unbind warning"},
   168  							expectedErr,
   169  						)
   170  					})
   171  
   172  					It("returns a translated security no found error", func() {
   173  						Expect(testUI.Err).To(Say("unbind warning"))
   174  
   175  						Expect(executeErr).To(MatchError(expectedErr))
   176  					})
   177  				})
   178  			})
   179  		})
   180  
   181  		When("the security group, org, and space are provided", func() {
   182  			BeforeEach(func() {
   183  				cmd.RequiredArgs.SecurityGroupName = "some-security-group"
   184  				cmd.RequiredArgs.OrganizationName = "some-org"
   185  				cmd.RequiredArgs.SpaceName = "some-space"
   186  			})
   187  
   188  			When("checking target fails", func() {
   189  				BeforeEach(func() {
   190  					fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
   191  				})
   192  
   193  				It("returns an error", func() {
   194  					Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: "faceman"}))
   195  
   196  					Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   197  					checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   198  					Expect(checkTargetedOrg).To(BeFalse())
   199  					Expect(checkTargetedSpace).To(BeFalse())
   200  				})
   201  			})
   202  
   203  			When("the user is logged in", func() {
   204  				BeforeEach(func() {
   205  					fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameReturns(
   206  						v2action.Warnings{"unbind warning"},
   207  						nil)
   208  				})
   209  
   210  				It("the security group is unbound from the targeted space", func() {
   211  					Expect(testUI.Out).To(Say(`Unbinding security group %s from org %s / space %s as %s\.\.\.`, "some-security-group", "some-org", "some-space", "some-user"))
   212  					Expect(testUI.Out).To(Say("OK\n\n"))
   213  					Expect(testUI.Out).To(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`))
   214  					Expect(testUI.Err).To(Say("unbind warning"))
   215  
   216  					Expect(fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameCallCount()).To(Equal(1))
   217  					securityGroupName, orgName, spaceName, lifecycle := fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameArgsForCall(0)
   218  					Expect(securityGroupName).To(Equal("some-security-group"))
   219  					Expect(orgName).To(Equal("some-org"))
   220  					Expect(spaceName).To(Equal("some-space"))
   221  					Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycle("some-lifecycle")))
   222  				})
   223  			})
   224  
   225  			When("the actor returns a security group not found error", func() {
   226  				BeforeEach(func() {
   227  					fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameReturns(
   228  						v2action.Warnings{"unbind warning"},
   229  						actionerror.SecurityGroupNotFoundError{Name: "some-security-group"},
   230  					)
   231  				})
   232  
   233  				It("returns a translated security group not found error", func() {
   234  					Expect(testUI.Err).To(Say("unbind warning"))
   235  
   236  					Expect(executeErr).To(MatchError(actionerror.SecurityGroupNotFoundError{Name: "some-security-group"}))
   237  				})
   238  			})
   239  
   240  			When("the actor returns a security group not bound error", func() {
   241  				BeforeEach(func() {
   242  					fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameReturns(
   243  						v2action.Warnings{"unbind warning"},
   244  						actionerror.SecurityGroupNotBoundError{
   245  							Name:      "some-security-group",
   246  							Lifecycle: constant.SecurityGroupLifecycle("some-lifecycle"),
   247  						})
   248  				})
   249  
   250  				It("returns a translated security group not bound warning but has no error", func() {
   251  					Expect(testUI.Err).To(Say("unbind warning"))
   252  					Expect(testUI.Err).To(Say("Security group some-security-group not bound to this space for lifecycle phase 'some-lifecycle'."))
   253  
   254  					Expect(testUI.Out).To(Say("OK"))
   255  					Expect(testUI.Out).NotTo(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`))
   256  
   257  					Expect(executeErr).NotTo(HaveOccurred())
   258  				})
   259  			})
   260  
   261  			When("the actor returns an error", func() {
   262  				var expectedErr error
   263  
   264  				BeforeEach(func() {
   265  					expectedErr = errors.New("some unbind security group error")
   266  					fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameReturns(
   267  						v2action.Warnings{"unbind warning"},
   268  						expectedErr,
   269  					)
   270  				})
   271  
   272  				It("returns a translated security no found error", func() {
   273  					Expect(testUI.Err).To(Say("unbind warning"))
   274  
   275  					Expect(executeErr).To(MatchError(expectedErr))
   276  				})
   277  			})
   278  		})
   279  
   280  		When("the security group and org are provided, but the space is not", func() {
   281  			BeforeEach(func() {
   282  				cmd.RequiredArgs.SecurityGroupName = "some-security-group"
   283  				cmd.RequiredArgs.OrganizationName = "some-org"
   284  			})
   285  
   286  			It("an error is returned", func() {
   287  				Expect(executeErr).To(MatchError(translatableerror.ThreeRequiredArgumentsError{
   288  					ArgumentName1: "SECURITY_GROUP",
   289  					ArgumentName2: "ORG",
   290  					ArgumentName3: "SPACE"}))
   291  
   292  				Expect(testUI.Out).NotTo(Say("Unbinding security group"))
   293  
   294  				Expect(fakeActor.UnbindSecurityGroupByNameOrganizationNameAndSpaceNameCallCount()).To(Equal(0))
   295  			})
   296  		})
   297  	})
   298  })