From 6459f3c2434817bbeab2250425b4d9d2ec1be4d4 Mon Sep 17 00:00:00 2001 From: eletallbetagouv <107104509+eletallbetagouv@users.noreply.github.com> Date: Wed, 22 Jan 2025 13:10:05 +0100 Subject: [PATCH] fix tests --- app/loader/SignalConsoApplicationLoader.scala | 8 +-- .../report/sampledata/ReportGenerator.scala | 4 +- .../report/SampleDataGenerationTaskTest.scala | 68 ++++++++++++++++++- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/app/loader/SignalConsoApplicationLoader.scala b/app/loader/SignalConsoApplicationLoader.scala index 781ae03d..e51307a9 100644 --- a/app/loader/SignalConsoApplicationLoader.scala +++ b/app/loader/SignalConsoApplicationLoader.scala @@ -635,10 +635,10 @@ class SignalConsoComponents( private val reportOrchestratorWithFakeMailer = buildReportOrchestrator(_ => Future.unit) - val openFoodFactsService = new OpenFoodFactsService - val openBeautyFactsService = new OpenBeautyFactsService - val barcodeProductRepository = new BarcodeProductRepository(dbConfig) - val gs1Service = new GS1Service(applicationConfiguration.gs1) + val openFoodFactsService = new OpenFoodFactsService + val openBeautyFactsService = new OpenBeautyFactsService + val barcodeProductRepository = new BarcodeProductRepository(dbConfig) + def gs1Service: GS1ServiceInterface = new GS1Service(applicationConfiguration.gs1) val gs1AuthTokenActor: typed.ActorRef[actors.GS1AuthTokenActor.Command] = actorSystem.spawn( GS1AuthTokenActor(gs1Service), "gs1-auth-token-actor" diff --git a/app/models/report/sampledata/ReportGenerator.scala b/app/models/report/sampledata/ReportGenerator.scala index 09956f01..d5613e35 100644 --- a/app/models/report/sampledata/ReportGenerator.scala +++ b/app/models/report/sampledata/ReportGenerator.scala @@ -25,6 +25,8 @@ import scala.util.Random object ReportGenerator { + val sampleGtin = "3474341105842" + case class SampleReportBlueprint( conso: ConsumerUser, category: ReportCategory, @@ -108,7 +110,7 @@ object ReportGenerator { "Avez-vous déjà contacté le commerçant ou le fabricant pour ce problème :" -> "Oui", "Description :" -> "Bonjour, Je souhaite signaler une erreur d'étiquetage d'un pain qui était censé être un pain aux figues mais qui était un pain \"sportif\" dans un mauvais emballage. Nous l'avons acheté à FRANCHOUILLE MAGASIN à Anse (69480) et c'est la deuxième fois que cela nous arrive. \nJe tiens à préciser que le pain sportif contient des fruits à coque, des allergènes qui peuvent provoquer un choc anaphylactique aux gens allergiques comportant un risque vital. Je considère cette erreur du Franchouille gravissime. Cela doit être signalé.\nCordialement" ), - barcodeProductGtin = Some("3474341105842") + barcodeProductGtin = Some(sampleGtin) ) val reportReponseConso = SampleReportBlueprint( diff --git a/test/tasks/report/SampleDataGenerationTaskTest.scala b/test/tasks/report/SampleDataGenerationTaskTest.scala index 44562484..d1b938e6 100644 --- a/test/tasks/report/SampleDataGenerationTaskTest.scala +++ b/test/tasks/report/SampleDataGenerationTaskTest.scala @@ -1,15 +1,22 @@ package tasks.report import cats.implicits.toTraverseOps +import loader.SignalConsoComponents +import models.barcode.gs1.OAuthAccessToken import models.company.CompanyWithAccess import models.report.Report import models.report.ReportStatus import models.report.sampledata.ProUserGenerator._ +import models.report.sampledata.ReportGenerator.sampleGtin import org.specs2.concurrent.ExecutionEnv import org.specs2.matcher.FutureMatchers import org.specs2.mutable.Specification +import play.api.Application +import play.api.ApplicationLoader +import play.api.libs.json.Json import play.api.mvc.Results import play.api.test.WithApplication +import services.GS1ServiceInterface import utils.AppSpec import utils.Fixtures import utils.TestApp @@ -25,7 +32,66 @@ class SampleDataGenerationTaskTest(implicit ee: ExecutionEnv) with Results with FutureMatchers { - val (app, components) = TestApp.buildApp() + val oauthAccessToken = OAuthAccessToken("dummyToken") + val exampleGs1Response = Json.parse(""" + { + "isA": [ + "gs1:Offer", + "s:Offer" + ], + "itemOffered": { + "isA": [ + "gs1:Product", + "s:Product" + ], + "gtin": "3474341105842", + "brandOwner": { + "isA": [ + "gs1:Organization", + "s:Organization" + ], + "license": { + "key": "347434" + }, + "companyName": "MONBANA", + "globalLocationNumber": "3014743400109" + }, + "postalAddress": { + "isA": "gs1:PostalAddress", + "postalCode": "53500", + "addressCountry": "France", + "addressLocality": { + "lang": "fr", + "value": "ERNEE" + } + }, + "additionalPartyIdentificationValue": "562100032" + } + } + """) + val mockGs1Service: GS1ServiceInterface = mock[GS1ServiceInterface] + + mockGs1Service.authenticate( + ) returns Future.successful(oauthAccessToken) + + mockGs1Service.getProductByGTIN( + oauthAccessToken, + sampleGtin + ) returns Future.successful(Right(Some(exampleGs1Response))) + + class FakeApplicationLoader extends ApplicationLoader { + var components: SignalConsoComponents = _ + override def load(context: ApplicationLoader.Context): Application = { + components = new SignalConsoComponents(context) { + override def gs1Service: GS1ServiceInterface = mockGs1Service + } + components.application + } + } + + val appLoader = new FakeApplicationLoader() + val app: Application = TestApp.buildApp(appLoader) + val components: SignalConsoComponents = appLoader.components lazy val reportRepository = components.reportRepository lazy val sampleDataGenerationTask = components.sampleDataGenerationTask