github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v7/delete_label_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/command/flag"
     5  	"errors"
     6  	"regexp"
     7  
     8  	"code.cloudfoundry.org/cli/actor/v7action"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	. "code.cloudfoundry.org/cli/command/v7"
    11  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    12  	"code.cloudfoundry.org/cli/types"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("delete-label command", func() {
    21  	var (
    22  		cmd             DeleteLabelCommand
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		testUI          *ui.UI
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v7fakes.FakeDeleteLabelActor
    27  		executeErr      error
    28  	)
    29  
    30  	When("deleting labels on apps", func() {
    31  		BeforeEach(func() {
    32  			testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  			fakeConfig = new(commandfakes.FakeConfig)
    34  			fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  			fakeActor = new(v7fakes.FakeDeleteLabelActor)
    36  			cmd = DeleteLabelCommand{
    37  				UI:          testUI,
    38  				Config:      fakeConfig,
    39  				SharedActor: fakeSharedActor,
    40  				Actor:       fakeActor,
    41  			}
    42  			cmd.RequiredArgs = flag.DeleteLabelArgs{
    43  				ResourceType: "app",
    44  			}
    45  		})
    46  
    47  		JustBeforeEach(func() {
    48  			executeErr = cmd.Execute(nil)
    49  		})
    50  
    51  		It("doesn't error", func() {
    52  			Expect(executeErr).ToNot(HaveOccurred())
    53  		})
    54  
    55  		It("checks that the user is logged in and targeted to an org and space", func() {
    56  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    57  			checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    58  			Expect(checkOrg).To(BeTrue())
    59  			Expect(checkSpace).To(BeTrue())
    60  		})
    61  
    62  		When("checking the target fails", func() {
    63  			BeforeEach(func() {
    64  				fakeSharedActor.CheckTargetReturns(errors.New("Target not found"))
    65  			})
    66  
    67  			It("we expect an error to be returned", func() {
    68  				Expect(executeErr).To(MatchError("Target not found"))
    69  			})
    70  		})
    71  
    72  		When("checking the target succeeds", func() {
    73  			var appName string
    74  
    75  			BeforeEach(func() {
    76  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
    77  				fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"})
    78  				appName = "some-app"
    79  				cmd.RequiredArgs.ResourceName = appName
    80  			})
    81  
    82  			When("getting the current user succeeds", func() {
    83  				BeforeEach(func() {
    84  					fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    85  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
    86  				})
    87  
    88  				It("informs the user that labels are being deleted", func() {
    89  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Deleting label(s) for app %s in org fake-org / space fake-space as some-user...`), appName))
    90  				})
    91  
    92  				When("updating the app labels succeeds", func() {
    93  					BeforeEach(func() {
    94  						fakeActor.UpdateApplicationLabelsByApplicationNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
    95  							nil)
    96  					})
    97  
    98  					It("does not return an error", func() {
    99  						Expect(executeErr).ToNot(HaveOccurred())
   100  					})
   101  
   102  					It("prints all warnings", func() {
   103  						Expect(testUI.Err).To(Say("some-warning-1"))
   104  						Expect(testUI.Err).To(Say("some-warning-2"))
   105  					})
   106  
   107  					It("passes the correct parameters into the actor", func() {
   108  						expectedMap := map[string]types.NullString{
   109  							"some-label":     types.NewNullString(),
   110  							"some-other-key": types.NewNullString()}
   111  
   112  						Expect(fakeActor.UpdateApplicationLabelsByApplicationNameCallCount()).To(Equal(1))
   113  						actualAppName, spaceGUID, labelsMap := fakeActor.UpdateApplicationLabelsByApplicationNameArgsForCall(0)
   114  						Expect(actualAppName).To(Equal(appName))
   115  						Expect(spaceGUID).To(Equal("some-space-guid"))
   116  						Expect(labelsMap).To(Equal(expectedMap))
   117  					})
   118  				})
   119  
   120  				When("updating the app labels fails", func() {
   121  					BeforeEach(func() {
   122  						fakeActor.UpdateApplicationLabelsByApplicationNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   123  							errors.New("api call failed"))
   124  					})
   125  
   126  					It("prints all warnings", func() {
   127  						Expect(testUI.Err).To(Say("some-warning-1"))
   128  						Expect(testUI.Err).To(Say("some-warning-2"))
   129  					})
   130  
   131  					It("returns the error", func() {
   132  						Expect(executeErr).To(MatchError("api call failed"))
   133  					})
   134  				})
   135  			})
   136  			When("getting the user fails", func() {
   137  				BeforeEach(func() {
   138  					fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("could not get user"))
   139  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   140  				})
   141  
   142  				It("returns the error", func() {
   143  					Expect(executeErr).To(MatchError("could not get user"))
   144  				})
   145  			})
   146  		})
   147  	})
   148  
   149  	When("deleting labels on orgs", func() {
   150  		BeforeEach(func() {
   151  			testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
   152  			fakeConfig = new(commandfakes.FakeConfig)
   153  			fakeSharedActor = new(commandfakes.FakeSharedActor)
   154  			fakeActor = new(v7fakes.FakeDeleteLabelActor)
   155  			cmd = DeleteLabelCommand{
   156  				Actor:       fakeActor,
   157  				UI:          testUI,
   158  				Config:      fakeConfig,
   159  				SharedActor: fakeSharedActor,
   160  			}
   161  			cmd.RequiredArgs = flag.DeleteLabelArgs{
   162  				ResourceType: "org",
   163  			}
   164  		})
   165  
   166  		JustBeforeEach(func() {
   167  			executeErr = cmd.Execute(nil)
   168  		})
   169  
   170  		When("checking target succeeds", func() {
   171  			var orgName = "some-org"
   172  
   173  			BeforeEach(func() {
   174  				fakeSharedActor.CheckTargetReturns(nil)
   175  				cmd.RequiredArgs.ResourceName = orgName
   176  
   177  			})
   178  
   179  			When("fetching current user's name succeeds", func() {
   180  				BeforeEach(func() {
   181  					fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   182  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   183  				})
   184  
   185  				It("informs the user that labels are being deleted", func() {
   186  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Deleting label(s) for org %s as some-user...`), orgName))
   187  				})
   188  
   189  				When("updating the org labels succeeds", func() {
   190  					BeforeEach(func() {
   191  						fakeActor.UpdateOrganizationLabelsByOrganizationNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   192  							nil)
   193  					})
   194  
   195  					It("does not return an error", func() {
   196  						Expect(executeErr).ToNot(HaveOccurred())
   197  					})
   198  
   199  					It("prints all warnings", func() {
   200  						Expect(testUI.Err).To(Say("some-warning-1"))
   201  						Expect(testUI.Err).To(Say("some-warning-2"))
   202  					})
   203  
   204  					It("passes the correct parameters into the actor", func() {
   205  						expectedMaps := map[string]types.NullString{
   206  							"some-label":     types.NewNullString(),
   207  							"some-other-key": types.NewNullString()}
   208  
   209  						Expect(fakeActor.UpdateOrganizationLabelsByOrganizationNameCallCount()).To(Equal(1))
   210  						actualOrgName, labelsMap := fakeActor.UpdateOrganizationLabelsByOrganizationNameArgsForCall(0)
   211  						Expect(actualOrgName).To(Equal(orgName))
   212  						Expect(labelsMap).To(Equal(expectedMaps))
   213  					})
   214  				})
   215  
   216  			})
   217  
   218  			When("fetching the current user's name fails", func() {
   219  				BeforeEach(func() {
   220  					fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("could not get user"))
   221  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   222  				})
   223  
   224  				It("returns the error", func() {
   225  					Expect(executeErr).To(MatchError("could not get user"))
   226  				})
   227  			})
   228  		})
   229  	})
   230  })