github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv3/organization_test.go (about)

     1  package ccv3_test
     2  
     3  // import (
     4  // 	"fmt"
     5  // 	"net/http"
     6  
     7  // 	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  // 	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  // 	"code.cloudfoundry.org/cli/types"
    10  // 	. "github.com/onsi/ginkgo"
    11  // 	. "github.com/onsi/gomega"
    12  // 	. "github.com/onsi/gomega/ghttp"
    13  // )
    14  
    15  // var _ = Describe("Organizations", func() {
    16  // 	var client *Client
    17  
    18  // 	BeforeEach(func() {
    19  // 		client, _ = NewTestClient()
    20  // 	})
    21  
    22  // 	Describe("GetIsolationSegmentOrganizations", func() {
    23  // 		var (
    24  // 			organizations []resources.Organization
    25  // 			warnings      Warnings
    26  // 			executeErr    error
    27  // 		)
    28  
    29  // 		JustBeforeEach(func() {
    30  // 			organizations, warnings, executeErr = client.GetIsolationSegmentOrganizations("some-iso-guid")
    31  // 		})
    32  
    33  // 		When("organizations exist", func() {
    34  // 			BeforeEach(func() {
    35  // 				response1 := fmt.Sprintf(`{
    36  // 	"pagination": {
    37  // 		"next": {
    38  // 			"href": "%s/v3/isolation_segments/some-iso-guid/organizations?page=2&per_page=2"
    39  // 		}
    40  // 	},
    41  //   "resources": [
    42  //     {
    43  //       "name": "org-name-1",
    44  //       "guid": "org-guid-1"
    45  //     },
    46  //     {
    47  //       "name": "org-name-2",
    48  //       "guid": "org-guid-2"
    49  //     }
    50  //   ]
    51  // }`, server.URL())
    52  // 				response2 := `{
    53  // 	"pagination": {
    54  // 		"next": null
    55  // 	},
    56  // 	"resources": [
    57  // 	  {
    58  //       "name": "org-name-3",
    59  // 		  "guid": "org-guid-3"
    60  // 		}
    61  // 	]
    62  // }`
    63  // 				server.AppendHandlers(
    64  // 					CombineHandlers(
    65  // 						VerifyRequest(http.MethodGet, "/v3/isolation_segments/some-iso-guid/organizations"),
    66  // 						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    67  // 					),
    68  // 				)
    69  // 				server.AppendHandlers(
    70  // 					CombineHandlers(
    71  // 						VerifyRequest(http.MethodGet, "/v3/isolation_segments/some-iso-guid/organizations", "page=2&per_page=2"),
    72  // 						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
    73  // 					),
    74  // 				)
    75  // 			})
    76  
    77  // 			It("returns the queried organizations and all warnings", func() {
    78  // 				Expect(executeErr).NotTo(HaveOccurred())
    79  
    80  // 				Expect(organizations).To(ConsistOf(
    81  // 					resources.Organization{Name: "org-name-1", GUID: "org-guid-1"},
    82  // 					resources.Organization{Name: "org-name-2", GUID: "org-guid-2"},
    83  // 					resources.Organization{Name: "org-name-3", GUID: "org-guid-3"},
    84  // 				))
    85  // 				Expect(warnings).To(ConsistOf("this is a warning", "this is another warning"))
    86  // 			})
    87  // 		})
    88  
    89  // 		When("the cloud controller returns errors and warnings", func() {
    90  // 			BeforeEach(func() {
    91  // 				response := `{
    92  //   "errors": [
    93  //     {
    94  //       "code": 10008,
    95  //       "detail": "The request is semantically invalid: command presence",
    96  //       "title": "CF-UnprocessableEntity"
    97  //     },
    98  // 		{
    99  //       "code": 10010,
   100  //       "detail": "Isolation segment not found",
   101  //       "title": "CF-ResourceNotFound"
   102  //     }
   103  //   ]
   104  // }`
   105  // 				server.AppendHandlers(
   106  // 					CombineHandlers(
   107  // 						VerifyRequest(http.MethodGet, "/v3/isolation_segments/some-iso-guid/organizations"),
   108  // 						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   109  // 					),
   110  // 				)
   111  // 			})
   112  
   113  // 			It("returns the error and all warnings", func() {
   114  // 				Expect(executeErr).To(MatchError(ccerror.MultiError{
   115  // 					ResponseCode: http.StatusTeapot,
   116  // 					Errors: []ccerror.V3Error{
   117  // 						{
   118  // 							Code:   10008,
   119  // 							Detail: "The request is semantically invalid: command presence",
   120  // 							Title:  "CF-UnprocessableEntity",
   121  // 						},
   122  // 						{
   123  // 							Code:   10010,
   124  // 							Detail: "Isolation segment not found",
   125  // 							Title:  "CF-ResourceNotFound",
   126  // 						},
   127  // 					},
   128  // 				}))
   129  // 				Expect(warnings).To(ConsistOf("this is a warning"))
   130  // 			})
   131  // 		})
   132  // 	})
   133  
   134  // 	Describe("GetOrganizations", func() {
   135  // 		var (
   136  // 			organizations []resources.Organization
   137  // 			warnings      Warnings
   138  // 			executeErr    error
   139  // 		)
   140  
   141  // 		JustBeforeEach(func() {
   142  // 			organizations, warnings, executeErr = client.GetOrganizations(Query{
   143  // 				Key:    NameFilter,
   144  // 				Values: []string{"some-org-name"},
   145  // 			})
   146  // 		})
   147  
   148  // 		When("organizations exist", func() {
   149  // 			BeforeEach(func() {
   150  // 				response1 := fmt.Sprintf(`{
   151  // 	"pagination": {
   152  // 		"next": {
   153  // 			"href": "%s/v3/organizations?names=some-org-name&page=2&per_page=2"
   154  // 		}
   155  // 	},
   156  //   "resources": [
   157  //     {
   158  //       "name": "org-name-1",
   159  //       "guid": "org-guid-1"
   160  //     },
   161  //     {
   162  //       "name": "org-name-2",
   163  //       "guid": "org-guid-2"
   164  //     }
   165  //   ]
   166  // }`, server.URL())
   167  // 				response2 := `{
   168  // 	"pagination": {
   169  // 		"next": null
   170  // 	},
   171  // 	"resources": [
   172  // 	  {
   173  //       "name": "org-name-3",
   174  // 		  "guid": "org-guid-3"
   175  // 		}
   176  // 	]
   177  // }`
   178  // 				server.AppendHandlers(
   179  // 					CombineHandlers(
   180  // 						VerifyRequest(http.MethodGet, "/v3/organizations", "names=some-org-name"),
   181  // 						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   182  // 					),
   183  // 				)
   184  // 				server.AppendHandlers(
   185  // 					CombineHandlers(
   186  // 						VerifyRequest(http.MethodGet, "/v3/organizations", "names=some-org-name&page=2&per_page=2"),
   187  // 						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   188  // 					),
   189  // 				)
   190  // 			})
   191  
   192  // 			It("returns the queried organizations and all warnings", func() {
   193  // 				Expect(executeErr).NotTo(HaveOccurred())
   194  
   195  // 				Expect(organizations).To(ConsistOf(
   196  // 					resources.Organization{Name: "org-name-1", GUID: "org-guid-1"},
   197  // 					resources.Organization{Name: "org-name-2", GUID: "org-guid-2"},
   198  // 					resources.Organization{Name: "org-name-3", GUID: "org-guid-3"},
   199  // 				))
   200  // 				Expect(warnings).To(ConsistOf("this is a warning", "this is another warning"))
   201  // 			})
   202  // 		})
   203  
   204  // 		When("the cloud controller returns errors and warnings", func() {
   205  // 			BeforeEach(func() {
   206  // 				response := `{
   207  //   "errors": [
   208  //     {
   209  //       "code": 10008,
   210  //       "detail": "The request is semantically invalid: command presence",
   211  //       "title": "CF-UnprocessableEntity"
   212  //     },
   213  //     {
   214  //       "code": 10010,
   215  //       "detail": "Org not found",
   216  //       "title": "CF-ResourceNotFound"
   217  //     }
   218  //   ]
   219  // }`
   220  // 				server.AppendHandlers(
   221  // 					CombineHandlers(
   222  // 						VerifyRequest(http.MethodGet, "/v3/organizations"),
   223  // 						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   224  // 					),
   225  // 				)
   226  // 			})
   227  
   228  // 			It("returns the error and all warnings", func() {
   229  // 				Expect(executeErr).To(MatchError(ccerror.MultiError{
   230  // 					ResponseCode: http.StatusTeapot,
   231  // 					Errors: []ccerror.V3Error{
   232  // 						{
   233  // 							Code:   10008,
   234  // 							Detail: "The request is semantically invalid: command presence",
   235  // 							Title:  "CF-UnprocessableEntity",
   236  // 						},
   237  // 						{
   238  // 							Code:   10010,
   239  // 							Detail: "Org not found",
   240  // 							Title:  "CF-ResourceNotFound",
   241  // 						},
   242  // 					},
   243  // 				}))
   244  // 				Expect(warnings).To(ConsistOf("this is a warning"))
   245  // 			})
   246  // 		})
   247  // 	})
   248  
   249  // 	Describe("UpdateOrganization", func() {
   250  // 		var (
   251  // 			orgToUpdate resources.Organization
   252  // 			updatedOrg  resources.Organization
   253  // 			warnings    Warnings
   254  // 			executeErr  error
   255  // 		)
   256  
   257  // 		JustBeforeEach(func() {
   258  // 			updatedOrg, warnings, executeErr = client.UpdateOrganization(orgToUpdate)
   259  // 		})
   260  
   261  // 		When("the organization is updated successfully", func() {
   262  // 			BeforeEach(func() {
   263  // 				response := `{
   264  // 					"guid": "some-app-guid",
   265  // 					"name": "some-app-name",
   266  // 					"metadata": {
   267  // 						"labels": {
   268  // 							"k1":"v1",
   269  // 							"k2":"v2"
   270  // 						}
   271  // 					}
   272  // 				}`
   273  
   274  // 				expectedBody := map[string]interface{}{
   275  // 					"name": "some-org-name",
   276  // 					"metadata": map[string]interface{}{
   277  // 						"labels": map[string]string{
   278  // 							"k1": "v1",
   279  // 							"k2": "v2",
   280  // 						},
   281  // 					},
   282  // 				}
   283  
   284  // 				server.AppendHandlers(
   285  // 					CombineHandlers(
   286  // 						VerifyRequest(http.MethodPatch, "/v3/organizations/some-guid"),
   287  // 						VerifyJSONRepresenting(expectedBody),
   288  // 						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   289  // 					),
   290  // 				)
   291  
   292  // 				orgToUpdate = resources.Organization{
   293  // 					Name: "some-org-name",
   294  // 					GUID: "some-guid",
   295  // 					Metadata: &Metadata{
   296  // 						Labels: map[string]types.NullString{
   297  // 							"k1": types.NewNullString("v1"),
   298  // 							"k2": types.NewNullString("v2"),
   299  // 						},
   300  // 					},
   301  // 				}
   302  // 			})
   303  
   304  // 			It("should include the labels in the JSON", func() {
   305  // 				Expect(executeErr).ToNot(HaveOccurred())
   306  // 				Expect(server.ReceivedRequests()).To(HaveLen(3))
   307  // 				Expect(len(warnings)).To(Equal(0))
   308  // 				Expect(updatedOrg.Metadata.Labels).To(BeEquivalentTo(
   309  // 					map[string]types.NullString{
   310  // 						"k1": types.NewNullString("v1"),
   311  // 						"k2": types.NewNullString("v2"),
   312  // 					}))
   313  // 			})
   314  
   315  // 		})
   316  
   317  // 	})
   318  // })