Skip to content

Commit

Permalink
Merge pull request #516 from hmrc/APB-8276-cleanup
Browse files Browse the repository at this point in the history
APB-8276 replacing fallback format with generic crypto format
  • Loading branch information
VLukovski authored Sep 25, 2024
2 parents 7cd12f9 + 2129322 commit e469be0
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 64 deletions.
20 changes: 10 additions & 10 deletions app/uk/gov/hmrc/agentservicesaccount/models/AgencyDetails.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package uk.gov.hmrc.agentservicesaccount.models

import play.api.libs.functional.syntax.{toFunctionalBuilderOps, unlift}
import play.api.libs.json.{Format, Json, OFormat, __}
import uk.gov.hmrc.agentservicesaccount.utils.EncryptedStringUtil.fallbackStringFormat
import uk.gov.hmrc.crypto.json.JsonEncryption.stringEncrypterDecrypter
import uk.gov.hmrc.crypto.{Decrypter, Encrypter}

case class BusinessAddress(
Expand All @@ -34,12 +34,12 @@ object BusinessAddress {

def databaseFormat(implicit crypto: Encrypter with Decrypter): Format[BusinessAddress] =
(
(__ \ "addressLine1").format[String](fallbackStringFormat) and
(__ \ "addressLine2").formatNullable[String](fallbackStringFormat) and
(__ \ "addressLine3").formatNullable[String](fallbackStringFormat) and
(__ \ "addressLine4").formatNullable[String](fallbackStringFormat) and
(__ \ "postalCode").formatNullable[String](fallbackStringFormat) and
(__ \ "countryCode").format[String](fallbackStringFormat)
(__ \ "addressLine1").format[String](stringEncrypterDecrypter) and
(__ \ "addressLine2").formatNullable[String](stringEncrypterDecrypter) and
(__ \ "addressLine3").formatNullable[String](stringEncrypterDecrypter) and
(__ \ "addressLine4").formatNullable[String](stringEncrypterDecrypter) and
(__ \ "postalCode").formatNullable[String](stringEncrypterDecrypter) and
(__ \ "countryCode").format[String](stringEncrypterDecrypter)
)(BusinessAddress.apply, unlift(BusinessAddress.unapply))
}

Expand All @@ -55,9 +55,9 @@ object AgencyDetails {

def databaseFormat(implicit crypto: Encrypter with Decrypter): Format[AgencyDetails] =
(
(__ \ "agencyName").formatNullable[String](fallbackStringFormat) and
(__ \ "agencyEmail").formatNullable[String](fallbackStringFormat) and
(__ \ "agencyTelephone").formatNullable[String](fallbackStringFormat) and
(__ \ "agencyName").formatNullable[String](stringEncrypterDecrypter) and
(__ \ "agencyEmail").formatNullable[String](stringEncrypterDecrypter) and
(__ \ "agencyTelephone").formatNullable[String](stringEncrypterDecrypter) and
(__ \ "agencyAddress").formatNullable[BusinessAddress](BusinessAddress.databaseFormat)
)(AgencyDetails.apply, unlift(AgencyDetails.unapply))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package uk.gov.hmrc.agentservicesaccount.models.desiDetails

import play.api.libs.functional.syntax.{toFunctionalBuilderOps, unlift}
import play.api.libs.json.{Format, Json, OFormat, __}
import uk.gov.hmrc.agentservicesaccount.utils.EncryptedStringUtil.fallbackStringFormat
import uk.gov.hmrc.crypto.json.JsonEncryption.stringEncrypterDecrypter
import uk.gov.hmrc.crypto.{Decrypter, Encrypter}
import uk.gov.hmrc.domain.{CtUtr, SaUtr}

Expand All @@ -33,7 +33,7 @@ object CtChanges {
def databaseFormat(implicit crypto: Encrypter with Decrypter): Format[CtChanges] =
(
(__ \ "applyChanges").format[Boolean] and
(__ \ "ctAgentReference").formatNullable[String](fallbackStringFormat)
(__ \ "ctAgentReference").formatNullable[String](stringEncrypterDecrypter)
.bimap[Option[CtUtr]](
_.map(CtUtr(_)),
_.map(_.utr)
Expand All @@ -53,7 +53,7 @@ object SaChanges {
def databaseFormat(implicit crypto: Encrypter with Decrypter): Format[SaChanges] =
(
(__ \ "applyChanges").format[Boolean] and
(__ \ "saAgentReference").formatNullable[String](fallbackStringFormat)
(__ \ "saAgentReference").formatNullable[String](stringEncrypterDecrypter)
.bimap[Option[SaUtr]](
_.map(SaUtr(_)),
_.map(_.utr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package uk.gov.hmrc.agentservicesaccount.models.desiDetails

import play.api.libs.functional.syntax.{toFunctionalBuilderOps, unlift}
import play.api.libs.json.{Format, Json, __}
import uk.gov.hmrc.agentservicesaccount.utils.EncryptedStringUtil.fallbackStringFormat
import uk.gov.hmrc.crypto.json.JsonEncryption.stringEncrypterDecrypter
import uk.gov.hmrc.crypto.{Decrypter, Encrypter}

case class YourDetails(fullName: String,
Expand All @@ -29,7 +29,7 @@ object YourDetails {

def databaseFormat(implicit crypto: Encrypter with Decrypter): Format[YourDetails] =
(
(__ \ "fullName").format[String](fallbackStringFormat) and
(__ \ "telephone").format[String](fallbackStringFormat)
(__ \ "fullName").format[String](stringEncrypterDecrypter) and
(__ \ "telephone").format[String](stringEncrypterDecrypter)
)(YourDetails.apply, unlift(YourDetails.unapply))
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import play.api.mvc.{Request, Result}
import uk.gov.hmrc.agentservicesaccount.controllers.{ARN, DESCRIPTION, DRAFT_NEW_CONTACT_DETAILS, DRAFT_SUBMITTED_BY, EMAIL, EMAIL_PENDING_VERIFICATION, NAME, PHONE, sessionKeys}
import uk.gov.hmrc.agentservicesaccount.models.desiDetails.{DesignatoryDetails, YourDetails}
import uk.gov.hmrc.agentservicesaccount.repository.SessionCacheRepository
import uk.gov.hmrc.agentservicesaccount.utils.EncryptedStringUtil
import uk.gov.hmrc.crypto.json.JsonEncryption.stringEncrypterDecrypter
import uk.gov.hmrc.crypto.{Decrypter, Encrypter}
import uk.gov.hmrc.mongo.cache.DataKey

Expand All @@ -43,7 +43,7 @@ class SessionCacheService @Inject()(sessionCacheRepository: SessionCacheReposito
(implicit reads: Reads[T], request: Request[_]): Future[Option[T]] = {
dataKey match {
case key: DataKey[String @unchecked] if Seq(NAME, EMAIL, PHONE, ARN, DESCRIPTION, EMAIL_PENDING_VERIFICATION).contains(key) =>
sessionCacheRepository.getFromSession(key)(EncryptedStringUtil.fallbackStringFormat, request)
sessionCacheRepository.getFromSession(key)(stringEncrypterDecrypter, request)
case key: DataKey[DesignatoryDetails @unchecked] if key == DRAFT_NEW_CONTACT_DETAILS =>
sessionCacheRepository.getFromSession(key)(DesignatoryDetails.databaseFormat, request)
case key: DataKey[YourDetails @unchecked] if key == DRAFT_SUBMITTED_BY =>
Expand All @@ -57,7 +57,7 @@ class SessionCacheService @Inject()(sessionCacheRepository: SessionCacheReposito
(implicit writes: Writes[T], request: Request[_]): Future[(String, String)] = {
dataKey match {
case key: DataKey[String @unchecked] if Seq(NAME, EMAIL, PHONE, ARN, DESCRIPTION, EMAIL_PENDING_VERIFICATION).contains(key) =>
sessionCacheRepository.putSession(key, value)(EncryptedStringUtil.fallbackStringFormat, request)
sessionCacheRepository.putSession(key, value)(stringEncrypterDecrypter, request)
case key: DataKey[DesignatoryDetails @unchecked] if key == DRAFT_NEW_CONTACT_DETAILS =>
sessionCacheRepository.putSession(key, value)(DesignatoryDetails.databaseFormat, request)
case key: DataKey[YourDetails @unchecked] if key == DRAFT_SUBMITTED_BY =>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,9 @@ class DesignatoryDetailsSpec extends UnitSpec {
}
}
"using crypto format" should {
"serialise to Json correctly" in {
"serialise to encrypted Json correctly" in {
Json.toJson(testDesignatoryDetails)(DesignatoryDetails.databaseFormat) shouldBe testEncryptedJson
}
"deserialise from unencrypted Json correctly" in {
testJson.as[DesignatoryDetails](DesignatoryDetails.databaseFormat) shouldBe testDesignatoryDetails
}
"deserialise from encrypted Json correctly" in {
testEncryptedJson.as[DesignatoryDetails](DesignatoryDetails.databaseFormat) shouldBe testDesignatoryDetails
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ class YourDetailsSpec extends UnitSpec {
}
}
"using crypto format" should {
"serialise to Json correctly" in {
"serialise to encrypted Json correctly" in {
Json.toJson(testYourDetails)(YourDetails.databaseFormat) shouldBe testEncryptedJson
}
"deserialise from unencrypted Json correctly" in {
testJson.as[YourDetails](YourDetails.databaseFormat) shouldBe testYourDetails
}
"deserialise from encrypted Json correctly" in {
testEncryptedJson.as[YourDetails](YourDetails.databaseFormat) shouldBe testYourDetails
}
Expand Down

0 comments on commit e469be0

Please sign in to comment.