github.com/companieshouse/insolvency-api@v0.0.0-20231024103413-440c973d9e9b/handlers/resolution_resource_test.go (about) 1 package handlers 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "net/http" 8 "net/http/httptest" 9 "os" 10 "testing" 11 12 "github.com/companieshouse/chs.go/log" 13 "github.com/companieshouse/insolvency-api/dao" 14 mock_dao "github.com/companieshouse/insolvency-api/mocks" 15 "github.com/companieshouse/insolvency-api/models" 16 "github.com/companieshouse/insolvency-api/utils" 17 "github.com/golang/mock/gomock" 18 "github.com/gorilla/mux" 19 "github.com/jarcoal/httpmock" 20 . "github.com/smartystreets/goconvey/convey" 21 ) 22 23 func serveHandleCreateResolution(body []byte, service dao.Service, helperService utils.HelperService, tranIDSet bool, res *httptest.ResponseRecorder) *httptest.ResponseRecorder { 24 path := "/transactions/123456789/insolvency/resolution" 25 req := httptest.NewRequest(http.MethodPost, path, bytes.NewReader(body)) 26 if tranIDSet { 27 req = mux.SetURLVars(req, map[string]string{"transaction_id": transactionID}) 28 } 29 30 handler := HandleCreateResolution(service, helperService) 31 handler.ServeHTTP(res, req) 32 33 return res 34 } 35 36 func TestUnitHandleCreateResolution(t *testing.T) { 37 err := os.Chdir("..") 38 if err != nil { 39 log.ErrorR(nil, fmt.Errorf("error accessing root directory")) 40 } 41 42 helperService := utils.NewHelperService() 43 44 Convey("Must need a transaction ID in the url", t, func() { 45 mockService, _, rec := mock_dao.CreateTestObjects(t) 46 47 body, _ := json.Marshal(&models.InsolvencyRequest{}) 48 49 res := serveHandleCreateResolution(body, mockService, helperService, false, rec) 50 51 So(res.Code, ShouldEqual, http.StatusBadRequest) 52 So(res.Body.String(), ShouldContainSubstring, "transaction ID is not in the URL path") 53 }) 54 55 Convey("Error checking if transaction is closed against transaction api", t, func() { 56 mockService, _, rec := mock_dao.CreateTestObjects(t) 57 httpmock.Activate() 58 59 // Expect the transaction api to be called and return an error 60 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusInternalServerError, "")) 61 62 body, _ := json.Marshal(&models.InsolvencyRequest{}) 63 64 res := serveHandleCreateResolution(body, mockService, helperService, true, rec) 65 66 So(res.Code, ShouldEqual, http.StatusInternalServerError) 67 So(res.Body.String(), ShouldContainSubstring, "error checking transaction status") 68 }) 69 70 Convey("Transaction is already closed and cannot be updated", t, func() { 71 mockService, _, rec := mock_dao.CreateTestObjects(t) 72 httpmock.Activate() 73 74 // Expect the transaction api to be called and return an already closed transaction 75 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponseClosed)) 76 77 body, _ := json.Marshal(&models.InsolvencyRequest{}) 78 79 res := serveHandleCreateResolution(body, mockService, helperService, true, rec) 80 81 So(res.Code, ShouldEqual, http.StatusForbidden) 82 So(res.Body.String(), ShouldContainSubstring, "already closed and cannot be updated") 83 }) 84 85 Convey("Failed to read request body", t, func() { 86 mockService, _, rec := mock_dao.CreateTestObjects(t) 87 httpmock.Activate() 88 89 // Expect the transaction api to be called and return an open transaction 90 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 91 92 body := []byte(`{"first_name":error`) 93 94 res := serveHandleCreateResolution(body, mockService, helperService, true, rec) 95 96 So(res.Code, ShouldEqual, http.StatusBadRequest) 97 So(res.Body.String(), ShouldContainSubstring, fmt.Sprintf("failed to read request body for transaction %s", transactionID)) 98 }) 99 100 Convey("Incoming request has date of resolution missing", t, func() { 101 mockService, _, rec := mock_dao.CreateTestObjects(t) 102 httpmock.Activate() 103 104 // Expect the transaction api to be called and return an open transaction 105 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 106 107 resolution := generateResolution() 108 resolution.DateOfResolution = "" 109 body, _ := json.Marshal(resolution) 110 111 res := serveHandleCreateResolution(body, mockService, helperService, true, rec) 112 113 So(res.Code, ShouldEqual, http.StatusBadRequest) 114 So(res.Body.String(), ShouldContainSubstring, "date_of_resolution is a required field") 115 }) 116 117 Convey("Incoming request has invalid date format", t, func() { 118 mockService, _, rec := mock_dao.CreateTestObjects(t) 119 httpmock.Activate() 120 121 // Expect the transaction api to be called and return an open transaction 122 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 123 124 resolution := generateResolution() 125 resolution.DateOfResolution = "21-01-01" 126 body, _ := json.Marshal(resolution) 127 128 res := serveHandleCreateResolution(body, mockService, helperService, true, rec) 129 130 So(res.Code, ShouldEqual, http.StatusBadRequest) 131 So(res.Body.String(), ShouldContainSubstring, "date_of_resolution does not match the 2006-01-02 format") 132 }) 133 134 Convey("Incoming request has attachments missing", t, func() { 135 mockService, _, rec := mock_dao.CreateTestObjects(t) 136 httpmock.Activate() 137 138 // Expect the transaction api to be called and return an open transaction 139 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 140 141 resolution := generateResolution() 142 resolution.Attachments = nil 143 body, _ := json.Marshal(resolution) 144 145 res := serveHandleCreateResolution(body, mockService, helperService, true, rec) 146 147 So(res.Code, ShouldEqual, http.StatusBadRequest) 148 So(res.Body.String(), ShouldContainSubstring, "attachments is a required field") 149 }) 150 151 Convey("Attachment is not associated with transaction", t, func() { 152 mockService, _, rec := mock_dao.CreateTestObjects(t) 153 httpmock.Activate() 154 155 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/company/1234", httpmock.NewStringResponder(http.StatusOK, companyProfileDateResponse("2000-06-26 00:00:00.000Z"))) 156 157 // Expect the transaction api to be called and return an open transaction 158 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 159 160 resolution := generateResolution() 161 162 body, _ := json.Marshal(resolution) 163 mockService.EXPECT().GetInsolvencyResource(transactionID).Return(generateInsolvencyResource(), nil) 164 // Expect GetAttachmentFromInsolvencyResource to be called once and return an empty attachment model, nil 165 mockService.EXPECT().GetAttachmentFromInsolvencyResource(transactionID, resolution.Attachments[0]).Return(models.AttachmentResourceDao{}, nil) 166 167 res := serveHandleCreateResolution(body, mockService, helperService, true, rec) 168 169 So(res.Code, ShouldEqual, http.StatusInternalServerError) 170 So(res.Body.String(), ShouldContainSubstring, "attachment not found on transaction") 171 }) 172 173 Convey("Failed to validate resolution", t, func() { 174 mockService, mockHelperService, rec := mock_dao.CreateTestObjects(t) 175 httpmock.Activate() 176 177 // Expect the transaction api to be called and return an open transaction 178 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 179 180 resolution := generateResolution() 181 182 body, _ := json.Marshal(resolution) 183 mockHelperService.EXPECT().HandleTransactionIdExistsValidation(gomock.Any(), gomock.Any(), transactionID).Return(true, transactionID).AnyTimes() 184 mockHelperService.EXPECT().HandleTransactionNotClosedValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 185 mockHelperService.EXPECT().HandleBodyDecodedValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 186 mockHelperService.EXPECT().HandleMandatoryFieldValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 187 mockHelperService.EXPECT().GenerateEtag().Return("etag", nil) 188 mockService.EXPECT().GetInsolvencyResource(transactionID).Return(models.InsolvencyResourceDao{}, fmt.Errorf("error")) 189 190 res := serveHandleCreateResolution(body, mockService, mockHelperService, true, rec) 191 192 So(res.Code, ShouldEqual, http.StatusInternalServerError) 193 So(res.Body.String(), ShouldContainSubstring, "there was a problem handling your request for transaction ID") 194 }) 195 196 Convey("Validation errors are present - date is in the past", t, func() { 197 mockService, mockHelperService, rec := mock_dao.CreateTestObjects(t) 198 httpmock.Activate() 199 200 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/company/1234", httpmock.NewStringResponder(http.StatusOK, companyProfileDateResponse("2000-06-26 00:00:00.000Z"))) 201 202 // Expect the transaction api to be called and return an open transaction 203 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 204 205 resolution := generateResolution() 206 resolution.DateOfResolution = "1999-01-01" 207 208 body, _ := json.Marshal(resolution) 209 mockHelperService.EXPECT().HandleTransactionIdExistsValidation(gomock.Any(), gomock.Any(), transactionID).Return(true, transactionID).AnyTimes() 210 mockHelperService.EXPECT().HandleTransactionNotClosedValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 211 mockHelperService.EXPECT().HandleBodyDecodedValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 212 mockHelperService.EXPECT().HandleMandatoryFieldValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 213 mockHelperService.EXPECT().GenerateEtag().Return("etag", nil) 214 mockService.EXPECT().GetInsolvencyResource(transactionID).Return(generateInsolvencyResource(), nil) 215 216 res := serveHandleCreateResolution(body, mockService, mockHelperService, true, rec) 217 218 So(res.Code, ShouldEqual, http.StatusBadRequest) 219 So(res.Body.String(), ShouldContainSubstring, fmt.Sprintf("date_of_resolution [%s] should not be in the future or before the company was incorporated", resolution.DateOfResolution)) 220 }) 221 222 Convey("Validation errors are present - multiple attachments", t, func() { 223 mockService, mockHelperService, rec := mock_dao.CreateTestObjects(t) 224 httpmock.Activate() 225 226 // Expect the transaction api to be called and return an open transaction 227 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 228 229 resolution := generateResolution() 230 resolution.Attachments = []string{ 231 "1234567890", 232 "0987654321", 233 } 234 235 body, _ := json.Marshal(resolution) 236 mockHelperService.EXPECT().HandleTransactionIdExistsValidation(gomock.Any(), gomock.Any(), transactionID).Return(true, transactionID).AnyTimes() 237 mockHelperService.EXPECT().HandleTransactionNotClosedValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 238 mockHelperService.EXPECT().HandleBodyDecodedValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 239 mockHelperService.EXPECT().HandleMandatoryFieldValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 240 mockHelperService.EXPECT().GenerateEtag().Return("etag", nil).AnyTimes() 241 242 res := serveHandleCreateResolution(body, mockService, mockHelperService, true, rec) 243 244 So(res.Code, ShouldEqual, http.StatusBadRequest) 245 So(res.Body.String(), ShouldContainSubstring, "please supply only one attachment") 246 }) 247 248 Convey("Validation errors are present - no attachment is present", t, func() { 249 mockService, mockHelperService, rec := mock_dao.CreateTestObjects(t) 250 httpmock.Activate() 251 252 // Expect the transaction api to be called and return an open transaction 253 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 254 255 resolution := generateResolution() 256 resolution.Attachments = []string{} 257 258 body, _ := json.Marshal(resolution) 259 mockHelperService.EXPECT().HandleTransactionIdExistsValidation(gomock.Any(), gomock.Any(), transactionID).Return(true, transactionID).AnyTimes() 260 mockHelperService.EXPECT().HandleTransactionNotClosedValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 261 mockHelperService.EXPECT().HandleBodyDecodedValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 262 mockHelperService.EXPECT().HandleMandatoryFieldValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 263 mockHelperService.EXPECT().GenerateEtag().Return("etag", nil).AnyTimes() 264 265 res := serveHandleCreateResolution(body, mockService, mockHelperService, true, rec) 266 267 So(res.Code, ShouldEqual, http.StatusBadRequest) 268 So(res.Body.String(), ShouldContainSubstring, "please supply only one attachment") 269 }) 270 271 Convey("Attachment is not of type resolution", t, func() { 272 mockService, _, rec := mock_dao.CreateTestObjects(t) 273 httpmock.Activate() 274 275 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/company/1234", httpmock.NewStringResponder(http.StatusOK, companyProfileDateResponse("2000-06-26 00:00:00.000Z"))) 276 277 // Expect the transaction api to be called and return an open transaction 278 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 279 280 resolution := generateResolution() 281 282 attachment := generateAttachment() 283 attachment.Type = "not-resolution" 284 285 body, _ := json.Marshal(resolution) 286 mockService.EXPECT().GetInsolvencyResource(transactionID).Return(generateInsolvencyResource(), nil) 287 // Expect GetAttachmentFromInsolvencyResource to be called once and return attachment, nil 288 mockService.EXPECT().GetAttachmentFromInsolvencyResource(transactionID, resolution.Attachments[0]).Return(attachment, nil) 289 290 res := serveHandleCreateResolution(body, mockService, helperService, true, rec) 291 292 So(res.Code, ShouldEqual, http.StatusBadRequest) 293 So(res.Body.String(), ShouldContainSubstring, "attachment is not a resolution") 294 }) 295 296 Convey("Generic error when adding resolution resource to mongo", t, func() { 297 mockService, _, rec := mock_dao.CreateTestObjects(t) 298 httpmock.Activate() 299 300 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/company/1234", httpmock.NewStringResponder(http.StatusOK, companyProfileDateResponse("2000-06-26 00:00:00.000Z"))) 301 302 // Expect the transaction api to be called and return an open transaction 303 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 304 305 resolution := generateResolution() 306 body, _ := json.Marshal(resolution) 307 mockService.EXPECT().GetInsolvencyResource(transactionID).Return(generateInsolvencyResource(), nil) 308 // Expect GetAttachmentFromInsolvencyResource to be called once and return attachment, nil 309 mockService.EXPECT().GetAttachmentFromInsolvencyResource(transactionID, resolution.Attachments[0]).Return(generateAttachment(), nil) 310 // Expect CreateResolutionResource to be called once and return an error 311 mockService.EXPECT().CreateResolutionResource(gomock.Any(), transactionID).Return(http.StatusInternalServerError, fmt.Errorf("there was a problem handling your request for transaction %s", transactionID)).Times(1) 312 313 res := serveHandleCreateResolution(body, mockService, helperService, true, rec) 314 315 So(res.Code, ShouldEqual, http.StatusInternalServerError) 316 So(res.Body.String(), ShouldContainSubstring, "there was a problem handling your request") 317 }) 318 319 Convey("Error adding resolution resource to mongo - insolvency case not found", t, func() { 320 mockService, _, rec := mock_dao.CreateTestObjects(t) 321 httpmock.Activate() 322 323 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/company/1234", httpmock.NewStringResponder(http.StatusOK, companyProfileDateResponse("2000-06-26 00:00:00.000Z"))) 324 325 // Expect the transaction api to be called and return an open transaction 326 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 327 328 resolution := generateResolution() 329 body, _ := json.Marshal(resolution) 330 mockService.EXPECT().GetInsolvencyResource(transactionID).Return(generateInsolvencyResource(), nil) 331 // Expect GetAttachmentFromInsolvencyResource to be called once and return attachment, nil 332 mockService.EXPECT().GetAttachmentFromInsolvencyResource(transactionID, resolution.Attachments[0]).Return(generateAttachment(), nil) 333 // Expect CreateResolutionResource to be called once and return an error 334 mockService.EXPECT().CreateResolutionResource(gomock.Any(), transactionID).Return(http.StatusNotFound, fmt.Errorf("there was a problem handling your request for transaction %s not found", transactionID)).Times(1) 335 336 res := serveHandleCreateResolution(body, mockService, helperService, true, rec) 337 338 So(res.Code, ShouldEqual, http.StatusNotFound) 339 So(res.Body.String(), ShouldContainSubstring, "not found") 340 }) 341 342 Convey("Successfully add insolvency resource to mongo", t, func() { 343 mockService, mockHelperService, rec := mock_dao.CreateTestObjects(t) 344 httpmock.Activate() 345 346 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/company/1234", httpmock.NewStringResponder(http.StatusOK, companyProfileDateResponse("2000-06-26 00:00:00.000Z"))) 347 348 // Expect the transaction api to be called and return an open transaction 349 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 350 351 resolution := generateResolution() 352 353 attachment := models.AttachmentResourceDao{ 354 ID: "1111", 355 Type: "resolution", 356 Status: "status", 357 Links: models.AttachmentResourceLinksDao{}, 358 } 359 360 body, _ := json.Marshal(resolution) 361 mockHelperService.EXPECT().HandleTransactionIdExistsValidation(gomock.Any(), gomock.Any(), transactionID).Return(true, transactionID).AnyTimes() 362 mockHelperService.EXPECT().HandleTransactionNotClosedValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 363 mockHelperService.EXPECT().HandleBodyDecodedValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 364 mockHelperService.EXPECT().HandleMandatoryFieldValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 365 mockHelperService.EXPECT().GenerateEtag().Return("etag", nil).AnyTimes() 366 mockService.EXPECT().GetInsolvencyResource(transactionID).Return(generateInsolvencyResource(), nil) 367 // Expect GetAttachmentFromInsolvencyResource to be called once and return attachment, nil 368 mockService.EXPECT().GetAttachmentFromInsolvencyResource(transactionID, resolution.Attachments[0]).Return(attachment, nil) 369 mockHelperService.EXPECT().HandleAttachmentValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 370 mockHelperService.EXPECT().HandleAttachmentTypeValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(http.StatusOK).AnyTimes() 371 // Expect CreateResolutionResource to be called once and return an error 372 mockService.EXPECT().CreateResolutionResource(gomock.Any(), transactionID).Return(http.StatusCreated, nil).Times(1) 373 mockHelperService.EXPECT().HandleCreateResourceValidation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(true).AnyTimes() 374 375 res := serveHandleCreateResolution(body, mockService, mockHelperService, true, rec) 376 377 So(res.Code, ShouldEqual, http.StatusCreated) 378 So(res.Body.String(), ShouldContainSubstring, "\"date_of_resolution\":\"2021-06-06\"") 379 }) 380 } 381 382 func serveHandleGetResolution(service dao.Service, tranIDSet bool) *httptest.ResponseRecorder { 383 path := "/transactions/123456789/insolvency/resolution" 384 req := httptest.NewRequest(http.MethodPost, path, nil) 385 if tranIDSet { 386 req = mux.SetURLVars(req, map[string]string{"transaction_id": transactionID}) 387 } 388 res := httptest.NewRecorder() 389 390 handler := HandleGetResolution(service) 391 handler.ServeHTTP(res, req) 392 393 return res 394 } 395 396 func TestUnitHandleGetResolution(t *testing.T) { 397 err := os.Chdir("..") 398 if err != nil { 399 log.ErrorR(nil, fmt.Errorf("error accessing root directory")) 400 } 401 402 mockCtrl := gomock.NewController(t) 403 defer mockCtrl.Finish() 404 405 mockService := mock_dao.NewMockService(mockCtrl) 406 407 Convey("Must need a transaction ID in the url", t, func() { 408 mockCtrl := gomock.NewController(t) 409 defer mockCtrl.Finish() 410 411 res := serveHandleGetResolution(mockService, false) 412 413 So(res.Code, ShouldEqual, http.StatusBadRequest) 414 }) 415 416 Convey("Failed to get resolution from Insolvency resource", t, func() { 417 httpmock.Activate() 418 defer httpmock.DeactivateAndReset() 419 420 // Expect GetResolutionResource to be called once and return an error 421 mockService.EXPECT().GetResolutionResource(transactionID).Return(models.ResolutionResourceDao{}, fmt.Errorf("failed to get resolution from insolvency resource in db for transaction [%s]: %v", transactionID, err)) 422 423 res := serveHandleGetResolution(mockService, true) 424 425 So(res.Code, ShouldEqual, http.StatusInternalServerError) 426 }) 427 428 Convey("Resolution was not found on supplied transaction", t, func() { 429 httpmock.Activate() 430 defer httpmock.DeactivateAndReset() 431 432 // Expect GetResolutionResource to be called once and return nil 433 mockService.EXPECT().GetResolutionResource(transactionID).Return(models.ResolutionResourceDao{}, nil) 434 435 res := serveHandleGetResolution(mockService, true) 436 437 So(res.Code, ShouldEqual, http.StatusNotFound) 438 }) 439 440 Convey("Success - Resolution was retrieved from insolvency resource", t, func() { 441 httpmock.Activate() 442 defer httpmock.DeactivateAndReset() 443 444 resolution := models.ResolutionResourceDao{ 445 DateOfResolution: "2021-06-06", 446 Attachments: []string{ 447 "1223-3445-5667", 448 }, 449 } 450 // Expect GetResolutionResource to be called once and return a resolution 451 mockService.EXPECT().GetResolutionResource(transactionID).Return(resolution, nil) 452 453 res := serveHandleGetResolution(mockService, true) 454 455 So(res.Code, ShouldEqual, http.StatusOK) 456 So(res.Body.String(), ShouldContainSubstring, "etag") 457 So(res.Body.String(), ShouldContainSubstring, "kind") 458 So(res.Body.String(), ShouldContainSubstring, "links") 459 So(res.Body.String(), ShouldContainSubstring, "date_of_resolution") 460 So(res.Body.String(), ShouldContainSubstring, "attachments") 461 }) 462 } 463 464 func serveHandleDeleteResolution(service dao.Service, tranIDSet bool) *httptest.ResponseRecorder { 465 path := "/transactions/123456789/insolvency/resolution" 466 req := httptest.NewRequest(http.MethodPost, path, nil) 467 if tranIDSet { 468 req = mux.SetURLVars(req, map[string]string{"transaction_id": transactionID}) 469 } 470 res := httptest.NewRecorder() 471 472 handler := HandleDeleteResolution(service) 473 handler.ServeHTTP(res, req) 474 475 return res 476 } 477 478 func TestUnitHandleDeleteResolution(t *testing.T) { 479 err := os.Chdir("..") 480 if err != nil { 481 log.ErrorR(nil, fmt.Errorf("error accessing root directory")) 482 } 483 484 mockCtrl := gomock.NewController(t) 485 defer mockCtrl.Finish() 486 487 mockService := mock_dao.NewMockService(mockCtrl) 488 489 Convey("Must need a transaction ID in the url", t, func() { 490 mockCtrl := gomock.NewController(t) 491 defer mockCtrl.Finish() 492 493 res := serveHandleDeleteResolution(mockService, false) 494 495 So(res.Code, ShouldEqual, http.StatusBadRequest) 496 }) 497 498 Convey("Error checking if transaction is closed against transaction api", t, func() { 499 httpmock.Activate() 500 defer httpmock.DeactivateAndReset() 501 502 // Expect the transaction api to be called and return an error 503 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusInternalServerError, "")) 504 505 res := serveHandleDeleteResolution(mockService, true) 506 507 So(res.Code, ShouldEqual, http.StatusInternalServerError) 508 }) 509 510 Convey("Transaction is already closed and cannot be updated", t, func() { 511 httpmock.Activate() 512 defer httpmock.DeactivateAndReset() 513 514 // Expect the transaction api to be called and return an already closed transaction 515 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponseClosed)) 516 517 res := serveHandleDeleteResolution(mockService, true) 518 519 So(res.Code, ShouldEqual, http.StatusForbidden) 520 }) 521 522 Convey("Failed to delete resolution from Insolvency resource", t, func() { 523 httpmock.Activate() 524 defer httpmock.DeactivateAndReset() 525 526 // Expect the transaction api to be called and return an open transaction 527 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 528 529 // Expect DeleteResolutionResource to be called once and return an error 530 mockService.EXPECT().DeleteResolutionResource(transactionID).Return(http.StatusInternalServerError, fmt.Errorf("there was a problem handling your request for transaction id [%s] - could not delete resolution", transactionID)) 531 532 res := serveHandleDeleteResolution(mockService, true) 533 534 So(res.Code, ShouldEqual, http.StatusInternalServerError) 535 }) 536 537 Convey("Resolution was not found on supplied transaction", t, func() { 538 httpmock.Activate() 539 defer httpmock.DeactivateAndReset() 540 541 // Expect the transaction api to be called and return an open transaction 542 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 543 544 // Expect DeleteResolutionResource to be called once and return an error 545 mockService.EXPECT().DeleteResolutionResource(transactionID).Return(http.StatusNotFound, fmt.Errorf("there was a problem handling your request for transaction id [%s] - resolution not found", transactionID)) 546 547 res := serveHandleDeleteResolution(mockService, true) 548 549 So(res.Code, ShouldEqual, http.StatusNotFound) 550 }) 551 552 Convey("Success - Resolution was deleted from insolvency resource", t, func() { 553 httpmock.Activate() 554 defer httpmock.DeactivateAndReset() 555 556 // Expect the transaction api to be called and return an open transaction 557 httpmock.RegisterResponder(http.MethodGet, "https://api.companieshouse.gov.uk/transactions/12345678", httpmock.NewStringResponder(http.StatusOK, transactionProfileResponse)) 558 559 // Expect DeleteResolutionResource to be called once and delete resolution 560 mockService.EXPECT().DeleteResolutionResource(transactionID).Return(http.StatusNoContent, nil) 561 562 res := serveHandleDeleteResolution(mockService, true) 563 564 So(res.Code, ShouldEqual, http.StatusNoContent) 565 }) 566 } 567 568 func generateResolution() models.Resolution { 569 return models.Resolution{ 570 DateOfResolution: "2021-06-06", 571 Attachments: []string{ 572 "123456789", 573 }, 574 } 575 } 576 577 func generateAttachment() models.AttachmentResourceDao { 578 return models.AttachmentResourceDao{ 579 ID: "1111", 580 Type: "resolution", 581 Status: "status", 582 Links: models.AttachmentResourceLinksDao{}, 583 } 584 } 585 586 func generateInsolvencyResource() models.InsolvencyResourceDao { 587 return models.InsolvencyResourceDao{ 588 Data: models.InsolvencyResourceDaoData{ 589 CompanyNumber: "1234", 590 CaseType: "CVL", 591 CompanyName: "Company", 592 }, 593 } 594 }