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

     1  package isolated
     2  
     3  import (
     4  	"time"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     7  	"code.cloudfoundry.org/cli/integration/helpers/servicebrokerstub"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gbytes"
    11  	. "github.com/onsi/gomega/gexec"
    12  )
    13  
    14  var _ = Describe("service-keys command", func() {
    15  	const command = "service-keys"
    16  
    17  	Describe("help", func() {
    18  		matchHelpMessage := SatisfyAll(
    19  			Say(`NAME:\n`),
    20  			Say(`\s+%s - List keys for a service instance\n`, command),
    21  			Say(`\n`),
    22  			Say(`USAGE:\n`),
    23  			Say(`\s+cf %s SERVICE_INSTANCE\n`, command),
    24  			Say(`\n`),
    25  			Say(`EXAMPLES:\n`),
    26  			Say(`\s+cf %s mydb\n`, command),
    27  			Say(`\n`),
    28  			Say(`ALIAS:\n`),
    29  			Say(`\s+sk\n`),
    30  			Say(`\n`),
    31  			Say(`SEE ALSO:\n`),
    32  			Say(`\s+delete-service-key\n`),
    33  		)
    34  
    35  		When("the -h flag is specified", func() {
    36  			It("succeeds and prints help", func() {
    37  				session := helpers.CF(command, "-h")
    38  				Eventually(session).Should(Exit(0))
    39  				Expect(session.Out).To(matchHelpMessage)
    40  			})
    41  		})
    42  
    43  		When("the --help flag is specified", func() {
    44  			It("succeeds and prints help", func() {
    45  				session := helpers.CF(command, "--help")
    46  				Eventually(session).Should(Exit(0))
    47  				Expect(session.Out).To(matchHelpMessage)
    48  			})
    49  		})
    50  
    51  		When("no arguments are provided", func() {
    52  			It("displays a warning, the help text, and exits 1", func() {
    53  				session := helpers.CF(command)
    54  				Eventually(session).Should(Exit(1))
    55  				Expect(session.Err).To(Say("Incorrect Usage: the required argument `SERVICE_INSTANCE` was not provided"))
    56  				Expect(session.Out).To(matchHelpMessage)
    57  			})
    58  		})
    59  
    60  		When("unknown flag is passed", func() {
    61  			It("displays a warning, the help text, and exits 1", func() {
    62  				session := helpers.CF(command, "-u")
    63  				Eventually(session).Should(Exit(1))
    64  				Expect(session.Err).To(Say("Incorrect Usage: unknown flag `u"))
    65  				Expect(session.Out).To(matchHelpMessage)
    66  			})
    67  		})
    68  	})
    69  
    70  	When("the environment is not setup correctly", func() {
    71  		It("fails with the appropriate errors", func() {
    72  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, command, "foo")
    73  		})
    74  	})
    75  
    76  	When("there is a service instance", func() {
    77  		var (
    78  			userName            string
    79  			orgName             string
    80  			spaceName           string
    81  			serviceInstanceName string
    82  			broker              *servicebrokerstub.ServiceBrokerStub
    83  		)
    84  
    85  		BeforeEach(func() {
    86  			userName, _ = helpers.GetCredentials()
    87  
    88  			orgName = helpers.NewOrgName()
    89  			spaceName = helpers.NewSpaceName()
    90  			helpers.SetupCF(orgName, spaceName)
    91  
    92  			broker = servicebrokerstub.EnableServiceAccess()
    93  
    94  			serviceInstanceName = helpers.NewServiceInstanceName()
    95  			helpers.CreateManagedServiceInstance(broker.FirstServiceOfferingName(), broker.FirstServicePlanName(), serviceInstanceName)
    96  
    97  			broker.WithAsyncDelay(time.Millisecond).Configure()
    98  		})
    99  
   100  		AfterEach(func() {
   101  			helpers.QuickDeleteOrg(orgName)
   102  			broker.Forget()
   103  		})
   104  
   105  		It("reports that there are no keys", func() {
   106  			session := helpers.CF(command, serviceInstanceName)
   107  			Eventually(session).Should(Exit(0))
   108  			Expect(string(session.Err.Contents())).To(BeEmpty())
   109  			Expect(session.Out).To(SatisfyAll(
   110  				Say(`Getting keys for service instance %s as %s\.\.\.\n`, serviceInstanceName, userName),
   111  				Say(`\n`),
   112  				Say(`No service keys for service instance %s\n`, serviceInstanceName),
   113  			))
   114  		})
   115  
   116  		When("there are service keys", func() {
   117  			var (
   118  				keyName1 string
   119  				keyName2 string
   120  			)
   121  
   122  			BeforeEach(func() {
   123  				keyName1 = helpers.RandomName()
   124  				keyName2 = helpers.RandomName()
   125  				Eventually(helpers.CF("create-service-key", serviceInstanceName, keyName1, "--wait")).Should(Exit(0))
   126  				Eventually(helpers.CF("create-service-key", serviceInstanceName, keyName2, "--wait")).Should(Exit(0))
   127  			})
   128  
   129  			It("prints the names of the keys in all spaces", func() {
   130  				session := helpers.CF(command, serviceInstanceName)
   131  				Eventually(session).Should(Exit(0))
   132  				Expect(string(session.Err.Contents())).To(BeEmpty())
   133  				Expect(session.Out).To(SatisfyAll(
   134  					Say(`Getting keys for service instance %s as %s\.\.\.\n`, serviceInstanceName, userName),
   135  					Say(`\n`),
   136  					Say(`name\s+last operation\s+message\n`),
   137  					Say(`%s\s+%s\s+%s\n`, keyName1, "create succeeded", "very happy service"),
   138  					Say(`%s\s+%s\s+%s\n`, keyName2, "create succeeded", "very happy service"),
   139  				))
   140  			})
   141  		})
   142  	})
   143  })