Skip to content

Commit

Permalink
Merge pull request #42 from hmrc/APID-191
Browse files Browse the repository at this point in the history
APID-191 - check for messageId in relatesTo before calling service
  • Loading branch information
sivaprakashiv authored Jun 28, 2021
2 parents 56ec4bd + fcd26e5 commit bb511d5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ class ConfirmationController @Inject()(cc: ControllerComponents,
val xml: NodeSeq = request.body

def callService(deliveryStatus: DeliveryStatus, id: String): Future[Result] = {
confirmationService.processConfirmation(xml, id, deliveryStatus) map {
case NoContentUpdateResult => NoContent
case _ =>
logger.warn(s"No message found with global ID [$id]. Request is ${xml}")
NotFound
if(id.trim.nonEmpty) {
confirmationService.processConfirmation(xml, id.trim, deliveryStatus) map {
case NoContentUpdateResult => NoContent
case _ =>
logger.warn(s"No message found with global ID [$id]. Request is ${xml}")
NotFound
}
} else {
Future.successful(BadRequest)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ class ConfirmationControllerSpec extends AnyWordSpec with Matchers with GuiceOne

"message" should {
val fakeRequest = FakeRequest("POST", "/acknowledgement")
val codMessage: String =
def createCodMessage(relatesTo: String) = {
"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ccn2="http://ccn2.ec.eu/CCN2.Service.Platform.Acknowledgement.Schema">
| <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
| <wsa:Action>CCN2.Service.Platform.AcknowledgementService/CoD</wsa:Action>
| <wsa:From>
| <wsa:Address>[FROM]</wsa:Address>
| </wsa:From>
| <wsa:RelatesTo RelationshipType="http://ccn2.ec.eu/addressing/ack">1234abcd</wsa:RelatesTo>
| <wsa:RelatesTo RelationshipType="http://ccn2.ec.eu/addressing/ack">[RELATES_TO]</wsa:RelatesTo>
| <wsa:MessageID>[COD_MESSAGE_ID]</wsa:MessageID>
| <wsa:To>[TO]</wsa:To>
| </soap:Header>
Expand All @@ -62,6 +62,8 @@ class ConfirmationControllerSpec extends AnyWordSpec with Matchers with GuiceOne
| </ccn2:CoD>
| </soap:Body>
|</soap:Envelope>""".stripMargin.replaceAll("\n", "")
.replaceAll("\\[RELATES_TO]", relatesTo)
}

val codMessageWithNoRelatesTo =
"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
Expand Down Expand Up @@ -123,7 +125,7 @@ class ConfirmationControllerSpec extends AnyWordSpec with Matchers with GuiceOne
val confirmationXmlRequestCaptor = ArgumentCaptor.forClass(classOf[NodeSeq])
val msgIdCaptor = ArgumentCaptor.forClass(classOf[String])
val confirmationTypeCaptor = ArgumentCaptor.forClass(classOf[DeliveryStatus])
val requestBodyXml: Elem = XML.loadString(codMessage)
val requestBodyXml: Elem = XML.loadString(createCodMessage("1234abcd"))
when(confirmationServiceMock.processConfirmation(confirmationXmlRequestCaptor.capture, msgIdCaptor.capture(), confirmationTypeCaptor.capture)(*))
.thenReturn(Future.successful(NoContentUpdateResult))
val result: Future[Result] = underTest.message()(fakeRequest.withBody(requestBodyXml)
Expand All @@ -144,6 +146,16 @@ class ConfirmationControllerSpec extends AnyWordSpec with Matchers with GuiceOne
verifyZeroInteractions(confirmationServiceMock)
}

"return 400 when exists an XML request with blank, whitespaces, newline and tab space in RelatesTo element" in new Setup {
Seq("", " ", "\n", "\t") foreach { relatesTo =>
val requestBodyXml: Elem = XML.loadString(createCodMessage(relatesTo))
val result: Future[Result] = underTest.message()(fakeRequest.withBody(requestBodyXml)
.withHeaders("ContentType" -> "text/xml", "x-soap-action" -> "CCN2.Service.Platform.AcknowledgementService/CoD"))
status(result) shouldBe BAD_REQUEST
verifyZeroInteractions(confirmationServiceMock)
}
}

"call the confirmation service with a CoE request" in new Setup {
val confirmationXmlRequestCaptor = ArgumentCaptor.forClass(classOf[NodeSeq])
val msgIdCaptor = ArgumentCaptor.forClass(classOf[String])
Expand All @@ -170,14 +182,14 @@ class ConfirmationControllerSpec extends AnyWordSpec with Matchers with GuiceOne
}

"handle receiving a request with an invalid SOAP action" in new Setup {
val result: Future[Result] = underTest.message()(fakeRequest.withBody(XML.loadString(codMessage))
val result: Future[Result] = underTest.message()(fakeRequest.withBody(XML.loadString(createCodMessage("1234abcd")))
.withHeaders("ContentType" -> "text/xml", "x-soap-action" -> "foobar"))
status(result) shouldBe BAD_REQUEST
verifyZeroInteractions(confirmationServiceMock)
}

"handle receiving a request with no x-soap-action header" in new Setup {
val result: Future[Result] = underTest.message()(fakeRequest.withBody(XML.loadString(codMessage))
val result: Future[Result] = underTest.message()(fakeRequest.withBody(XML.loadString(createCodMessage("1234abcd")))
.withHeaders("ContentType" -> "text/xml"))
status(result) shouldBe BAD_REQUEST
verifyZeroInteractions(confirmationServiceMock)
Expand Down

0 comments on commit bb511d5

Please sign in to comment.