github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/integration/v7/isolated/update_user_provided_service_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"os"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gbytes"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("update-user-provided-service command", func() {
    14  	expectOKMessage := func(session *Session, serviceName, orgName, spaceName, userName string) {
    15  		Expect(session.Out).To(Say("Updating user provided service %s in org %s / space %s as %s...", serviceName, orgName, spaceName, userName))
    16  		Expect(session.Out).To(Say("OK"))
    17  		Expect(session.Out).To(Say("TIP: Use 'cf restage' for any bound apps to ensure your env variable changes take effect"))
    18  	}
    19  
    20  	expectHelpMessage := func(session *Session) {
    21  		Expect(session).To(Say(`NAME:`))
    22  		Expect(session).To(Say(`\s+update-user-provided-service - Update user-provided service instance`))
    23  		Expect(session).To(Say(`USAGE:`))
    24  		Expect(session).To(Say(`\s+cf update-user-provided-service SERVICE_INSTANCE \[-p CREDENTIALS\] \[-l SYSLOG_DRAIN_URL\] \[-r ROUTE_SERVICE_URL\] \[-t TAGS\]`))
    25  		Expect(session).To(Say(`\s+Pass comma separated credential parameter names to enable interactive mode:`))
    26  		Expect(session).To(Say(`\s+cf update-user-provided-service SERVICE_INSTANCE -p "comma, separated, parameter, names"`))
    27  		Expect(session).To(Say(`\s+Pass credential parameters as JSON to create a service non-interactively:`))
    28  		Expect(session).To(Say(`\s+cf update-user-provided-service SERVICE_INSTANCE -p '{"key1":"value1","key2":"value2"}'`))
    29  		Expect(session).To(Say(`\s+Specify a path to a file containing JSON:`))
    30  		Expect(session).To(Say(`\s+cf update-user-provided-service SERVICE_INSTANCE -p PATH_TO_FILE`))
    31  		Expect(session).To(Say(`EXAMPLES:`))
    32  		Expect(session).To(Say(`\s+cf update-user-provided-service my-db-mine -p '{"username":"admin", "password":"pa55woRD"}'`))
    33  		Expect(session).To(Say(`\s+cf update-user-provided-service my-db-mine -p /path/to/credentials.json`))
    34  		Expect(session).To(Say(`\s+cf create-user-provided-service my-db-mine -t "list, of, tags"`))
    35  		Expect(session).To(Say(`\s+cf update-user-provided-service my-drain-service -l syslog://example.com`))
    36  		Expect(session).To(Say(`\s+cf update-user-provided-service my-route-service -r https://example.com`))
    37  		Expect(session).To(Say(`ALIAS:`))
    38  		Expect(session).To(Say(`\s+uups`))
    39  		Expect(session).To(Say(`OPTIONS:`))
    40  		Expect(session).To(Say(`\s+-l\s+URL to which logs for bound applications will be streamed`))
    41  		Expect(session).To(Say(`\s+-p\s+Credentials, provided inline or in a file, to be exposed in the VCAP_SERVICES environment variable for bound applications. Provided credentials will override existing credentials.`))
    42  		Expect(session).To(Say(`\s+-r\s+URL to which requests for bound routes will be forwarded. Scheme for this URL must be https`))
    43  		Expect(session).To(Say(`\s+-t\s+User provided tags`))
    44  		Expect(session).To(Say(`SEE ALSO:`))
    45  		Expect(session).To(Say(`\s+rename-service, services, update-service`))
    46  	}
    47  
    48  	randomUserProvidedServiceName := func() string {
    49  		return helpers.PrefixedRandomName("ups")
    50  	}
    51  
    52  	createUserProvidedService := func(name string) {
    53  		Eventually(helpers.CF("create-user-provided-service", name)).Should(Exit(0))
    54  	}
    55  
    56  	Describe("help", func() {
    57  		When("--help flag is set", func() {
    58  			It("displays command usage to output", func() {
    59  				session := helpers.CF("update-user-provided-service", "--help")
    60  				Eventually(session).Should(Exit(0))
    61  
    62  				expectHelpMessage(session)
    63  			})
    64  		})
    65  	})
    66  
    67  	When("the environment is not setup correctly", func() {
    68  		It("fails with the appropriate errors", func() {
    69  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "update-user-provided-service", "foo")
    70  		})
    71  	})
    72  
    73  	When("an api is targeted, the user is logged in, and an org and space are targeted", func() {
    74  		var (
    75  			userName  string
    76  			orgName   string
    77  			spaceName string
    78  		)
    79  
    80  		BeforeEach(func() {
    81  			orgName = helpers.NewOrgName()
    82  			spaceName = helpers.NewSpaceName()
    83  			helpers.SetupCF(orgName, spaceName)
    84  			userName, _ = helpers.GetCredentials()
    85  		})
    86  
    87  		AfterEach(func() {
    88  			helpers.QuickDeleteOrg(orgName)
    89  		})
    90  
    91  		When("the user-provided service instance name is not provided", func() {
    92  			It("displays the help message and exits 1", func() {
    93  				session := helpers.CF("update-user-provided-service")
    94  				Eventually(session).Should(Exit(1))
    95  
    96  				expectHelpMessage(session)
    97  			})
    98  		})
    99  
   100  		When("there are unknown additional arguments", func() {
   101  			It("displays the help message and exits 1", func() {
   102  				session := helpers.CF("update-user-provided-service", "service-name", "additional", "invalid", "arguments")
   103  				Eventually(session).Should(Exit(1))
   104  
   105  				expectHelpMessage(session)
   106  			})
   107  		})
   108  
   109  		When("the user-provided service instance does not exist", func() {
   110  			It("displays an informative error and exits 1", func() {
   111  				session := helpers.CF("update-user-provided-service", "nonexistent-service", "-l", "syslog://example.com")
   112  				Eventually(session).Should(Exit(1))
   113  
   114  				Expect(session.Err).To(Say("Service instance 'nonexistent-service' not found"))
   115  				Expect(session.Out).To(Say("FAILED"))
   116  			})
   117  		})
   118  
   119  		When("the user-provided service exists", func() {
   120  			var serviceName string
   121  
   122  			BeforeEach(func() {
   123  				serviceName = randomUserProvidedServiceName()
   124  				createUserProvidedService(serviceName)
   125  			})
   126  
   127  			When("no flags are provided", func() {
   128  				It("displays an informative message and exits 0", func() {
   129  					session := helpers.CF("update-user-provided-service", serviceName)
   130  					Eventually(session).Should(Exit(0))
   131  
   132  					Expect(session.Out).To(Say("No flags specified. No changes were made."))
   133  				})
   134  			})
   135  
   136  			When("flags are provided", func() {
   137  				It("displays success message, exits 0, and updates the service", func() {
   138  					session := helpers.CF(
   139  						`update-user-provided-service`, serviceName,
   140  						`-l`, `syslog://example.com`,
   141  						`-p`, `{"some": "credentials"}`,
   142  						`-r`, `https://example.com`,
   143  						`-t`, `"tag1,tag2"`,
   144  					)
   145  
   146  					Eventually(session).Should(Exit(0))
   147  					expectOKMessage(session, serviceName, orgName, spaceName, userName)
   148  
   149  					session = helpers.CF("service", serviceName)
   150  					Eventually(session).Should(Exit(0))
   151  
   152  					Expect(session).To(Say(`tags:\s+tag1,\s*tag2`))
   153  					Expect(session).To(Say(`route service url:\s+https://example.com`))
   154  				})
   155  			})
   156  
   157  			Context("when the user-provided service already has values", func() {
   158  				BeforeEach(func() {
   159  					session := helpers.CF(
   160  						`update-user-provided-service`, serviceName,
   161  						`-l`, `syslog://example.com`,
   162  						`-p`, `{"some": "credentials"}`,
   163  						`-r`, `https://example.com`,
   164  						`-t`, `"tag1,tag2"`,
   165  					)
   166  					Eventually(session).Should(Exit(0))
   167  
   168  					session = helpers.CF("service", serviceName)
   169  					Eventually(session).Should(Exit(0))
   170  
   171  					Eventually(session).Should(Say(`tags:\s+tag1,\s*tag2`))
   172  					Eventually(session).Should(Say(`route service url:\s+https://example.com`))
   173  				})
   174  
   175  				It("can unset previous values by providing empty strings as flag values", func() {
   176  					session := helpers.CF(
   177  						`update-user-provided-service`, serviceName,
   178  						`-l`, `""`,
   179  						`-p`, `""`,
   180  						`-r`, `""`,
   181  						`-t`, `""`,
   182  					)
   183  					Eventually(session).Should(Exit(0))
   184  
   185  					session = helpers.CF("service", serviceName)
   186  					Eventually(session).Should(Exit(0))
   187  
   188  					Expect(session).NotTo(Say(`tags:\s+tag1,\s*tag2`))
   189  					Expect(session).NotTo(Say(`route service url:\s+https://example.com`))
   190  				})
   191  
   192  				It("does not unset previous values for flags that are not provided", func() {
   193  					session := helpers.CF(
   194  						`update-user-provided-service`, serviceName,
   195  						`-l`, `""`,
   196  						`-p`, `""`,
   197  					)
   198  					Eventually(session).Should(Exit(0))
   199  
   200  					session = helpers.CF("service", serviceName)
   201  					Eventually(session).Should(Exit(0))
   202  
   203  					Expect(session).To(Say(`tags:\s+tag1,\s*tag2`))
   204  					Expect(session).To(Say(`route service url:\s+https://example.com`))
   205  				})
   206  			})
   207  
   208  			When("requesting interactive credentials", func() {
   209  				var buffer *Buffer
   210  
   211  				BeforeEach(func() {
   212  					buffer = NewBuffer()
   213  					_, err := buffer.Write([]byte("fake-username\nfake-password\n"))
   214  					Expect(err).ToNot(HaveOccurred())
   215  				})
   216  
   217  				It("requests the credentials at a prompt", func() {
   218  					session := helpers.CFWithStdin(buffer, "update-user-provided-service", serviceName, "-p", `"username,password"`)
   219  					Eventually(session).Should(Exit(0))
   220  
   221  					Expect(session).To(Say("username: "))
   222  					Expect(session).To(Say("password: "))
   223  					Expect(session).NotTo(Say("fake-username"), "credentials should not be echoed to the user")
   224  					Expect(session).NotTo(Say("fake-password"), "credentials should not be echoed to the user")
   225  					expectOKMessage(session, serviceName, orgName, spaceName, userName)
   226  				})
   227  			})
   228  
   229  			When("reading JSON credentials from a file", func() {
   230  				var path string
   231  
   232  				BeforeEach(func() {
   233  					path = helpers.TempFileWithContent(`{"some": "credentials"}`)
   234  				})
   235  
   236  				AfterEach(func() {
   237  					Expect(os.Remove(path)).To(Succeed())
   238  				})
   239  
   240  				It("accepts a file path", func() {
   241  					session := helpers.CF("update-user-provided-service", serviceName, "-p", path)
   242  
   243  					By("checking that it does not interpret the file name as request for an interactive credential prompt")
   244  					Consistently(session.Out.Contents()).ShouldNot(ContainSubstring(path))
   245  
   246  					By("succeeding")
   247  					Eventually(session).Should(Exit(0))
   248  					expectOKMessage(session, serviceName, orgName, spaceName, userName)
   249  				})
   250  			})
   251  		})
   252  	})
   253  })