github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/v6/isolated/delete_space_command_test.go (about)

     1  package isolated
     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  )
    10  
    11  var _ = Describe("delete-space command", func() {
    12  	Describe("help", func() {
    13  		It("shows usage", func() {
    14  			session := helpers.CF("help", "delete-space")
    15  			Eventually(session).Should(Say("NAME:"))
    16  			Eventually(session).Should(Say(`\s+delete-space - Delete a space`))
    17  			Eventually(session).Should(Say("USAGE:"))
    18  			Eventually(session).Should(Say(`delete-space SPACE \[-o ORG\] \[-f\]`))
    19  			Eventually(session).Should(Say("OPTIONS:"))
    20  			Eventually(session).Should(Say(`\s+-f\s+Force deletion without confirmation`))
    21  			Eventually(session).Should(Say(`\s+-o\s+Delete space within specified org`))
    22  			Eventually(session).Should(Exit(0))
    23  		})
    24  	})
    25  
    26  	When("the environment is not setup correctly", func() {
    27  		When("the org is provided", func() {
    28  			It("fails with the appropriate errors", func() {
    29  				helpers.CheckEnvironmentTargetedCorrectly(false, false, ReadOnlyOrg, "delete-space", "space-name", "-o", ReadOnlyOrg)
    30  			})
    31  		})
    32  
    33  		When("the org is not provided", func() {
    34  			It("fails with the appropriate errors", func() {
    35  				helpers.CheckEnvironmentTargetedCorrectly(true, false, ReadOnlyOrg, "delete-space", "space-name")
    36  			})
    37  		})
    38  	})
    39  
    40  	When("the space name it not provided", func() {
    41  		It("displays an error and help", func() {
    42  			session := helpers.CF("delete-space")
    43  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `SPACE` was not provided"))
    44  			Eventually(session).Should(Say("USAGE"))
    45  			Eventually(session).Should(Exit(1))
    46  		})
    47  	})
    48  
    49  	When("the space does not exist", func() {
    50  		var orgName string
    51  
    52  		BeforeEach(func() {
    53  			helpers.LoginCF()
    54  
    55  			orgName = helpers.NewOrgName()
    56  			helpers.CreateOrg(orgName)
    57  		})
    58  
    59  		AfterEach(func() {
    60  			helpers.QuickDeleteOrg(orgName)
    61  		})
    62  
    63  		It("fails and displays space not found", func() {
    64  			username, _ := helpers.GetCredentials()
    65  			session := helpers.CF("delete-space", "-f", "-o", orgName, "please-do-not-exist-in-real-life")
    66  			Eventually(session).Should(Say("Deleting space please-do-not-exist-in-real-life in org %s as %s...", orgName, username))
    67  			Eventually(session).Should(Say("FAILED"))
    68  			Eventually(session.Err).Should(Say(`Space 'please-do-not-exist-in-real-life' not found\.`))
    69  			Eventually(session).Should(Exit(1))
    70  		})
    71  	})
    72  
    73  	When("the space exists", func() {
    74  		var orgName string
    75  		var spaceName string
    76  
    77  		BeforeEach(func() {
    78  			helpers.LoginCF()
    79  
    80  			orgName = helpers.NewOrgName()
    81  			spaceName = helpers.NewSpaceName()
    82  			helpers.CreateOrgAndSpace(orgName, spaceName)
    83  		})
    84  
    85  		AfterEach(func() {
    86  			helpers.QuickDeleteOrg(orgName)
    87  		})
    88  
    89  		When("the -f flag not is provided", func() {
    90  			var buffer *Buffer
    91  
    92  			BeforeEach(func() {
    93  				buffer = NewBuffer()
    94  			})
    95  
    96  			When("the user enters 'y'", func() {
    97  				BeforeEach(func() {
    98  					_, err := buffer.Write([]byte("y\n"))
    99  					Expect(err).ToNot(HaveOccurred())
   100  				})
   101  
   102  				It("deletes the space", func() {
   103  					username, _ := helpers.GetCredentials()
   104  					session := helpers.CFWithStdin(buffer, "delete-space", spaceName)
   105  					Eventually(session).Should(Say(`Really delete the space %s\? \[yN\]`, spaceName))
   106  					Eventually(session).Should(Say("Deleting space %s in org %s as %s...", spaceName, orgName, username))
   107  					Eventually(session).Should(Say("OK"))
   108  					Eventually(session).Should(Exit(0))
   109  					Eventually(helpers.CF("space", spaceName)).Should(Exit(1))
   110  				})
   111  			})
   112  
   113  			When("the user enters 'n'", func() {
   114  				BeforeEach(func() {
   115  					_, err := buffer.Write([]byte("n\n"))
   116  					Expect(err).ToNot(HaveOccurred())
   117  				})
   118  
   119  				It("does not delete the space", func() {
   120  					session := helpers.CFWithStdin(buffer, "delete-space", spaceName)
   121  					Eventually(session).Should(Say(`Really delete the space %s\? \[yN\]`, spaceName))
   122  					Eventually(session).Should(Say("Delete cancelled"))
   123  					Eventually(session).Should(Exit(0))
   124  					Eventually(helpers.CF("space", spaceName)).Should(Exit(0))
   125  				})
   126  			})
   127  
   128  			When("the user enters the default input (hits return)", func() {
   129  				BeforeEach(func() {
   130  					_, err := buffer.Write([]byte("\n"))
   131  					Expect(err).ToNot(HaveOccurred())
   132  				})
   133  
   134  				It("does not delete the org", func() {
   135  					session := helpers.CFWithStdin(buffer, "delete-space", spaceName)
   136  					Eventually(session).Should(Say(`Really delete the space %s\? \[yN\]`, spaceName))
   137  					Eventually(session).Should(Say("Delete cancelled"))
   138  					Eventually(session).Should(Exit(0))
   139  					Eventually(helpers.CF("space", spaceName)).Should(Exit(0))
   140  				})
   141  			})
   142  
   143  			When("the user enters an invalid answer", func() {
   144  				BeforeEach(func() {
   145  					// The second '\n' is intentional. Otherwise the buffer will be
   146  					// closed while the interaction is still waiting for input; it gets
   147  					// an EOF and causes an error.
   148  					_, err := buffer.Write([]byte("wat\n\n"))
   149  					Expect(err).ToNot(HaveOccurred())
   150  				})
   151  
   152  				It("asks again", func() {
   153  					session := helpers.CFWithStdin(buffer, "delete-space", spaceName)
   154  					Eventually(session).Should(Say(`Really delete the space %s\? \[yN\]`, spaceName))
   155  					Eventually(session).Should(Say(`invalid input \(not y, n, yes, or no\)`))
   156  					Eventually(session).Should(Say(`Really delete the space %s\? \[yN\]`, spaceName))
   157  					Eventually(session).Should(Exit(0))
   158  					Eventually(helpers.CF("space", spaceName)).Should(Exit(0))
   159  				})
   160  			})
   161  		})
   162  
   163  		When("the -f flag is provided", func() {
   164  			It("deletes the space", func() {
   165  				username, _ := helpers.GetCredentials()
   166  				session := helpers.CF("delete-space", spaceName, "-f")
   167  				Eventually(session).Should(Say(`Deleting space %s in org %s as %s\.\.\.`, spaceName, orgName, username))
   168  				Eventually(session).Should(Say("OK"))
   169  				Eventually(session).Should(Exit(0))
   170  				Eventually(helpers.CF("space", spaceName)).Should(Exit(1))
   171  			})
   172  
   173  			When("the space was targeted", func() {
   174  				BeforeEach(func() {
   175  					Eventually(helpers.CF("target", "-s", spaceName)).Should(Exit(0))
   176  				})
   177  
   178  				It("deletes the space and clears the target", func() {
   179  					username, _ := helpers.GetCredentials()
   180  					session := helpers.CF("delete-space", spaceName, "-f")
   181  					Eventually(session).Should(Say(`Deleting space %s in org %s as %s\.\.\.`, spaceName, orgName, username))
   182  					Eventually(session).Should(Say("OK"))
   183  					Eventually(session).Should(Say(`TIP: No space targeted, use 'cf target -s' to target a space\.`))
   184  					Eventually(session).Should(Exit(0))
   185  
   186  					Eventually(helpers.CF("space", spaceName)).Should(Exit(1))
   187  
   188  					session = helpers.CF("target")
   189  					Eventually(session).Should(Say("No space targeted, use 'cf target -s SPACE'"))
   190  					Eventually(session).Should(Exit(0))
   191  				})
   192  			})
   193  		})
   194  	})
   195  
   196  	When("the -o organzation does not exist", func() {
   197  		BeforeEach(func() {
   198  			helpers.LoginCF()
   199  		})
   200  
   201  		It("fails and displays org not found", func() {
   202  			username, _ := helpers.GetCredentials()
   203  			session := helpers.CF("delete-space", "-f", "-o", "please-do-not-exist-in-real-life", "please-do-not-exist-in-real-life")
   204  			Eventually(session).Should(Say("Deleting space please-do-not-exist-in-real-life in org please-do-not-exist-in-real-life as %s...", username))
   205  			Eventually(session.Err).Should(Say(`Organization 'please-do-not-exist-in-real-life' not found\.`))
   206  			Eventually(session).Should(Say("FAILED"))
   207  			Eventually(session).Should(Exit(1))
   208  		})
   209  	})
   210  })