github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/application/unset_env_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/applications/applicationsfakes"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/requirements"
    11  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    12  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    13  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    15  
    16  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("unset-env command", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		app                 models.Application
    25  		appRepo             *applicationsfakes.FakeRepository
    26  		configRepo          coreconfig.Repository
    27  		requirementsFactory *requirementsfakes.FakeFactory
    28  		deps                commandregistry.Dependency
    29  	)
    30  
    31  	updateCommandDependency := func(pluginCall bool) {
    32  		deps.UI = ui
    33  		deps.Config = configRepo
    34  		deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo)
    35  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("unset-env").SetDependency(deps, pluginCall))
    36  	}
    37  
    38  	BeforeEach(func() {
    39  		ui = &testterm.FakeUI{}
    40  		app = models.Application{}
    41  		app.Name = "my-app"
    42  		app.GUID = "my-app-guid"
    43  		appRepo = new(applicationsfakes.FakeRepository)
    44  		requirementsFactory = new(requirementsfakes.FakeFactory)
    45  		configRepo = testconfig.NewRepositoryWithDefaults()
    46  	})
    47  
    48  	runCommand := func(args ...string) bool {
    49  		return testcmd.RunCLICommand("unset-env", args, requirementsFactory, updateCommandDependency, false, ui)
    50  	}
    51  
    52  	Describe("requirements", func() {
    53  		It("fails when not logged in", func() {
    54  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    55  
    56  			Expect(runCommand("foo", "bar")).To(BeFalse())
    57  		})
    58  
    59  		It("fails when a space is not targeted", func() {
    60  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    61  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"})
    62  
    63  			Expect(runCommand("foo", "bar")).To(BeFalse())
    64  		})
    65  
    66  		It("fails with usage when not provided with exactly 2 args", func() {
    67  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    68  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    69  			applicationReq := new(requirementsfakes.FakeApplicationRequirement)
    70  			applicationReq.GetApplicationReturns(app)
    71  			requirementsFactory.NewApplicationRequirementReturns(applicationReq)
    72  
    73  			Expect(runCommand("too", "many", "args")).To(BeFalse())
    74  		})
    75  	})
    76  
    77  	Context("when logged in, a space is targeted and provided enough args", func() {
    78  		BeforeEach(func() {
    79  			app.EnvironmentVars = map[string]interface{}{"foo": "bar", "DATABASE_URL": "mysql://example.com/my-db"}
    80  
    81  			applicationReq := new(requirementsfakes.FakeApplicationRequirement)
    82  			applicationReq.GetApplicationReturns(app)
    83  			requirementsFactory.NewApplicationRequirementReturns(applicationReq)
    84  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    85  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    86  		})
    87  
    88  		It("updates the app and tells the user what happened", func() {
    89  			runCommand("my-app", "DATABASE_URL")
    90  
    91  			Expect(ui.Outputs()).To(ContainSubstrings(
    92  				[]string{"Removing env variable", "DATABASE_URL", "my-app", "my-org", "my-space", "my-user"},
    93  				[]string{"OK"},
    94  			))
    95  
    96  			appGUID, params := appRepo.UpdateArgsForCall(0)
    97  			Expect(appGUID).To(Equal("my-app-guid"))
    98  			Expect(*params.EnvironmentVars).To(Equal(map[string]interface{}{
    99  				"foo": "bar",
   100  			}))
   101  		})
   102  
   103  		Context("when updating the app fails", func() {
   104  			BeforeEach(func() {
   105  				appRepo.UpdateReturns(models.Application{}, errors.New("Error updating app."))
   106  				appRepo.ReadReturns(app, nil)
   107  			})
   108  
   109  			It("fails and alerts the user", func() {
   110  				runCommand("does-not-exist", "DATABASE_URL")
   111  
   112  				Expect(ui.Outputs()).To(ContainSubstrings(
   113  					[]string{"Removing env variable"},
   114  					[]string{"FAILED"},
   115  					[]string{"Error updating app."},
   116  				))
   117  			})
   118  		})
   119  
   120  		It("tells the user if the specified env var was not set", func() {
   121  			runCommand("my-app", "CANT_STOP_WONT_STOP_UNSETTIN_THIS_ENV")
   122  
   123  			Expect(ui.Outputs()).To(ContainSubstrings(
   124  				[]string{"Removing env variable"},
   125  				[]string{"OK"},
   126  				[]string{"CANT_STOP_WONT_STOP_UNSETTIN_THIS_ENV", "was not set."},
   127  			))
   128  		})
   129  	})
   130  })