Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ class AddressSubmissionControllerHelper @Inject() (
def isStartDateError(error: UpstreamErrorResponse): Boolean =
error.statusCode == 400 && error.message.toLowerCase().contains("start date")

val p85enabled: Boolean = !isUk(journeyData.submittedInternationalAddressChoiceDto)
val p85enabled: Boolean = journeyData.submittedInternationalAddressChoiceDto match {
case Some(choice) => !isUk(Some(choice))
case None => true
}

val startDateErrorResponse: Result =
BadRequest(
Expand Down
64 changes: 35 additions & 29 deletions app/models/dto/AddressDto.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,41 @@ case class AddressDto(
def toAddress(`type`: String, startDate: LocalDate): Address = {
val List(newLine2, newLine3, newline4OrTown, newline5OrCounty) =
List(line2, line3, line4OrTown, line5OrCounty).flatten.map(Some(_)).padTo(4, None)
postcode match {
case Some(postcode) =>
Address(
Some(line1),
newLine2,
newLine3,
newline4OrTown,
newline5OrCounty,
Some(formatMandatoryPostCode(postcode)),
None,
Some(startDate),
None,
Some(`type`),
isRls = false
)
case None =>
Address(
Some(line1),
newLine2,
newLine3,
newline4OrTown,
newline5OrCounty,
None,
country,
Some(startDate),
None,
Some(`type`),
isRls = false
)

val normalisedPostcode: Option[String] =
postcode.map(_.trim).filter(_.nonEmpty)

val isInternational: Boolean =
country.exists(_.trim.nonEmpty)

if (isInternational) {
Address(
Some(line1),
newLine2,
newLine3,
newline4OrTown,
newline5OrCounty,
None,
country,
Some(startDate),
None,
Some(`type`),
isRls = false
)
} else {
Address(
Some(line1),
newLine2,
newLine3,
newline4OrTown,
newline5OrCounty,
normalisedPostcode.map(formatMandatoryPostCode),
None,
Some(startDate),
None,
Some(`type`),
isRls = false
)
}
}

Expand Down
6 changes: 6 additions & 0 deletions app/services/AddressMovedService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import models.addresslookup.Country
import models.{AddressChanged, AnyOtherMove, MovedFromScotland, MovedToScotland}
import uk.gov.hmrc.http.HeaderCarrier
import play.api.Logging
import util.PertaxValidators.PostcodeRegex

import scala.concurrent.{ExecutionContext, Future}

class AddressMovedService @Inject() (addressLookupService: AddressLookupConnector) extends Logging {

private def isValidUkPostcode(p: String): Boolean = PostcodeRegex.pattern.matcher(p.trim.toUpperCase).matches()

def moved(originalPostcode: String, newPostcode: String, p85Enabled: Boolean)(implicit
hc: HeaderCarrier,
ec: ExecutionContext
Expand All @@ -39,6 +42,9 @@ class AddressMovedService @Inject() (addressLookupService: AddressLookupConnecto
logger.error("New postcode is empty when checking for address move.")
Future.successful(AnyOtherMove)
case ("", _) => Future.successful(AnyOtherMove)
case (originalPostcode, newPostcode)
if !isValidUkPostcode(originalPostcode) || !isValidUkPostcode(newPostcode) =>
Future.successful(AnyOtherMove)
case (originalPostcode, newPostcode) =>
(for {
fromResponse <- addressLookupService.lookup(originalPostcode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,19 @@ import models.dto.{AddressDto, DateDto}
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito.{reset, times, verify, when}
import play.api
import play.api.http.Status.{BAD_REQUEST, INTERNAL_SERVER_ERROR, OK}
import play.api.i18n.{Lang, Messages, MessagesImpl, MessagesProvider}
import play.api.mvc.Request
import play.api.test.Helpers.{contentAsString, defaultAwaitTimeout, status}
import services.{AddressMovedService, CitizenDetailsService}
import testUtils.BaseSpec
import play.api.http.Status.{BAD_REQUEST, INTERNAL_SERVER_ERROR, OK}
import play.api.mvc.Request
import testUtils.Fixtures.buildPersonDetails
import testUtils.UserRequestFixture.buildUserRequest
import uk.gov.hmrc.http.UpstreamErrorResponse
import uk.gov.hmrc.play.audit.http.connector.{AuditConnector, AuditResult}
import uk.gov.hmrc.play.audit.model.DataEvent
import uk.gov.hmrc.play.language.LanguageUtils
import views.html.personaldetails.{CannotUpdateAddressEarlyDateView, UpdateAddressConfirmationView}
import testUtils.Fixtures.buildPersonDetails

import java.time.LocalDate
import scala.concurrent.ExecutionContext.Implicits.global
Expand Down Expand Up @@ -89,21 +88,15 @@ class AddressSubmissionControllerHelperSpec extends BaseSpec {

"Calling updateCitizenDetailsAddress" must {
"update address" when {

"address is residential" in {
when(mockCitizenDetailsService.updateAddress(any(), any(), any(), any())(any(), any(), any()))
.thenReturn(EitherT.rightT[Future, UpstreamErrorResponse](true))
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(
Future.successful(AuditResult.Success)
)
when(mockEditAddressLockRepository.insert(any(), any())).thenReturn(
Future.successful(true)
)
when(mockAddressMovedService.moved(any(), any(), any())(any(), any())).thenReturn(
Future.successful(AnyOtherMove)
)
when(mockAddressMovedService.toMessageKey(any())).thenReturn(
Some("label.mockAddressMovedService")
)
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(Future.successful(AuditResult.Success))
when(mockEditAddressLockRepository.insert(any(), any())).thenReturn(Future.successful(true))
when(mockAddressMovedService.moved(any(), any(), any())(any(), any()))
.thenReturn(Future.successful(AnyOtherMove))
when(mockAddressMovedService.toMessageKey(any())).thenReturn(Some("label.mockAddressMovedService"))

val addressJourneyData = AddressJourneyData(
None,
Expand All @@ -112,7 +105,7 @@ class AddressSubmissionControllerHelperSpec extends BaseSpec {
None,
None,
None,
None,
Some(models.dto.InternationalAddressChoiceDto.Scotland),
None
)

Expand Down Expand Up @@ -143,18 +136,11 @@ class AddressSubmissionControllerHelperSpec extends BaseSpec {
"address is postal" in {
when(mockCitizenDetailsService.updateAddress(any(), any(), any(), any())(any(), any(), any()))
.thenReturn(EitherT.rightT[Future, UpstreamErrorResponse](true))
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(
Future.successful(AuditResult.Success)
)
when(mockEditAddressLockRepository.insert(any(), any())).thenReturn(
Future.successful(true)
)
when(mockAddressMovedService.moved(any(), any(), any())(any(), any())).thenReturn(
Future.successful(AnyOtherMove)
)
when(mockAddressMovedService.toMessageKey(any())).thenReturn(
Some("label.mockAddressMovedService")
)
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(Future.successful(AuditResult.Success))
when(mockEditAddressLockRepository.insert(any(), any())).thenReturn(Future.successful(true))
when(mockAddressMovedService.moved(any(), any(), any())(any(), any()))
.thenReturn(Future.successful(AnyOtherMove))
when(mockAddressMovedService.toMessageKey(any())).thenReturn(Some("label.mockAddressMovedService"))

val addressJourneyData = AddressJourneyData(
None,
Expand All @@ -163,7 +149,7 @@ class AddressSubmissionControllerHelperSpec extends BaseSpec {
None,
None,
None,
None,
Some(models.dto.InternationalAddressChoiceDto.Scotland),
None
)

Expand Down Expand Up @@ -194,18 +180,11 @@ class AddressSubmissionControllerHelperSpec extends BaseSpec {
"address is international" in {
when(mockCitizenDetailsService.updateAddress(any(), any(), any(), any())(any(), any(), any()))
.thenReturn(EitherT.rightT[Future, UpstreamErrorResponse](true))
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(
Future.successful(AuditResult.Success)
)
when(mockEditAddressLockRepository.insert(any(), any())).thenReturn(
Future.successful(true)
)
when(mockAddressMovedService.moved(any(), any(), any())(any(), any())).thenReturn(
Future.successful(AnyOtherMove)
)
when(mockAddressMovedService.toMessageKey(any())).thenReturn(
Some("label.mockAddressMovedService")
)
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(Future.successful(AuditResult.Success))
when(mockEditAddressLockRepository.insert(any(), any())).thenReturn(Future.successful(true))
when(mockAddressMovedService.moved(any(), any(), any())(any(), any()))
.thenReturn(Future.successful(AnyOtherMove))
when(mockAddressMovedService.toMessageKey(any())).thenReturn(Some("label.mockAddressMovedService"))

val addressJourneyData = AddressJourneyData(
None,
Expand Down Expand Up @@ -254,18 +233,10 @@ class AddressSubmissionControllerHelperSpec extends BaseSpec {
)
)
)
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(
Future.successful(AuditResult.Success)
)
when(mockEditAddressLockRepository.insert(any(), any())).thenReturn(
Future.successful(true)
)
when(mockAddressMovedService.moved(any(), any(), any())(any(), any())).thenReturn(
Future.successful(AnyOtherMove)
)
when(mockAddressMovedService.toMessageKey(any())).thenReturn(
Some("label.mockAddressMovedService")
)
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(Future.successful(AuditResult.Success))
when(mockEditAddressLockRepository.insert(any(), any())).thenReturn(Future.successful(true))
when(mockAddressMovedService.moved(any(), any(), any())(any(), any())).thenReturn(Future.successful(AnyOtherMove))
when(mockAddressMovedService.toMessageKey(any())).thenReturn(Some("label.mockAddressMovedService"))

val addressJourneyData = AddressJourneyData(
None,
Expand Down Expand Up @@ -312,18 +283,10 @@ class AddressSubmissionControllerHelperSpec extends BaseSpec {
)
)
)
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(
Future.successful(AuditResult.Success)
)
when(mockEditAddressLockRepository.insert(any(), any())).thenReturn(
Future.successful(true)
)
when(mockAddressMovedService.moved(any(), any(), any())(any(), any())).thenReturn(
Future.successful(AnyOtherMove)
)
when(mockAddressMovedService.toMessageKey(any())).thenReturn(
Some("label.mockAddressMovedService")
)
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(Future.successful(AuditResult.Success))
when(mockEditAddressLockRepository.insert(any(), any())).thenReturn(Future.successful(true))
when(mockAddressMovedService.moved(any(), any(), any())(any(), any())).thenReturn(Future.successful(AnyOtherMove))
when(mockAddressMovedService.toMessageKey(any())).thenReturn(Some("label.mockAddressMovedService"))

val addressJourneyData = AddressJourneyData(
None,
Expand Down Expand Up @@ -359,9 +322,7 @@ class AddressSubmissionControllerHelperSpec extends BaseSpec {

"Calling handleAddressChangeAuditing" must {
"audit when not modified" in {
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(
Future.successful(AuditResult.Success)
)
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(Future.successful(AuditResult.Success))

val addressDto: AddressDto =
AddressDto("AddressLine1", Some("AddressLine2"), None, None, None, Some("TestPostcode"), None, None, None)
Expand All @@ -384,9 +345,7 @@ class AddressSubmissionControllerHelperSpec extends BaseSpec {
}

"audit when heavily modified" in {
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(
Future.successful(AuditResult.Success)
)
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(Future.successful(AuditResult.Success))

val addressDto: AddressDto =
AddressDto("AddressLine1", Some("AddressLine2"), None, None, None, Some("TestPostcode"), None, None, None)
Expand All @@ -412,9 +371,7 @@ class AddressSubmissionControllerHelperSpec extends BaseSpec {
}

"audit when slightly modified" in {
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(
Future.successful(AuditResult.Success)
)
when(mockAuditConnector.sendEvent(any())(any(), any())).thenReturn(Future.successful(AuditResult.Success))

val addressDto: AddressDto =
AddressDto("AddressLine1", Some("AddressLine2"), None, None, None, Some("TestPostcode"), None, None, None)
Expand Down
41 changes: 36 additions & 5 deletions test/models/dto/AddressDtoSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ class AddressDtoSpec extends BaseSpec {

"Calling AddressDto.toAddress" must {

"return address with postcode and not country when postcode exists" in {
"return address with postcode and not country when postcode exists and country is empty" in {
val addressDto =
AddressDto(
"Line 1",
Expand All @@ -762,7 +762,7 @@ class AddressDtoSpec extends BaseSpec {
Some("Line 4"),
Some("Line 5"),
Some("AA1 1AA"),
Some("UK"),
None,
None,
None
)
Expand All @@ -784,6 +784,37 @@ class AddressDtoSpec extends BaseSpec {
)
}

"return address with country and drop postcode when country exists even if postcode exists (international)" in {
val addressDto =
AddressDto(
"Line 1",
Some("Line 2"),
Some("Line 3"),
Some("Line 4"),
Some("Line 5"),
Some("75001"),
Some("France"),
None,
None
)
val addressTye = "residential"
val startDate = LocalDate.of(2019, 1, 1)

addressDto.toAddress(addressTye, startDate) mustBe Address(
Some("Line 1"),
Some("Line 2"),
Some("Line 3"),
Some("Line 4"),
Some("Line 5"),
None,
Some("France"),
Some(startDate),
None,
Some(addressTye),
isRls = false
)
}

"return address with country when postcode does not exist" in {
val addressDto = AddressDto("Line 1", Some("Line 2"), Some("Line 3"), None, None, None, Some("UK"), None, None)
val addressTye = "residential"
Expand All @@ -809,7 +840,7 @@ class AddressDtoSpec extends BaseSpec {

"return formatted postcode when it contains 7 characters" in {
val addressDto =
AddressDto("Line 1", Some("Line 2"), Some("Line 3"), None, None, Some("AA9A9AA"), Some("UK"), None, None)
AddressDto("Line 1", Some("Line 2"), Some("Line 3"), None, None, Some("AA9A9AA"), None, None, None)
val addressTye = "residential"
val startDate = LocalDate.of(2019, 1, 1)

Expand All @@ -830,7 +861,7 @@ class AddressDtoSpec extends BaseSpec {

"return formatted postcode when it contains 6 characters" in {
val addressDto =
AddressDto("Line 1", Some("Line 2"), Some("Line 3"), None, None, Some("A9A9AA"), Some("UK"), None, None)
AddressDto("Line 1", Some("Line 2"), Some("Line 3"), None, None, Some("A9A9AA"), None, None, None)
val addressTye = "residential"
val startDate = LocalDate.of(2019, 1, 1)

Expand All @@ -851,7 +882,7 @@ class AddressDtoSpec extends BaseSpec {

"return formatted postcode when it contains 5 characters" in {
val addressDto =
AddressDto("Line 1", Some("Line 2"), Some("Line 3"), None, None, Some("A99AA"), Some("UK"), None, None)
AddressDto("Line 1", Some("Line 2"), Some("Line 3"), None, None, Some("A99AA"), None, None, None)
val addressTye = "residential"
val startDate = LocalDate.of(2019, 1, 1)

Expand Down
Loading