github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/environmentvariablegroup/set_running_environment_variable_group_test.go (about) 1 package environmentvariablegroup_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/environmentvariablegroups/environmentvariablegroupsfakes" 5 "code.cloudfoundry.org/cli/cf/commandregistry" 6 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 7 cf_errors "code.cloudfoundry.org/cli/cf/errors" 8 "code.cloudfoundry.org/cli/cf/requirements" 9 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 10 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 11 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 12 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 13 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("set-running-environment-variable-group command", func() { 19 var ( 20 ui *testterm.FakeUI 21 requirementsFactory *requirementsfakes.FakeFactory 22 configRepo coreconfig.Repository 23 environmentVariableGroupRepo *environmentvariablegroupsfakes.FakeRepository 24 deps commandregistry.Dependency 25 ) 26 27 updateCommandDependency := func(pluginCall bool) { 28 deps.UI = ui 29 deps.RepoLocator = deps.RepoLocator.SetEnvironmentVariableGroupsRepository(environmentVariableGroupRepo) 30 deps.Config = configRepo 31 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("set-running-environment-variable-group").SetDependency(deps, pluginCall)) 32 } 33 34 BeforeEach(func() { 35 ui = &testterm.FakeUI{} 36 configRepo = testconfig.NewRepositoryWithDefaults() 37 requirementsFactory = new(requirementsfakes.FakeFactory) 38 environmentVariableGroupRepo = new(environmentvariablegroupsfakes.FakeRepository) 39 }) 40 41 runCommand := func(args ...string) bool { 42 return testcmd.RunCLICommand("set-running-environment-variable-group", args, requirementsFactory, updateCommandDependency, false, ui) 43 } 44 45 Describe("requirements", func() { 46 It("requires the user to be logged in", func() { 47 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 48 Expect(runCommand()).ToNot(HavePassedRequirements()) 49 }) 50 51 It("fails with usage when it does not receive any arguments", func() { 52 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 53 runCommand() 54 Expect(ui.Outputs()).To(ContainSubstrings( 55 []string{"Incorrect Usage", "Requires an argument"}, 56 )) 57 }) 58 }) 59 60 Describe("when logged in", func() { 61 BeforeEach(func() { 62 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 63 }) 64 65 It("Sets the running environment variable group", func() { 66 runCommand(`{"abc":"123", "def": "456"}`) 67 68 Expect(ui.Outputs()).To(ContainSubstrings( 69 []string{"Setting the contents of the running environment variable group as my-user..."}, 70 []string{"OK"}, 71 )) 72 Expect(environmentVariableGroupRepo.SetRunningArgsForCall(0)).To(Equal(`{"abc":"123", "def": "456"}`)) 73 }) 74 75 It("Fails with a reasonable message when invalid JSON is passed", func() { 76 environmentVariableGroupRepo.SetRunningReturns(cf_errors.NewHTTPError(400, cf_errors.MessageParseError, "Request invalid due to parse error")) 77 runCommand(`{"abc":"123", "invalid : "json"}`) 78 Expect(ui.Outputs()).To(ContainSubstrings( 79 []string{"Setting the contents of the running environment variable group as my-user..."}, 80 []string{"FAILED"}, 81 []string{`Your JSON string syntax is invalid. Proper syntax is this: cf set-running-environment-variable-group '{"name":"value","name":"value"}'`}, 82 )) 83 }) 84 }) 85 })