From 023cdd9fe8093d84ee937e4b43b03fdf3474ade2 Mon Sep 17 00:00:00 2001 From: ssedoudbgouv Date: Sun, 29 Sep 2024 18:56:01 +0200 Subject: [PATCH] TRELLO-2537 : fix unit tests --- app/loader/SignalConsoApplicationLoader.scala | 19 +++++++++++++++- app/services/S3Service.scala | 21 ++---------------- test/controllers/ReportControllerSpec.scala | 22 +++++-------------- test/utils/AppSpec.scala | 2 +- 4 files changed, 26 insertions(+), 38 deletions(-) diff --git a/app/loader/SignalConsoApplicationLoader.scala b/app/loader/SignalConsoApplicationLoader.scala index 8f8950c7..2d28fc7b 100644 --- a/app/loader/SignalConsoApplicationLoader.scala +++ b/app/loader/SignalConsoApplicationLoader.scala @@ -7,6 +7,9 @@ import org.apache.pekko.actor.typed import org.apache.pekko.actor.typed.scaladsl.adapter.ClassicActorSystemOps import org.apache.pekko.util.Timeout import authentication._ +import com.amazonaws.auth.{AWSStaticCredentialsProvider, BasicAWSCredentials} +import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration +import com.amazonaws.services.s3.{AmazonS3, AmazonS3ClientBuilder} import com.typesafe.config.Config import com.typesafe.config.ConfigFactory import config._ @@ -248,7 +251,21 @@ class SignalConsoComponents( amazonBucketName = applicationConfiguration.amazonBucketName ) - val s3Service: S3ServiceInterface = new S3Service() + private val awsS3Client: AmazonS3 = AmazonS3ClientBuilder + .standard() + .withEndpointConfiguration( + new EndpointConfiguration("https://cellar-c2.services.clever-cloud.com", "us-east-1") + ) + .withCredentials( + new AWSStaticCredentialsProvider( + new BasicAWSCredentials( + bucketConfiguration.keyId, + bucketConfiguration.secretKey + ) + ) + ) + .build() + def s3Service: S3ServiceInterface = new S3Service(awsS3Client) // Actor val antivirusScanActor: typed.ActorRef[AntivirusScanActor.ScanCommand] = actorSystem.spawn( diff --git a/app/services/S3Service.scala b/app/services/S3Service.scala index 13d28c93..2130f3fc 100644 --- a/app/services/S3Service.scala +++ b/app/services/S3Service.scala @@ -10,10 +10,7 @@ import org.apache.pekko.util.ByteString import java.nio.file.Path import com.amazonaws.HttpMethod -import com.amazonaws.auth.AWSStaticCredentialsProvider -import com.amazonaws.auth.BasicAWSCredentials -import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration -import com.amazonaws.services.s3.AmazonS3ClientBuilder +import com.amazonaws.services.s3.AmazonS3 import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest import com.amazonaws.services.s3.model.ResponseHeaderOverrides import config.BucketConfiguration @@ -25,7 +22,7 @@ import play.api.Logger import scala.concurrent.ExecutionContext import scala.concurrent.Future -class S3Service(implicit +class S3Service(awsS3Client: AmazonS3)(implicit val materializer: Materializer, val executionContext: ExecutionContext, val bucketConfiguration: BucketConfiguration @@ -34,20 +31,6 @@ class S3Service(implicit private[this] val bucketName = bucketConfiguration.amazonBucketName private val pekkoS3Client = S3 - private val awsS3Client = AmazonS3ClientBuilder - .standard() - .withEndpointConfiguration( - new EndpointConfiguration("https://cellar-c2.services.clever-cloud.com", "us-east-1") - ) - .withCredentials( - new AWSStaticCredentialsProvider( - new BasicAWSCredentials( - bucketConfiguration.keyId, - bucketConfiguration.secretKey - ) - ) - ) - .build() override def upload(bucketKey: String): Sink[ByteString, Future[MultipartUploadResult]] = pekkoS3Client.multipartUpload(bucketName, bucketKey) diff --git a/test/controllers/ReportControllerSpec.scala b/test/controllers/ReportControllerSpec.scala index b0775722..5f3b32d3 100644 --- a/test/controllers/ReportControllerSpec.scala +++ b/test/controllers/ReportControllerSpec.scala @@ -35,15 +35,14 @@ import play.api.mvc.Results import play.api.test.Helpers._ import play.api.test._ import repositories.event.EventFilter +import services.S3ServiceInterface import utils.Constants.ActionEvent.CONSUMER_THREATEN_BY_PRO import utils.Constants.ActionEvent.POST_ACCOUNT_ACTIVATION_DOC import utils.Constants.ActionEvent.REFUND_BLACKMAIL import utils.Constants.ActionEvent.OTHER_REASON_DELETE_REQUEST import utils.Constants.ActionEvent.SOLVED_CONTRACTUAL_DISPUTE import utils.Constants.EventType -import utils.EmailAddress -import utils.Fixtures -import utils.TestApp +import utils.{EmailAddress, Fixtures, S3ServiceMock, TestApp} import utils.AuthHelpers._ import java.time.temporal.ChronoUnit @@ -299,20 +298,9 @@ class ReportControllerSpec(implicit ee: ExecutionEnv) extends Specification with super.configuration ) -// override val s3Service: S3ServiceInterface = { -// println(s"------------------ = /////////////////////////////////////// ------------------") -// println(s"------------------ = /////////////////////////////////////// ------------------") -// println(s"------------------ = /////////////////////////////////////// ------------------") -// println(s"------------------ = /////////////////////////////////////// ------------------") -// println(s"------------------ = /////////////////////////////////////// ------------------") -// println(s"------------------ = /////////////////////////////////////// ------------------") -// println(s"------------------ = /////////////////////////////////////// ------------------") -// println(s"------------------ = /////////////////////////////////////// ------------------") -// println(s"------------------ = /////////////////////////////////////// ------------------") -// println(s"------------------ = /////////////////////////////////////// ------------------") -// println(s"------------------ = /////////////////////////////////////// ------------------") -// new S3ServiceMock() -// } + override def s3Service: S3ServiceInterface = { + new S3ServiceMock() + } override def tokenConfiguration = TokenConfiguration(None, None, 12.hours, Period.ofDays(60), Period.ZERO, None, Period.ZERO) override def uploadConfiguration = UploadConfiguration(Seq.empty, false, "/tmp") diff --git a/test/utils/AppSpec.scala b/test/utils/AppSpec.scala index 64040502..4a3d1ad0 100644 --- a/test/utils/AppSpec.scala +++ b/test/utils/AppSpec.scala @@ -125,7 +125,7 @@ class DefaultApplicationLoader( override def load(context: ApplicationLoader.Context): Application = { components = new SignalConsoComponents(context) { - override val s3Service = + override def s3Service = new S3ServiceMock() override lazy val mailRetriesService: MailRetriesService = mailRetriesServiceMock