github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/api/cloudcontroller/ccv2/domain_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  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/ghttp"
    11  )
    12  
    13  var _ = Describe("Domain", func() {
    14  	var client *Client
    15  
    16  	BeforeEach(func() {
    17  		client = NewTestClient()
    18  	})
    19  
    20  	Describe("GetSharedDomain", func() {
    21  		Context("when the shared domain exists", func() {
    22  			BeforeEach(func() {
    23  				response := `{
    24  						"metadata": {
    25  							"guid": "shared-domain-guid",
    26  							"updated_at": null
    27  						},
    28  						"entity": {
    29  							"name": "shared-domain-1.com"
    30  						}
    31  				}`
    32  				server.AppendHandlers(
    33  					CombineHandlers(
    34  						VerifyRequest(http.MethodGet, "/v2/shared_domains/shared-domain-guid"),
    35  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    36  					),
    37  				)
    38  			})
    39  
    40  			It("returns the shared domain and all warnings", func() {
    41  				domain, warnings, err := client.GetSharedDomain("shared-domain-guid")
    42  				Expect(err).NotTo(HaveOccurred())
    43  				Expect(domain).To(Equal(Domain{Name: "shared-domain-1.com", GUID: "shared-domain-guid"}))
    44  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    45  			})
    46  		})
    47  
    48  		Context("when the shared domain does not exist", func() {
    49  			BeforeEach(func() {
    50  				response := `{
    51  					"code": 130002,
    52  					"description": "The domain could not be found: shared-domain-guid",
    53  					"error_code": "CF-DomainNotFound"
    54  				}`
    55  				server.AppendHandlers(
    56  					CombineHandlers(
    57  						VerifyRequest(http.MethodGet, "/v2/shared_domains/shared-domain-guid"),
    58  						RespondWith(http.StatusNotFound, response),
    59  					),
    60  				)
    61  			})
    62  
    63  			It("returns an error", func() {
    64  				domain, _, err := client.GetSharedDomain("shared-domain-guid")
    65  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
    66  					Message: "The domain could not be found: shared-domain-guid",
    67  				}))
    68  				Expect(domain).To(Equal(Domain{}))
    69  			})
    70  		})
    71  	})
    72  
    73  	Describe("GetPrivateDomain", func() {
    74  		Context("when the private domain exists", func() {
    75  			BeforeEach(func() {
    76  				response := `{
    77  						"metadata": {
    78  							"guid": "private-domain-guid",
    79  							"updated_at": null
    80  						},
    81  						"entity": {
    82  							"name": "private-domain-1.com"
    83  						}
    84  				}`
    85  				server.AppendHandlers(
    86  					CombineHandlers(
    87  						VerifyRequest(http.MethodGet, "/v2/private_domains/private-domain-guid"),
    88  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    89  					),
    90  				)
    91  			})
    92  
    93  			It("returns the private domain and all warnings", func() {
    94  				domain, warnings, err := client.GetPrivateDomain("private-domain-guid")
    95  				Expect(err).NotTo(HaveOccurred())
    96  				Expect(domain).To(Equal(Domain{Name: "private-domain-1.com", GUID: "private-domain-guid"}))
    97  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    98  			})
    99  		})
   100  
   101  		Context("when the private domain does not exist", func() {
   102  			BeforeEach(func() {
   103  				response := `{
   104  					"code": 130002,
   105  					"description": "The domain could not be found: private-domain-guid",
   106  					"error_code": "CF-DomainNotFound"
   107  				}`
   108  				server.AppendHandlers(
   109  					CombineHandlers(
   110  						VerifyRequest(http.MethodGet, "/v2/private_domains/private-domain-guid"),
   111  						RespondWith(http.StatusNotFound, response),
   112  					),
   113  				)
   114  			})
   115  
   116  			It("returns an error", func() {
   117  				domain, _, err := client.GetPrivateDomain("private-domain-guid")
   118  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
   119  					Message: "The domain could not be found: private-domain-guid",
   120  				}))
   121  				Expect(domain).To(Equal(Domain{}))
   122  			})
   123  		})
   124  	})
   125  
   126  	Describe("GetSharedDomains", func() {
   127  		Context("when the cloud controller does not return an error", func() {
   128  			BeforeEach(func() {
   129  				response1 := `{
   130  				"next_url": "/v2/shared_domains?page=2",
   131  				"resources": [
   132  					{
   133  						"metadata": {
   134  							"guid": "domain-guid-1"
   135  						},
   136  						"entity": {
   137  							"name": "domain-name-1"
   138  						}
   139  					},
   140  					{
   141  						"metadata": {
   142  							"guid": "domain-guid-2"
   143  						},
   144  						"entity": {
   145  							"name": "domain-name-2"
   146  						}
   147  					}
   148  				]
   149  			}`
   150  				response2 := `{
   151  				"next_url": null,
   152  				"resources": [
   153  					{
   154  						"metadata": {
   155  							"guid": "domain-guid-3"
   156  						},
   157  						"entity": {
   158  							"name": "domain-name-3"
   159  						}
   160  					},
   161  					{
   162  						"metadata": {
   163  							"guid": "domain-guid-4"
   164  						},
   165  						"entity": {
   166  							"name": "domain-name-4"
   167  						}
   168  					}
   169  				]
   170  			}`
   171  				server.AppendHandlers(
   172  					CombineHandlers(
   173  						VerifyRequest(http.MethodGet, "/v2/shared_domains"),
   174  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   175  					),
   176  				)
   177  				server.AppendHandlers(
   178  					CombineHandlers(
   179  						VerifyRequest(http.MethodGet, "/v2/shared_domains", "page=2"),
   180  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   181  					),
   182  				)
   183  			})
   184  
   185  			It("returns the shared domain and warnings", func() {
   186  				domains, warnings, err := client.GetSharedDomains()
   187  				Expect(err).NotTo(HaveOccurred())
   188  				Expect(domains).To(Equal([]Domain{
   189  					{
   190  						GUID: "domain-guid-1",
   191  						Name: "domain-name-1",
   192  					},
   193  					{
   194  						GUID: "domain-guid-2",
   195  						Name: "domain-name-2",
   196  					},
   197  					{
   198  						GUID: "domain-guid-3",
   199  						Name: "domain-name-3",
   200  					},
   201  					{
   202  						GUID: "domain-guid-4",
   203  						Name: "domain-name-4",
   204  					},
   205  				}))
   206  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   207  			})
   208  		})
   209  
   210  		Context("when the cloud controller returns an error", func() {
   211  			BeforeEach(func() {
   212  				response := `{
   213  					"code": 1,
   214  					"description": "some error description",
   215  					"error_code": "CF-SomeError"
   216  				}`
   217  				server.AppendHandlers(
   218  					CombineHandlers(
   219  						VerifyRequest(http.MethodGet, "/v2/shared_domains"),
   220  						RespondWith(http.StatusInternalServerError, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   221  					),
   222  				)
   223  			})
   224  
   225  			It("returns the warnings and error", func() {
   226  				domains, warnings, err := client.GetSharedDomains()
   227  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   228  					V2ErrorResponse: ccerror.V2ErrorResponse{
   229  						Code:        1,
   230  						Description: "some error description",
   231  						ErrorCode:   "CF-SomeError",
   232  					},
   233  					ResponseCode: http.StatusInternalServerError,
   234  				}))
   235  				Expect(domains).To(Equal([]Domain{}))
   236  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   237  			})
   238  		})
   239  	})
   240  
   241  	Describe("GetOrganizationPrivateDomains", func() {
   242  		Context("when the cloud controller does not return an error", func() {
   243  			BeforeEach(func() {
   244  				response1 := `{
   245  					"next_url": "/v2/organizations/some-org-guid/private_domains?page=2",
   246  					"resources": [
   247  						{
   248  							"metadata": {
   249  								"guid": "private-domain-guid-1"
   250  							},
   251  							"entity": {
   252  								"name": "private-domain-name-1"
   253  							}
   254  						},
   255  						{
   256  							"metadata": {
   257  								"guid": "private-domain-guid-2"
   258  							},
   259  							"entity": {
   260  								"name": "private-domain-name-2"
   261  							}
   262  						}
   263  					]
   264  				}`
   265  				response2 := `{
   266  					"next_url": null,
   267  					"resources": [
   268  						{
   269  							"metadata": {
   270  								"guid": "private-domain-guid-3"
   271  							},
   272  							"entity": {
   273  								"name": "private-domain-name-3"
   274  							}
   275  						},
   276  						{
   277  							"metadata": {
   278  								"guid": "private-domain-guid-4"
   279  							},
   280  							"entity": {
   281  								"name": "private-domain-name-4"
   282  							}
   283  						}
   284  					]
   285  				}`
   286  				server.AppendHandlers(
   287  					CombineHandlers(
   288  						VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains"),
   289  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   290  					),
   291  				)
   292  				server.AppendHandlers(
   293  					CombineHandlers(
   294  						VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains", "page=2"),
   295  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   296  					),
   297  				)
   298  			})
   299  
   300  			It("returns the domains and warnings", func() {
   301  				domains, warnings, err := client.GetOrganizationPrivateDomains("some-org-guid", []Query{})
   302  				Expect(err).NotTo(HaveOccurred())
   303  				Expect(domains).To(Equal([]Domain{
   304  					{
   305  						Name: "private-domain-name-1",
   306  						GUID: "private-domain-guid-1",
   307  					},
   308  					{
   309  						Name: "private-domain-name-2",
   310  						GUID: "private-domain-guid-2",
   311  					},
   312  					{
   313  						Name: "private-domain-name-3",
   314  						GUID: "private-domain-guid-3",
   315  					},
   316  					{
   317  						Name: "private-domain-name-4",
   318  						GUID: "private-domain-guid-4",
   319  					},
   320  				}))
   321  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   322  			})
   323  		})
   324  
   325  		Context("when the client includes includes query parameters for name", func() {
   326  			It("it includes the query parameters in the request", func() {
   327  				server.AppendHandlers(
   328  					CombineHandlers(
   329  						VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains", "q=name:private-domain-name"),
   330  						RespondWith(http.StatusOK, ""),
   331  					),
   332  				)
   333  
   334  				client.GetOrganizationPrivateDomains("some-org-guid", []Query{{
   335  					Filter:   NameFilter,
   336  					Operator: EqualOperator,
   337  					Value:    "private-domain-name",
   338  				}})
   339  			})
   340  
   341  		})
   342  
   343  		Context("when the cloud controller returns an error", func() {
   344  			BeforeEach(func() {
   345  				response := `{
   346  					   "description": "The organization could not be found: glah",
   347  					   "error_code": "CF-OrganizationNotFound",
   348  					   "code": 30003
   349  				}`
   350  				server.AppendHandlers(
   351  					CombineHandlers(
   352  						VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains"),
   353  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   354  					),
   355  				)
   356  			})
   357  			It("returns the warnings and error", func() {
   358  				domains, warnings, err := client.GetOrganizationPrivateDomains("some-org-guid", []Query{})
   359  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
   360  					Message: "The organization could not be found: glah",
   361  				}))
   362  				Expect(domains).To(Equal([]Domain{}))
   363  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   364  			})
   365  		})
   366  
   367  	})
   368  
   369  })