github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v3/v3_unset_env_command_test.go (about)

     1  package v3_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/actor/sharedaction"
     7  	"github.com/liamawhite/cli-with-i18n/actor/v3action"
     8  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccversion"
     9  	"github.com/liamawhite/cli-with-i18n/command/commandfakes"
    10  	"github.com/liamawhite/cli-with-i18n/command/translatableerror"
    11  	"github.com/liamawhite/cli-with-i18n/command/v3"
    12  	"github.com/liamawhite/cli-with-i18n/command/v3/v3fakes"
    13  	"github.com/liamawhite/cli-with-i18n/util/configv3"
    14  	"github.com/liamawhite/cli-with-i18n/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("v3-unset-env Command", func() {
    21  	var (
    22  		cmd             v3.V3UnsetEnvCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v3fakes.FakeV3UnsetEnvActor
    27  		binaryName      string
    28  		executeErr      error
    29  		appName         string
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v3fakes.FakeV3UnsetEnvActor)
    37  
    38  		cmd = v3.V3UnsetEnvCommand{
    39  			UI:          testUI,
    40  			Config:      fakeConfig,
    41  			SharedActor: fakeSharedActor,
    42  			Actor:       fakeActor,
    43  		}
    44  
    45  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionV3)
    46  		binaryName = "faceman"
    47  		fakeConfig.BinaryNameReturns(binaryName)
    48  		appName = "some-app"
    49  
    50  		cmd.RequiredArgs.AppName = appName
    51  		cmd.RequiredArgs.EnvironmentVariableName = "some-key"
    52  	})
    53  
    54  	JustBeforeEach(func() {
    55  		executeErr = cmd.Execute(nil)
    56  	})
    57  
    58  	Context("when the API version is below the minimum", func() {
    59  		BeforeEach(func() {
    60  			fakeActor.CloudControllerAPIVersionReturns("0.0.0")
    61  		})
    62  
    63  		It("returns a MinimumAPIVersionNotMetError", func() {
    64  			Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{
    65  				CurrentVersion: "0.0.0",
    66  				MinimumVersion: ccversion.MinVersionV3,
    67  			}))
    68  		})
    69  	})
    70  
    71  	Context("when checking target fails", func() {
    72  		BeforeEach(func() {
    73  			fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName})
    74  		})
    75  
    76  		It("returns an error", func() {
    77  			Expect(executeErr).To(MatchError(translatableerror.NotLoggedInError{BinaryName: binaryName}))
    78  
    79  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    80  			_, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    81  			Expect(checkTargetedOrg).To(BeTrue())
    82  			Expect(checkTargetedSpace).To(BeTrue())
    83  		})
    84  	})
    85  
    86  	Context("when the user is logged in, an org is targeted and a space is targeted", func() {
    87  		BeforeEach(func() {
    88  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"})
    89  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
    90  		})
    91  
    92  		Context("when getting the current user returns an error", func() {
    93  			BeforeEach(func() {
    94  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("some-error"))
    95  			})
    96  
    97  			It("returns the error", func() {
    98  				Expect(executeErr).To(MatchError("some-error"))
    99  			})
   100  		})
   101  
   102  		Context("when getting the current user succeeds", func() {
   103  			BeforeEach(func() {
   104  				fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
   105  			})
   106  
   107  			Context("when unsetting the environment variable succeeds", func() {
   108  				BeforeEach(func() {
   109  					fakeActor.UnsetEnvironmentVariableByApplicationNameAndSpaceReturns(v3action.Warnings{"set-warning-1", "set-warning-2"}, nil)
   110  				})
   111  
   112  				It("sets the environment variable and value pair", func() {
   113  					Expect(executeErr).ToNot(HaveOccurred())
   114  
   115  					Expect(testUI.Out).To(Say("Removing env variable some-key from app some-app in org some-org / space some-space as banana\\.\\.\\."))
   116  
   117  					Expect(testUI.Err).To(Say("set-warning-1"))
   118  					Expect(testUI.Err).To(Say("set-warning-2"))
   119  					Expect(testUI.Out).To(Say("OK"))
   120  					Expect(testUI.Out).To(Say("TIP: Use 'cf v3-stage some-app' to ensure your env variable changes take effect\\."))
   121  
   122  					Expect(fakeActor.UnsetEnvironmentVariableByApplicationNameAndSpaceCallCount()).To(Equal(1))
   123  					appName, spaceGUID, envVarName := fakeActor.UnsetEnvironmentVariableByApplicationNameAndSpaceArgsForCall(0)
   124  					Expect(appName).To(Equal("some-app"))
   125  					Expect(spaceGUID).To(Equal("some-space-guid"))
   126  					Expect(envVarName).To(Equal("some-key"))
   127  				})
   128  			})
   129  
   130  			Context("when unsetting the environment returns an EnvironmentVariableNotSetError", func() {
   131  				BeforeEach(func() {
   132  					fakeActor.UnsetEnvironmentVariableByApplicationNameAndSpaceReturns(v3action.Warnings{"unset-warning-1", "unset-warning-2"}, v3action.EnvironmentVariableNotSetError{EnvironmentVariableName: "some-key"})
   133  				})
   134  
   135  				It("displays okay and the error", func() {
   136  					Expect(executeErr).ToNot(HaveOccurred())
   137  
   138  					Expect(testUI.Out).To(Say("Removing env variable some-key from app some-app in org some-org / space some-space as banana\\.\\.\\."))
   139  					Expect(testUI.Out).To(Say("Env variable some-key was not set"))
   140  					Expect(testUI.Out).To(Say("OK"))
   141  				})
   142  			})
   143  
   144  			Context("when the set environment variable returns an unknown error", func() {
   145  				var expectedErr error
   146  				BeforeEach(func() {
   147  					expectedErr = errors.New("some-error")
   148  					fakeActor.UnsetEnvironmentVariableByApplicationNameAndSpaceReturns(v3action.Warnings{"get-warning-1", "get-warning-2"}, expectedErr)
   149  				})
   150  
   151  				It("returns the error", func() {
   152  					Expect(executeErr).To(Equal(expectedErr))
   153  					Expect(testUI.Out).To(Say("Removing env variable some-key from app some-app in org some-org / space some-space as banana\\.\\.\\."))
   154  
   155  					Expect(testUI.Err).To(Say("get-warning-1"))
   156  					Expect(testUI.Err).To(Say("get-warning-2"))
   157  					Expect(testUI.Out).ToNot(Say("OK"))
   158  				})
   159  			})
   160  		})
   161  	})
   162  })