github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/shared/global/unshare_service_command_test.go (about)

     1  package global
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     5  	"code.cloudfoundry.org/cli/integration/helpers"
     6  	"code.cloudfoundry.org/cli/integration/helpers/fakeservicebroker"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gbytes"
    10  	. "github.com/onsi/gomega/gexec"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("unshare-service command", func() {
    15  	var (
    16  		sourceOrgName     string
    17  		sourceSpaceName   string
    18  		sharedToOrgName   string
    19  		sharedToSpaceName string
    20  		serviceInstance   string
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		helpers.SkipIfVersionLessThan(ccversion.MinVersionShareServiceV3)
    25  
    26  		sourceOrgName = helpers.NewOrgName()
    27  		sourceSpaceName = helpers.NewSpaceName()
    28  		sharedToOrgName = helpers.NewOrgName()
    29  		sharedToSpaceName = helpers.NewSpaceName()
    30  		serviceInstance = helpers.PrefixedRandomName("svc-inst")
    31  
    32  		helpers.LoginCF()
    33  	})
    34  
    35  	Describe("help", func() {
    36  		When("--help flag is set", func() {
    37  			It("Displays command usage to output", func() {
    38  				session := helpers.CF("unshare-service", "--help")
    39  				Eventually(session).Should(Say("NAME:"))
    40  				Eventually(session).Should(Say("unshare-service - Unshare a shared service instance from a space"))
    41  				Eventually(session).Should(Say("USAGE:"))
    42  				Eventually(session).Should(Say(`cf unshare-service SERVICE_INSTANCE -s OTHER_SPACE \[-o OTHER_ORG\] \[-f\]`))
    43  				Eventually(session).Should(Say("OPTIONS:"))
    44  				Eventually(session).Should(Say(`-o\s+Org of the other space \(Default: targeted org\)`))
    45  				Eventually(session).Should(Say(`-s\s+Space to unshare the service instance from`))
    46  				Eventually(session).Should(Say(`-f\s+Force unshare without confirmation`))
    47  				Eventually(session).Should(Say("SEE ALSO:"))
    48  				Eventually(session).Should(Say("delete-service, service, services, share-service, unbind-service"))
    49  				Eventually(session).Should(Exit(0))
    50  			})
    51  		})
    52  	})
    53  
    54  	When("the service instance name is not provided", func() {
    55  		It("tells the user that the service instance name is required, prints help text, and exits 1", func() {
    56  			session := helpers.CF("unshare-service", "-s", sharedToSpaceName)
    57  
    58  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `SERVICE_INSTANCE` was not provided"))
    59  			Eventually(session).Should(Say("NAME:"))
    60  			Eventually(session).Should(Exit(1))
    61  		})
    62  	})
    63  
    64  	When("the space name is not provided", func() {
    65  		It("tells the user that the space name is required, prints help text, and exits 1", func() {
    66  			session := helpers.CF("unshare-service")
    67  
    68  			Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `-s' was not specified"))
    69  			Eventually(session).Should(Say("NAME:"))
    70  			Eventually(session).Should(Exit(1))
    71  		})
    72  	})
    73  
    74  	When("the environment is not setup correctly", func() {
    75  		It("fails with the appropriate errors", func() {
    76  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "unshare-service", serviceInstance, "-s", sharedToSpaceName)
    77  		})
    78  
    79  		When("the v3 api version is lower than the minimum version", func() {
    80  			var server *Server
    81  
    82  			BeforeEach(func() {
    83  				server = helpers.StartAndTargetMockServerWithAPIVersions(helpers.DefaultV2Version, ccversion.MinSupportedV3ClientVersion)
    84  			})
    85  
    86  			AfterEach(func() {
    87  				server.Close()
    88  			})
    89  
    90  			It("fails with error message that the minimum version is not met", func() {
    91  				session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName)
    92  				Eventually(session).Should(Say("FAILED"))
    93  				Eventually(session.Err).Should(Say(`This command requires CF API version 3\.36\.0 or higher\.`))
    94  				Eventually(session).Should(Exit(1))
    95  			})
    96  		})
    97  	})
    98  
    99  	When("the environment is set up correctly", func() {
   100  		var (
   101  			service     string
   102  			servicePlan string
   103  		)
   104  
   105  		BeforeEach(func() {
   106  			helpers.CreateOrgAndSpace(sharedToOrgName, sharedToSpaceName)
   107  			helpers.SetupCF(sourceOrgName, sourceSpaceName)
   108  		})
   109  
   110  		AfterEach(func() {
   111  			helpers.QuickDeleteOrg(sourceOrgName)
   112  			helpers.QuickDeleteOrg(sharedToOrgName)
   113  		})
   114  
   115  		When("there is a managed service instance in my current targeted space", func() {
   116  			var broker *fakeservicebroker.FakeServiceBroker
   117  
   118  			BeforeEach(func() {
   119  				broker = fakeservicebroker.New().EnsureBrokerIsAvailable()
   120  				service = broker.ServiceName()
   121  				servicePlan = broker.ServicePlanName()
   122  
   123  				Eventually(helpers.CF("enable-service-access", service)).Should(Exit(0))
   124  				Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   125  			})
   126  
   127  			AfterEach(func() {
   128  				broker.Destroy()
   129  			})
   130  
   131  			When("the service instance has not been shared to this space", func() {
   132  				It("displays info and idempotently exits 0", func() {
   133  					session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName, "-f")
   134  					Eventually(session).Should(Say(`Service instance %s is not shared with space %s in organization %s\.`, serviceInstance, sharedToSpaceName, sharedToOrgName))
   135  					Eventually(session).Should(Say("OK"))
   136  					Eventually(session).Should(Exit(0))
   137  				})
   138  			})
   139  
   140  			When("I have shared my service instance to a space in another org ('-o' flag provided)", func() {
   141  				BeforeEach(func() {
   142  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   143  					Eventually(session).Should(Exit(0))
   144  				})
   145  
   146  				When("the org I want to unshare from does not exist", func() {
   147  					It("fails with an org not found error", func() {
   148  						session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", "missing-org", "-f")
   149  						Eventually(session).Should(Say(`Service instance %s is not shared with space %s in organization missing-org\.`, serviceInstance, sharedToSpaceName))
   150  						Eventually(session).Should(Say("OK"))
   151  						Eventually(session).Should(Exit(0))
   152  					})
   153  				})
   154  
   155  				When("the space I want to unshare from does not exist", func() {
   156  					It("fails with a space not found error", func() {
   157  						session := helpers.CF("unshare-service", serviceInstance, "-s", "missing-space", "-o", sharedToOrgName, "-f")
   158  						Eventually(session).Should(Say(`Service instance %s is not shared with space missing-space in organization %s\.`, serviceInstance, sharedToOrgName))
   159  						Eventually(session).Should(Say("OK"))
   160  						Eventually(session).Should(Exit(0))
   161  					})
   162  				})
   163  
   164  				When("I want to unshare my service instance from a space and org", func() {
   165  					It("successfully unshares the service instance", func() {
   166  						username, _ := helpers.GetCredentials()
   167  						session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName, "-f")
   168  						Eventually(session).Should(Say(`Unsharing service instance %s from org %s / space %s as %s\.\.\.`, serviceInstance, sharedToOrgName, sharedToSpaceName, username))
   169  						Eventually(session).Should(Say("OK"))
   170  						Eventually(session).Should(Exit(0))
   171  					})
   172  				})
   173  			})
   174  
   175  			When("I have shared my service instance to a space within the targeted org ('-o' flag NOT provided)", func() {
   176  				BeforeEach(func() {
   177  					helpers.CreateSpace(sharedToSpaceName)
   178  
   179  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)
   180  					Eventually(session).Should(Exit(0))
   181  				})
   182  
   183  				When("the space I want to unshare from does not exist", func() {
   184  					It("fails with a space not found error", func() {
   185  						session := helpers.CF("unshare-service", serviceInstance, "-s", "missing-space", "-f")
   186  						Eventually(session).Should(Say(`Service instance %s is not shared with space missing-space in organization %s\.`, serviceInstance, sourceOrgName))
   187  						Eventually(session).Should(Say("OK"))
   188  						Eventually(session).Should(Exit(0))
   189  					})
   190  				})
   191  
   192  				When("I want to unshare my service instance from the space", func() {
   193  					It("successfully unshares the service instance when I am admin", func() {
   194  						username, _ := helpers.GetCredentials()
   195  						session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-f")
   196  						Eventually(session).Should(Say(`Unsharing service instance %s from org %s / space %s as %s\.\.\.`, serviceInstance, sourceOrgName, sharedToSpaceName, username))
   197  						Eventually(session).Should(Say("OK"))
   198  						Eventually(session).Should(Exit(0))
   199  					})
   200  
   201  					When("I have no access to the shared-to space", func() {
   202  						var (
   203  							username string
   204  							password string
   205  						)
   206  
   207  						BeforeEach(func() {
   208  							username = helpers.NewUsername()
   209  							password = helpers.NewPassword()
   210  							Eventually(helpers.CF("create-user", username, password)).Should(Exit(0))
   211  							Eventually(helpers.CF("set-space-role", username, sourceOrgName, sourceSpaceName, "SpaceDeveloper")).Should(Exit(0))
   212  							env := map[string]string{
   213  								"CF_USERNAME": username,
   214  								"CF_PASSWORD": password,
   215  							}
   216  							helpers.LogoutCF()
   217  							Eventually(helpers.CFWithEnv(env, "auth")).Should(Exit(0))
   218  							helpers.TargetOrgAndSpace(sourceOrgName, sourceSpaceName)
   219  						})
   220  
   221  						AfterEach(func() {
   222  							helpers.LoginCF()
   223  							helpers.TargetOrgAndSpace(sourceOrgName, sourceSpaceName)
   224  							session := helpers.CF("delete-user", username, "-f")
   225  							Eventually(session).Should(Say("OK"))
   226  							Eventually(session).Should(Exit(0))
   227  						})
   228  
   229  						It("successfully unshares the service instance", func() {
   230  							session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-f")
   231  							Eventually(session).Should(Say("OK"))
   232  							Eventually(session).Should(Exit(0))
   233  						})
   234  					})
   235  				})
   236  			})
   237  		})
   238  
   239  		When("the service instance does not exist", func() {
   240  			When("the -f flag is provided", func() {
   241  				It("fails with a service instance not found error", func() {
   242  					session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-f")
   243  					Eventually(session).Should(Say("FAILED"))
   244  					Eventually(session.Err).Should(Say(`Specified instance not found or not a managed service instance\. Sharing is not supported for user provided services\.`))
   245  					Eventually(session).Should(Exit(1))
   246  				})
   247  			})
   248  
   249  			When("the -f flag not is provided", func() {
   250  				var buffer *Buffer
   251  
   252  				BeforeEach(func() {
   253  					buffer = NewBuffer()
   254  				})
   255  
   256  				When("the user enters 'y'", func() {
   257  					BeforeEach(func() {
   258  						_, err := buffer.Write([]byte("y\n"))
   259  						Expect(err).ToNot(HaveOccurred())
   260  					})
   261  
   262  					It("fails with a service instance not found error", func() {
   263  						username, _ := helpers.GetCredentials()
   264  						session := helpers.CFWithStdin(buffer, "unshare-service", serviceInstance, "-s", sharedToSpaceName)
   265  						Eventually(session.Err).Should(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\.`))
   266  						Eventually(session).Should(Say(`Really unshare the service instance\? \[yN\]`))
   267  						Eventually(session).Should(Say(`Unsharing service instance %s from org %s / space %s as %s\.\.\.`, serviceInstance, sourceOrgName, sharedToSpaceName, username))
   268  						Eventually(session).Should(Say("FAILED"))
   269  						Eventually(session.Err).Should(Say(`Specified instance not found or not a managed service instance\. Sharing is not supported for user provided services\.`))
   270  						Eventually(session).Should(Exit(1))
   271  					})
   272  				})
   273  
   274  				When("the user enters 'n'", func() {
   275  					BeforeEach(func() {
   276  						_, err := buffer.Write([]byte("n\n"))
   277  						Expect(err).ToNot(HaveOccurred())
   278  					})
   279  
   280  					It("does not attempt to unshare", func() {
   281  						session := helpers.CFWithStdin(buffer, "unshare-service", serviceInstance, "-s", sharedToSpaceName)
   282  						Eventually(session.Err).Should(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  						Eventually(session).Should(Say(`Really unshare the service instance\? \[yN\]`))
   284  						Eventually(session).Should(Say("Unshare cancelled"))
   285  						Eventually(session).Should(Exit(0))
   286  					})
   287  				})
   288  
   289  				When("the user enters the default input (hits return)", func() {
   290  					BeforeEach(func() {
   291  						_, err := buffer.Write([]byte("\n"))
   292  						Expect(err).ToNot(HaveOccurred())
   293  					})
   294  
   295  					It("does not attempt to unshare", func() {
   296  						session := helpers.CFWithStdin(buffer, "unshare-service", serviceInstance, "-s", sharedToSpaceName)
   297  						Eventually(session.Err).Should(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\.`))
   298  						Eventually(session).Should(Say(`Really unshare the service instance\? \[yN\]`))
   299  						Eventually(session).Should(Say("Unshare cancelled"))
   300  						Eventually(session).Should(Exit(0))
   301  					})
   302  				})
   303  
   304  				When("the user enters an invalid answer", func() {
   305  					BeforeEach(func() {
   306  						// The second '\n' is intentional. Otherwise the buffer will be
   307  						// closed while the interaction is still waiting for input; it gets
   308  						// an EOF and causes an error.
   309  						_, err := buffer.Write([]byte("wat\n\n"))
   310  						Expect(err).ToNot(HaveOccurred())
   311  					})
   312  
   313  					It("asks again", func() {
   314  						session := helpers.CFWithStdin(buffer, "unshare-service", serviceInstance, "-s", sharedToSpaceName)
   315  						Eventually(session.Err).Should(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\.`))
   316  						Eventually(session).Should(Say(`Really unshare the service instance\? \[yN\]`))
   317  						Eventually(session).Should(Say(`invalid input \(not y, n, yes, or no\)`))
   318  						Eventually(session).Should(Say(`Really unshare the service instance\? \[yN\]`))
   319  						Eventually(session).Should(Say("Unshare cancelled"))
   320  						Eventually(session).Should(Exit(0))
   321  					})
   322  				})
   323  			})
   324  		})
   325  
   326  		When("there is a shared service instance in my currently targeted space", func() {
   327  			var (
   328  				broker   *fakeservicebroker.FakeServiceBroker
   329  				user     string
   330  				password string
   331  			)
   332  
   333  			BeforeEach(func() {
   334  				broker = fakeservicebroker.New().EnsureBrokerIsAvailable()
   335  				service = broker.ServiceName()
   336  				servicePlan = broker.ServicePlanName()
   337  
   338  				user = helpers.NewUsername()
   339  				password = helpers.NewPassword()
   340  
   341  				helpers.SetupCF(sourceOrgName, sourceSpaceName)
   342  				Eventually(helpers.CF("enable-service-access", service)).Should(Exit(0))
   343  				Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   344  				Eventually(helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)).Should(Exit(0))
   345  
   346  				Eventually(helpers.CF("create-user", user, password)).Should(Exit(0))
   347  				Eventually(helpers.CF("set-space-role", user, sharedToOrgName, sharedToSpaceName, "SpaceDeveloper")).Should(Exit(0))
   348  			})
   349  
   350  			AfterEach(func() {
   351  				helpers.SetupCF(sourceOrgName, sourceSpaceName)
   352  				Eventually(helpers.CF("delete-user", user)).Should(Exit(0))
   353  				broker.Destroy()
   354  			})
   355  
   356  			Context("and I have no access to the source space", func() {
   357  				BeforeEach(func() {
   358  					env := map[string]string{
   359  						"CF_USERNAME": user,
   360  						"CF_PASSWORD": password,
   361  					}
   362  					helpers.LogoutCF()
   363  					Eventually(helpers.CFWithEnv(env, "auth")).Should(Exit(0))
   364  					Eventually(helpers.CF("target", "-o", sharedToOrgName, "-s", sharedToSpaceName)).Should(Exit(0))
   365  				})
   366  
   367  				It("returns a permission error on an attempt to unshare the service", func() {
   368  					session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName, "-f")
   369  					Eventually(session).Should(Say("FAILED"))
   370  					Eventually(session.Err).Should(Say("You are not authorized to perform the requested action"))
   371  					Eventually(session).Should(Exit(1))
   372  				})
   373  			})
   374  
   375  			Context("and I have SpaceAuditor access to the source space", func() {
   376  				BeforeEach(func() {
   377  					env := map[string]string{
   378  						"CF_USERNAME": user,
   379  						"CF_PASSWORD": password,
   380  					}
   381  					Eventually(helpers.CF("set-space-role", user, sourceOrgName, sourceSpaceName, "SpaceAuditor")).Should(Exit(0))
   382  					helpers.LogoutCF()
   383  					Eventually(helpers.CFWithEnv(env, "auth")).Should(Exit(0))
   384  					Eventually(helpers.CF("target", "-o", sharedToOrgName, "-s", sharedToSpaceName)).Should(Exit(0))
   385  				})
   386  
   387  				It("returns a permission error on an attempt to unshare the service", func() {
   388  					session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName, "-f")
   389  					Eventually(session).Should(Say("FAILED"))
   390  					Eventually(session.Err).Should(Say("You are not authorized to perform the requested action"))
   391  					Eventually(session).Should(Exit(1))
   392  				})
   393  			})
   394  		})
   395  	})
   396  })