github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv2/organization_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("Organization", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client = NewTestClient()
    19  	})
    20  
    21  	Describe("CreateOrganization", func() {
    22  		var (
    23  			org        Organization
    24  			warnings   Warnings
    25  			executeErr error
    26  		)
    27  
    28  		JustBeforeEach(func() {
    29  			org, warnings, executeErr = client.CreateOrganization("some-org", "some-quota-guid")
    30  		})
    31  
    32  		When("the organization exists", func() {
    33  			BeforeEach(func() {
    34  				response := `{
    35  					"metadata": {
    36  						"guid": "some-org-guid"
    37  					},
    38  					"entity": {
    39  						"name": "some-org",
    40  						"quota_definition_guid": "some-quota-guid"
    41  					}
    42  				}`
    43  				requestBody := map[string]interface{}{
    44  					"name":                  "some-org",
    45  					"quota_definition_guid": "some-quota-guid",
    46  				}
    47  
    48  				server.AppendHandlers(
    49  					CombineHandlers(
    50  						VerifyRequest(http.MethodPost, "/v2/organizations"),
    51  						VerifyJSONRepresenting(requestBody),
    52  						RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
    53  					))
    54  			})
    55  
    56  			It("returns the org and all warnings", func() {
    57  				Expect(executeErr).NotTo(HaveOccurred())
    58  				Expect(org).To(Equal(
    59  					Organization{
    60  						GUID:                "some-org-guid",
    61  						Name:                "some-org",
    62  						QuotaDefinitionGUID: "some-quota-guid",
    63  					},
    64  				))
    65  				Expect(warnings).To(ConsistOf("warning-1"))
    66  			})
    67  		})
    68  	})
    69  
    70  	Describe("DeleteOrganization", func() {
    71  		When("no errors are encountered", func() {
    72  			BeforeEach(func() {
    73  				jsonResponse := `{
    74  				"metadata": {
    75  					"guid": "job-guid",
    76  					"created_at": "2016-06-08T16:41:27Z",
    77  					"url": "/v2/jobs/job-guid"
    78  				},
    79  				"entity": {
    80  					"guid": "job-guid",
    81  					"status": "queued"
    82  				}
    83  			}`
    84  
    85  				server.AppendHandlers(
    86  					CombineHandlers(
    87  						VerifyRequest(http.MethodDelete, "/v2/organizations/some-organization-guid", "recursive=true&async=true"),
    88  						RespondWith(http.StatusAccepted, jsonResponse, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
    89  					))
    90  			})
    91  
    92  			It("deletes the Organization and returns all warnings", func() {
    93  				job, warnings, err := client.DeleteOrganization("some-organization-guid")
    94  
    95  				Expect(err).NotTo(HaveOccurred())
    96  				Expect(warnings).To(ConsistOf(Warnings{"warning-1", "warning-2"}))
    97  				Expect(job.GUID).To(Equal("job-guid"))
    98  				Expect(job.Status).To(Equal(constant.JobStatusQueued))
    99  			})
   100  		})
   101  
   102  		When("an error is encountered", func() {
   103  			BeforeEach(func() {
   104  				response := `{
   105  "code": 30003,
   106  "description": "The Organization could not be found: some-organization-guid",
   107  "error_code": "CF-OrganizationNotFound"
   108  }`
   109  				server.AppendHandlers(
   110  					CombineHandlers(
   111  						VerifyRequest(http.MethodDelete, "/v2/organizations/some-organization-guid", "recursive=true&async=true"),
   112  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   113  					))
   114  			})
   115  
   116  			It("returns an error and all warnings", func() {
   117  				_, warnings, err := client.DeleteOrganization("some-organization-guid")
   118  
   119  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
   120  					Message: "The Organization could not be found: some-organization-guid",
   121  				}))
   122  				Expect(warnings).To(ConsistOf(Warnings{"warning-1", "warning-2"}))
   123  			})
   124  		})
   125  	})
   126  
   127  	Describe("GetOrganization", func() {
   128  		When("the organization exists", func() {
   129  			BeforeEach(func() {
   130  				response := `{
   131  					"metadata": {
   132  						"guid": "some-org-guid"
   133  					},
   134  					"entity": {
   135  						"name": "some-org",
   136  						"quota_definition_guid": "some-quota-guid"
   137  					}
   138  				}`
   139  				server.AppendHandlers(
   140  					CombineHandlers(
   141  						VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid"),
   142  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   143  					))
   144  			})
   145  
   146  			It("returns the org and all warnings", func() {
   147  				orgs, warnings, err := client.GetOrganization("some-org-guid")
   148  
   149  				Expect(err).NotTo(HaveOccurred())
   150  				Expect(orgs).To(Equal(
   151  					Organization{
   152  						GUID:                "some-org-guid",
   153  						Name:                "some-org",
   154  						QuotaDefinitionGUID: "some-quota-guid",
   155  					},
   156  				))
   157  				Expect(warnings).To(ConsistOf("warning-1"))
   158  			})
   159  		})
   160  
   161  		When("an error is encountered", func() {
   162  			BeforeEach(func() {
   163  				response := `{
   164  					"code": 10001,
   165  					"description": "Some Error",
   166  					"error_code": "CF-SomeError"
   167  				}`
   168  				server.AppendHandlers(
   169  					CombineHandlers(
   170  						VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid"),
   171  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   172  					))
   173  			})
   174  
   175  			It("returns an error and all warnings", func() {
   176  				_, warnings, err := client.GetOrganization("some-org-guid")
   177  
   178  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   179  					ResponseCode: http.StatusTeapot,
   180  					V2ErrorResponse: ccerror.V2ErrorResponse{
   181  						Code:        10001,
   182  						Description: "Some Error",
   183  						ErrorCode:   "CF-SomeError",
   184  					},
   185  				}))
   186  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   187  			})
   188  		})
   189  	})
   190  
   191  	Describe("GetOrganizations", func() {
   192  		When("no errors are encountered", func() {
   193  			When("results are paginated", func() {
   194  				BeforeEach(func() {
   195  					response1 := `{
   196  					"next_url": "/v2/organizations?q=some-query:some-value&page=2&order-by=name",
   197  					"resources": [
   198  						{
   199  							"metadata": {
   200  								"guid": "org-guid-1"
   201  							},
   202  							"entity": {
   203  								"name": "org-1",
   204  								"quota_definition_guid": "some-quota-guid",
   205  								"default_isolation_segment_guid": "some-default-isolation-segment-guid"
   206  							}
   207  						},
   208  						{
   209  							"metadata": {
   210  								"guid": "org-guid-2"
   211  							},
   212  							"entity": {
   213  								"name": "org-2",
   214  								"quota_definition_guid": "some-quota-guid"
   215  							}
   216  						}
   217  					]
   218  				}`
   219  					response2 := `{
   220  					"next_url": null,
   221  					"resources": [
   222  						{
   223  							"metadata": {
   224  								"guid": "org-guid-3"
   225  							},
   226  							"entity": {
   227  								"name": "org-3",
   228  								"quota_definition_guid": "some-quota-guid"
   229  							}
   230  						},
   231  						{
   232  							"metadata": {
   233  								"guid": "org-guid-4"
   234  							},
   235  							"entity": {
   236  								"name": "org-4",
   237  								"quota_definition_guid": "some-quota-guid"
   238  							}
   239  						}
   240  					]
   241  				}`
   242  					server.AppendHandlers(
   243  						CombineHandlers(
   244  							VerifyRequest(http.MethodGet, "/v2/organizations", "q=some-query:some-value&order-by=name"),
   245  							RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   246  						))
   247  					server.AppendHandlers(
   248  						CombineHandlers(
   249  							VerifyRequest(http.MethodGet, "/v2/organizations", "q=some-query:some-value&page=2&order-by=name"),
   250  							RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}),
   251  						))
   252  				})
   253  
   254  				It("returns paginated results and all warnings", func() {
   255  					orgs, warnings, err := client.GetOrganizations(Filter{
   256  						Type:     "some-query",
   257  						Operator: constant.EqualOperator,
   258  						Values:   []string{"some-value"},
   259  					})
   260  
   261  					Expect(err).NotTo(HaveOccurred())
   262  					Expect(orgs).To(Equal([]Organization{
   263  						{
   264  							GUID:                        "org-guid-1",
   265  							Name:                        "org-1",
   266  							QuotaDefinitionGUID:         "some-quota-guid",
   267  							DefaultIsolationSegmentGUID: "some-default-isolation-segment-guid",
   268  						},
   269  						{
   270  							GUID:                        "org-guid-2",
   271  							Name:                        "org-2",
   272  							QuotaDefinitionGUID:         "some-quota-guid",
   273  							DefaultIsolationSegmentGUID: "",
   274  						},
   275  						{
   276  							GUID:                        "org-guid-3",
   277  							Name:                        "org-3",
   278  							QuotaDefinitionGUID:         "some-quota-guid",
   279  							DefaultIsolationSegmentGUID: "",
   280  						},
   281  						{
   282  							GUID:                        "org-guid-4",
   283  							Name:                        "org-4",
   284  							QuotaDefinitionGUID:         "some-quota-guid",
   285  							DefaultIsolationSegmentGUID: "",
   286  						},
   287  					}))
   288  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   289  				})
   290  			})
   291  		})
   292  
   293  		When("an error is encountered", func() {
   294  			BeforeEach(func() {
   295  				response := `{
   296    "code": 10001,
   297    "description": "Some Error",
   298    "error_code": "CF-SomeError"
   299  }`
   300  				server.AppendHandlers(
   301  					CombineHandlers(
   302  						VerifyRequest(http.MethodGet, "/v2/organizations", "order-by=name"),
   303  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   304  					))
   305  			})
   306  
   307  			It("returns an error and all warnings", func() {
   308  				_, warnings, err := client.GetOrganizations()
   309  
   310  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   311  					ResponseCode: http.StatusTeapot,
   312  					V2ErrorResponse: ccerror.V2ErrorResponse{
   313  						Code:        10001,
   314  						Description: "Some Error",
   315  						ErrorCode:   "CF-SomeError",
   316  					},
   317  				}))
   318  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   319  			})
   320  		})
   321  	})
   322  
   323  	Describe("UpdateOrganizationManagerByUsername", func() {
   324  		Context("when the organization exists", func() {
   325  			var (
   326  				warnings Warnings
   327  				err      error
   328  			)
   329  
   330  			BeforeEach(func() {
   331  				expectedRequest := `{
   332  					"username": "some-user"
   333  				}`
   334  
   335  				response := `{
   336  					"metadata": {
   337  						"guid": "some-org-guid"
   338  					},
   339  					"entity": {
   340  						"name": "some-org",
   341  						"quota_definition_guid": "some-quota-guid"
   342  					}
   343  				}`
   344  
   345  				server.AppendHandlers(
   346  					CombineHandlers(
   347  						VerifyRequest(http.MethodPut, "/v2/organizations/some-org-guid/managers"),
   348  						VerifyJSON(expectedRequest),
   349  						RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   350  					))
   351  			})
   352  
   353  			JustBeforeEach(func() {
   354  				warnings, err = client.UpdateOrganizationManagerByUsername("some-org-guid", "some-user")
   355  			})
   356  
   357  			It("returns warnings", func() {
   358  				Expect(err).ToNot(HaveOccurred())
   359  				Expect(warnings).To(ConsistOf("warning-1"))
   360  			})
   361  		})
   362  	})
   363  
   364  	Describe("UpdateOrganizationManager", func() {
   365  		var (
   366  			warnings Warnings
   367  			err      error
   368  		)
   369  
   370  		JustBeforeEach(func() {
   371  			warnings, err = client.UpdateOrganizationManager("some-org-guid", "some-manager-guid")
   372  		})
   373  
   374  		When("the organization exists", func() {
   375  			BeforeEach(func() {
   376  				response := `{
   377  					"metadata": {
   378  						"guid": "some-org-guid"
   379  					},
   380  					"entity": {
   381  						"name": "some-org",
   382  					}
   383  				}`
   384  
   385  				server.AppendHandlers(
   386  					CombineHandlers(
   387  						VerifyRequest(http.MethodPut, "/v2/organizations/some-org-guid/managers/some-manager-guid"),
   388  						RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   389  					))
   390  			})
   391  
   392  			It("returns warnings", func() {
   393  				Expect(err).ToNot(HaveOccurred())
   394  				Expect(warnings).To(ConsistOf("warning-1"))
   395  			})
   396  		})
   397  
   398  		When("the server returns an error", func() {
   399  			BeforeEach(func() {
   400  				response := `{
   401  					"code": 10001,
   402  					"description": "Some Error",
   403  					"error_code": "CF-SomeError"
   404  				}`
   405  
   406  				server.AppendHandlers(
   407  					CombineHandlers(
   408  						VerifyRequest(http.MethodPut, "/v2/organizations/some-org-guid/managers/some-manager-guid"),
   409  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   410  					))
   411  			})
   412  
   413  			It("returns the error and any warnings", func() {
   414  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   415  					ResponseCode: http.StatusTeapot,
   416  					V2ErrorResponse: ccerror.V2ErrorResponse{
   417  						Code:        10001,
   418  						Description: "Some Error",
   419  						ErrorCode:   "CF-SomeError",
   420  					},
   421  				}))
   422  				Expect(warnings).To(ConsistOf("warning-1"))
   423  			})
   424  		})
   425  	})
   426  
   427  	Describe("UpdateOrganizationUser", func() {
   428  		var (
   429  			warnings   Warnings
   430  			executeErr error
   431  		)
   432  
   433  		JustBeforeEach(func() {
   434  			warnings, executeErr = client.UpdateOrganizationUser("some-org-guid", "some-uaa-id")
   435  		})
   436  
   437  		When("the request succeeds", func() {
   438  			BeforeEach(func() {
   439  				server.AppendHandlers(
   440  					CombineHandlers(
   441  						VerifyRequest(http.MethodPut, "/v2/organizations/some-org-guid/users/some-uaa-id"),
   442  						RespondWith(http.StatusCreated, "", http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   443  					))
   444  			})
   445  
   446  			It("returns warnings", func() {
   447  				Expect(executeErr).NotTo(HaveOccurred())
   448  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   449  			})
   450  		})
   451  
   452  		When("the server returns an error", func() {
   453  			BeforeEach(func() {
   454  				response := `{
   455  					"code": 10001,
   456  					"description": "Some Error",
   457  					"error_code": "CF-SomeError"
   458  				}`
   459  
   460  				server.AppendHandlers(
   461  					CombineHandlers(
   462  						VerifyRequest(http.MethodPut, "/v2/organizations/some-org-guid/users/some-uaa-id"),
   463  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   464  					))
   465  			})
   466  
   467  			It("returns warnings", func() {
   468  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   469  			})
   470  
   471  			It("returns the error", func() {
   472  				Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{
   473  					ResponseCode: http.StatusTeapot,
   474  					V2ErrorResponse: ccerror.V2ErrorResponse{
   475  						Code:        10001,
   476  						Description: "Some Error",
   477  						ErrorCode:   "CF-SomeError",
   478  					},
   479  				}))
   480  			})
   481  		})
   482  	})
   483  	Describe("UpdateOrganizationUserByUsername", func() {
   484  		var (
   485  			warnings Warnings
   486  			err      error
   487  		)
   488  
   489  		JustBeforeEach(func() {
   490  			warnings, err = client.UpdateOrganizationUserByUsername("some-org-guid", "some-user")
   491  		})
   492  
   493  		When("the request succeeds", func() {
   494  			BeforeEach(func() {
   495  				expectedRequest := `{
   496  					"username": "some-user"
   497  				}`
   498  
   499  				response := `{
   500  					"metadata": {
   501  						"guid": "some-org-guid"
   502  					},
   503  					"entity": {
   504  						"name": "some-org",
   505  						"quota_definition_guid": "some-quota-guid"
   506  					}
   507  				}`
   508  
   509  				server.AppendHandlers(
   510  					CombineHandlers(
   511  						VerifyRequest(http.MethodPut, "/v2/organizations/some-org-guid/users"),
   512  						VerifyJSON(expectedRequest),
   513  						RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   514  					))
   515  			})
   516  
   517  			It("returns warnings", func() {
   518  				Expect(err).ToNot(HaveOccurred())
   519  				Expect(warnings).To(ConsistOf("warning-1"))
   520  			})
   521  		})
   522  
   523  		When("the server returns an error", func() {
   524  			BeforeEach(func() {
   525  				response := `{
   526  					"code": 10001,
   527  					"description": "Some Error",
   528  					"error_code": "CF-SomeError"
   529  				}`
   530  
   531  				server.AppendHandlers(
   532  					CombineHandlers(
   533  						VerifyRequest(http.MethodPut, "/v2/organizations/some-org-guid/users"),
   534  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   535  					))
   536  			})
   537  
   538  			It("returns warnings", func() {
   539  				Expect(warnings).To(ConsistOf("warning-1"))
   540  			})
   541  
   542  			It("returns the error", func() {
   543  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   544  					ResponseCode: http.StatusTeapot,
   545  					V2ErrorResponse: ccerror.V2ErrorResponse{
   546  						Code:        10001,
   547  						Description: "Some Error",
   548  						ErrorCode:   "CF-SomeError",
   549  					},
   550  				}))
   551  			})
   552  		})
   553  	})
   554  })