code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/set_staging_environment_variable_group_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     8  
     9  	"code.cloudfoundry.org/cli/actor/actionerror"
    10  	"code.cloudfoundry.org/cli/actor/v7action"
    11  	"code.cloudfoundry.org/cli/command/commandfakes"
    12  	. "code.cloudfoundry.org/cli/command/v7"
    13  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  	. "github.com/onsi/gomega/gbytes"
    20  )
    21  
    22  var _ = Describe("set-staging-environment-variable-group Command", func() {
    23  	var (
    24  		cmd             SetStagingEnvironmentVariableGroupCommand
    25  		testUI          *ui.UI
    26  		fakeConfig      *commandfakes.FakeConfig
    27  		fakeSharedActor *commandfakes.FakeSharedActor
    28  		fakeActor       *v7fakes.FakeActor
    29  		executeErr      error
    30  		binaryName      string
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    35  		fakeConfig = new(commandfakes.FakeConfig)
    36  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    37  		fakeActor = new(v7fakes.FakeActor)
    38  
    39  		cmd = SetStagingEnvironmentVariableGroupCommand{
    40  			BaseCommand: BaseCommand{
    41  				UI:          testUI,
    42  				Config:      fakeConfig,
    43  				SharedActor: fakeSharedActor,
    44  				Actor:       fakeActor,
    45  			},
    46  		}
    47  
    48  		cmd.RequiredArgs.EnvVarGroupJson = `{"key1":"val1", "key2":"val2"}`
    49  
    50  		binaryName = "faceman"
    51  		fakeConfig.BinaryNameReturns(binaryName)
    52  	})
    53  
    54  	JustBeforeEach(func() {
    55  		executeErr = cmd.Execute(nil)
    56  	})
    57  
    58  	When("the environment is not set up correctly", func() {
    59  		BeforeEach(func() {
    60  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    61  		})
    62  
    63  		It("returns an error", func() {
    64  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    65  
    66  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    67  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    68  			Expect(checkTargetedOrg).To(BeFalse())
    69  			Expect(checkTargetedSpace).To(BeFalse())
    70  		})
    71  	})
    72  
    73  	When("the environment is setup correctly", func() {
    74  		BeforeEach(func() {
    75  			fakeConfig.CurrentUserReturns(configv3.User{Name: "apple"}, nil)
    76  		})
    77  
    78  		It("should print text indicating its running", func() {
    79  			Expect(executeErr).NotTo(HaveOccurred())
    80  			Expect(testUI.Out).To(Say(`Setting the contents of the staging environment variable group as apple\.\.\.`))
    81  		})
    82  
    83  		When("jsonUnmarshalling fails", func() {
    84  			BeforeEach(func() {
    85  				cmd.RequiredArgs.EnvVarGroupJson = "bad json"
    86  			})
    87  
    88  			It("should err", func() {
    89  				Expect(executeErr).To(MatchError("Invalid environment variable group provided. Please provide a valid JSON object."))
    90  			})
    91  		})
    92  
    93  		When("setting the environment variables fails", func() {
    94  			BeforeEach(func() {
    95  				fakeActor.SetEnvironmentVariableGroupReturns(
    96  					v7action.Warnings{"some-warning-1", "some-warning-2"},
    97  					errors.New("some-error"),
    98  				)
    99  			})
   100  
   101  			It("prints warnings and returns error", func() {
   102  				Expect(executeErr).To(MatchError("some-error"))
   103  
   104  				Expect(testUI.Err).To(Say("some-warning-1"))
   105  				Expect(testUI.Err).To(Say("some-warning-2"))
   106  			})
   107  		})
   108  
   109  		When("setting the environment variables succeeds", func() {
   110  			BeforeEach(func() {
   111  				fakeActor.SetEnvironmentVariableGroupReturns(
   112  					v7action.Warnings{"some-warning-1", "some-warning-2"},
   113  					nil,
   114  				)
   115  			})
   116  
   117  			It("should print text indicating its set", func() {
   118  				Expect(fakeActor.SetEnvironmentVariableGroupCallCount()).To(Equal(1))
   119  				group, envVars := fakeActor.SetEnvironmentVariableGroupArgsForCall(0)
   120  				Expect(group).To(Equal(constant.StagingEnvironmentVariableGroup))
   121  				Expect(envVars).To(Equal(ccv3.EnvironmentVariables{
   122  					"key1": {Value: "val1", IsSet: true},
   123  					"key2": {Value: "val2", IsSet: true},
   124  				}))
   125  				Expect(executeErr).NotTo(HaveOccurred())
   126  				Expect(testUI.Out).To(Say("OK"))
   127  			})
   128  		})
   129  	})
   130  })