github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/application/unset_env_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	testApplication "github.com/cloudfoundry/cli/cf/api/applications/fakes"
     5  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     6  	"github.com/cloudfoundry/cli/cf/models"
     7  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     8  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
     9  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    10  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    11  
    12  	. "github.com/cloudfoundry/cli/cf/commands/application"
    13  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("unset-env command", func() {
    19  	var (
    20  		ui                  *testterm.FakeUI
    21  		app                 models.Application
    22  		appRepo             *testApplication.FakeApplicationRepository
    23  		configRepo          core_config.ReadWriter
    24  		requirementsFactory *testreq.FakeReqFactory
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		ui = &testterm.FakeUI{}
    29  		app = models.Application{}
    30  		app.Name = "my-app"
    31  		app.Guid = "my-app-guid"
    32  		appRepo = &testApplication.FakeApplicationRepository{}
    33  		requirementsFactory = &testreq.FakeReqFactory{}
    34  		configRepo = testconfig.NewRepositoryWithDefaults()
    35  	})
    36  
    37  	runCommand := func(args ...string) bool {
    38  		return testcmd.RunCommand(NewUnsetEnv(ui, configRepo, appRepo), args, requirementsFactory)
    39  	}
    40  
    41  	Describe("requirements", func() {
    42  		It("fails when not logged in", func() {
    43  			requirementsFactory.TargetedSpaceSuccess = true
    44  			requirementsFactory.Application = app
    45  
    46  			Expect(runCommand("foo", "bar")).To(BeFalse())
    47  		})
    48  
    49  		It("fails when a space is not targeted", func() {
    50  			requirementsFactory.LoginSuccess = true
    51  			requirementsFactory.Application = app
    52  
    53  			Expect(runCommand("foo", "bar")).To(BeFalse())
    54  		})
    55  
    56  		It("fails with usage when not provided with exactly 2 args", func() {
    57  			requirementsFactory.LoginSuccess = true
    58  			requirementsFactory.TargetedSpaceSuccess = true
    59  			requirementsFactory.Application = app
    60  
    61  			Expect(runCommand("too", "many", "args")).To(BeFalse())
    62  		})
    63  	})
    64  
    65  	Context("when logged in, a space is targeted and provided enough args", func() {
    66  		BeforeEach(func() {
    67  			app.EnvironmentVars = map[string]interface{}{"foo": "bar", "DATABASE_URL": "mysql://example.com/my-db"}
    68  
    69  			requirementsFactory.Application = app
    70  			requirementsFactory.LoginSuccess = true
    71  			requirementsFactory.TargetedSpaceSuccess = true
    72  		})
    73  
    74  		It("updates the app and tells the user what happened", func() {
    75  			runCommand("my-app", "DATABASE_URL")
    76  
    77  			Expect(ui.Outputs).To(ContainSubstrings(
    78  				[]string{"Removing env variable", "DATABASE_URL", "my-app", "my-org", "my-space", "my-user"},
    79  				[]string{"OK"},
    80  			))
    81  
    82  			Expect(requirementsFactory.ApplicationName).To(Equal("my-app"))
    83  			Expect(appRepo.UpdateAppGuid).To(Equal("my-app-guid"))
    84  			Expect(*appRepo.UpdateParams.EnvironmentVars).To(Equal(map[string]interface{}{
    85  				"foo": "bar",
    86  			}))
    87  		})
    88  
    89  		Context("when updating the app fails", func() {
    90  			BeforeEach(func() {
    91  				appRepo.UpdateErr = true
    92  				appRepo.ReadReturns.App = app
    93  			})
    94  
    95  			It("fails and alerts the user", func() {
    96  				runCommand("does-not-exist", "DATABASE_URL")
    97  
    98  				Expect(ui.Outputs).To(ContainSubstrings(
    99  					[]string{"Removing env variable"},
   100  					[]string{"FAILED"},
   101  					[]string{"Error updating app."},
   102  				))
   103  			})
   104  		})
   105  
   106  		It("tells the user if the specified env var was not set", func() {
   107  			runCommand("my-app", "CANT_STOP_WONT_STOP_UNSETTIN_THIS_ENV")
   108  
   109  			Expect(ui.Outputs).To(ContainSubstrings(
   110  				[]string{"Removing env variable"},
   111  				[]string{"OK"},
   112  				[]string{"CANT_STOP_WONT_STOP_UNSETTIN_THIS_ENV", "was not set."},
   113  			))
   114  		})
   115  	})
   116  })