github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/space_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    12  	uaaconst "code.cloudfoundry.org/cli/api/uaa/constant"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Space", func() {
    18  	var (
    19  		actor                     *Actor
    20  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    21  		fakeConfig                *v2actionfakes.FakeConfig
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    26  		fakeConfig = new(v2actionfakes.FakeConfig)
    27  		actor = NewActor(fakeCloudControllerClient, nil, fakeConfig)
    28  	})
    29  
    30  	Describe("CreateSpace", func() {
    31  		var (
    32  			quotaName string
    33  
    34  			space      Space
    35  			warnings   Warnings
    36  			executeErr error
    37  		)
    38  
    39  		BeforeEach(func() {
    40  			quotaName = ""
    41  		})
    42  
    43  		JustBeforeEach(func() {
    44  			space, warnings, executeErr = actor.CreateSpace("some-space", "some-org-name", quotaName)
    45  		})
    46  
    47  		When("the org is not found", func() {
    48  			BeforeEach(func() {
    49  				fakeCloudControllerClient.GetOrganizationsReturns(
    50  					[]ccv2.Organization{},
    51  					ccv2.Warnings{
    52  						"warning-1",
    53  						"warning-2",
    54  					},
    55  					actionerror.OrganizationNotFoundError{Name: "some-org-name"},
    56  				)
    57  			})
    58  
    59  			It("returns an OrganizationNotFoundError", func() {
    60  				Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org-name"}))
    61  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    62  			})
    63  		})
    64  
    65  		When("the org is found", func() {
    66  			BeforeEach(func() {
    67  				fakeCloudControllerClient.GetOrganizationsReturns([]ccv2.Organization{
    68  					ccv2.Organization{GUID: "some-org-guid", Name: "some-org-name"}},
    69  					ccv2.Warnings{},
    70  					nil,
    71  				)
    72  			})
    73  
    74  			When("the specified quota is not found", func() {
    75  				BeforeEach(func() {
    76  					quotaName = "some-space-quota"
    77  					fakeCloudControllerClient.GetSpaceQuotasReturns(
    78  						[]ccv2.SpaceQuota{},
    79  						ccv2.Warnings{
    80  							"get-quota-warning-1",
    81  							"get-quota-warning-2",
    82  						},
    83  						nil,
    84  					)
    85  				})
    86  
    87  				It("returns a QuotaNotFoundForNameError", func() {
    88  					Expect(executeErr).To(MatchError(actionerror.QuotaNotFoundForNameError{Name: quotaName}))
    89  					Expect(warnings).To(ContainElement("get-quota-warning-1"))
    90  					Expect(warnings).To(ContainElement("get-quota-warning-2"))
    91  				})
    92  
    93  				It("does not create the space", func() {
    94  					Expect(fakeCloudControllerClient.CreateSpaceCallCount()).To(Equal(0))
    95  				})
    96  			})
    97  
    98  			// When("the specified quota is found")
    99  
   100  			// When("no quota is specified")
   101  
   102  			When("creating the space succeeds", func() {
   103  				var expectedSpace Space
   104  				BeforeEach(func() {
   105  					expectedSpace = Space{
   106  						GUID: "some-space-guid",
   107  						Name: "some-space",
   108  					}
   109  
   110  					fakeCloudControllerClient.CreateSpaceReturns(
   111  						ccv2.Space{
   112  							GUID: "some-space-guid",
   113  							Name: "some-space",
   114  						},
   115  						ccv2.Warnings{"create-space-warning-1", "create-space-warning-2"},
   116  						nil)
   117  				})
   118  
   119  				It("should return the space and all its warnings", func() {
   120  					Expect(executeErr).ToNot(HaveOccurred())
   121  					spaceName, orgGuid := fakeCloudControllerClient.CreateSpaceArgsForCall(0)
   122  					Expect(spaceName).To(Equal("some-space"))
   123  					Expect(orgGuid).To(Equal("some-org-guid"))
   124  
   125  					Expect(space).To(Equal(expectedSpace))
   126  					Expect(warnings).To(ConsistOf("create-space-warning-1", "create-space-warning-2"))
   127  				})
   128  
   129  				When("the quota is found", func() {
   130  					BeforeEach(func() {
   131  						spaceQuotaGuid := "space-quota-guid"
   132  						quotaName = "some-quota"
   133  						fakeCloudControllerClient.GetSpaceQuotasReturns(
   134  							[]ccv2.SpaceQuota{
   135  								ccv2.SpaceQuota{Name: quotaName, GUID: spaceQuotaGuid},
   136  							},
   137  							ccv2.Warnings{
   138  								"get-quota-warning-1",
   139  								"get-quota-warning-2",
   140  							},
   141  							nil,
   142  						)
   143  					})
   144  
   145  					It("sets the quota on the space", func() {
   146  						Expect(executeErr).ToNot(HaveOccurred())
   147  						Expect(warnings).To(ContainElement("get-quota-warning-1"))
   148  						Expect(warnings).To(ContainElement("get-quota-warning-2"))
   149  						Expect(fakeCloudControllerClient.SetSpaceQuotaCallCount()).To(Equal(1))
   150  					})
   151  				})
   152  			})
   153  
   154  			When("the space name is taken", func() {
   155  				BeforeEach(func() {
   156  					fakeCloudControllerClient.CreateSpaceReturns(ccv2.Space{}, ccv2.Warnings{"some-warning"}, ccerror.SpaceNameTakenError{
   157  						Message: "nice try",
   158  					})
   159  				})
   160  
   161  				It("returns a SpaceNameTakenError", func() {
   162  					Expect(executeErr).To(MatchError(actionerror.SpaceNameTakenError{Name: "some-space"}))
   163  					Expect(warnings).To(ConsistOf("some-warning"))
   164  				})
   165  			})
   166  		})
   167  	})
   168  
   169  	Describe("DeleteSpaceByNameAndOrganizationName", func() {
   170  		var (
   171  			warnings Warnings
   172  			err      error
   173  		)
   174  
   175  		JustBeforeEach(func() {
   176  			warnings, err = actor.DeleteSpaceByNameAndOrganizationName("some-space", "some-org")
   177  		})
   178  
   179  		When("the org is not found", func() {
   180  			BeforeEach(func() {
   181  				fakeCloudControllerClient.GetOrganizationsReturns(
   182  					[]ccv2.Organization{},
   183  					ccv2.Warnings{
   184  						"warning-1",
   185  						"warning-2",
   186  					},
   187  					nil,
   188  				)
   189  			})
   190  
   191  			It("returns an OrganizationNotFoundError", func() {
   192  				Expect(err).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"}))
   193  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   194  			})
   195  		})
   196  
   197  		When("the org is found", func() {
   198  			BeforeEach(func() {
   199  				fakeCloudControllerClient.GetOrganizationsReturns(
   200  					[]ccv2.Organization{{Name: "some-org", GUID: "some-org-guid"}},
   201  					ccv2.Warnings{"warning-1", "warning-2"},
   202  					nil,
   203  				)
   204  			})
   205  
   206  			When("the space is not found", func() {
   207  				BeforeEach(func() {
   208  					fakeCloudControllerClient.GetSpacesReturns(
   209  						[]ccv2.Space{},
   210  						ccv2.Warnings{"warning-3", "warning-4"},
   211  						nil,
   212  					)
   213  				})
   214  
   215  				It("returns an SpaceNotFoundError", func() {
   216  					Expect(err).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"}))
   217  					Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4"))
   218  				})
   219  			})
   220  
   221  			When("the space is found", func() {
   222  				BeforeEach(func() {
   223  					fakeCloudControllerClient.GetSpacesReturns(
   224  						[]ccv2.Space{{GUID: "some-space-guid"}},
   225  						ccv2.Warnings{"warning-3", "warning-4"},
   226  						nil,
   227  					)
   228  				})
   229  
   230  				When("the delete returns an error", func() {
   231  					var expectedErr error
   232  
   233  					BeforeEach(func() {
   234  						expectedErr = errors.New("some delete space error")
   235  						fakeCloudControllerClient.DeleteSpaceJobReturns(
   236  							ccv2.Job{},
   237  							ccv2.Warnings{"warning-5", "warning-6"},
   238  							expectedErr,
   239  						)
   240  					})
   241  
   242  					It("returns the error", func() {
   243  						Expect(err).To(Equal(expectedErr))
   244  						Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4", "warning-5", "warning-6"))
   245  					})
   246  				})
   247  
   248  				When("the delete returns a job", func() {
   249  					BeforeEach(func() {
   250  						fakeCloudControllerClient.DeleteSpaceJobReturns(
   251  							ccv2.Job{GUID: "some-job-guid"},
   252  							ccv2.Warnings{"warning-5", "warning-6"},
   253  							nil,
   254  						)
   255  					})
   256  
   257  					When("polling errors", func() {
   258  						var expectedErr error
   259  
   260  						BeforeEach(func() {
   261  							expectedErr = errors.New("Never expected, by anyone")
   262  							fakeCloudControllerClient.PollJobReturns(ccv2.Warnings{"warning-7", "warning-8"}, expectedErr)
   263  						})
   264  
   265  						It("returns the error", func() {
   266  							Expect(err).To(Equal(expectedErr))
   267  							Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4", "warning-5", "warning-6", "warning-7", "warning-8"))
   268  						})
   269  					})
   270  
   271  					When("the job is successful", func() {
   272  						BeforeEach(func() {
   273  							fakeCloudControllerClient.PollJobReturns(ccv2.Warnings{"warning-7", "warning-8"}, nil)
   274  						})
   275  
   276  						It("returns warnings and no error", func() {
   277  							Expect(err).ToNot(HaveOccurred())
   278  							Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4", "warning-5", "warning-6", "warning-7", "warning-8"))
   279  
   280  							Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   281  							Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(Equal([]ccv2.Filter{{
   282  								Type:     constant.NameFilter,
   283  								Operator: constant.EqualOperator,
   284  								Values:   []string{"some-org"},
   285  							}}))
   286  
   287  							Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   288  							Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(Equal([]ccv2.Filter{{
   289  								Type:     constant.NameFilter,
   290  								Operator: constant.EqualOperator,
   291  								Values:   []string{"some-space"},
   292  							},
   293  								{
   294  									Type:     constant.OrganizationGUIDFilter,
   295  									Operator: constant.EqualOperator,
   296  									Values:   []string{"some-org-guid"},
   297  								},
   298  							}))
   299  
   300  							Expect(fakeCloudControllerClient.DeleteSpaceJobCallCount()).To(Equal(1))
   301  							Expect(fakeCloudControllerClient.DeleteSpaceJobArgsForCall(0)).To(Equal("some-space-guid"))
   302  
   303  							Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1))
   304  							Expect(fakeCloudControllerClient.PollJobArgsForCall(0)).To(Equal(ccv2.Job{GUID: "some-job-guid"}))
   305  						})
   306  					})
   307  				})
   308  			})
   309  		})
   310  	})
   311  
   312  	Describe("GetOrganizationSpaces", func() {
   313  		When("there are spaces in the org", func() {
   314  			BeforeEach(func() {
   315  				fakeCloudControllerClient.GetSpacesReturns(
   316  					[]ccv2.Space{
   317  						{
   318  							GUID:                     "space-1-guid",
   319  							Name:                     "space-1",
   320  							AllowSSH:                 true,
   321  							SpaceQuotaDefinitionGUID: "some-space-quota-guid",
   322  						},
   323  						{
   324  							GUID:     "space-2-guid",
   325  							Name:     "space-2",
   326  							AllowSSH: false,
   327  						},
   328  					},
   329  					ccv2.Warnings{"warning-1", "warning-2"},
   330  					nil)
   331  			})
   332  
   333  			It("returns all spaces and all warnings", func() {
   334  				spaces, warnings, err := actor.GetOrganizationSpaces("some-org-guid")
   335  
   336  				Expect(err).ToNot(HaveOccurred())
   337  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   338  				Expect(spaces).To(Equal(
   339  					[]Space{
   340  						{
   341  							GUID:                     "space-1-guid",
   342  							Name:                     "space-1",
   343  							AllowSSH:                 true,
   344  							SpaceQuotaDefinitionGUID: "some-space-quota-guid",
   345  						},
   346  						{
   347  							GUID:     "space-2-guid",
   348  							Name:     "space-2",
   349  							AllowSSH: false,
   350  						},
   351  					}))
   352  
   353  				Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   354  				Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(Equal(
   355  					[]ccv2.Filter{
   356  						{
   357  							Type:     constant.OrganizationGUIDFilter,
   358  							Operator: constant.EqualOperator,
   359  							Values:   []string{"some-org-guid"},
   360  						},
   361  					}))
   362  			})
   363  		})
   364  
   365  		When("an error is encountered", func() {
   366  			var returnedErr error
   367  
   368  			BeforeEach(func() {
   369  				returnedErr = errors.New("cc-get-spaces-error")
   370  				fakeCloudControllerClient.GetSpacesReturns(
   371  					[]ccv2.Space{},
   372  					ccv2.Warnings{"warning-1", "warning-2"},
   373  					returnedErr,
   374  				)
   375  			})
   376  
   377  			It("returns the error and all warnings", func() {
   378  				_, warnings, err := actor.GetOrganizationSpaces("some-org-guid")
   379  
   380  				Expect(err).To(MatchError(returnedErr))
   381  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   382  			})
   383  		})
   384  	})
   385  
   386  	Describe("GetSpaceByOrganizationAndName", func() {
   387  		var (
   388  			space      Space
   389  			warnings   Warnings
   390  			executeErr error
   391  		)
   392  
   393  		JustBeforeEach(func() {
   394  			space, warnings, executeErr = actor.GetSpaceByOrganizationAndName("some-org-guid", "some-space")
   395  		})
   396  
   397  		When("the space exists", func() {
   398  			BeforeEach(func() {
   399  				fakeCloudControllerClient.GetSpacesReturns(
   400  					[]ccv2.Space{
   401  						{
   402  							GUID:                     "some-space-guid",
   403  							Name:                     "some-space",
   404  							AllowSSH:                 true,
   405  							SpaceQuotaDefinitionGUID: "some-space-quota-guid",
   406  						},
   407  					},
   408  					ccv2.Warnings{"warning-1", "warning-2"},
   409  					nil)
   410  			})
   411  
   412  			It("returns the space and all warnings", func() {
   413  				Expect(executeErr).ToNot(HaveOccurred())
   414  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   415  				Expect(space).To(Equal(Space{
   416  					GUID:                     "some-space-guid",
   417  					Name:                     "some-space",
   418  					AllowSSH:                 true,
   419  					SpaceQuotaDefinitionGUID: "some-space-quota-guid",
   420  				}))
   421  
   422  				Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   423  				Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(ConsistOf(
   424  					[]ccv2.Filter{
   425  						{
   426  							Type:     constant.OrganizationGUIDFilter,
   427  							Operator: constant.EqualOperator,
   428  							Values:   []string{"some-org-guid"},
   429  						},
   430  						{
   431  							Type:     constant.NameFilter,
   432  							Operator: constant.EqualOperator,
   433  							Values:   []string{"some-space"},
   434  						},
   435  					}))
   436  			})
   437  		})
   438  
   439  		When("an error is encountered", func() {
   440  			var returnedErr error
   441  
   442  			BeforeEach(func() {
   443  				returnedErr = errors.New("cc-get-spaces-error")
   444  				fakeCloudControllerClient.GetSpacesReturns(
   445  					[]ccv2.Space{},
   446  					ccv2.Warnings{"warning-1", "warning-2"},
   447  					returnedErr,
   448  				)
   449  			})
   450  
   451  			It("return the error and all warnings", func() {
   452  				Expect(executeErr).To(MatchError(returnedErr))
   453  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   454  			})
   455  		})
   456  
   457  		When("the space does not exist", func() {
   458  			BeforeEach(func() {
   459  				fakeCloudControllerClient.GetSpacesReturns(
   460  					[]ccv2.Space{},
   461  					nil,
   462  					nil,
   463  				)
   464  			})
   465  
   466  			It("returns SpaceNotFoundError", func() {
   467  				Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{
   468  					Name: "some-space",
   469  				}))
   470  			})
   471  		})
   472  
   473  		When("multiple spaces exists", func() {
   474  			BeforeEach(func() {
   475  				fakeCloudControllerClient.GetSpacesReturns(
   476  					[]ccv2.Space{
   477  						{
   478  							GUID:     "some-space-guid",
   479  							Name:     "some-space",
   480  							AllowSSH: true,
   481  						},
   482  						{
   483  							GUID:     "another-space-guid",
   484  							Name:     "another-space",
   485  							AllowSSH: true,
   486  						},
   487  					},
   488  					nil,
   489  					nil,
   490  				)
   491  			})
   492  
   493  			It("returns MultipleSpacesFoundError", func() {
   494  				Expect(executeErr).To(MatchError(actionerror.MultipleSpacesFoundError{
   495  					OrgGUID: "some-org-guid",
   496  					Name:    "some-space",
   497  				}))
   498  			})
   499  		})
   500  	})
   501  
   502  	Describe("GrantSpaceManagerByUsername", func() {
   503  		var (
   504  			warnings   Warnings
   505  			executeErr error
   506  		)
   507  
   508  		JustBeforeEach(func() {
   509  			warnings, executeErr = actor.GrantSpaceManagerByUsername("some-org-guid", "some-space-guid", "some-username")
   510  		})
   511  
   512  		When("acting as a user", func() {
   513  			When("ensuring the user is a member of the org succeeds", func() {
   514  				When("the cloud controller returns with success", func() {
   515  					BeforeEach(func() {
   516  						fakeCloudControllerClient.UpdateSpaceManagerByUsernameReturns(
   517  							ccv2.Warnings{"warning-1", "warning-2"},
   518  							nil,
   519  						)
   520  						fakeCloudControllerClient.UpdateOrganizationUserByUsernameReturns(ccv2.Warnings{"org-user-warning"}, nil)
   521  					})
   522  
   523  					It("returns all the warnings", func() {
   524  						Expect(executeErr).NotTo(HaveOccurred())
   525  						Expect(warnings).To(ConsistOf("warning-1", "warning-2", "org-user-warning"))
   526  					})
   527  
   528  					It("makes the user a member of the org", func() {
   529  						Expect(fakeCloudControllerClient.UpdateOrganizationUserByUsernameCallCount()).To(Equal(1))
   530  						orgGUID, username := fakeCloudControllerClient.UpdateOrganizationUserByUsernameArgsForCall(0)
   531  						Expect(orgGUID).To(Equal("some-org-guid"))
   532  						Expect(username).To(Equal("some-username"))
   533  					})
   534  
   535  					It("makes the user a space manager in the requested space", func() {
   536  						Expect(fakeCloudControllerClient.UpdateSpaceManagerByUsernameCallCount()).To(Equal(1))
   537  					})
   538  				})
   539  			})
   540  
   541  			When("ensuring the user is a member of the org fails", func() {
   542  				BeforeEach(func() {
   543  					fakeCloudControllerClient.UpdateOrganizationUserByUsernameReturns(
   544  						ccv2.Warnings{"some org user warning"},
   545  						errors.New("could not add user to org"),
   546  					)
   547  				})
   548  
   549  				It("returns the error and warnings", func() {
   550  					Expect(warnings).To(ConsistOf("some org user warning"))
   551  					Expect(executeErr).To(MatchError("could not add user to org"))
   552  				})
   553  
   554  				It("does not make the user a space manager", func() {
   555  					Expect(fakeCloudControllerClient.UpdateSpaceManagerByUsernameCallCount()).To(Equal(0))
   556  				})
   557  			})
   558  
   559  			When("making the user a space manager errors", func() {
   560  				var returnedErr error
   561  
   562  				BeforeEach(func() {
   563  					returnedErr = errors.New("cc-grant-space-manager-error")
   564  					fakeCloudControllerClient.UpdateSpaceManagerByUsernameReturns(
   565  						ccv2.Warnings{"warning-1", "warning-2"},
   566  						returnedErr,
   567  					)
   568  				})
   569  
   570  				It("returns the error and all the warnings", func() {
   571  					Expect(executeErr).To(MatchError(returnedErr))
   572  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   573  				})
   574  			})
   575  
   576  			It("does not treat the username as a guid", func() {
   577  				Expect(fakeCloudControllerClient.UpdateOrganizationUserCallCount()).To(Equal(0))
   578  				Expect(fakeCloudControllerClient.UpdateSpaceManagerCallCount()).To(Equal(0))
   579  			})
   580  		})
   581  
   582  		When("acting as a client", func() {
   583  			BeforeEach(func() {
   584  				fakeConfig.UAAGrantTypeReturns(string(uaaconst.GrantTypeClientCredentials))
   585  			})
   586  
   587  			When("ensuring the client is a member of the org succeeds", func() {
   588  				When("the cloud controller returns with success", func() {
   589  					BeforeEach(func() {
   590  						fakeCloudControllerClient.UpdateOrganizationUserReturns(ccv2.Warnings{"org-user-warning"}, nil)
   591  						fakeCloudControllerClient.UpdateSpaceManagerReturns(
   592  							ccv2.Warnings{"warning-1", "warning-2"},
   593  							nil,
   594  						)
   595  					})
   596  
   597  					It("returns all the warnings", func() {
   598  						Expect(executeErr).NotTo(HaveOccurred())
   599  						Expect(warnings).To(ConsistOf("warning-1", "warning-2", "org-user-warning"))
   600  					})
   601  
   602  					It("makes the client a member of the org", func() {
   603  						Expect(fakeCloudControllerClient.UpdateOrganizationUserCallCount()).To(Equal(1))
   604  						orgGUID, username := fakeCloudControllerClient.UpdateOrganizationUserArgsForCall(0)
   605  						Expect(orgGUID).To(Equal("some-org-guid"))
   606  						Expect(username).To(Equal("some-username"))
   607  					})
   608  
   609  					It("makes the user a space manager for the requested space", func() {
   610  						Expect(fakeCloudControllerClient.UpdateSpaceManagerCallCount()).To(Equal(1))
   611  						spaceGUID, clientID := fakeCloudControllerClient.UpdateSpaceManagerArgsForCall(0)
   612  						Expect(spaceGUID).To(Equal("some-space-guid"))
   613  						Expect(clientID).To(Equal("some-username"))
   614  					})
   615  				})
   616  			})
   617  
   618  			When("ensuring the client is a member of the org fails", func() {
   619  				BeforeEach(func() {
   620  					fakeCloudControllerClient.UpdateOrganizationUserReturns(
   621  						ccv2.Warnings{"some org user warning"},
   622  						errors.New("ya done goofed"),
   623  					)
   624  				})
   625  
   626  				It("returns the error and warnings", func() {
   627  					Expect(warnings).To(ConsistOf("some org user warning"))
   628  					Expect(executeErr).To(MatchError("ya done goofed"))
   629  				})
   630  
   631  				It("does not make the user a space manager", func() {
   632  					Expect(fakeCloudControllerClient.UpdateSpaceManagerCallCount()).To(Equal(0))
   633  				})
   634  			})
   635  
   636  			When("making the client a space manager errors", func() {
   637  				var returnedErr error
   638  
   639  				BeforeEach(func() {
   640  					fakeCloudControllerClient.UpdateOrganizationUserReturns(
   641  						ccv2.Warnings{"some org user warning"},
   642  						nil,
   643  					)
   644  
   645  					returnedErr = errors.New("cc-grant-space-manager-error")
   646  					fakeCloudControllerClient.UpdateSpaceManagerReturns(
   647  						ccv2.Warnings{"warning-1", "warning-2"},
   648  						returnedErr,
   649  					)
   650  				})
   651  
   652  				It("returns the error and all the warnings", func() {
   653  					Expect(executeErr).To(MatchError(returnedErr))
   654  					Expect(warnings).To(ConsistOf("warning-1", "warning-2", "some org user warning"))
   655  				})
   656  			})
   657  
   658  			It("does not treat the client ID as a username", func() {
   659  				Expect(fakeCloudControllerClient.UpdateOrganizationUserByUsernameCallCount()).To(Equal(0))
   660  				Expect(fakeCloudControllerClient.UpdateSpaceManagerByUsernameCallCount()).To(Equal(0))
   661  			})
   662  		})
   663  	})
   664  
   665  	Describe("GrantSpaceDeveloperByUsername", func() {
   666  		var (
   667  			warnings   Warnings
   668  			executeErr error
   669  		)
   670  
   671  		JustBeforeEach(func() {
   672  			warnings, executeErr = actor.GrantSpaceDeveloperByUsername("some-space-guid", "some-username")
   673  		})
   674  
   675  		When("acting as a user", func() {
   676  			It("attempts to add the user to the space developers list", func() {
   677  				Expect(fakeCloudControllerClient.UpdateSpaceDeveloperByUsernameCallCount()).To(Equal(1))
   678  				spaceGUID, username := fakeCloudControllerClient.UpdateSpaceDeveloperByUsernameArgsForCall(0)
   679  				Expect(spaceGUID).To(Equal("some-space-guid"))
   680  				Expect(username).To(Equal("some-username"))
   681  			})
   682  
   683  			When("the request is successful", func() {
   684  				BeforeEach(func() {
   685  					fakeCloudControllerClient.UpdateSpaceDeveloperByUsernameReturns(
   686  						ccv2.Warnings{"warning-1", "warning-2"},
   687  						nil,
   688  					)
   689  				})
   690  
   691  				It("returns all warnings", func() {
   692  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   693  				})
   694  			})
   695  
   696  			When("the request fails", func() {
   697  				BeforeEach(func() {
   698  					fakeCloudControllerClient.UpdateSpaceDeveloperByUsernameReturns(
   699  						ccv2.Warnings{"warning-1", "warning-2"},
   700  						errors.New("the request failed"),
   701  					)
   702  				})
   703  
   704  				It("return the error", func() {
   705  					Expect(executeErr).To(MatchError("the request failed"))
   706  				})
   707  
   708  				It("returns all warnings", func() {
   709  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   710  				})
   711  			})
   712  		})
   713  
   714  		When("acting as a client", func() {
   715  			BeforeEach(func() {
   716  				fakeConfig.UAAGrantTypeReturns(string(uaaconst.GrantTypeClientCredentials))
   717  			})
   718  
   719  			It("attempts to add the client to the space developers list", func() {
   720  				Expect(fakeCloudControllerClient.UpdateSpaceDeveloperCallCount()).To(Equal(1))
   721  				spaceGUID, username := fakeCloudControllerClient.UpdateSpaceDeveloperArgsForCall(0)
   722  				Expect(spaceGUID).To(Equal("some-space-guid"))
   723  				Expect(username).To(Equal("some-username"))
   724  			})
   725  
   726  			It("does not treat the client ID as a username", func() {
   727  				Expect(fakeCloudControllerClient.UpdateSpaceDeveloperByUsernameCallCount()).To(Equal(0))
   728  			})
   729  
   730  			When("making the user a space developer succeeds", func() {
   731  				BeforeEach(func() {
   732  					fakeCloudControllerClient.UpdateSpaceDeveloperReturns(
   733  						ccv2.Warnings{"warning-1", "warning-2"},
   734  						nil,
   735  					)
   736  				})
   737  
   738  				It("returns all the warnings", func() {
   739  					Expect(executeErr).NotTo(HaveOccurred())
   740  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   741  				})
   742  			})
   743  
   744  			When("making the user a space developer fails", func() {
   745  				BeforeEach(func() {
   746  					fakeCloudControllerClient.UpdateSpaceDeveloperReturns(
   747  						ccv2.Warnings{"warning-1", "warning-2"},
   748  						errors.New("boom!"),
   749  					)
   750  				})
   751  
   752  				It("returns all the warnings", func() {
   753  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   754  				})
   755  
   756  				It("returns the error", func() {
   757  					Expect(executeErr).To(MatchError("boom!"))
   758  				})
   759  			})
   760  		})
   761  	})
   762  })