github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/unshare_service_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/v2v3action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  	. "code.cloudfoundry.org/cli/command/v6"
    12  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("unshare-service Command", func() {
    21  	var (
    22  		cmd             UnshareServiceCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v6fakes.FakeUnshareServiceActor
    27  		input           *Buffer
    28  		binaryName      string
    29  		executeErr      error
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		input = NewBuffer()
    34  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    35  		fakeConfig = new(commandfakes.FakeConfig)
    36  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    37  		fakeActor = new(v6fakes.FakeUnshareServiceActor)
    38  
    39  		cmd = UnshareServiceCommand{
    40  			UI:          testUI,
    41  			Config:      fakeConfig,
    42  			SharedActor: fakeSharedActor,
    43  			Actor:       fakeActor,
    44  		}
    45  
    46  		cmd.RequiredArgs.ServiceInstance = "some-service-instance"
    47  
    48  		binaryName = "faceman"
    49  		fakeConfig.BinaryNameReturns(binaryName)
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	When("the API version is below the minimum", func() {
    57  		BeforeEach(func() {
    58  			fakeActor.CloudControllerV3APIVersionReturns(ccversion.MinSupportedV3ClientVersion)
    59  		})
    60  
    61  		It("returns a MinimumAPIVersionNotMetError", func() {
    62  			Expect(executeErr).To(MatchError(translatableerror.MinimumCFAPIVersionNotMetError{
    63  				CurrentVersion: ccversion.MinSupportedV3ClientVersion,
    64  				MinimumVersion: ccversion.MinVersionShareServiceV3,
    65  			}))
    66  		})
    67  	})
    68  
    69  	When("checking target fails", func() {
    70  		BeforeEach(func() {
    71  			fakeActor.CloudControllerV3APIVersionReturns(ccversion.MinVersionShareServiceV3)
    72  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    73  		})
    74  
    75  		It("returns an error", func() {
    76  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    77  
    78  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    79  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    80  			Expect(checkTargetedOrg).To(BeTrue())
    81  			Expect(checkTargetedSpace).To(BeTrue())
    82  		})
    83  	})
    84  
    85  	When("the user is logged in, and a space and org are targeted", func() {
    86  		BeforeEach(func() {
    87  			fakeActor.CloudControllerV3APIVersionReturns(ccversion.MinVersionShareServiceV3)
    88  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    89  				GUID: "some-org-guid",
    90  				Name: "some-org",
    91  			})
    92  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    93  				GUID: "some-space-guid",
    94  				Name: "some-space",
    95  			})
    96  		})
    97  
    98  		When("an error occurs getting the current user", func() {
    99  			var expectedErr error
   100  
   101  			BeforeEach(func() {
   102  				expectedErr = errors.New("get current user error")
   103  				fakeConfig.CurrentUserReturns(
   104  					configv3.User{},
   105  					expectedErr)
   106  			})
   107  
   108  			It("returns the error", func() {
   109  				Expect(executeErr).To(MatchError(expectedErr))
   110  				Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
   111  			})
   112  		})
   113  
   114  		When("no errors occur getting the current user", func() {
   115  			BeforeEach(func() {
   116  				fakeConfig.CurrentUserReturns(
   117  					configv3.User{Name: "some-user"},
   118  					nil)
   119  			})
   120  
   121  			When("the '-f' flag is provided (to force non-prompting)", func() {
   122  				BeforeEach(func() {
   123  					cmd.Force = true
   124  				})
   125  
   126  				When("the '-o' flag is NOT provided (when we want to unshare a space in the currently targeted org)", func() {
   127  					BeforeEach(func() {
   128  						cmd.SharedToSpaceName = "some-shared-to-space"
   129  					})
   130  
   131  					When("no errors occur unsharing the service instance", func() {
   132  						BeforeEach(func() {
   133  							fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceReturns(
   134  								v2v3action.Warnings{"unshare-service-warning"},
   135  								nil)
   136  						})
   137  
   138  						It("unshares the service instance from the currently targetd org and provided space name, and displays all warnings", func() {
   139  							Expect(executeErr).ToNot(HaveOccurred())
   140  
   141  							Expect(testUI.Out).To(Say(`Unsharing service instance some-service-instance from org some-org / space some-shared-to-space as some-user\.\.\.`))
   142  							Expect(testUI.Out).To(Say("OK"))
   143  
   144  							Expect(testUI.Err).ToNot(Say("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working."))
   145  							Expect(testUI.Err).To(Say("unshare-service-warning"))
   146  
   147  							Expect(fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceCallCount()).To(Equal(1))
   148  							sharedToOrgNameArg, sharedToSpaceNameArg, serviceInstanceNameArg, currentlyTargetedSpaceGUIDArg := fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceArgsForCall(0)
   149  							Expect(sharedToOrgNameArg).To(Equal("some-org"))
   150  							Expect(sharedToSpaceNameArg).To(Equal("some-shared-to-space"))
   151  							Expect(serviceInstanceNameArg).To(Equal("some-service-instance"))
   152  							Expect(currentlyTargetedSpaceGUIDArg).To(Equal("some-space-guid"))
   153  						})
   154  					})
   155  
   156  					When("an error occurs unsharing the service instance", func() {
   157  						var expectedErr error
   158  
   159  						BeforeEach(func() {
   160  							expectedErr = errors.New("unshare error")
   161  							fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceReturns(
   162  								v2v3action.Warnings{"unshare-service-warning"},
   163  								expectedErr)
   164  						})
   165  
   166  						It("returns the error and displays all warnings", func() {
   167  							Expect(executeErr).To(MatchError(expectedErr))
   168  							Expect(testUI.Err).To(Say("unshare-service-warning"))
   169  						})
   170  					})
   171  
   172  					When("the service instance is not shared to the space we want to unshare from", func() {
   173  						BeforeEach(func() {
   174  							fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceReturns(
   175  								v2v3action.Warnings{"unshare-service-warning"},
   176  								actionerror.ServiceInstanceNotSharedToSpaceError{ServiceInstanceName: "some-service-instance"})
   177  						})
   178  
   179  						It("does not return an error, displays that the service instance is not shared and displays all warnings", func() {
   180  							Expect(executeErr).ToNot(HaveOccurred())
   181  							Expect(testUI.Out).To(Say(`Service instance some-service-instance is not shared with space some-shared-to-space in organization some-org\.`))
   182  							Expect(testUI.Out).To(Say("OK"))
   183  							Expect(testUI.Err).To(Say("unshare-service-warning"))
   184  						})
   185  					})
   186  				})
   187  
   188  				When("the '-o' flag is provided (when the space we want to unshare does not belong to the currently targeted org)", func() {
   189  					BeforeEach(func() {
   190  						cmd.SharedToSpaceName = "some-other-space"
   191  						cmd.SharedToOrgName = "some-other-org"
   192  
   193  						fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceReturns(
   194  							v2v3action.Warnings{"unshare-service-warning"},
   195  							nil)
   196  					})
   197  
   198  					It("performs the unshare with the provided org name", func() {
   199  						Expect(executeErr).ToNot(HaveOccurred())
   200  
   201  						Expect(testUI.Out).To(Say(`Unsharing service instance some-service-instance from org some-other-org / space some-other-space as some-user\.\.\.`))
   202  						Expect(testUI.Out).To(Say("OK"))
   203  
   204  						Expect(testUI.Err).ToNot(Say("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working."))
   205  						Expect(testUI.Err).To(Say("unshare-service-warning"))
   206  
   207  						Expect(fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceCallCount()).To(Equal(1))
   208  						sharedToOrgNameArg, sharedToSpaceNameArg, serviceInstanceNameArg, currentlyTargetedSpaceGUIDArg := fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceArgsForCall(0)
   209  						Expect(sharedToOrgNameArg).To(Equal("some-other-org"))
   210  						Expect(sharedToSpaceNameArg).To(Equal("some-other-space"))
   211  						Expect(serviceInstanceNameArg).To(Equal("some-service-instance"))
   212  						Expect(currentlyTargetedSpaceGUIDArg).To(Equal("some-space-guid"))
   213  					})
   214  				})
   215  			})
   216  
   217  			When("the -f flag is NOT provided", func() {
   218  				BeforeEach(func() {
   219  					cmd.Force = false
   220  					cmd.SharedToSpaceName = "some-shared-to-space"
   221  				})
   222  
   223  				When("the user inputs yes", func() {
   224  					BeforeEach(func() {
   225  						_, err := input.Write([]byte("y\n"))
   226  						Expect(err).ToNot(HaveOccurred())
   227  
   228  						fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceReturns(
   229  							v2v3action.Warnings{"unshare-service-warning"},
   230  							nil)
   231  					})
   232  
   233  					It("unshares the service instance and displays all warnings", func() {
   234  						Expect(executeErr).ToNot(HaveOccurred())
   235  
   236  						Expect(testUI.Out).To(Say(`Really unshare the service instance\? \[yN\]`))
   237  						Expect(testUI.Out).To(Say(`Unsharing service instance some-service-instance from org some-org / space some-shared-to-space as some-user\.\.\.`))
   238  						Expect(testUI.Out).To(Say("OK"))
   239  
   240  						Expect(testUI.Err).To(Say("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working."))
   241  						Expect(testUI.Err).To(Say("unshare-service-warning"))
   242  
   243  						Expect(fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceCallCount()).To(Equal(1))
   244  						sharedToOrgNameArg, sharedToSpaceNameArg, serviceInstanceNameArg, currentlyTargetedSpaceGUIDArg := fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceArgsForCall(0)
   245  						Expect(sharedToOrgNameArg).To(Equal("some-org"))
   246  						Expect(sharedToSpaceNameArg).To(Equal("some-shared-to-space"))
   247  						Expect(serviceInstanceNameArg).To(Equal("some-service-instance"))
   248  						Expect(currentlyTargetedSpaceGUIDArg).To(Equal("some-space-guid"))
   249  					})
   250  				})
   251  
   252  				When("the user inputs no", func() {
   253  					BeforeEach(func() {
   254  						_, err := input.Write([]byte("n\n"))
   255  						Expect(err).ToNot(HaveOccurred())
   256  					})
   257  
   258  					It("cancels the unshared", func() {
   259  						Expect(executeErr).ToNot(HaveOccurred())
   260  
   261  						Expect(testUI.Out).To(Say(`Really unshare the service instance\? \[yN\]`))
   262  						Expect(testUI.Out).To(Say("Unshare cancelled"))
   263  
   264  						Expect(testUI.Err).To(Say("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working."))
   265  
   266  						Expect(fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceCallCount()).To(Equal(0))
   267  					})
   268  				})
   269  
   270  				When("the user chooses the default", func() {
   271  					BeforeEach(func() {
   272  						_, err := input.Write([]byte("\n"))
   273  						Expect(err).ToNot(HaveOccurred())
   274  					})
   275  
   276  					It("cancels the unshare", func() {
   277  						Expect(executeErr).ToNot(HaveOccurred())
   278  
   279  						Expect(testUI.Out).To(Say(`Really unshare the service instance\? \[yN\]`))
   280  						Expect(testUI.Out).To(Say("Unshare cancelled"))
   281  
   282  						Expect(testUI.Err).To(Say("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working."))
   283  
   284  						Expect(fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceCallCount()).To(Equal(0))
   285  					})
   286  				})
   287  
   288  				When("the user input is invalid", func() {
   289  					BeforeEach(func() {
   290  						_, err := input.Write([]byte("e\n\n"))
   291  						Expect(err).ToNot(HaveOccurred())
   292  					})
   293  
   294  					It("prompts for unshare confirmation again", func() {
   295  						Expect(executeErr).NotTo(HaveOccurred())
   296  
   297  						Expect(testUI.Out).To(Say(`Really unshare the service instance\? \[yN\]`))
   298  						Expect(testUI.Out).To(Say(`invalid input \(not y, n, yes, or no\)`))
   299  						Expect(testUI.Out).To(Say(`Really unshare the service instance\? \[yN\]`))
   300  
   301  						Expect(testUI.Err).To(Say("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working."))
   302  
   303  						Expect(fakeActor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpaceCallCount()).To(Equal(0))
   304  					})
   305  				})
   306  			})
   307  		})
   308  	})
   309  })