github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/delete_security_group_command_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/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("delete-security-group Command", func() {
    19  	var (
    20  		cmd             DeleteSecurityGroupCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v7fakes.FakeActor
    25  		input           *Buffer
    26  		binaryName      string
    27  		executeErr      error
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		input = NewBuffer()
    32  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v7fakes.FakeActor)
    36  
    37  		cmd = DeleteSecurityGroupCommand{
    38  			BaseCommand: BaseCommand{
    39  				UI:          testUI,
    40  				Config:      fakeConfig,
    41  				SharedActor: fakeSharedActor,
    42  				Actor:       fakeActor,
    43  			},
    44  		}
    45  
    46  		cmd.RequiredArgs.SecurityGroup = "some-security-group"
    47  		binaryName = "faceman"
    48  		fakeConfig.BinaryNameReturns(binaryName)
    49  	})
    50  
    51  	JustBeforeEach(func() {
    52  		executeErr = cmd.Execute(nil)
    53  	})
    54  
    55  	When("a cloud controller API endpoint is set", func() {
    56  		BeforeEach(func() {
    57  			fakeConfig.TargetReturns("some-url")
    58  		})
    59  
    60  		When("checking target fails", func() {
    61  			BeforeEach(func() {
    62  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    63  			})
    64  
    65  			It("returns an error", func() {
    66  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    67  
    68  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    69  				checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    70  				Expect(checkTargetedOrg).To(BeFalse())
    71  				Expect(checkTargetedSpace).To(BeFalse())
    72  			})
    73  		})
    74  
    75  		When("the user is logged in", func() {
    76  			When("getting the current user returns an error", func() {
    77  				var returnedErr error
    78  
    79  				BeforeEach(func() {
    80  					returnedErr = errors.New("some error")
    81  					fakeConfig.CurrentUserReturns(configv3.User{}, returnedErr)
    82  				})
    83  
    84  				It("returns the error", func() {
    85  					Expect(executeErr).To(MatchError(returnedErr))
    86  				})
    87  			})
    88  
    89  			When("getting the current user does not return an error", func() {
    90  				BeforeEach(func() {
    91  					fakeConfig.CurrentUserReturns(
    92  						configv3.User{Name: "some-user"},
    93  						nil)
    94  				})
    95  
    96  				When("the '-f' flag is provided", func() {
    97  					BeforeEach(func() {
    98  						cmd.Force = true
    99  					})
   100  
   101  					When("no errors are encountered", func() {
   102  						BeforeEach(func() {
   103  							fakeActor.DeleteSecurityGroupReturns(v7action.Warnings{"warning-1", "warning-2"}, nil)
   104  						})
   105  
   106  						It("does not prompt for user confirmation, displays warnings, and deletes the security group", func() {
   107  							Expect(executeErr).ToNot(HaveOccurred())
   108  
   109  							Expect(testUI.Out).ToNot(Say("Really delete the security group some-security-group"))
   110  							Expect(testUI.Out).To(Say("Deleting security group some-security-group as some-user..."))
   111  
   112  							Expect(fakeActor.DeleteSecurityGroupCallCount()).To(Equal(1))
   113  							securityGroupName := fakeActor.DeleteSecurityGroupArgsForCall(0)
   114  							Expect(securityGroupName).To(Equal("some-security-group"))
   115  
   116  							Expect(testUI.Err).To(Say("warning-1"))
   117  							Expect(testUI.Err).To(Say("warning-2"))
   118  							Expect(testUI.Out).To(Say("OK"))
   119  						})
   120  					})
   121  
   122  					When("an error is encountered deleting the security group", func() {
   123  						BeforeEach(func() {
   124  							fakeActor.DeleteSecurityGroupReturns(
   125  								v7action.Warnings{"warning-1", "warning-2"},
   126  								actionerror.SecurityGroupNotFoundError{
   127  									Name: "some-security-group",
   128  								},
   129  							)
   130  						})
   131  
   132  						It("returns an SecurityGroupNotFoundError and displays all warnings", func() {
   133  							Expect(executeErr).NotTo(HaveOccurred())
   134  
   135  							Expect(testUI.Out).To(Say("Deleting security group some-security-group as some-user..."))
   136  
   137  							Expect(fakeActor.DeleteSecurityGroupCallCount()).To(Equal(1))
   138  							securityGroupName := fakeActor.DeleteSecurityGroupArgsForCall(0)
   139  							Expect(securityGroupName).To(Equal("some-security-group"))
   140  
   141  							Expect(testUI.Err).To(Say("warning-1"))
   142  							Expect(testUI.Err).To(Say("warning-2"))
   143  
   144  							Expect(testUI.Err).To(Say(`Security group 'some-security-group' does not exist\.`))
   145  							Expect(testUI.Out).To(Say("OK"))
   146  						})
   147  					})
   148  				})
   149  
   150  				// Testing the prompt.
   151  				When("the '-f' flag is not provided", func() {
   152  					When("the user chooses the default", func() {
   153  						BeforeEach(func() {
   154  							_, err := input.Write([]byte("\n"))
   155  							Expect(err).ToNot(HaveOccurred())
   156  						})
   157  
   158  						It("does not delete the security group", func() {
   159  							Expect(executeErr).ToNot(HaveOccurred())
   160  
   161  							Expect(testUI.Out).To(Say(`Security group 'some-security-group' has not been deleted\.`))
   162  
   163  							Expect(fakeActor.DeleteSecurityGroupCallCount()).To(Equal(0))
   164  						})
   165  					})
   166  
   167  					When("the user inputs no", func() {
   168  						BeforeEach(func() {
   169  							_, err := input.Write([]byte("n\n"))
   170  							Expect(err).ToNot(HaveOccurred())
   171  						})
   172  
   173  						It("does not delete the security group", func() {
   174  							Expect(executeErr).ToNot(HaveOccurred())
   175  
   176  							Expect(testUI.Out).To(Say(`Security group 'some-security-group' has not been deleted\.`))
   177  
   178  							Expect(fakeActor.DeleteSecurityGroupCallCount()).To(Equal(0))
   179  						})
   180  					})
   181  
   182  					When("the user inputs yes", func() {
   183  						BeforeEach(func() {
   184  							_, err := input.Write([]byte("y\n"))
   185  							Expect(err).ToNot(HaveOccurred())
   186  						})
   187  
   188  						It("deletes the security group", func() {
   189  							Expect(executeErr).ToNot(HaveOccurred())
   190  
   191  							Expect(testUI.Out).To(Say("Really delete the security group some-security-group"))
   192  							Expect(testUI.Out).To(Say("Deleting security group some-security-group as some-user..."))
   193  
   194  							Expect(fakeActor.DeleteSecurityGroupCallCount()).To(Equal(1))
   195  							securityGroupName := fakeActor.DeleteSecurityGroupArgsForCall(0)
   196  							Expect(securityGroupName).To(Equal("some-security-group"))
   197  
   198  							Expect(testUI.Out).To(Say("OK"))
   199  						})
   200  					})
   201  
   202  					When("the user input is invalid", func() {
   203  						BeforeEach(func() {
   204  							_, err := input.Write([]byte("e\n\n"))
   205  							Expect(err).ToNot(HaveOccurred())
   206  						})
   207  
   208  						It("does not delete the security group", func() {
   209  							Expect(executeErr).NotTo(HaveOccurred())
   210  
   211  							Expect(testUI.Out).To(Say("Really delete the security group some-security-group"))
   212  							Expect(fakeActor.DeleteSecurityGroupCallCount()).To(Equal(0))
   213  						})
   214  					})
   215  
   216  					When("displaying the prompt returns an error", func() {
   217  						// if nothing is written to input, display bool prompt returns EOF
   218  						It("returns the error", func() {
   219  							Expect(executeErr).To(MatchError("EOF"))
   220  						})
   221  					})
   222  				})
   223  			})
   224  		})
   225  	})
   226  })