github.com/arunkumar7540/cli@v6.45.0+incompatible/integration/shared/isolated/enable_service_access_command_test.go (about)

     1  package isolated
     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  )
    11  
    12  var _ = Describe("enable service access command", func() {
    13  	Describe("help", func() {
    14  		When("--help flag is set", func() {
    15  			It("displays command usage to output", func() {
    16  				session := helpers.CF("enable-service-access", "--help")
    17  				Eventually(session).Should(Say("NAME:"))
    18  				Eventually(session).Should(Say("\\s+enable-service-access - Enable access to a service or service plan for one or all orgs"))
    19  				Eventually(session).Should(Say("USAGE:"))
    20  				Eventually(session).Should(Say("\\s+cf enable-service-access SERVICE \\[-b BROKER\\] \\[-p PLAN\\] \\[-o ORG\\]"))
    21  				Eventually(session).Should(Say("OPTIONS:"))
    22  				Eventually(session).Should(Say("\\s+\\-b\\s+Enable access to a service from a particular service broker. Required when service name is ambiguous"))
    23  				Eventually(session).Should(Say("\\s+\\-o\\s+Enable access for a specified organization"))
    24  				Eventually(session).Should(Say("\\s+\\-p\\s+Enable access to a specified service plan"))
    25  				Eventually(session).Should(Say("SEE ALSO:"))
    26  				Eventually(session).Should(Say("\\s+marketplace, service-access, service-brokers"))
    27  				Eventually(session).Should(Exit(0))
    28  			})
    29  		})
    30  
    31  		When("no service argument was provided", func() {
    32  			It("displays a warning, the help text, and exits 1", func() {
    33  				session := helpers.CF("enable-service-access")
    34  				Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `SERVICE` was not provided"))
    35  				Eventually(session).Should(Say("NAME:"))
    36  				Eventually(session).Should(Say("\\s+enable-service-access - Enable access to a service or service plan for one or all orgs"))
    37  				Eventually(session).Should(Say("USAGE:"))
    38  				Eventually(session).Should(Say("\\s+cf enable-service-access SERVICE \\[-b BROKER\\] \\[-p PLAN\\] \\[-o ORG\\]"))
    39  				Eventually(session).Should(Say("OPTIONS:"))
    40  				Eventually(session).Should(Say("\\s+\\-b\\s+Enable access to a service from a particular service broker. Required when service name is ambiguous"))
    41  				Eventually(session).Should(Say("\\s+\\-o\\s+Enable access for a specified organization"))
    42  				Eventually(session).Should(Say("\\s+\\-p\\s+Enable access to a specified service plan"))
    43  				Eventually(session).Should(Say("SEE ALSO:"))
    44  				Eventually(session).Should(Say("\\s+marketplace, service-access, service-brokers"))
    45  				Eventually(session).Should(Exit(1))
    46  			})
    47  		})
    48  
    49  		When("two services arguments are provided", func() {
    50  			It("displays an error, and exits 1", func() {
    51  				session := helpers.CF("enable-service-access", "a-service", "another-service")
    52  				Eventually(session).Should(Say("FAILED"))
    53  				Eventually(session.Err).Should(Say(`Incorrect Usage: unexpected argument "another-service"`))
    54  				Eventually(session).Should(Say("NAME:"))
    55  				Eventually(session).Should(Say("\\s+enable-service-access - Enable access to a service or service plan for one or all orgs"))
    56  				Eventually(session).Should(Say("USAGE:"))
    57  				Eventually(session).Should(Say("\\s+cf enable-service-access SERVICE \\[-b BROKER\\] \\[-p PLAN\\] \\[-o ORG\\]"))
    58  				Eventually(session).Should(Say("OPTIONS:"))
    59  				Eventually(session).Should(Say("\\s+\\-b\\s+Enable access to a service from a particular service broker. Required when service name is ambiguous"))
    60  				Eventually(session).Should(Say("\\s+\\-o\\s+Enable access for a specified organization"))
    61  				Eventually(session).Should(Say("\\s+\\-p\\s+Enable access to a specified service plan"))
    62  				Eventually(session).Should(Say("SEE ALSO:"))
    63  				Eventually(session).Should(Say("\\s+marketplace, service-access, service-brokers"))
    64  				Eventually(session).Should(Exit(1))
    65  			})
    66  		})
    67  	})
    68  
    69  	Context("not logged in", func() {
    70  		BeforeEach(func() {
    71  			helpers.LogoutCF()
    72  		})
    73  
    74  		It("displays FAILED, an informative error message, and exits 1", func() {
    75  			session := helpers.CF("enable-service-access", "does-not-matter")
    76  			Eventually(session).Should(Say("FAILED"))
    77  			Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    78  			Eventually(session).Should(Exit(1))
    79  		})
    80  	})
    81  
    82  	Context("logged in", func() {
    83  		BeforeEach(func() {
    84  			helpers.LoginCF()
    85  		})
    86  
    87  		Context("the service does not exist", func() {
    88  			When("only the service is specified", func() {
    89  				It("displays FAILED, an informative error message, and exits 1", func() {
    90  					session := helpers.CF("enable-service-access", "some-service")
    91  					Eventually(session).Should(Say("Enabling access to all plans of service some-service for all orgs as admin\\.\\.\\."))
    92  					Eventually(session).Should(Say("FAILED"))
    93  					Eventually(session.Err).Should(Say("Service offering 'some-service' not found"))
    94  					Eventually(session).Should(Exit(1))
    95  				})
    96  			})
    97  
    98  			When("a service and an org are specified", func() {
    99  				It("displays FAILED, an informative error message, and exits 1", func() {
   100  					session := helpers.CF("enable-service-access", "some-service", "-o", "some-org")
   101  					Eventually(session).Should(Say("Enabling access to all plans of service some-service for the org some-org as admin\\.\\.\\."))
   102  					Eventually(session).Should(Say("FAILED"))
   103  					Eventually(session.Err).Should(Say("Service offering 'some-service' not found"))
   104  					Eventually(session).Should(Exit(1))
   105  				})
   106  			})
   107  
   108  			When("a service and a plan are specified", func() {
   109  				It("displays FAILED, an informative error message, and exits 1", func() {
   110  					session := helpers.CF("enable-service-access", "some-service", "-p", "some-plan")
   111  					Eventually(session).Should(Say("Enabling access of plan some-plan for service some-service as admin\\.\\.\\."))
   112  					Eventually(session).Should(Say("FAILED"))
   113  					Eventually(session.Err).Should(Say("Service offering 'some-service' not found"))
   114  					Eventually(session).Should(Exit(1))
   115  				})
   116  			})
   117  
   118  			When("a service, a plan and an org are specified", func() {
   119  				It("displays FAILED, an informative error message, and exits 1", func() {
   120  					session := helpers.CF("enable-service-access", "some-service", "-p", "some-plan", "-o", "some-org")
   121  					Eventually(session).Should(Say("Enabling access to plan some-plan of service some-service for org some-org as admin\\.\\.\\."))
   122  					Eventually(session).Should(Say("FAILED"))
   123  					Eventually(session.Err).Should(Say("Service offering 'some-service' not found"))
   124  					Eventually(session).Should(Exit(1))
   125  				})
   126  			})
   127  		})
   128  
   129  		Context("a service broker is registered", func() {
   130  			var (
   131  				orgName      string
   132  				spaceName    string
   133  				domain       string
   134  				service      string
   135  				servicePlan  string
   136  				broker       helpers.ServiceBroker
   137  				secondBroker helpers.ServiceBroker
   138  			)
   139  
   140  			BeforeEach(func() {
   141  				orgName = helpers.NewOrgName()
   142  				spaceName = helpers.NewSpaceName()
   143  				helpers.SetupCF(orgName, spaceName)
   144  
   145  				domain = helpers.DefaultSharedDomain()
   146  				service = helpers.PrefixedRandomName("SERVICE")
   147  				servicePlan = helpers.PrefixedRandomName("SERVICE-PLAN")
   148  
   149  				broker = helpers.CreateBroker(domain, service, servicePlan)
   150  			})
   151  
   152  			AfterEach(func() {
   153  				broker.Destroy()
   154  				helpers.QuickDeleteOrg(orgName)
   155  			})
   156  
   157  			When("a service name is provided", func() {
   158  				It("displays an informative message, exits 0, and enables the service for all orgs", func() {
   159  					session := helpers.CF("enable-service-access", service)
   160  					Eventually(session).Should(Say("Enabling access to all plans of service %s for all orgs as admin...", service))
   161  					Eventually(session).Should(Say("OK"))
   162  					Eventually(session).Should(Exit(0))
   163  
   164  					session = helpers.CF("service-access", "-e", service)
   165  					Eventually(session).Should(Exit(0))
   166  					Eventually(session).Should(Say("broker:\\s+%s", broker.Name))
   167  					Eventually(session).Should(Say("%s\\s+%s\\s+all",
   168  						service,
   169  						servicePlan,
   170  					))
   171  				})
   172  
   173  				When("service is already enabled for an org for a plan", func() {
   174  					BeforeEach(func() {
   175  						session := helpers.CF("enable-service-access", service, "-p", servicePlan, "-o", orgName)
   176  						Eventually(session).Should(Say("OK"))
   177  						Eventually(session).Should(Exit(0))
   178  					})
   179  
   180  					It("disables the limited access for that org", func() {
   181  						session := helpers.CF("enable-service-access", service, "-p", servicePlan)
   182  						Eventually(session).Should(Say("Enabling access of plan %s for service %s as admin...", servicePlan, service))
   183  						Eventually(session).Should(Say("OK"))
   184  						Eventually(session).Should(Exit(0))
   185  
   186  						session = helpers.CF("service-access", "-e", service)
   187  						Eventually(session).Should(Exit(0))
   188  						Eventually(session).Should(Say("broker:\\s+%s", broker.Name))
   189  						Eventually(session).Should(Say("%s\\s+%s\\s+all\\s",
   190  							service,
   191  							servicePlan,
   192  						))
   193  
   194  					})
   195  
   196  					When("enabling access globally", func() {
   197  						It("should disbale org level access first", func() {
   198  							session := helpers.CF("enable-service-access", service)
   199  							Eventually(session).Should(Say("OK"))
   200  							Eventually(session).Should(Exit(0))
   201  
   202  							session = helpers.CF("service-access", "-e", service)
   203  							Eventually(session).Should(Exit(0))
   204  							Eventually(session).Should(Say("broker:\\s+%s", broker.Name))
   205  							Eventually(session).Should(Say("%s\\s+%s\\s+all",
   206  								service,
   207  								servicePlan,
   208  							))
   209  							Consistently(session).ShouldNot(Say(orgName))
   210  						})
   211  					})
   212  				})
   213  			})
   214  
   215  			When("two services with the same name are registered", func() {
   216  				BeforeEach(func() {
   217  					helpers.SkipIfVersionLessThan(ccversion.MinVersionMultiServiceRegistrationV2)
   218  					secondBroker = helpers.CreateBroker(domain, service, servicePlan)
   219  				})
   220  
   221  				AfterEach(func() {
   222  					secondBroker.Destroy()
   223  				})
   224  
   225  				When("a service name and broker name are provided", func() {
   226  					It("displays an informative message, exits 0, and enables access to the service", func() {
   227  						session := helpers.CF("enable-service-access", service, "-b", secondBroker.Name)
   228  						Eventually(session).Should(Say("Enabling access to all plans of service %s from broker %s for all orgs as admin...", service, secondBroker.Name))
   229  						Eventually(session).Should(Say("OK"))
   230  						Eventually(session).Should(Exit(0))
   231  
   232  						session = helpers.CF("service-access", "-b", secondBroker.Name)
   233  						Eventually(session).Should(Exit(0))
   234  						Eventually(session).Should(Say("broker:\\s+%s", secondBroker.Name))
   235  						Eventually(session).Should(Say("%s\\s+%s\\s+all",
   236  							service,
   237  							servicePlan,
   238  						))
   239  					})
   240  
   241  					When("a broker name is not provided", func() {
   242  						It("fails to create the service", func() {
   243  							session := helpers.CF("enable-service-access", service)
   244  							Eventually(session).Should(Say("Enabling access to all plans of service %s for all orgs as admin...", service))
   245  							Eventually(session.Err).Should(Say("Service '%s' is provided by multiple service brokers. Specify a broker by using the '-b' flag.", service))
   246  							Eventually(session).Should(Say("FAILED"))
   247  							Eventually(session).Should(Exit(1))
   248  
   249  							session = helpers.CF("service-access", "-b", secondBroker.Name)
   250  							Eventually(session).Should(Exit(0))
   251  							Eventually(session).Should(Say("broker:\\s+%s", secondBroker.Name))
   252  							Eventually(session).Should(Say("%s\\s+%s\\s+none",
   253  								service,
   254  								servicePlan,
   255  							))
   256  						})
   257  					})
   258  
   259  					When("no broker by that name exists", func() {
   260  						It("displays an informative message, exits 1", func() {
   261  							session := helpers.CF("enable-service-access", service, "-b", "non-existent-broker")
   262  							Eventually(session).Should(Say("Enabling access to all plans of service %s from broker %s for all orgs as admin...", service, "non-existent-broker"))
   263  							Eventually(session.Err).Should(Say("Service broker 'non-existent-broker' not found"))
   264  							Eventually(session.Err).Should(Say("TIP: Use 'cf service-brokers' to see a list of available brokers."))
   265  							Eventually(session).Should(Say("FAILED"))
   266  							Eventually(session).Should(Exit(1))
   267  						})
   268  					})
   269  				})
   270  			})
   271  
   272  			When("a service name and plan name are provided", func() {
   273  				It("displays an informative message, exits 0, and enables the plan for all orgs", func() {
   274  					session := helpers.CF("enable-service-access", service, "-p", servicePlan)
   275  					Eventually(session).Should(Say("Enabling access of plan %s for service %s as admin...", servicePlan, service))
   276  					Eventually(session).Should(Say("OK"))
   277  					Eventually(session).Should(Exit(0))
   278  
   279  					session = helpers.CF("service-access", "-e", service)
   280  					Eventually(session).Should(Exit(0))
   281  					Eventually(session).Should(Say("broker:\\s+%s", broker.Name))
   282  					Eventually(session).Should(Say("%s\\s+%s\\s+all",
   283  						service,
   284  						servicePlan,
   285  					))
   286  				})
   287  			})
   288  
   289  			When("a service name and org is provided", func() {
   290  				It("displays an informative message, and exits 0", func() {
   291  					session := helpers.CF("enable-service-access", service, "-o", orgName)
   292  					Eventually(session).Should(Say("Enabling access to all plans of service %s for the org %s as admin...", service, orgName))
   293  					Eventually(session).Should(Say("OK"))
   294  					Eventually(session).Should(Exit(0))
   295  				})
   296  			})
   297  
   298  			When("a service name, plan name and org is provided", func() {
   299  				It("displays an informative message, and exits 0", func() {
   300  					session := helpers.CF("enable-service-access", service, "-p", servicePlan, "-o", orgName)
   301  					Eventually(session).Should(Say("Enabling access to plan %s of service %s for org %s as admin...", servicePlan, service, orgName))
   302  					Eventually(session).Should(Say("OK"))
   303  					Eventually(session).Should(Exit(0))
   304  
   305  					session = helpers.CF("service-access", "-e", service)
   306  					Eventually(session).Should(Exit(0))
   307  					Eventually(session).Should(Say("broker:\\s+%s", broker.Name))
   308  					Eventually(session).Should(Say("%s\\s+%s\\s+%s\\s+%s",
   309  						service,
   310  						servicePlan,
   311  						"limited",
   312  						orgName,
   313  					))
   314  				})
   315  			})
   316  
   317  			When("the org does not exist", func() {
   318  				It("displays FAILED, an informative error message, and exits 1", func() {
   319  					session := helpers.CF("enable-service-access", service, "-o", "not-a-real-org")
   320  					Eventually(session).Should(Say("Enabling access to all plans of service %s for the org not-a-real-org as admin...", service))
   321  					Eventually(session).Should(Say("FAILED"))
   322  					Eventually(session.Err).Should(Say("Organization 'not-a-real-org' not found"))
   323  					Eventually(session).Should(Exit(1))
   324  				})
   325  			})
   326  
   327  			When("the plan does not exist", func() {
   328  				It("displays FAILED, an informative error message, and exits 1", func() {
   329  					session := helpers.CF("enable-service-access", service, "-p", "plan-does-not-exist")
   330  					Eventually(session).Should(Say("Enabling access of plan plan-does-not-exist for service %s as admin...", service))
   331  					Eventually(session).Should(Say("FAILED"))
   332  					Eventually(session.Err).Should(Say("The plan plan-does-not-exist could not be found for service %s", service))
   333  					Eventually(session).Should(Exit(1))
   334  				})
   335  			})
   336  
   337  			When("the plan does exist and the org does not exist", func() {
   338  				It("displays FAILED, an informative error message, and exits 1", func() {
   339  					session := helpers.CF("enable-service-access", service, "-p", servicePlan, "-o", "not-a-real-org")
   340  					Eventually(session).Should(Say("Enabling access to plan %s of service %s for org not-a-real-org as admin...", servicePlan, service))
   341  					Eventually(session).Should(Say("FAILED"))
   342  					Eventually(session.Err).Should(Say("Organization 'not-a-real-org' not found"))
   343  					Eventually(session).Should(Exit(1))
   344  				})
   345  			})
   346  
   347  			When("the plan does not exist and the org does exist", func() {
   348  				It("displays FAILED, an informative error message, and exits 1", func() {
   349  					session := helpers.CF("enable-service-access", service, "-p", "not-a-real-plan", "-o", orgName)
   350  					Eventually(session).Should(Say("Enabling access to plan not-a-real-plan of service %s for org %s as admin...", service, orgName))
   351  					Eventually(session).Should(Say("FAILED"))
   352  					Eventually(session.Err).Should(Say("Service plan 'not-a-real-plan' not found"))
   353  					Eventually(session).Should(Exit(1))
   354  				})
   355  			})
   356  
   357  			Context("when access is already enabled in the org", func() {
   358  				BeforeEach(func() {
   359  					session := helpers.CF("enable-service-access", service, "-o", orgName)
   360  					Eventually(session).Should(Say("OK"))
   361  				})
   362  
   363  				It("displays OK, and exits 0", func() {
   364  					session := helpers.CF("enable-service-access", service, "-o", orgName)
   365  					Eventually(session).Should(Say("Enabling access to all plans of service %s for the org %s as admin...", service, orgName))
   366  					Eventually(session).Should(Say("OK"))
   367  					Eventually(session).Should(Exit(0))
   368  				})
   369  			})
   370  
   371  			Context("when access is already globally enabled", func() {
   372  				BeforeEach(func() {
   373  					Eventually(helpers.CF("enable-service-access", service)).Should(Exit(0))
   374  				})
   375  
   376  				When("when we try to enable access for an org", func() {
   377  					It("should still be enabled only globally", func() {
   378  						session := helpers.CF("enable-service-access", service, "-o", orgName)
   379  						Eventually(session).Should(Say("OK"))
   380  						Eventually(session).Should(Exit(0))
   381  
   382  						session = helpers.CF("service-access", "-e", service)
   383  						Eventually(session).Should(Exit(0))
   384  						Eventually(session).Should(Say("broker:\\s+%s", broker.Name))
   385  						Eventually(session).Should(Say("%s\\s+%s\\s+all",
   386  							service,
   387  							servicePlan,
   388  						))
   389  						Consistently(session).ShouldNot(Say(orgName))
   390  
   391  					})
   392  				})
   393  
   394  				When("when we try to enable access for an org for a plan", func() {
   395  					It("should still be enabled only globally", func() {
   396  						session := helpers.CF("enable-service-access", service, "-o", orgName, "-p", servicePlan)
   397  						Eventually(session).Should(Say("OK"))
   398  						Eventually(session).Should(Exit(0))
   399  
   400  						session = helpers.CF("service-access", "-e", service)
   401  						Eventually(session).Should(Exit(0))
   402  						Eventually(session).Should(Say("broker:\\s+%s", broker.Name))
   403  						Eventually(session).Should(Say("%s\\s+%s\\s+all",
   404  							service,
   405  							servicePlan,
   406  						))
   407  						Consistently(session).ShouldNot(Say(orgName))
   408  					})
   409  				})
   410  
   411  				When("the service already has access globally enabled", func() {
   412  					It("displays an informative message, and exits 0", func() {
   413  						session := helpers.CF("enable-service-access", service)
   414  						Eventually(session).Should(Say("Enabling access to all plans of service %s for all orgs as admin...", service))
   415  						Eventually(session).Should(Say("OK"))
   416  						Eventually(session).Should(Exit(0))
   417  					})
   418  				})
   419  
   420  				When("the plan already has access globally enabled", func() {
   421  					It("displays an informative message, and exits 0", func() {
   422  						session := helpers.CF("enable-service-access", service, "-p", servicePlan)
   423  						Eventually(session).Should(Say("Enabling access of plan %s for service %s as admin...", servicePlan, service))
   424  						Eventually(session).Should(Say("OK"))
   425  						Eventually(session).Should(Exit(0))
   426  					})
   427  				})
   428  			})
   429  		})
   430  	})
   431  })