github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/command/v7/delete_label_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  	"regexp"
     6  
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    11  	"code.cloudfoundry.org/cli/types"
    12  	"code.cloudfoundry.org/cli/util/configv3"
    13  	"code.cloudfoundry.org/cli/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("delete-label command", func() {
    20  	var (
    21  		cmd             DeleteLabelCommand
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		testUI          *ui.UI
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v7fakes.FakeDeleteLabelActor
    26  		executeErr      error
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    31  		fakeConfig = new(commandfakes.FakeConfig)
    32  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    33  		fakeActor = new(v7fakes.FakeDeleteLabelActor)
    34  		cmd = DeleteLabelCommand{
    35  			UI:          testUI,
    36  			Config:      fakeConfig,
    37  			SharedActor: fakeSharedActor,
    38  			Actor:       fakeActor,
    39  		}
    40  	})
    41  
    42  	JustBeforeEach(func() {
    43  		executeErr = cmd.Execute(nil)
    44  	})
    45  
    46  	It("doesn't error", func() {
    47  		Expect(executeErr).ToNot(HaveOccurred())
    48  	})
    49  
    50  	It("checks that the user is logged in and targeted to an org and space", func() {
    51  		Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    52  		checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    53  		Expect(checkOrg).To(BeTrue())
    54  		Expect(checkSpace).To(BeTrue())
    55  	})
    56  
    57  	When("checking the target fails", func() {
    58  		BeforeEach(func() {
    59  			fakeSharedActor.CheckTargetReturns(errors.New("Target not found"))
    60  		})
    61  
    62  		It("we expect an error to be returned", func() {
    63  			Expect(executeErr).To(MatchError("Target not found"))
    64  		})
    65  	})
    66  
    67  	When("checking the target succeeds", func() {
    68  		var appName string
    69  
    70  		BeforeEach(func() {
    71  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
    72  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"})
    73  			appName = "some-app"
    74  			cmd.RequiredArgs.ResourceName = appName
    75  		})
    76  
    77  		When("getting the current user succeeds", func() {
    78  			BeforeEach(func() {
    79  				fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    80  				cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
    81  			})
    82  
    83  			It("informs the user that labels are being deleted", func() {
    84  				Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Deleting label(s) for app %s in org fake-org / space fake-space as some-user...`), appName))
    85  			})
    86  
    87  			When("updating the app labels succeeds", func() {
    88  				BeforeEach(func() {
    89  					fakeActor.UpdateApplicationLabelsByApplicationNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
    90  						nil)
    91  				})
    92  
    93  				It("does not return an error", func() {
    94  					Expect(executeErr).ToNot(HaveOccurred())
    95  				})
    96  
    97  				It("prints all warnings", func() {
    98  					Expect(testUI.Err).To(Say("some-warning-1"))
    99  					Expect(testUI.Err).To(Say("some-warning-2"))
   100  				})
   101  
   102  				It("passes the correct parameters into the actor", func() {
   103  					expectedMaps := map[string]types.NullString{
   104  						"some-label":     types.NewNullString(),
   105  						"some-other-key": types.NewNullString()}
   106  
   107  					Expect(fakeActor.UpdateApplicationLabelsByApplicationNameCallCount()).To(Equal(1))
   108  					actualAppName, spaceGUID, labelsMap := fakeActor.UpdateApplicationLabelsByApplicationNameArgsForCall(0)
   109  					Expect(actualAppName).To(Equal(appName))
   110  					Expect(spaceGUID).To(Equal("some-space-guid"))
   111  					Expect(labelsMap).To(Equal(expectedMaps))
   112  				})
   113  			})
   114  
   115  			When("updating the app labels fails", func() {
   116  				BeforeEach(func() {
   117  					fakeActor.UpdateApplicationLabelsByApplicationNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   118  						errors.New("api call failed"))
   119  				})
   120  
   121  				It("prints all warnings", func() {
   122  					Expect(testUI.Err).To(Say("some-warning-1"))
   123  					Expect(testUI.Err).To(Say("some-warning-2"))
   124  				})
   125  
   126  				It("returns the error", func() {
   127  					Expect(executeErr).To(MatchError("api call failed"))
   128  				})
   129  			})
   130  		})
   131  		When("getting the user fails", func() {
   132  			BeforeEach(func() {
   133  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("could not get user"))
   134  				cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   135  			})
   136  
   137  			It("returns the error", func() {
   138  				Expect(executeErr).To(MatchError("could not get user"))
   139  			})
   140  		})
   141  	})
   142  })