github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/experimental/v3_unshare_service_command_test.go (about)

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