github.com/wanddynosios/cli@v7.1.0+incompatible/integration/v6/experimental/v3_unset_env_command_test.go (about)

     1  package experimental
     2  
     3  import (
     4  	"fmt"
     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("v3-unset-env command", func() {
    14  	var (
    15  		orgName    string
    16  		spaceName  string
    17  		appName    string
    18  		envVarName string
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		orgName = helpers.NewOrgName()
    23  		spaceName = helpers.NewSpaceName()
    24  		appName = helpers.PrefixedRandomName("app")
    25  		envVarName = "SOME_ENV_VAR"
    26  	})
    27  
    28  	Describe("help", func() {
    29  		When("--help flag is set", func() {
    30  			It("displays command usage to output", func() {
    31  				session := helpers.CF("v3-unset-env", "--help")
    32  
    33  				Eventually(session).Should(Say("NAME:"))
    34  				Eventually(session).Should(Say("v3-unset-env - Remove an env variable from an app"))
    35  				Eventually(session).Should(Say("USAGE:"))
    36  				Eventually(session).Should(Say("cf v3-unset-env APP_NAME ENV_VAR_NAME"))
    37  				Eventually(session).Should(Say("SEE ALSO:"))
    38  				Eventually(session).Should(Say("v3-apps, v3-env, v3-restart, v3-set-env, v3-stage"))
    39  				Eventually(session).Should(Exit(0))
    40  			})
    41  		})
    42  	})
    43  
    44  	When("the app name is not provided", func() {
    45  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    46  			session := helpers.CF("v3-unset-env")
    47  
    48  			Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `APP_NAME` and `ENV_VAR_NAME` were not provided"))
    49  			Eventually(session).Should(Say("NAME:"))
    50  			Eventually(session).Should(Exit(1))
    51  		})
    52  	})
    53  
    54  	When("ENV_VAR_NAME is not provided", func() {
    55  		It("tells the user that ENV_VAR_NAME is required, prints help text, and exits 1", func() {
    56  			session := helpers.CF("v3-unset-env", appName)
    57  
    58  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `ENV_VAR_NAME` was not provided"))
    59  			Eventually(session).Should(Say("NAME:"))
    60  			Eventually(session).Should(Exit(1))
    61  		})
    62  	})
    63  
    64  	It("displays the experimental warning", func() {
    65  		session := helpers.CF("v3-unset-env", appName, envVarName)
    66  		Eventually(session.Err).Should(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    67  		Eventually(session).Should(Exit())
    68  	})
    69  
    70  	When("the environment is not setup correctly", func() {
    71  		When("no API endpoint is set", func() {
    72  			BeforeEach(func() {
    73  				helpers.UnsetAPI()
    74  			})
    75  
    76  			It("fails with no API endpoint set message", func() {
    77  				session := helpers.CF("v3-unset-env", appName, envVarName)
    78  				Eventually(session).Should(Say("FAILED"))
    79  				Eventually(session.Err).Should(Say(`No API endpoint set\. Use 'cf login' or 'cf api' to target an endpoint\.`))
    80  				Eventually(session).Should(Exit(1))
    81  			})
    82  		})
    83  
    84  		When("not logged in", func() {
    85  			BeforeEach(func() {
    86  				helpers.LogoutCF()
    87  			})
    88  
    89  			It("fails with not logged in message", func() {
    90  				session := helpers.CF("v3-unset-env", appName, envVarName)
    91  				Eventually(session).Should(Say("FAILED"))
    92  				Eventually(session.Err).Should(Say(`Not logged in\. Use 'cf login' or 'cf login --sso' to log in\.`))
    93  				Eventually(session).Should(Exit(1))
    94  			})
    95  		})
    96  
    97  		When("there is no org set", func() {
    98  			BeforeEach(func() {
    99  				helpers.LogoutCF()
   100  				helpers.LoginCF()
   101  			})
   102  
   103  			It("fails with no org targeted error message", func() {
   104  				session := helpers.CF("v3-unset-env", appName, envVarName)
   105  				Eventually(session).Should(Say("FAILED"))
   106  				Eventually(session.Err).Should(Say(`No org targeted, use 'cf target -o ORG' to target an org\.`))
   107  				Eventually(session).Should(Exit(1))
   108  			})
   109  		})
   110  
   111  		When("there is no space set", func() {
   112  			BeforeEach(func() {
   113  				helpers.LogoutCF()
   114  				helpers.LoginCF()
   115  				helpers.TargetOrg(ReadOnlyOrg)
   116  			})
   117  
   118  			It("fails with no space targeted error message", func() {
   119  				session := helpers.CF("v3-unset-env", appName, envVarName)
   120  				Eventually(session).Should(Say("FAILED"))
   121  				Eventually(session.Err).Should(Say(`No space targeted, use 'cf target -s SPACE' to target a space\.`))
   122  				Eventually(session).Should(Exit(1))
   123  			})
   124  		})
   125  	})
   126  
   127  	When("the environment is set up correctly", func() {
   128  		var userName string
   129  
   130  		BeforeEach(func() {
   131  			helpers.SetupCF(orgName, spaceName)
   132  			userName, _ = helpers.GetCredentials()
   133  		})
   134  
   135  		When("the app does not exist", func() {
   136  			It("displays app not found and exits 1", func() {
   137  				invalidAppName := "invalid-app-name"
   138  				session := helpers.CF("v3-unset-env", invalidAppName, envVarName)
   139  
   140  				Eventually(session).Should(Say(`Removing env variable %s from app %s in org %s / space %s as %s\.\.\.`, envVarName, invalidAppName, orgName, spaceName, userName))
   141  				Eventually(session.Err).Should(Say("App '%s' not found", invalidAppName))
   142  				Eventually(session).Should(Say("FAILED"))
   143  				Eventually(session).Should(Exit(1))
   144  			})
   145  		})
   146  
   147  		When("the app exists", func() {
   148  			BeforeEach(func() {
   149  				helpers.WithHelloWorldApp(func(appDir string) {
   150  					Eventually(helpers.CF("v3-push", appName, "-p", appDir)).Should(Exit(0))
   151  				})
   152  			})
   153  
   154  			When("the environment variable has not been previously set", func() {
   155  				It("returns a warning indicating variable was not set", func() {
   156  					session := helpers.CF("v3-unset-env", appName, envVarName)
   157  
   158  					Eventually(session).Should(Say(`Removing env variable %s from app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   159  					Eventually(session).Should(Say("Env variable %s was not set.", envVarName))
   160  					Eventually(session).Should(Say("OK"))
   161  					Eventually(session).Should(Exit(0))
   162  				})
   163  			})
   164  
   165  			When("the environment variable has been previously set", func() {
   166  				BeforeEach(func() {
   167  					Eventually(helpers.CF("v3-set-env", appName, envVarName, "some-value")).Should(Exit(0))
   168  				})
   169  
   170  				It("overrides the value of the existing environment variable", func() {
   171  					session := helpers.CF("v3-unset-env", appName, envVarName)
   172  
   173  					Eventually(session).Should(Say(`Removing env variable %s from app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   174  					Eventually(session).Should(Say("OK"))
   175  					Eventually(session).Should(Say(`TIP: Use 'cf v3-stage %s' to ensure your env variable changes take effect\.`, appName))
   176  
   177  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   178  					Eventually(session).ShouldNot(Say(`"%s"`, envVarName))
   179  					Eventually(session).Should(Exit(0))
   180  				})
   181  			})
   182  		})
   183  	})
   184  })