github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/job_url_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/ghttp"
    11  )
    12  
    13  var _ = Describe("Job URL", func() {
    14  	var client *Client
    15  	var fakedMultiErrResponse string
    16  	var expectedMultiErr ccerror.MultiError
    17  
    18  	BeforeEach(func() {
    19  		client, _ = NewTestClient()
    20  
    21  		expectedMultiErr = ccerror.MultiError{
    22  			ResponseCode: http.StatusTeapot,
    23  			Errors: []ccerror.V3Error{
    24  				{
    25  					Code:   1001,
    26  					Detail: "Request invalid due to parse error: invalid request body",
    27  					Title:  "CF-MessageParseError",
    28  				},
    29  				{
    30  					Code:   10010,
    31  					Detail: "App not found",
    32  					Title:  "CF-ResourceNotFound",
    33  				},
    34  			},
    35  		}
    36  		fakedMultiErrResponse = `{
    37  		 "errors": [
    38  		   {
    39  		     "code": 1001,
    40  		     "detail": "Request invalid due to parse error: invalid request body",
    41  		     "title": "CF-MessageParseError"
    42  		   },
    43  		   {
    44  		     "code": 10010,
    45  		     "detail": "App not found",
    46  		     "title": "CF-ResourceNotFound"
    47  		   }
    48  		 ]
    49  		}`
    50  
    51  	})
    52  
    53  	Describe("DeleteApplication", func() {
    54  		var (
    55  			jobLocation JobURL
    56  			warnings    Warnings
    57  			executeErr  error
    58  		)
    59  
    60  		JustBeforeEach(func() {
    61  			jobLocation, warnings, executeErr = client.DeleteApplication("some-app-guid")
    62  		})
    63  
    64  		When("the application is deleted successfully", func() {
    65  			BeforeEach(func() {
    66  				server.AppendHandlers(
    67  					CombineHandlers(
    68  						VerifyRequest(http.MethodDelete, "/v3/apps/some-app-guid"),
    69  						RespondWith(http.StatusAccepted, ``,
    70  							http.Header{
    71  								"X-Cf-Warnings": {"some-warning"},
    72  								"Location":      {"/v3/jobs/some-location"},
    73  							},
    74  						),
    75  					),
    76  				)
    77  			})
    78  
    79  			It("returns all warnings", func() {
    80  				Expect(executeErr).ToNot(HaveOccurred())
    81  				Expect(jobLocation).To(Equal(JobURL("/v3/jobs/some-location")))
    82  				Expect(warnings).To(ConsistOf("some-warning"))
    83  			})
    84  		})
    85  
    86  		When("deleting the application returns an error", func() {
    87  			BeforeEach(func() {
    88  				response := `{
    89    "errors": [
    90      {
    91        "code": 1001,
    92        "detail": "Request invalid due to parse error: invalid request body",
    93        "title": "CF-MessageParseError"
    94      },
    95      {
    96        "code": 10010,
    97        "detail": "App not found",
    98        "title": "CF-ResourceNotFound"
    99      }
   100    ]
   101  }`
   102  				server.AppendHandlers(
   103  					CombineHandlers(
   104  						VerifyRequest(http.MethodDelete, "/v3/apps/some-app-guid"),
   105  						RespondWith(http.StatusBadRequest, response, http.Header{"X-Cf-Warnings": {"some-warning"}}),
   106  					),
   107  				)
   108  			})
   109  
   110  			It("returns all warnings", func() {
   111  				Expect(executeErr).To(MatchError(ccerror.MultiError{
   112  					ResponseCode: http.StatusBadRequest,
   113  					Errors: []ccerror.V3Error{
   114  						{
   115  							Code:   1001,
   116  							Detail: "Request invalid due to parse error: invalid request body",
   117  							Title:  "CF-MessageParseError",
   118  						},
   119  						{
   120  							Code:   10010,
   121  							Detail: "App not found",
   122  							Title:  "CF-ResourceNotFound",
   123  						},
   124  					},
   125  				}))
   126  				Expect(warnings).To(ConsistOf("some-warning"))
   127  			})
   128  		})
   129  	})
   130  
   131  	Describe("UpdateApplicationApplyManifest", func() {
   132  		var (
   133  			manifestBody []byte
   134  
   135  			jobURL     JobURL
   136  			warnings   Warnings
   137  			executeErr error
   138  		)
   139  
   140  		JustBeforeEach(func() {
   141  			jobURL, warnings, executeErr = client.UpdateApplicationApplyManifest(
   142  				"some-app-guid",
   143  				manifestBody,
   144  			)
   145  		})
   146  
   147  		When("the manifest application is successful", func() {
   148  			var expectedJobURL string
   149  
   150  			BeforeEach(func() {
   151  				manifestBody = []byte("fake-yaml-body")
   152  				expectedJobURL = "i-am-a-job-url"
   153  
   154  				server.AppendHandlers(
   155  					CombineHandlers(
   156  						VerifyRequest(http.MethodPost, "/v3/apps/some-app-guid/actions/apply_manifest"),
   157  						VerifyHeaderKV("Content-type", "application/x-yaml"),
   158  						VerifyBody(manifestBody),
   159  						RespondWith(http.StatusAccepted, "", http.Header{
   160  							"X-Cf-Warnings": {"this is a warning"},
   161  							"Location":      {expectedJobURL},
   162  						}),
   163  					),
   164  				)
   165  			})
   166  
   167  			It("returns the job URL and warnings", func() {
   168  				Expect(executeErr).NotTo(HaveOccurred())
   169  				Expect(warnings).To(ConsistOf("this is a warning"))
   170  
   171  				Expect(jobURL).To(Equal(JobURL(expectedJobURL)))
   172  			})
   173  		})
   174  
   175  		When("the manifest application fails", func() {
   176  			BeforeEach(func() {
   177  				server.AppendHandlers(
   178  					CombineHandlers(
   179  						VerifyRequest(http.MethodPost, "/v3/apps/some-app-guid/actions/apply_manifest"),
   180  						VerifyHeaderKV("Content-type", "application/x-yaml"),
   181  						RespondWith(http.StatusTeapot, fakedMultiErrResponse, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   182  					),
   183  				)
   184  			})
   185  
   186  			It("returns the error and all warnings", func() {
   187  				Expect(executeErr).To(MatchError(expectedMultiErr))
   188  				Expect(warnings).To(ConsistOf("this is a warning"))
   189  			})
   190  		})
   191  	})
   192  
   193  	Describe("UpdateSpaceApplyManifest", func() {
   194  		var (
   195  			manifestBody []byte
   196  
   197  			responseJobURL JobURL
   198  			warnings       Warnings
   199  			executeErr     error
   200  
   201  			expectedJobURL string
   202  		)
   203  
   204  		JustBeforeEach(func() {
   205  			responseJobURL, warnings, executeErr = client.UpdateSpaceApplyManifest(
   206  				"some-space-guid",
   207  				manifestBody,
   208  			)
   209  		})
   210  
   211  		BeforeEach(func() {
   212  			manifestBody = []byte("fake-manifest-yml-body")
   213  			expectedJobURL = "apply-manifest-job-url"
   214  		})
   215  
   216  		When("applying the manifest to the space succeeds", func() {
   217  			BeforeEach(func() {
   218  				server.AppendHandlers(
   219  					CombineHandlers(
   220  						VerifyRequest(http.MethodPost, "/v3/spaces/some-space-guid/actions/apply_manifest"),
   221  						VerifyHeaderKV("Content-type", "application/x-yaml"),
   222  						VerifyBody(manifestBody),
   223  						RespondWith(http.StatusAccepted, "", http.Header{
   224  							"X-Cf-Warnings": {"some-ccv3-warning"},
   225  							"Location":      {expectedJobURL},
   226  						}),
   227  					),
   228  				)
   229  			})
   230  
   231  			It("returns the job URL and warnings", func() {
   232  				Expect(executeErr).To(Not(HaveOccurred()))
   233  				Expect(warnings).To(ConsistOf("some-ccv3-warning"))
   234  
   235  				Expect(responseJobURL).To(Equal(JobURL(expectedJobURL)))
   236  			})
   237  		})
   238  
   239  		When("applying the manifest to the space fails", func() {
   240  			BeforeEach(func() {
   241  				server.AppendHandlers(
   242  					CombineHandlers(
   243  						VerifyRequest(http.MethodPost, "/v3/spaces/some-space-guid/actions/apply_manifest"),
   244  						VerifyHeaderKV("Content-type", "application/x-yaml"),
   245  						RespondWith(
   246  							http.StatusTeapot, fakedMultiErrResponse, http.Header{"X-Cf-Warnings": {"some warning"}}),
   247  					),
   248  				)
   249  			})
   250  
   251  			It("returns an error and warnings", func() {
   252  				Expect(executeErr).To(HaveOccurred())
   253  				Expect(executeErr).To(MatchError(expectedMultiErr))
   254  			})
   255  		})
   256  	})
   257  })