Exemplo n.º 1
0
 public S3StaticWebsiteService(IAmazonS3 amazonS3,
                               IAmazonCloudFront cloudFrontService,
                               ILogger <S3StaticWebsiteService> logger,
                               AWSConfig awsConfig,
                               EnvironmentConfig environmentConfig,
                               CloudFrontConfig cloudFrontConfig)
 {
     _amazonS3          = amazonS3;
     _logger            = logger;
     _awsConfig         = awsConfig;
     _cloudFrontService = cloudFrontService;
     _environmentConfig = environmentConfig;
     _cloudFrontConfig  = cloudFrontConfig;
 }
Exemplo n.º 2
0
        public async Task ReturnErrorCodeIfCacheInvalidationFails()
        {
            //Arrange
            var mockS3 = new Mock <IAmazonS3>();

            mockS3.Setup(s3 => s3.PutObjectAsync(It.IsAny <PutObjectRequest>(), default(CancellationToken)))
            .ReturnsAsync(new PutObjectResponse {
                HttpStatusCode = HttpStatusCode.OK
            });

            var mockCloudFrontService = new Mock <IAmazonCloudFront>();

            mockCloudFrontService.Setup(cf => cf.CreateInvalidationAsync(It.IsAny <CreateInvalidationRequest>(), default(CancellationToken)))
            .ReturnsAsync(new CreateInvalidationResponse {
                HttpStatusCode = HttpStatusCode.InternalServerError
            });

            var cfConfig = new CloudFrontConfig()
            {
                Enabled = true
            };
            var service = new S3StaticWebsiteService(mockS3.Object, mockCloudFrontService.Object, Mock.Of <ILogger <S3StaticWebsiteService> >(), Mock.Of <AWSConfig>(), Mock.Of <EnvironmentConfig>(), cfConfig);

            var a = new StaticContentRequest {
                FilePath = "sitemap.xml", ContentStream = new System.IO.MemoryStream()
            };
            var b = new StaticContentRequest {
                FilePath = "abc.html", ContentBody = "Some html"
            };
            var filePaths = new List <string>()
            {
                "/" + a.FilePath, "/" + b.FilePath
            };

            //Act
            var result = service.WriteFilesAsync(a, b).Result;

            //Assert
            mockCloudFrontService.Verify(x => x.CreateInvalidationAsync(It.Is <CreateInvalidationRequest>(
                                                                            o => Enumerable.SequenceEqual(o.InvalidationBatch.Paths.Items, filePaths)), default(CancellationToken)), Times.Once);
            mockS3.Verify(x => x.PutObjectAsync(It.Is <PutObjectRequest>(o => o.Key == a.FilePath), default(CancellationToken)), Times.Exactly(1));
            mockS3.Verify(x => x.PutObjectAsync(It.Is <PutObjectRequest>(o => o.Key == b.FilePath), default(CancellationToken)), Times.Exactly(1));
            Assert.Equal(HttpStatusCode.InternalServerError, result);
        }
Exemplo n.º 3
0
        // This method gets called by the runtime. Use this method to add services to the container
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            services.AddLogging(builder => {
                builder.AddILoggingBuilderInstance();
            });

            services.TryAddSingleton <ISeriLogger, SeriLogger>();
            services.TryAddSingleton <IStaticWebsiteService, S3StaticWebsiteService>();
            services.TryAddSingleton <IViewRenderer, ViewRenderer>();
            services.TryAddTransient <IMailService, MailService>();
            services.TryAddTransient <IContentService, ContentService>();
            services.TryAddTransient <IBankHolidayService, BankHolidayService>();

            EnvironmentConfig environmentConfig = new EnvironmentConfig();
            AWSConfig         awsConfig         = new AWSConfig();
            CloudFrontConfig  cloudFrontConfig  = new CloudFrontConfig();
            CMSConfig         cmsConfig         = new CMSConfig();
            MailChimpConfig   mailChimpConfig   = new MailChimpConfig();
            MailConfig        mailConfig        = new MailConfig();

            Configuration.Bind("AppSettings:Environment", environmentConfig);
            Configuration.Bind("AWS", awsConfig);
            Configuration.Bind("AWS:CloudFront", cloudFrontConfig);
            Configuration.Bind("CMS", cmsConfig);
            Configuration.Bind("MailChimp", mailChimpConfig);
            Configuration.Bind("Mail", mailConfig);

            services.AddSingleton(environmentConfig)
            .AddSingleton(awsConfig)
            .AddSingleton(cloudFrontConfig)
            .AddSingleton(cmsConfig)
            .AddSingleton(mailChimpConfig)
            .AddSingleton(mailConfig);

            services.AddMailChimpClient(mailChimpConfig.ApiKey);

            AmazonS3Config s3config;

            if (environmentConfig.Name == "local")  //TODO: Should use Environment.IsDevelopment() here. When running tests it returns "Production"
            {
                s3config = new AmazonS3Config()
                {
                    RegionEndpoint = Region,
                    ServiceURL     = awsConfig.ServiceURL,
                    ForcePathStyle = true
                };
            }
            else
            {
                s3config = new AmazonS3Config()
                {
                    RegionEndpoint = Region
                };
            }
            services.AddTransient <IAmazonS3>((sP) =>
            {
                return(new AmazonS3Client(awsConfig.AccessKey, awsConfig.SecretKey, s3config));
            });

            var cloudfrontConfig = new AmazonCloudFrontConfig()
            {
                RegionEndpoint = Region
            };

            services.AddTransient <IAmazonCloudFront>((acf) =>
            {
                return(new AmazonCloudFrontClient(awsConfig.AccessKey, awsConfig.SecretKey, cloudfrontConfig));
            });

            services.AddControllers();
        }