github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/unset_label_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  	"regexp"
     6  
     7  	"strings"
     8  
     9  	"code.cloudfoundry.org/cli/actor/v7action"
    10  	"code.cloudfoundry.org/cli/command/commandfakes"
    11  	"code.cloudfoundry.org/cli/command/flag"
    12  	"code.cloudfoundry.org/cli/command/translatableerror"
    13  	. "code.cloudfoundry.org/cli/command/v7"
    14  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    15  	"code.cloudfoundry.org/cli/types"
    16  	"code.cloudfoundry.org/cli/util/configv3"
    17  	"code.cloudfoundry.org/cli/util/ui"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	. "github.com/onsi/gomega/gbytes"
    21  )
    22  
    23  var _ = Describe("unset-label command", func() {
    24  	var (
    25  		cmd             UnsetLabelCommand
    26  		fakeConfig      *commandfakes.FakeConfig
    27  		testUI          *ui.UI
    28  		fakeSharedActor *commandfakes.FakeSharedActor
    29  		fakeActor       *v7fakes.FakeSetLabelActor
    30  		executeErr      error
    31  	)
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v7fakes.FakeSetLabelActor)
    37  		cmd = UnsetLabelCommand{
    38  			UI:          testUI,
    39  			Config:      fakeConfig,
    40  			SharedActor: fakeSharedActor,
    41  			Actor:       fakeActor,
    42  		}
    43  	})
    44  
    45  	When("unsetting labels on apps", func() {
    46  		BeforeEach(func() {
    47  			cmd.RequiredArgs = flag.UnsetLabelArgs{
    48  				ResourceType: "app",
    49  			}
    50  		})
    51  
    52  		JustBeforeEach(func() {
    53  			executeErr = cmd.Execute(nil)
    54  		})
    55  
    56  		It("doesn't error", func() {
    57  			Expect(executeErr).ToNot(HaveOccurred())
    58  		})
    59  
    60  		It("checks that the user is logged in and targeted to an org and space", func() {
    61  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    62  			checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    63  			Expect(checkOrg).To(BeTrue())
    64  			Expect(checkSpace).To(BeTrue())
    65  		})
    66  
    67  		When("checking the target fails", func() {
    68  			BeforeEach(func() {
    69  				fakeSharedActor.CheckTargetReturns(errors.New("Target not found"))
    70  			})
    71  
    72  			It("we expect an error to be returned", func() {
    73  				Expect(executeErr).To(MatchError("Target not found"))
    74  			})
    75  		})
    76  
    77  		When("checking the target succeeds", func() {
    78  			var appName string
    79  
    80  			BeforeEach(func() {
    81  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
    82  				fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"})
    83  				appName = "some-app"
    84  				cmd.RequiredArgs.ResourceName = appName
    85  			})
    86  
    87  			When("getting the current user succeeds", func() {
    88  				var expectedMap map[string]types.NullString
    89  
    90  				BeforeEach(func() {
    91  					fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    92  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
    93  				})
    94  
    95  				It("informs the user that labels are being removed", func() {
    96  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for app %s in org fake-org / space fake-space as some-user...`), appName))
    97  				})
    98  
    99  				When("updating the app labels succeeds", func() {
   100  					BeforeEach(func() {
   101  						fakeActor.UpdateApplicationLabelsByApplicationNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   102  							nil)
   103  						expectedMap = map[string]types.NullString{
   104  							"some-label":     types.NewNullString(),
   105  							"some-other-key": types.NewNullString()}
   106  					})
   107  
   108  					It("does not return an error", func() {
   109  						Expect(executeErr).ToNot(HaveOccurred())
   110  					})
   111  
   112  					It("prints all warnings", func() {
   113  						Expect(testUI.Err).To(Say("some-warning-1"))
   114  						Expect(testUI.Err).To(Say("some-warning-2"))
   115  					})
   116  
   117  					It("passes the correct parameters into the actor", func() {
   118  
   119  						Expect(fakeActor.UpdateApplicationLabelsByApplicationNameCallCount()).To(Equal(1))
   120  						actualAppName, spaceGUID, labelsMap := fakeActor.UpdateApplicationLabelsByApplicationNameArgsForCall(0)
   121  						Expect(actualAppName).To(Equal(appName))
   122  						Expect(spaceGUID).To(Equal("some-space-guid"))
   123  						Expect(labelsMap).To(Equal(expectedMap))
   124  					})
   125  
   126  					When("the resource type argument is not lowercase", func() {
   127  						BeforeEach(func() {
   128  							cmd.RequiredArgs.ResourceType = "aPp"
   129  						})
   130  
   131  						It("passes the correct parameters into the actor", func() {
   132  							Expect(executeErr).ToNot(HaveOccurred())
   133  
   134  							Expect(fakeActor.UpdateApplicationLabelsByApplicationNameCallCount()).To(Equal(1))
   135  							actualAppName, spaceGUID, labelsMap := fakeActor.UpdateApplicationLabelsByApplicationNameArgsForCall(0)
   136  							Expect(actualAppName).To(Equal(appName))
   137  							Expect(spaceGUID).To(Equal("some-space-guid"))
   138  							Expect(labelsMap).To(Equal(expectedMap))
   139  						})
   140  					})
   141  				})
   142  
   143  				When("updating the app labels fails", func() {
   144  					BeforeEach(func() {
   145  						fakeActor.UpdateApplicationLabelsByApplicationNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   146  							errors.New("api call failed"))
   147  					})
   148  
   149  					It("prints all warnings", func() {
   150  						Expect(testUI.Err).To(Say("some-warning-1"))
   151  						Expect(testUI.Err).To(Say("some-warning-2"))
   152  					})
   153  
   154  					It("returns the error", func() {
   155  						Expect(executeErr).To(MatchError("api call failed"))
   156  					})
   157  				})
   158  			})
   159  			When("getting the user fails", func() {
   160  				BeforeEach(func() {
   161  					fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("could not get user"))
   162  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   163  				})
   164  
   165  				It("returns the error", func() {
   166  					Expect(executeErr).To(MatchError("could not get user"))
   167  				})
   168  			})
   169  		})
   170  	})
   171  
   172  	When("Unsetting labels on buildpacks", func() {
   173  		var resourceName string
   174  
   175  		BeforeEach(func() {
   176  			testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
   177  			fakeConfig = new(commandfakes.FakeConfig)
   178  			fakeSharedActor = new(commandfakes.FakeSharedActor)
   179  			fakeActor = new(v7fakes.FakeSetLabelActor)
   180  			resourceName = "some-buildpack"
   181  			cmd = UnsetLabelCommand{
   182  				Actor:       fakeActor,
   183  				UI:          testUI,
   184  				Config:      fakeConfig,
   185  				SharedActor: fakeSharedActor,
   186  			}
   187  			cmd.RequiredArgs = flag.UnsetLabelArgs{
   188  				ResourceType: "buildpack",
   189  				ResourceName: resourceName,
   190  			}
   191  			cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   192  		})
   193  
   194  		JustBeforeEach(func() {
   195  			executeErr = cmd.Execute(nil)
   196  		})
   197  
   198  		When("checking target succeeds", func() {
   199  			var buildpackName = "some-buildpack"
   200  
   201  			BeforeEach(func() {
   202  				fakeSharedActor.CheckTargetReturns(nil)
   203  			})
   204  
   205  			It("doesn't error", func() {
   206  				Expect(executeErr).ToNot(HaveOccurred())
   207  			})
   208  
   209  			It("checks that the user is logged in", func() {
   210  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   211  				checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   212  				Expect(checkOrg).To(BeFalse())
   213  				Expect(checkSpace).To(BeFalse())
   214  			})
   215  
   216  			When("fetching the current user's name fails", func() {
   217  				BeforeEach(func() {
   218  					fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("boom"))
   219  				})
   220  
   221  				It("returns an error", func() {
   222  					Expect(executeErr).To(MatchError("boom"))
   223  				})
   224  			})
   225  
   226  			When("fetching current user's name succeeds", func() {
   227  				BeforeEach(func() {
   228  					fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   229  				})
   230  
   231  				When("all the provided labels are valid", func() {
   232  					BeforeEach(func() {
   233  						cmd.RequiredArgs = flag.UnsetLabelArgs{
   234  							ResourceType: "buildpack",
   235  							ResourceName: buildpackName,
   236  							LabelKeys:    []string{"FOO", "ENV"},
   237  						}
   238  
   239  						fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackReturns(
   240  							v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   241  							nil,
   242  						)
   243  					})
   244  
   245  					When("updating the buildpack labels succeeds", func() {
   246  						When("the stack is specified", func() {
   247  							BeforeEach(func() {
   248  								cmd.BuildpackStack = "globinski"
   249  							})
   250  
   251  							It("unsets the provided labels on the buildpack", func() {
   252  								Expect(fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackCallCount()).To(Equal(1))
   253  								name, stack, labels := fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackArgsForCall(0)
   254  								Expect(name).To(Equal(resourceName), "failed to pass buildpack name")
   255  								Expect(stack).To(Equal("globinski"), "failed to pass stack name")
   256  								Expect(labels).To(BeEquivalentTo(map[string]types.NullString{
   257  									"FOO": types.NewNullString(),
   258  									"ENV": types.NewNullString(),
   259  								}))
   260  							})
   261  
   262  							It("displays a message", func() {
   263  								Expect(executeErr).ToNot(HaveOccurred())
   264  
   265  								Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   266  
   267  								Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s with stack %s as some-user...`), resourceName, cmd.BuildpackStack))
   268  								Expect(testUI.Out).To(Say("OK"))
   269  							})
   270  
   271  							It("prints all warnings", func() {
   272  								Expect(testUI.Err).To(Say("some-warning-1"))
   273  								Expect(testUI.Err).To(Say("some-warning-2"))
   274  							})
   275  						})
   276  
   277  						When("the stack is not specified", func() {
   278  							It("unsets the provided labels on the buildpack", func() {
   279  								Expect(fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackCallCount()).To(Equal(1))
   280  								name, stack, labels := fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackArgsForCall(0)
   281  								Expect(name).To(Equal(resourceName), "failed to pass buildpack name")
   282  								Expect(stack).To(Equal(""), "failed to pass stack name")
   283  								Expect(labels).To(BeEquivalentTo(map[string]types.NullString{
   284  									"FOO": types.NewNullString(),
   285  									"ENV": types.NewNullString(),
   286  								}))
   287  							})
   288  
   289  							It("displays a message", func() {
   290  								Expect(executeErr).ToNot(HaveOccurred())
   291  
   292  								Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   293  
   294  								Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s as some-user...`), resourceName))
   295  								Expect(testUI.Out).To(Say("OK"))
   296  							})
   297  
   298  							It("prints all warnings", func() {
   299  								Expect(testUI.Err).To(Say("some-warning-1"))
   300  								Expect(testUI.Err).To(Say("some-warning-2"))
   301  							})
   302  						})
   303  					})
   304  				})
   305  
   306  				When("the resource type is not lowercase", func() {
   307  					BeforeEach(func() {
   308  						cmd.RequiredArgs = flag.UnsetLabelArgs{
   309  							ResourceType: "bUiLdPaCk",
   310  							ResourceName: buildpackName,
   311  							LabelKeys:    []string{"FOO", "ENV"},
   312  						}
   313  
   314  						fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackReturns(
   315  							v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   316  							nil,
   317  						)
   318  					})
   319  
   320  					When("updating the buildpack labels succeeds", func() {
   321  						When("the stack is specified", func() {
   322  							BeforeEach(func() {
   323  								cmd.BuildpackStack = "globinski"
   324  							})
   325  
   326  							It("does not display an argument combination error", func() {
   327  								Expect(executeErr).ToNot(HaveOccurred())
   328  							})
   329  
   330  							It("unsets the provided labels on the buildpack", func() {
   331  								Expect(fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackCallCount()).To(Equal(1))
   332  								name, stack, labels := fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackArgsForCall(0)
   333  								Expect(name).To(Equal(resourceName), "failed to pass buildpack name")
   334  								Expect(stack).To(Equal("globinski"), "failed to pass stack name")
   335  								Expect(labels).To(BeEquivalentTo(map[string]types.NullString{
   336  									"FOO": types.NewNullString(),
   337  									"ENV": types.NewNullString(),
   338  								}))
   339  							})
   340  
   341  							It("displays a message", func() {
   342  								Expect(executeErr).ToNot(HaveOccurred())
   343  
   344  								Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   345  
   346  								Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s with stack globinski as some-user...`), resourceName))
   347  								Expect(testUI.Out).To(Say("OK"))
   348  							})
   349  
   350  							It("prints all warnings", func() {
   351  								Expect(testUI.Err).To(Say("some-warning-1"))
   352  								Expect(testUI.Err).To(Say("some-warning-2"))
   353  							})
   354  						})
   355  
   356  						When("the stack is not specified", func() {
   357  							It("unsets the provided labels on the buildpack", func() {
   358  								Expect(fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackCallCount()).To(Equal(1))
   359  								name, stack, labels := fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackArgsForCall(0)
   360  								Expect(name).To(Equal(resourceName), "failed to pass buildpack name")
   361  								Expect(stack).To(Equal(""), "failed to pass stack name")
   362  								Expect(labels).To(BeEquivalentTo(map[string]types.NullString{
   363  									"FOO": types.NewNullString(),
   364  									"ENV": types.NewNullString(),
   365  								}))
   366  							})
   367  
   368  							It("displays a message", func() {
   369  								Expect(executeErr).ToNot(HaveOccurred())
   370  
   371  								Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   372  
   373  								Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s as some-user...`), resourceName))
   374  								Expect(testUI.Out).To(Say("OK"))
   375  							})
   376  
   377  							It("prints all warnings", func() {
   378  								Expect(testUI.Err).To(Say("some-warning-1"))
   379  								Expect(testUI.Err).To(Say("some-warning-2"))
   380  							})
   381  						})
   382  					})
   383  				})
   384  			})
   385  
   386  			When("fetching the current user's name fails", func() {
   387  				BeforeEach(func() {
   388  					fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("could not get user"))
   389  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   390  				})
   391  
   392  				It("returns the error", func() {
   393  					Expect(executeErr).To(MatchError("could not get user"))
   394  				})
   395  			})
   396  		})
   397  	})
   398  
   399  	When("unsetting labels on domains", func() {
   400  		var resourceName string
   401  		BeforeEach(func() {
   402  			resourceName = "example.com"
   403  			cmd.RequiredArgs = flag.UnsetLabelArgs{
   404  				ResourceType: "domain",
   405  				ResourceName: resourceName,
   406  			}
   407  			cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   408  
   409  			fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   410  			fakeActor.UpdateDomainLabelsByDomainNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   411  				nil)
   412  		})
   413  
   414  		JustBeforeEach(func() {
   415  			executeErr = cmd.Execute(nil)
   416  		})
   417  
   418  		It("doesn't error", func() {
   419  			Expect(executeErr).ToNot(HaveOccurred())
   420  		})
   421  
   422  		It("informs the user that labels are being removed", func() {
   423  			Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for domain %s as some-user...`), resourceName))
   424  		})
   425  
   426  		It("removes the provided labels from the domain", func() {
   427  			Expect(fakeActor.UpdateDomainLabelsByDomainNameCallCount()).To(Equal(1))
   428  			name, labels := fakeActor.UpdateDomainLabelsByDomainNameArgsForCall(0)
   429  			Expect(name).To(Equal(resourceName), "failed to pass domain name")
   430  			Expect(labels).To(BeEquivalentTo(map[string]types.NullString{
   431  				"some-label":     types.NewNullString(),
   432  				"some-other-key": types.NewNullString(),
   433  			}))
   434  		})
   435  
   436  		It("prints all warnings", func() {
   437  			Expect(testUI.Err).To(Say("some-warning-1"))
   438  			Expect(testUI.Err).To(Say("some-warning-2"))
   439  		})
   440  
   441  		When("the resource type argument is not lowercase", func() {
   442  			BeforeEach(func() {
   443  				cmd.RequiredArgs.ResourceType = "DoMaiN"
   444  			})
   445  
   446  			It("passes the correct parameters into the actor", func() {
   447  				Expect(executeErr).ToNot(HaveOccurred())
   448  
   449  				Expect(fakeActor.UpdateDomainLabelsByDomainNameCallCount()).To(Equal(1))
   450  
   451  				name, labels := fakeActor.UpdateDomainLabelsByDomainNameArgsForCall(0)
   452  				Expect(name).To(Equal(resourceName), "failed to pass domain name")
   453  				Expect(labels).To(BeEquivalentTo(map[string]types.NullString{
   454  					"some-label":     types.NewNullString(),
   455  					"some-other-key": types.NewNullString(),
   456  				}))
   457  			})
   458  		})
   459  
   460  		When("updating the domain labels fails", func() {
   461  			BeforeEach(func() {
   462  				fakeActor.UpdateDomainLabelsByDomainNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   463  					errors.New("api call failed"))
   464  			})
   465  
   466  			It("prints all warnings", func() {
   467  				Expect(testUI.Err).To(Say("some-warning-1"))
   468  				Expect(testUI.Err).To(Say("some-warning-2"))
   469  			})
   470  
   471  			It("returns the error", func() {
   472  				Expect(executeErr).To(MatchError("api call failed"))
   473  			})
   474  		})
   475  
   476  		When("getting the user fails", func() {
   477  			BeforeEach(func() {
   478  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("could not get user"))
   479  				cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   480  			})
   481  
   482  			It("returns the error", func() {
   483  				Expect(executeErr).To(MatchError("could not get user"))
   484  			})
   485  		})
   486  	})
   487  
   488  	When("Unsetting labels on orgs", func() {
   489  		BeforeEach(func() {
   490  			cmd.RequiredArgs = flag.UnsetLabelArgs{
   491  				ResourceType: "org",
   492  			}
   493  		})
   494  
   495  		JustBeforeEach(func() {
   496  			executeErr = cmd.Execute(nil)
   497  		})
   498  
   499  		When("checking target succeeds", func() {
   500  			var orgName = "some-org"
   501  
   502  			BeforeEach(func() {
   503  				fakeSharedActor.CheckTargetReturns(nil)
   504  				cmd.RequiredArgs.ResourceName = orgName
   505  
   506  			})
   507  
   508  			When("fetching current user's name succeeds", func() {
   509  				BeforeEach(func() {
   510  					fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   511  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   512  				})
   513  
   514  				It("informs the user that labels are being removed", func() {
   515  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for org %s as some-user...`), orgName))
   516  				})
   517  
   518  				When("updating the org labels succeeds", func() {
   519  					var expectedMap map[string]types.NullString
   520  
   521  					BeforeEach(func() {
   522  						fakeActor.UpdateOrganizationLabelsByOrganizationNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   523  							nil)
   524  						expectedMap = map[string]types.NullString{
   525  							"some-label":     types.NewNullString(),
   526  							"some-other-key": types.NewNullString()}
   527  					})
   528  
   529  					It("does not return an error", func() {
   530  						Expect(executeErr).ToNot(HaveOccurred())
   531  					})
   532  
   533  					It("prints all warnings", func() {
   534  						Expect(testUI.Err).To(Say("some-warning-1"))
   535  						Expect(testUI.Err).To(Say("some-warning-2"))
   536  					})
   537  
   538  					It("passes the correct parameters into the actor", func() {
   539  						Expect(fakeActor.UpdateOrganizationLabelsByOrganizationNameCallCount()).To(Equal(1))
   540  						actualOrgName, labelsMap := fakeActor.UpdateOrganizationLabelsByOrganizationNameArgsForCall(0)
   541  						Expect(actualOrgName).To(Equal(orgName))
   542  						Expect(labelsMap).To(Equal(expectedMap))
   543  					})
   544  
   545  					When("the resource type argument is not lowercase", func() {
   546  						BeforeEach(func() {
   547  							cmd.RequiredArgs.ResourceType = "OrG"
   548  						})
   549  
   550  						It("retrieves the labels associated with the org", func() {
   551  							Expect(executeErr).ToNot(HaveOccurred())
   552  							Expect(fakeActor.UpdateOrganizationLabelsByOrganizationNameCallCount()).To(Equal(1))
   553  							actualOrgName, labelsMap := fakeActor.UpdateOrganizationLabelsByOrganizationNameArgsForCall(0)
   554  							Expect(actualOrgName).To(Equal(orgName))
   555  							Expect(labelsMap).To(Equal(expectedMap))
   556  						})
   557  					})
   558  				})
   559  			})
   560  
   561  			When("fetching the current user's name fails", func() {
   562  				BeforeEach(func() {
   563  					fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("could not get user"))
   564  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   565  				})
   566  
   567  				It("returns the error", func() {
   568  					Expect(executeErr).To(MatchError("could not get user"))
   569  				})
   570  			})
   571  		})
   572  	})
   573  
   574  	When("unsetting labels on routes", func() {
   575  		var resourceName string
   576  		BeforeEach(func() {
   577  			resourceName = "some-route.example.com"
   578  			cmd.RequiredArgs = flag.UnsetLabelArgs{
   579  				ResourceType: "route",
   580  				ResourceName: resourceName,
   581  			}
   582  			cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   583  
   584  			fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   585  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
   586  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "space-guid"})
   587  			fakeActor.UpdateRouteLabelsReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   588  				nil)
   589  		})
   590  
   591  		JustBeforeEach(func() {
   592  			executeErr = cmd.Execute(nil)
   593  		})
   594  
   595  		It("doesn't error", func() {
   596  			Expect(executeErr).ToNot(HaveOccurred())
   597  		})
   598  
   599  		It("informs the user that labels are being removed", func() {
   600  			Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for route %s in org fake-org / space fake-space as some-user...`), resourceName))
   601  		})
   602  
   603  		It("removes the provided labels from the route", func() {
   604  			Expect(fakeActor.UpdateRouteLabelsCallCount()).To(Equal(1))
   605  			name, spaceGUID, labels := fakeActor.UpdateRouteLabelsArgsForCall(0)
   606  			Expect(name).To(Equal(resourceName), "failed to pass route name")
   607  			Expect(labels).To(BeEquivalentTo(map[string]types.NullString{
   608  				"some-label":     types.NewNullString(),
   609  				"some-other-key": types.NewNullString(),
   610  			}))
   611  			Expect(spaceGUID).To(Equal("space-guid"))
   612  		})
   613  
   614  		It("prints all warnings", func() {
   615  			Expect(testUI.Err).To(Say("some-warning-1"))
   616  			Expect(testUI.Err).To(Say("some-warning-2"))
   617  		})
   618  
   619  		When("the resource type argument is not lowercase", func() {
   620  			BeforeEach(func() {
   621  				cmd.RequiredArgs.ResourceType = "rouTE"
   622  			})
   623  
   624  			It("passes the correct parameters into the actor", func() {
   625  				Expect(executeErr).ToNot(HaveOccurred())
   626  
   627  				Expect(fakeActor.UpdateRouteLabelsCallCount()).To(Equal(1))
   628  
   629  				name, spaceGUID, labels := fakeActor.UpdateRouteLabelsArgsForCall(0)
   630  
   631  				Expect(name).To(Equal(resourceName), "failed to pass route name")
   632  				Expect(spaceGUID).To(Equal("space-guid"))
   633  				Expect(labels).To(BeEquivalentTo(map[string]types.NullString{
   634  					"some-label":     types.NewNullString(),
   635  					"some-other-key": types.NewNullString(),
   636  				}))
   637  			})
   638  		})
   639  
   640  		When("updating the route labels fails", func() {
   641  			BeforeEach(func() {
   642  				fakeActor.UpdateRouteLabelsReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   643  					errors.New("api call failed"))
   644  			})
   645  
   646  			It("prints all warnings", func() {
   647  				Expect(testUI.Err).To(Say("some-warning-1"))
   648  				Expect(testUI.Err).To(Say("some-warning-2"))
   649  			})
   650  
   651  			It("returns the error", func() {
   652  				Expect(executeErr).To(MatchError("api call failed"))
   653  			})
   654  		})
   655  	})
   656  
   657  	When("Unsetting labels on spaces", func() {
   658  		BeforeEach(func() {
   659  			cmd.RequiredArgs = flag.UnsetLabelArgs{
   660  				ResourceType: "space",
   661  			}
   662  		})
   663  
   664  		JustBeforeEach(func() {
   665  			executeErr = cmd.Execute(nil)
   666  		})
   667  
   668  		It("doesn't error", func() {
   669  			Expect(executeErr).ToNot(HaveOccurred())
   670  		})
   671  
   672  		It("checks that the user is logged in and targeted to an org and space", func() {
   673  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   674  			checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   675  			Expect(checkOrg).To(BeTrue())
   676  			Expect(checkSpace).To(BeFalse())
   677  		})
   678  
   679  		When("checking the target fails", func() {
   680  			BeforeEach(func() {
   681  				fakeSharedActor.CheckTargetReturns(errors.New("Target not found"))
   682  			})
   683  
   684  			It("we expect an error to be returned", func() {
   685  				Expect(executeErr).To(MatchError("Target not found"))
   686  			})
   687  		})
   688  
   689  		When("checking the target succeeds", func() {
   690  			var (
   691  				spaceName   string
   692  				expectedMap map[string]types.NullString
   693  			)
   694  
   695  			BeforeEach(func() {
   696  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org", GUID: "some-org-guid"})
   697  				spaceName = "spiff"
   698  				cmd.RequiredArgs.ResourceName = spaceName
   699  			})
   700  
   701  			When("getting the current user succeeds", func() {
   702  				BeforeEach(func() {
   703  					fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   704  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   705  				})
   706  
   707  				It("informs the user that labels are being removed", func() {
   708  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for space %s in org fake-org as some-user...`), spaceName))
   709  				})
   710  
   711  				When("updating the space labels succeeds", func() {
   712  					BeforeEach(func() {
   713  						fakeActor.UpdateSpaceLabelsBySpaceNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   714  							nil)
   715  						expectedMap = map[string]types.NullString{
   716  							"some-label":     types.NewNullString(),
   717  							"some-other-key": types.NewNullString()}
   718  					})
   719  
   720  					It("does not return an error", func() {
   721  						Expect(executeErr).ToNot(HaveOccurred())
   722  					})
   723  
   724  					It("prints all warnings", func() {
   725  						Expect(testUI.Err).To(Say("some-warning-1"))
   726  						Expect(testUI.Err).To(Say("some-warning-2"))
   727  					})
   728  
   729  					It("passes the correct parameters into the actor", func() {
   730  						Expect(fakeActor.UpdateSpaceLabelsBySpaceNameCallCount()).To(Equal(1))
   731  						actualSpaceName, orgGUID, labelsMap := fakeActor.UpdateSpaceLabelsBySpaceNameArgsForCall(0)
   732  						Expect(actualSpaceName).To(Equal(spaceName))
   733  						Expect(orgGUID).To(Equal("some-org-guid"))
   734  						Expect(labelsMap).To(Equal(expectedMap))
   735  					})
   736  
   737  					When("the resource type argument is not lowercase", func() {
   738  						BeforeEach(func() {
   739  							cmd.RequiredArgs.ResourceType = "SpAcE"
   740  						})
   741  
   742  						It("retrieves the labels associated with the space", func() {
   743  							Expect(executeErr).ToNot(HaveOccurred())
   744  							Expect(fakeActor.UpdateSpaceLabelsBySpaceNameCallCount()).To(Equal(1))
   745  							actualSpaceName, orgGUID, labelsMap := fakeActor.UpdateSpaceLabelsBySpaceNameArgsForCall(0)
   746  							Expect(actualSpaceName).To(Equal(spaceName))
   747  							Expect(orgGUID).To(Equal("some-org-guid"))
   748  							Expect(labelsMap).To(Equal(expectedMap))
   749  						})
   750  					})
   751  				})
   752  
   753  				When("updating the space labels fails", func() {
   754  					BeforeEach(func() {
   755  						fakeActor.UpdateSpaceLabelsBySpaceNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   756  							errors.New("api call failed"))
   757  					})
   758  
   759  					It("prints all warnings", func() {
   760  						Expect(testUI.Err).To(Say("some-warning-1"))
   761  						Expect(testUI.Err).To(Say("some-warning-2"))
   762  					})
   763  
   764  					It("returns the error", func() {
   765  						Expect(executeErr).To(MatchError("api call failed"))
   766  					})
   767  				})
   768  			})
   769  			When("getting the user fails", func() {
   770  				BeforeEach(func() {
   771  					fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("could not get user"))
   772  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   773  				})
   774  
   775  				It("returns the error", func() {
   776  					Expect(executeErr).To(MatchError("could not get user"))
   777  				})
   778  			})
   779  		})
   780  	})
   781  
   782  	When("Unsetting labels on stacks", func() {
   783  		BeforeEach(func() {
   784  			cmd.RequiredArgs = flag.UnsetLabelArgs{
   785  				ResourceType: "stack",
   786  			}
   787  		})
   788  
   789  		JustBeforeEach(func() {
   790  			executeErr = cmd.Execute(nil)
   791  		})
   792  
   793  		When("checking target succeeds", func() {
   794  			var (
   795  				stackName   = "some-stack"
   796  				expectedMap map[string]types.NullString
   797  			)
   798  
   799  			BeforeEach(func() {
   800  				fakeSharedActor.CheckTargetReturns(nil)
   801  				cmd.RequiredArgs.ResourceName = stackName
   802  				expectedMap = map[string]types.NullString{
   803  					"some-label":     types.NewNullString(),
   804  					"some-other-key": types.NewNullString(),
   805  				}
   806  			})
   807  
   808  			When("fetching current user's name succeeds", func() {
   809  				BeforeEach(func() {
   810  					fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   811  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   812  				})
   813  
   814  				It("informs the user that labels are being removed", func() {
   815  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for stack %s as some-user...`), stackName))
   816  				})
   817  
   818  				When("updating the stack labels succeeds", func() {
   819  					BeforeEach(func() {
   820  						fakeActor.UpdateStackLabelsByStackNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   821  							nil)
   822  					})
   823  
   824  					It("does not return an error", func() {
   825  						Expect(executeErr).ToNot(HaveOccurred())
   826  					})
   827  
   828  					It("prints all warnings", func() {
   829  						Expect(testUI.Err).To(Say("some-warning-1"))
   830  						Expect(testUI.Err).To(Say("some-warning-2"))
   831  					})
   832  
   833  					It("passes the correct parameters into the actor", func() {
   834  
   835  						Expect(fakeActor.UpdateStackLabelsByStackNameCallCount()).To(Equal(1))
   836  						actualStackName, labelsMap := fakeActor.UpdateStackLabelsByStackNameArgsForCall(0)
   837  						Expect(actualStackName).To(Equal(stackName))
   838  						Expect(labelsMap).To(Equal(expectedMap))
   839  					})
   840  
   841  					When("the resource type argument is not lowercase", func() {
   842  						BeforeEach(func() {
   843  							cmd.RequiredArgs.ResourceType = "sTaCk"
   844  						})
   845  						It("passes the correct parameters into the actor", func() {
   846  							Expect(executeErr).ToNot(HaveOccurred())
   847  							Expect(fakeActor.UpdateStackLabelsByStackNameCallCount()).To(Equal(1))
   848  							actualStackName, labelsMap := fakeActor.UpdateStackLabelsByStackNameArgsForCall(0)
   849  							Expect(actualStackName).To(Equal(stackName))
   850  							Expect(labelsMap).To(Equal(expectedMap))
   851  						})
   852  					})
   853  				})
   854  
   855  			})
   856  
   857  			When("fetching the current user's name fails", func() {
   858  				BeforeEach(func() {
   859  					fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("could not get user"))
   860  					cmd.RequiredArgs.LabelKeys = []string{"some-label", "some-other-key"}
   861  				})
   862  
   863  				It("returns the error", func() {
   864  					Expect(executeErr).To(MatchError("could not get user"))
   865  				})
   866  			})
   867  		})
   868  	})
   869  
   870  	Describe("disallowed --stack option", func() {
   871  		When("specifying --stack", func() {
   872  			It("complains", func() {
   873  				names := []string{"app", "space", "stack", "org"}
   874  				for _, name := range names {
   875  					cmd.RequiredArgs = flag.UnsetLabelArgs{
   876  						ResourceType: name,
   877  						ResourceName: "oshkosh",
   878  						LabelKeys:    []string{"FOO", "ENV"},
   879  					}
   880  					cmd.BuildpackStack = "cflinuxfs3"
   881  					executeErr := cmd.Execute(nil)
   882  					argumentCombinationError := translatableerror.ArgumentCombinationError{
   883  						Args: []string{strings.ToLower(cmd.RequiredArgs.ResourceType), "--stack, -s"},
   884  					}
   885  					Expect(executeErr).To(MatchError(argumentCombinationError))
   886  				}
   887  			})
   888  		})
   889  	})
   890  })