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