github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v6/unshare_service_command_test.go (about)

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