Skip to content

Commit

Permalink
Merge pull request #1841 from betagouv/master
Browse files Browse the repository at this point in the history
MEP TRELLO-2865 sanitize incoming phone in reports
  • Loading branch information
eletallbetagouv authored Jan 14, 2025
2 parents ad6e538 + 998d0f0 commit 6e2e5c3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
20 changes: 13 additions & 7 deletions app/orchestrators/ReportOrchestrator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,22 @@ class ReportOrchestrator(
}.sequence
}

def validateAndCreateReport(draftReport: ReportDraft, consumerIp: ConsumerIp): Future[Report] =
def validateAndCreateReport(draft: ReportDraft, consumerIp: ConsumerIp): Future[Report] =
for {
_ <- validateCompany(draftReport)
_ <- validateSpamSimilarReport(draftReport)
_ <- validateReportIdentification(draftReport)
_ <- validateConsumerEmail(draftReport)
_ <- validateNumberOfAttachments(draftReport)
createdReport <- createReport(draftReport, consumerIp)
_ <- validateCompany(draft)
_ <- validateSpamSimilarReport(draft)
_ <- validateReportIdentification(draft)
_ <- validateConsumerEmail(draft)
_ <- validateNumberOfAttachments(draft)
cleanDraft = sanitizeDraft(draft)
createdReport <- createReport(cleanDraft, consumerIp)
} yield createdReport

private def sanitizeDraft(draft: ReportDraft): ReportDraft =
draft.copy(
phone = draft.phone.map(PhoneNumberUtils.sanitizeIncomingPhoneNumber)
)

private def validateReportIdentification(draftReport: ReportDraft) =
if (ReportDraft.isValid(draftReport)) {
Future.unit
Expand Down
15 changes: 15 additions & 0 deletions app/utils/PhoneNumberUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utils

object PhoneNumberUtils {

def sanitizeIncomingPhoneNumber(phone: String): String =
phone
// remove all spaces
.replaceAll("\\s+", "")
// remove all dots
.replaceAll("\\.+", "")
// Replace french indicator format +33X or 0033X by just the classic 0X
.replaceFirst("^\\+33", "0")
.replaceFirst("^0033", "0")

}
38 changes: 38 additions & 0 deletions test/utils/PhoneNumberUtilsSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package utils

import org.specs2.mutable.Specification
import utils.PhoneNumberUtils.sanitizeIncomingPhoneNumber

class PhoneNumberUtilsSpec extends Specification {
"PhoneNumberUtils" should {

"leave typical phone number as is" in {
sanitizeIncomingPhoneNumber("0545653312") shouldEqual "0545653312"
}

"leave typical small phone number as is" in {
sanitizeIncomingPhoneNumber("3605") shouldEqual "3605"
}

"remove spaces" in {
sanitizeIncomingPhoneNumber("05 45 65 33 12") shouldEqual "0545653312"
}

"remove dots" in {
sanitizeIncomingPhoneNumber("05.45.65.33.12") shouldEqual "0545653312"
}

"remove +33" in {
sanitizeIncomingPhoneNumber("+33545653312") shouldEqual "0545653312"
}

"remove 0033" in {
sanitizeIncomingPhoneNumber("0033545653312") shouldEqual "0545653312"
}

"remove a combination of all of that" in {
sanitizeIncomingPhoneNumber(" +33 5 45.65.33. 12 ") shouldEqual "0545653312"
}

}
}

0 comments on commit 6e2e5c3

Please sign in to comment.