Skip to content

Commit

Permalink
[TRELLO-2037] Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
charlescd committed Oct 18, 2023
1 parent eb56001 commit 1803a42
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/controllers/AdminController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import services.Email.ConsumerReportClosedNoAction
import services.Email.ConsumerReportClosedNoReading
import services.Email.ConsumerReportReadByProNotification
import services.Email.ConsumerValidateEmail
import services.Email.DgccrfAccessLink
import services.Email.AgentAccessLink
import services.Email.DgccrfDangerousProductReportNotification
import services.Email.DgccrfReportNotification
import services.Email.InactiveDgccrfAccount
Expand Down Expand Up @@ -274,7 +274,7 @@ class AdminController(
)
}),
"dgccrf.access_link" ->
(DgccrfAccessLink(_, frontRoute.dashboard.Dgccrf.register(token = "abc"))),
(AgentAccessLink("DGCCRF")(_, frontRoute.dashboard.Agent.register(token = "abc"))),
"dgccrf.validate_email" ->
(ValidateEmail(_, 7, frontRoute.dashboard.validateEmail(""))),
"dgccrf.report_dangerous_product_notification" -> (recipient =>
Expand Down
12 changes: 6 additions & 6 deletions app/orchestrators/AccessesOrchestrator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import models.token.TokenKind.ValidateEmail
import play.api.Logger
import repositories.accesstoken.AccessTokenRepositoryInterface
import services.Email.AdminAccessLink
import services.Email.DgccrfAccessLink
import services.Email.AgentAccessLink
import services.Email
import services.EmailAddressService
import services.MailServiceInterface
Expand Down Expand Up @@ -108,15 +108,15 @@ class AccessesOrchestrator(
(
EmailAddressService.isEmailAcceptableForDgccrfAccount _,
tokenConfiguration.dgccrfJoinDuration,
DgccrfAccessLink,
frontRoute.dashboard.Dgccrf.register _
AgentAccessLink("DGCCRF") _,
frontRoute.dashboard.Agent.register _
)
case DGALAccount =>
(
EmailAddressService.isEmailAcceptableForDgccrfAccount _,
EmailAddressService.isEmailAcceptableForDgalAccount _,
tokenConfiguration.dgccrfJoinDuration,
DgccrfAccessLink,
frontRoute.dashboard.Dgccrf.register _
AgentAccessLink("DGAL") _,
frontRoute.dashboard.Agent.register _
)
case AdminAccount =>
(
Expand Down
4 changes: 2 additions & 2 deletions app/services/Email.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ object Email {
views.html.mails.dgccrf.reportDangerousProductNotification(report)(frontRoute, contact).toString
}

final case class DgccrfAccessLink(recipient: EmailAddress, invitationUrl: URI) extends DgccrfEmail {
final case class AgentAccessLink(role: String)(recipient: EmailAddress, invitationUrl: URI) extends DgccrfEmail {
override val subject: String = EmailSubjects.DGCCRF_ACCESS_LINK
override def getBody: (FrontRoute, EmailAddress) => String = (_, _) =>
views.html.mails.dgccrf.accessLink(invitationUrl).toString
views.html.mails.dgccrf.accessLink(invitationUrl, role).toString
override val recipients: List[EmailAddress] = List(recipient)
}

Expand Down
4 changes: 4 additions & 0 deletions app/services/EmailAddressService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ object EmailAddressService {

private val adminEmailRegexp = """.+\.betagouv(\+.+)?@gmail\.com|.+@beta\.gouv\.fr|.+@dgccrf\.finances\.gouv\.fr""".r
private val dgccrfEmailRegexp = """.+\.gouv\.fr""".r
private val dgalEmailRegexp = """.+\.gouv\.fr""".r

def isEmailAcceptableForAdminAccount(emailAddress: String): Boolean =
adminEmailRegexp.matches(emailAddress)

def isEmailAcceptableForDgccrfAccount(emailAddress: String): Boolean =
dgccrfEmailRegexp.matches(emailAddress)

def isEmailAcceptableForDgalAccount(emailAddress: String): Boolean =
dgalEmailRegexp.matches(emailAddress)

}
4 changes: 2 additions & 2 deletions app/utils/FrontRoute.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class FrontRoute(signalConsoConfiguration: SignalConsoConfiguration) {
object Admin {
def register(token: String) = url(s"/admin/rejoindre/?token=$token")
}
object Dgccrf {
def register(token: String) = url(s"/dgccrf/rejoindre/?token=$token")
object Agent {
def register(token: String) = url(s"/agent/rejoindre/?token=$token")
}
object Pro {
def subscriptionDGCCRFCompanySummary(companyId: UUID) = url(
Expand Down
4 changes: 2 additions & 2 deletions app/views/mails/dgccrf/accessLink.scala.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
@import java.net.URI
@import utils.FrontRoute
@(invitationUrl: URI)
@(invitationUrl: URI, role: String)

@views.html.mails.layout("Accédez à SignalConso") {
<p>
Bonjour,
</p>

<p>
Vous avez été invité à accéder à l'interface DGCCRF sur SignalConso.
Vous avez été invité à accéder à l'interface @role sur SignalConso.
<br/>Afin de procéder à l'activation de votre compte, veuillez suivre le lien sécurisé ci-dessous :
</p>

Expand Down
18 changes: 18 additions & 0 deletions test/controllers/AccountControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ class AccountControllerSpec(implicit ee: ExecutionEnv)
Helpers.status(result) must beEqualTo(200)
}

"send an invalid DGAL invitation" in {
val request = FakeRequest(POST, routes.AccountController.sendAgentInvitation(UserRole.DGAL).toString)
.withAuthenticator[AuthEnv](identLoginInfo)
.withJsonBody(Json.obj("email" -> "user@example.com"))

val result = route(app, request).get
Helpers.status(result) must beEqualTo(403)
}

"send a DGAL invitation" in {
val request = FakeRequest(POST, routes.AccountController.sendAgentInvitation(UserRole.DGAL).toString)
.withAuthenticator[AuthEnv](identLoginInfo)
.withJsonBody(Json.obj("email" -> "user@dgal.gouv.fr"))

val result = route(app, request).get
Helpers.status(result) must beEqualTo(200)
}

"activate the DGCCF user" in {
val ccrfUser = Fixtures.genUser.sample.get
val ccrfToken =
Expand Down
39 changes: 39 additions & 0 deletions test/tasks/report/ReportNotificationTaskTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package tasks.report
import models.Subscription
import models.UserRole
import models.company.Address
import models.report.ReportCategory
import models.report.ReportFile
import models.report.ReportTag
import org.specs2.mutable.Specification
import repositories.report.ReportRepository.ReportOrdering
import utils.Country.Bangladesh
Expand Down Expand Up @@ -45,6 +47,43 @@ class ReportNotificationTaskTest extends Specification {
res.keys.toList shouldEqual List(frenchReport)

}

"filter reports when user is DGAL" in {
val company = Fixtures.genCompany.sample.get
val baseReport = Fixtures.genReportForCompany(company).sample.get

val accessibleReport1 =
baseReport.copy(id = UUID.randomUUID(), category = ReportCategory.IntoxicationAlimentaire.entryName)
val accessibleReport2 = baseReport.copy(id = UUID.randomUUID(), tags = List(ReportTag.Hygiene))
val accessibleReport3 = baseReport.copy(id = UUID.randomUUID(), tags = List(ReportTag.ProduitAlimentaire))
val inaccessibleReport = baseReport.copy(
id = UUID.randomUUID(),
tags = List(ReportTag.ProduitDangereux),
category = ReportCategory.AchatMagasin.entryName
)

val reports = List(
(accessibleReport1, List.empty[ReportFile]),
(accessibleReport2, List.empty[ReportFile]),
(accessibleReport3, List.empty[ReportFile]),
(inaccessibleReport, List.empty[ReportFile])
)

val reportsMap = SortedMap.from(reports)(ReportOrdering)

val subscription =
new Subscription(
userId = None,
email = None,
departments = List.empty,
frequency = java.time.Period.ofDays(1),
userRole = Some(UserRole.DGAL)
)

val res = ReportNotificationTask.refineReportBasedOnSubscriptionFilters(reportsMap, subscription)

res.keys.toList should contain(accessibleReport1, accessibleReport2, accessibleReport3)
}
}

}

0 comments on commit 1803a42

Please sign in to comment.