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

     1  package experimental
     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  	. "github.com/onsi/gomega/ghttp"
    10  )
    11  
    12  var _ = Describe("v3-delete command", func() {
    13  	var (
    14  		orgName   string
    15  		spaceName string
    16  		appName   string
    17  	)
    18  
    19  	BeforeEach(func() {
    20  		orgName = helpers.NewOrgName()
    21  		spaceName = helpers.NewSpaceName()
    22  		appName = helpers.PrefixedRandomName("app")
    23  	})
    24  
    25  	Context("when --help flag is set", func() {
    26  		It("Displays command usage to output", func() {
    27  			session := helpers.CF("v3-delete", "--help")
    28  			Eventually(session).Should(Say("NAME:"))
    29  			Eventually(session).Should(Say("v3-delete - Delete a V3 App"))
    30  			Eventually(session).Should(Say("USAGE:"))
    31  			Eventually(session).Should(Say("cf v3-delete APP_NAME \\[-f\\]"))
    32  			Eventually(session).Should(Say("OPTIONS:"))
    33  			Eventually(session).Should(Say("\\s+-f\\s+Force deletion without confirmation"))
    34  			Eventually(session).Should(Exit(0))
    35  		})
    36  	})
    37  
    38  	Context("when the app name is not provided", func() {
    39  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    40  			session := helpers.CF("v3-delete")
    41  
    42  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    43  			Eventually(session.Out).Should(Say("NAME:"))
    44  			Eventually(session).Should(Exit(1))
    45  		})
    46  	})
    47  
    48  	Context("when the environment is not setup correctly", func() {
    49  		Context("when no API endpoint is set", func() {
    50  			BeforeEach(func() {
    51  				helpers.UnsetAPI()
    52  			})
    53  
    54  			It("fails with no API endpoint set message", func() {
    55  				session := helpers.CF("v3-delete", appName)
    56  				Eventually(session).Should(Say("FAILED"))
    57  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    58  				Eventually(session).Should(Exit(1))
    59  			})
    60  		})
    61  
    62  		Context("when the v3 api does not exist", func() {
    63  			var server *Server
    64  
    65  			BeforeEach(func() {
    66  				server = helpers.StartAndTargetServerWithoutV3API()
    67  			})
    68  
    69  			AfterEach(func() {
    70  				server.Close()
    71  			})
    72  
    73  			It("fails with error message that the minimum version is not met", func() {
    74  				session := helpers.CF("v3-delete", appName)
    75  				Eventually(session).Should(Say("FAILED"))
    76  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\."))
    77  				Eventually(session).Should(Exit(1))
    78  			})
    79  		})
    80  
    81  		Context("when the v3 api version is lower than the minimum version", func() {
    82  			var server *Server
    83  
    84  			BeforeEach(func() {
    85  				server = helpers.StartAndTargetServerWithV3Version("3.0.0")
    86  			})
    87  
    88  			AfterEach(func() {
    89  				server.Close()
    90  			})
    91  
    92  			It("fails with error message that the minimum version is not met", func() {
    93  				session := helpers.CF("v3-delete", appName)
    94  				Eventually(session).Should(Say("FAILED"))
    95  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\."))
    96  				Eventually(session).Should(Exit(1))
    97  			})
    98  		})
    99  
   100  		Context("when not logged in", func() {
   101  			BeforeEach(func() {
   102  				helpers.LogoutCF()
   103  			})
   104  
   105  			It("fails with not logged in message", func() {
   106  				session := helpers.CF("v3-delete", appName)
   107  				Eventually(session).Should(Say("FAILED"))
   108  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
   109  				Eventually(session).Should(Exit(1))
   110  			})
   111  		})
   112  
   113  		Context("when there is no org set", func() {
   114  			BeforeEach(func() {
   115  				helpers.LogoutCF()
   116  				helpers.LoginCF()
   117  			})
   118  
   119  			It("fails with no targeted org error message", func() {
   120  				session := helpers.CF("v3-delete", appName)
   121  				Eventually(session.Out).Should(Say("FAILED"))
   122  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
   123  				Eventually(session).Should(Exit(1))
   124  			})
   125  		})
   126  
   127  		Context("when there is no space set", func() {
   128  			BeforeEach(func() {
   129  				helpers.LogoutCF()
   130  				helpers.LoginCF()
   131  				helpers.TargetOrg(ReadOnlyOrg)
   132  			})
   133  
   134  			It("fails with no targeted space error message", func() {
   135  				session := helpers.CF("v3-delete", appName)
   136  				Eventually(session.Out).Should(Say("FAILED"))
   137  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space."))
   138  				Eventually(session).Should(Exit(1))
   139  			})
   140  		})
   141  	})
   142  
   143  	Context("when the environment is setup correctly", func() {
   144  		BeforeEach(func() {
   145  			setupCF(orgName, spaceName)
   146  		})
   147  
   148  		AfterEach(func() {
   149  			helpers.QuickDeleteOrg(orgName)
   150  		})
   151  
   152  		Context("when the app does not exist", func() {
   153  			Context("when the -f flag is provided", func() {
   154  				It("it displays the app does not exist", func() {
   155  					username, _ := helpers.GetCredentials()
   156  					session := helpers.CF("v3-delete", appName, "-f")
   157  					Eventually(session.Out).Should(Say("Deleting app %s in org %s / space %s as %s...", appName, orgName, spaceName, username))
   158  					Eventually(session.Out).Should(Say("App %s does not exist", appName))
   159  					Eventually(session.Out).Should(Say("OK"))
   160  					Eventually(session).Should(Exit(0))
   161  				})
   162  			})
   163  
   164  			Context("when the -f flag not is provided", func() {
   165  				var buffer *Buffer
   166  
   167  				BeforeEach(func() {
   168  					buffer = NewBuffer()
   169  				})
   170  
   171  				Context("when the user enters 'y'", func() {
   172  					BeforeEach(func() {
   173  						buffer.Write([]byte("y\n"))
   174  					})
   175  
   176  					It("it displays the app does not exist", func() {
   177  						username, _ := helpers.GetCredentials()
   178  						session := helpers.CFWithStdin(buffer, "v3-delete", appName)
   179  						Eventually(session.Out).Should(Say("Really delete the app %s\\? \\[yN\\]", appName))
   180  						Eventually(session.Out).Should(Say("Deleting app %s in org %s / space %s as %s...", appName, orgName, spaceName, username))
   181  						Eventually(session.Out).Should(Say("App %s does not exist", appName))
   182  						Eventually(session.Out).Should(Say("OK"))
   183  						Eventually(session).Should(Exit(0))
   184  					})
   185  				})
   186  
   187  				Context("when the user enters 'n'", func() {
   188  					BeforeEach(func() {
   189  						buffer.Write([]byte("n\n"))
   190  					})
   191  
   192  					It("does not delete the app", func() {
   193  						session := helpers.CFWithStdin(buffer, "v3-delete", appName)
   194  						Eventually(session.Out).Should(Say("Really delete the app %s\\? \\[yN\\]", appName))
   195  						Eventually(session.Out).Should(Say("Delete cancelled"))
   196  						Eventually(session).Should(Exit(0))
   197  					})
   198  				})
   199  
   200  				Context("when the user enters the default input (hits return)", func() {
   201  					BeforeEach(func() {
   202  						buffer.Write([]byte("\n"))
   203  					})
   204  
   205  					It("does not delete the app", func() {
   206  						session := helpers.CFWithStdin(buffer, "v3-delete", appName)
   207  						Eventually(session.Out).Should(Say("Really delete the app %s\\? \\[yN\\]", appName))
   208  						Eventually(session.Out).Should(Say("Delete cancelled"))
   209  						Eventually(session).Should(Exit(0))
   210  					})
   211  				})
   212  
   213  				Context("when the user enters an invalid answer", func() {
   214  					BeforeEach(func() {
   215  						// The second '\n' is intentional. Otherwise the buffer will be
   216  						// closed while the interaction is still waiting for input; it gets
   217  						// an EOF and causes an error.
   218  						buffer.Write([]byte("wat\n\n"))
   219  					})
   220  
   221  					It("asks again", func() {
   222  						session := helpers.CFWithStdin(buffer, "v3-delete", appName)
   223  						Eventually(session.Out).Should(Say("Really delete the app %s\\? \\[yN\\]", appName))
   224  						Eventually(session.Out).Should(Say("invalid input \\(not y, n, yes, or no\\)"))
   225  						Eventually(session.Out).Should(Say("Really delete the app %s\\? \\[yN\\]", appName))
   226  						Eventually(session).Should(Exit(0))
   227  					})
   228  				})
   229  			})
   230  		})
   231  
   232  		Context("when the app exists", func() {
   233  			BeforeEach(func() {
   234  				helpers.WithHelloWorldApp(func(appDir string) {
   235  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "v3-push", appName)).Should(Exit(0))
   236  				})
   237  			})
   238  
   239  			It("deletes the app", func() {
   240  				session := helpers.CF("v3-delete", appName, "-f")
   241  				username, _ := helpers.GetCredentials()
   242  				Eventually(session.Out).Should(Say("Deleting app %s in org %s / space %s as %s...", appName, orgName, spaceName, username))
   243  				Eventually(session).Should(Say("OK"))
   244  				Eventually(session).Should(Exit(0))
   245  
   246  				Eventually(helpers.CF("v3-app", appName)).Should(Exit(1))
   247  			})
   248  		})
   249  	})
   250  })