github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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.MinV3ClientVersion)
    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.NewServiceBroker(helpers.NewServiceBrokerName(), helpers.NewAssets().ServiceBroker, domain, service, servicePlan)
   125  				broker.Push()
   126  				broker.Configure(true)
   127  				broker.Create()
   128  
   129  				Eventually(helpers.CF("enable-service-access", service)).Should(Exit(0))
   130  				Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   131  			})
   132  
   133  			AfterEach(func() {
   134  				broker.Destroy()
   135  			})
   136  
   137  			When("the service instance has not been shared to this space", func() {
   138  				It("displays info and idempotently exits 0", func() {
   139  					session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName, "-f")
   140  					Eventually(session).Should(Say(`Service instance %s is not shared with space %s in organization %s\.`, serviceInstance, sharedToSpaceName, sharedToOrgName))
   141  					Eventually(session).Should(Say("OK"))
   142  					Eventually(session).Should(Exit(0))
   143  				})
   144  			})
   145  
   146  			When("I have shared my service instance to a space in another org ('-o' flag provided)", func() {
   147  				BeforeEach(func() {
   148  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   149  					Eventually(session).Should(Exit(0))
   150  				})
   151  
   152  				When("the org I want to unshare from does not exist", func() {
   153  					It("fails with an org not found error", func() {
   154  						session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", "missing-org", "-f")
   155  						Eventually(session).Should(Say(`Service instance %s is not shared with space %s in organization missing-org\.`, serviceInstance, sharedToSpaceName))
   156  						Eventually(session).Should(Say("OK"))
   157  						Eventually(session).Should(Exit(0))
   158  					})
   159  				})
   160  
   161  				When("the space I want to unshare from does not exist", func() {
   162  					It("fails with a space not found error", func() {
   163  						session := helpers.CF("unshare-service", serviceInstance, "-s", "missing-space", "-o", sharedToOrgName, "-f")
   164  						Eventually(session).Should(Say(`Service instance %s is not shared with space missing-space in organization %s\.`, serviceInstance, sharedToOrgName))
   165  						Eventually(session).Should(Say("OK"))
   166  						Eventually(session).Should(Exit(0))
   167  					})
   168  				})
   169  
   170  				When("I want to unshare my service instance from a space and org", func() {
   171  					It("successfully unshares the service instance", func() {
   172  						username, _ := helpers.GetCredentials()
   173  						session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName, "-f")
   174  						Eventually(session).Should(Say(`Unsharing service instance %s from org %s / space %s as %s\.\.\.`, serviceInstance, sharedToOrgName, sharedToSpaceName, username))
   175  						Eventually(session).Should(Say("OK"))
   176  						Eventually(session).Should(Exit(0))
   177  					})
   178  				})
   179  			})
   180  
   181  			When("I have shared my service instance to a space within the targeted org ('-o' flag NOT provided)", func() {
   182  				BeforeEach(func() {
   183  					helpers.CreateSpace(sharedToSpaceName)
   184  
   185  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)
   186  					Eventually(session).Should(Exit(0))
   187  				})
   188  
   189  				When("the space I want to unshare from does not exist", func() {
   190  					It("fails with a space not found error", func() {
   191  						session := helpers.CF("unshare-service", serviceInstance, "-s", "missing-space", "-f")
   192  						Eventually(session).Should(Say(`Service instance %s is not shared with space missing-space in organization %s\.`, serviceInstance, sourceOrgName))
   193  						Eventually(session).Should(Say("OK"))
   194  						Eventually(session).Should(Exit(0))
   195  					})
   196  				})
   197  
   198  				When("I want to unshare my service instance from the space", func() {
   199  					It("successfully unshares the service instance when I am admin", func() {
   200  						username, _ := helpers.GetCredentials()
   201  						session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-f")
   202  						Eventually(session).Should(Say(`Unsharing service instance %s from org %s / space %s as %s\.\.\.`, serviceInstance, sourceOrgName, sharedToSpaceName, username))
   203  						Eventually(session).Should(Say("OK"))
   204  						Eventually(session).Should(Exit(0))
   205  					})
   206  
   207  					When("I have no access to the shared-to space", func() {
   208  						var (
   209  							username string
   210  							password string
   211  						)
   212  
   213  						BeforeEach(func() {
   214  							username = helpers.NewUsername()
   215  							password = helpers.NewPassword()
   216  							Eventually(helpers.CF("create-user", username, password)).Should(Exit(0))
   217  							Eventually(helpers.CF("set-space-role", username, sourceOrgName, sourceSpaceName, "SpaceDeveloper")).Should(Exit(0))
   218  							env := map[string]string{
   219  								"CF_USERNAME": username,
   220  								"CF_PASSWORD": password,
   221  							}
   222  							Eventually(helpers.CFWithEnv(env, "auth")).Should(Exit(0))
   223  							helpers.TargetOrgAndSpace(sourceOrgName, sourceSpaceName)
   224  						})
   225  
   226  						AfterEach(func() {
   227  							helpers.LoginCF()
   228  							helpers.TargetOrgAndSpace(sourceOrgName, sourceSpaceName)
   229  							session := helpers.CF("delete-user", username, "-f")
   230  							Eventually(session).Should(Say("OK"))
   231  							Eventually(session).Should(Exit(0))
   232  						})
   233  
   234  						It("successfully unshares the service instance", func() {
   235  							session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-f")
   236  							Eventually(session).Should(Say("OK"))
   237  							Eventually(session).Should(Exit(0))
   238  						})
   239  					})
   240  				})
   241  			})
   242  		})
   243  
   244  		When("the service instance does not exist", func() {
   245  			When("the -f flag is provided", func() {
   246  				It("fails with a service instance not found error", func() {
   247  					session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-f")
   248  					Eventually(session).Should(Say("FAILED"))
   249  					Eventually(session.Err).Should(Say(`Specified instance not found or not a managed service instance\. Sharing is not supported for user provided services\.`))
   250  					Eventually(session).Should(Exit(1))
   251  				})
   252  			})
   253  
   254  			When("the -f flag not is provided", func() {
   255  				var buffer *Buffer
   256  
   257  				BeforeEach(func() {
   258  					buffer = NewBuffer()
   259  				})
   260  
   261  				When("the user enters 'y'", func() {
   262  					BeforeEach(func() {
   263  						buffer.Write([]byte("y\n"))
   264  					})
   265  
   266  					It("fails with a service instance not found error", func() {
   267  						username, _ := helpers.GetCredentials()
   268  						session := helpers.CFWithStdin(buffer, "unshare-service", serviceInstance, "-s", sharedToSpaceName)
   269  						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\.`))
   270  						Eventually(session).Should(Say(`Really unshare the service instance\? \[yN\]`))
   271  						Eventually(session).Should(Say(`Unsharing service instance %s from org %s / space %s as %s\.\.\.`, serviceInstance, sourceOrgName, sharedToSpaceName, username))
   272  						Eventually(session).Should(Say("FAILED"))
   273  						Eventually(session.Err).Should(Say(`Specified instance not found or not a managed service instance\. Sharing is not supported for user provided services\.`))
   274  						Eventually(session).Should(Exit(1))
   275  					})
   276  				})
   277  
   278  				When("the user enters 'n'", func() {
   279  					BeforeEach(func() {
   280  						buffer.Write([]byte("n\n"))
   281  					})
   282  
   283  					It("does not attempt to unshare", func() {
   284  						session := helpers.CFWithStdin(buffer, "unshare-service", serviceInstance, "-s", sharedToSpaceName)
   285  						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\.`))
   286  						Eventually(session).Should(Say(`Really unshare the service instance\? \[yN\]`))
   287  						Eventually(session).Should(Say("Unshare cancelled"))
   288  						Eventually(session).Should(Exit(0))
   289  					})
   290  				})
   291  
   292  				When("the user enters the default input (hits return)", func() {
   293  					BeforeEach(func() {
   294  						buffer.Write([]byte("\n"))
   295  					})
   296  
   297  					It("does not attempt to unshare", func() {
   298  						session := helpers.CFWithStdin(buffer, "unshare-service", serviceInstance, "-s", sharedToSpaceName)
   299  						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\.`))
   300  						Eventually(session).Should(Say(`Really unshare the service instance\? \[yN\]`))
   301  						Eventually(session).Should(Say("Unshare cancelled"))
   302  						Eventually(session).Should(Exit(0))
   303  					})
   304  				})
   305  
   306  				When("the user enters an invalid answer", func() {
   307  					BeforeEach(func() {
   308  						// The second '\n' is intentional. Otherwise the buffer will be
   309  						// closed while the interaction is still waiting for input; it gets
   310  						// an EOF and causes an error.
   311  						buffer.Write([]byte("wat\n\n"))
   312  					})
   313  
   314  					It("asks again", func() {
   315  						session := helpers.CFWithStdin(buffer, "unshare-service", serviceInstance, "-s", sharedToSpaceName)
   316  						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\.`))
   317  						Eventually(session).Should(Say(`Really unshare the service instance\? \[yN\]`))
   318  						Eventually(session).Should(Say(`invalid input \(not y, n, yes, or no\)`))
   319  						Eventually(session).Should(Say(`Really unshare the service instance\? \[yN\]`))
   320  						Eventually(session).Should(Say("Unshare cancelled"))
   321  						Eventually(session).Should(Exit(0))
   322  					})
   323  				})
   324  			})
   325  		})
   326  
   327  		When("there is a shared service instance in my currently targeted space", func() {
   328  			var broker helpers.ServiceBroker
   329  			var user string
   330  			var password string
   331  
   332  			BeforeEach(func() {
   333  				broker = helpers.NewServiceBroker(helpers.NewServiceBrokerName(), helpers.NewAssets().ServiceBroker, domain, service, servicePlan)
   334  				broker.Push()
   335  				broker.Configure(true)
   336  				broker.Create()
   337  				user = helpers.NewUsername()
   338  				password = helpers.NewPassword()
   339  
   340  				helpers.SetupCF(sourceOrgName, sourceSpaceName)
   341  				Eventually(helpers.CF("enable-service-access", service)).Should(Exit(0))
   342  				Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   343  				Eventually(helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)).Should(Exit(0))
   344  
   345  				Eventually(helpers.CF("create-user", user, password)).Should(Exit(0))
   346  				Eventually(helpers.CF("set-space-role", user, sharedToOrgName, sharedToSpaceName, "SpaceDeveloper")).Should(Exit(0))
   347  			})
   348  
   349  			AfterEach(func() {
   350  				helpers.SetupCF(sourceOrgName, sourceSpaceName)
   351  				Eventually(helpers.CF("delete-user", user)).Should(Exit(0))
   352  				broker.Destroy()
   353  			})
   354  
   355  			Context("and I have no access to the source space", func() {
   356  				BeforeEach(func() {
   357  					env := map[string]string{
   358  						"CF_USERNAME": user,
   359  						"CF_PASSWORD": password,
   360  					}
   361  					Eventually(helpers.CFWithEnv(env, "auth")).Should(Exit(0))
   362  					Eventually(helpers.CF("target", "-o", sharedToOrgName, "-s", sharedToSpaceName)).Should(Exit(0))
   363  				})
   364  
   365  				It("returns a permission error on an attempt to unshare the service", func() {
   366  					session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName, "-f")
   367  					Eventually(session).Should(Say("FAILED"))
   368  					Eventually(session.Err).Should(Say("You are not authorized to perform the requested action"))
   369  					Eventually(session).Should(Exit(1))
   370  				})
   371  			})
   372  
   373  			Context("and I have SpaceAuditor access to the source space", func() {
   374  				BeforeEach(func() {
   375  					env := map[string]string{
   376  						"CF_USERNAME": user,
   377  						"CF_PASSWORD": password,
   378  					}
   379  					Eventually(helpers.CF("set-space-role", user, sourceOrgName, sourceSpaceName, "SpaceAuditor")).Should(Exit(0))
   380  					Eventually(helpers.CFWithEnv(env, "auth")).Should(Exit(0))
   381  					Eventually(helpers.CF("target", "-o", sharedToOrgName, "-s", sharedToSpaceName)).Should(Exit(0))
   382  				})
   383  
   384  				It("returns a permission error on an attempt to unshare the service", func() {
   385  					session := helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName, "-f")
   386  					Eventually(session).Should(Say("FAILED"))
   387  					Eventually(session.Err).Should(Say("You are not authorized to perform the requested action"))
   388  					Eventually(session).Should(Exit(1))
   389  				})
   390  			})
   391  		})
   392  	})
   393  })