Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
- Fiddled around with some pointer stuff
- Fixed a typo that made the handleError function return the wrong error
  for conflict errors
- Fixed a typo that made the test for conflict errors use a mock
  not found response
- Gave not found error responses a payload (and refactored out the
  identical payloads used in the other error responses)
- Removed check for payload of 500 response. I'm not 100% sure if this
  is right. I see some of our handlers return payloads for 500s, and
  some that do not.
  • Loading branch information
mboren committed Jul 18, 2023
1 parent 000ff4c commit 6baeb00
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
13 changes: 7 additions & 6 deletions pkg/handlers/ghcapi/mto_shipment.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,13 +893,15 @@ func (h ReviewShipmentAddressUpdateHandler) Handle(params shipmentops.ReviewShip
remarks := params.Body.OfficeRemarks

response, err := h.ShipmentAddressUpdateRequester.ReviewShipmentAddressChange(appCtx, shipmentID, models.ShipmentAddressUpdateStatus(*addressApprovalStatus), *remarks)
fmt.Println(response)
handleError := func(err error) (middleware.Responder, error) {
appCtx.Logger().Error("ghcapi.ReviewShipmentAddressUpdateHandler", zap.Error(err))
payload := ghcmessages.Error{
Message: handlers.FmtString(err.Error()),
}

switch e := err.(type) {
case apperror.NotFoundError:
return shipmentops.NewReviewShipmentAddressUpdateNotFound(), err
return shipmentops.NewReviewShipmentAddressUpdateNotFound().WithPayload(&payload), err
case apperror.InvalidInputError:
payload := payloadForValidationError(
"Validation errors",
Expand All @@ -909,10 +911,10 @@ func (h ReviewShipmentAddressUpdateHandler) Handle(params shipmentops.ReviewShip
return shipmentops.NewReviewShipmentAddressUpdateUnprocessableEntity().WithPayload(payload), err
case apperror.PreconditionFailedError:
return shipmentops.NewReviewShipmentAddressUpdatePreconditionFailed().
WithPayload(&ghcmessages.Error{Message: handlers.FmtString(err.Error())}), err
case mtoshipment.ConflictStatusError:
WithPayload(&payload), err
case apperror.ConflictError:
return shipmentops.NewReviewShipmentAddressUpdateConflict().
WithPayload(&ghcmessages.Error{Message: handlers.FmtString(err.Error())}), err
WithPayload(&payload), err
default:
return shipmentops.NewReviewShipmentAddressUpdateInternalServerError(), err
}
Expand All @@ -921,7 +923,6 @@ func (h ReviewShipmentAddressUpdateHandler) Handle(params shipmentops.ReviewShip
return handleError(err)
}
payload := payloads.ShipmentAddressUpdate(response)
fmt.Println(payload)
return shipmentops.NewReviewShipmentAddressUpdateOK().WithPayload(payload), nil
})
}
Expand Down
17 changes: 6 additions & 11 deletions pkg/handlers/ghcapi/mto_shipment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2503,11 +2503,10 @@ func (suite *HandlerSuite) TestReviewShipmentAddressUpdateHandler() {
suite.NoError(params.Body.Validate(strfmt.Default))

response := handler.Handle(params)
errResponse := response.(*shipmentops.ReviewShipmentAddressUpdateUnprocessableEntity)
suite.IsType(shipmentops.ReviewShipmentAddressUpdateUnprocessableEntity{}, *errResponse)
suite.IsType(&shipmentops.ReviewShipmentAddressUpdateUnprocessableEntity{}, response)

errResponse := response.(*shipmentops.ReviewShipmentAddressUpdateUnprocessableEntity)
suite.NoError(errResponse.Payload.Validate(strfmt.Default))

})

suite.Run("PATCH Failure - 409 Request conflict response error", func() {
Expand Down Expand Up @@ -2536,7 +2535,7 @@ func (suite *HandlerSuite) TestReviewShipmentAddressUpdateHandler() {
ShipmentID: *handlers.FmtUUID(addressChange.ShipmentID),
}

err := apperror.NewNotFoundError(uuid.Nil, "unable to create ReviewShipmentAddressChange")
err := apperror.NewConflictError(uuid.Nil, "unable to create ReviewShipmentAddressChange")

mockCreator.On("ReviewShipmentAddressChange",
mock.AnythingOfType("*appcontext.appContext"),
Expand All @@ -2548,6 +2547,7 @@ func (suite *HandlerSuite) TestReviewShipmentAddressUpdateHandler() {
suite.NoError(params.Body.Validate(strfmt.Default))

response := handler.Handle(params)
suite.NotNil(response)
suite.IsType(&shipmentops.ReviewShipmentAddressUpdateConflict{}, response)
errResponse := response.(*shipmentops.ReviewShipmentAddressUpdateConflict)

Expand Down Expand Up @@ -2592,11 +2592,9 @@ func (suite *HandlerSuite) TestReviewShipmentAddressUpdateHandler() {
suite.NoError(params.Body.Validate(strfmt.Default))

response := handler.Handle(params)
suite.IsType(shipmentops.ReviewShipmentAddressUpdateNotFound{}, response)
suite.IsType(&shipmentops.ReviewShipmentAddressUpdateNotFound{}, response)
errResponse := response.(*shipmentops.ReviewShipmentAddressUpdateNotFound)

suite.NoError(errResponse.Payload.Validate(strfmt.Default))

})

suite.Run("500 server error", func() {
Expand Down Expand Up @@ -2637,10 +2635,7 @@ func (suite *HandlerSuite) TestReviewShipmentAddressUpdateHandler() {
suite.NoError(params.Body.Validate(strfmt.Default))

response := handler.Handle(params)
suite.IsType(shipmentops.ReviewShipmentAddressUpdateInternalServerError{}, response)
errResponse := response.(*shipmentops.ReviewShipmentAddressUpdateInternalServerError)

suite.NoError(errResponse.Payload.Validate(strfmt.Default))
suite.IsType(&shipmentops.ReviewShipmentAddressUpdateInternalServerError{}, response)
})
}

Expand Down

0 comments on commit 6baeb00

Please sign in to comment.