github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/integration/isolated/delete_org_command_test.go (about)

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