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