Skip to content

Commit

Permalink
Merge pull request #155 from TomaszKandula/dev
Browse files Browse the repository at this point in the history
merge: dev to master
  • Loading branch information
TomaszKandula authored Apr 23, 2024
2 parents ca4cdc0 + aa2723f commit 71fc513
Show file tree
Hide file tree
Showing 48 changed files with 576 additions and 174 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Builder;
using Microsoft.Net.Http.Headers;

namespace EmailSender.WebApi.Configuration;
namespace EmailSender.Backend.Configuration;

[ExcludeFromCodeCoverage]
public static class CorsPolicy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Net;
using System.Net.Sockets;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.DependencyInjection;

namespace EmailSender.WebApi.Configuration;
namespace EmailSender.Backend.Configuration;

[ExcludeFromCodeCoverage]
public static class DockerSupport
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.0" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.26" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.26" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="6.0.26" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.26">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.26" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.35.0" />
<PackageReference Include="Polly" Version="8.2.1" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Include="Serilog.Sinks.AzureBlobStorage" Version="3.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.35.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EmailSender.Backend.Core\EmailSender.Backend.Core.csproj" />
<ProjectReference Include="..\EmailSender.Backend.Shared\EmailSender.Backend.Shared.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Newtonsoft.Json;

namespace EmailSender.Backend.Configuration;

public static class HealthCheckSupport
{
public static HealthCheckOptions WriteResponse()
{
return new HealthCheckOptions
{
ResponseWriter = async (context, report) =>
{
var result = new
{
status = report.Status.ToString(),
errors = report.Entries.Select(pair
=> new
{
key = pair.Key,
value = Enum.GetName(typeof(HealthStatus), pair.Value.Status)
})
};
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(JsonConvert.SerializeObject(result));
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Microsoft.Extensions.Configuration;
using Serilog;
using Serilog.Events;
using ILogger = Serilog.ILogger;

namespace EmailSender.WebApi.Configuration.Logger;
namespace EmailSender.Backend.Configuration.Logger;

public static class Configuration
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Polly.Extensions.Http;
using Polly;

namespace EmailSender.WebApi.Configuration;
namespace EmailSender.Backend.Configuration;

[ExcludeFromCodeCoverage]
public static class PollyPolicyHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;

namespace EmailSender.Backend.Configuration;

[ExcludeFromCodeCoverage]
public static class PollySupport
{
public static void SetupRetryPolicyWithPolly(IServiceCollection services)
{
services.AddHttpClient("RetryHttpClient", options =>
{
options.DefaultRequestHeaders.Add("Accept", "application/json");
options.Timeout = TimeSpan.FromMinutes(5);
options.DefaultRequestHeaders.ConnectionClose = true;
}).AddPolicyHandler(PollyPolicyHandler.SetupRetry());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;

namespace EmailSender.WebApi.Configuration;
namespace EmailSender.Backend.Configuration;

[ExcludeFromCodeCoverage]
public static class SwaggerSupport
Expand Down
2 changes: 1 addition & 1 deletion EmailSender.ClientApp/clientapp.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ WORKDIR /usr/share/nginx/html

RUN rm -rf ./*
RUN apt-get update
RUN apt-get upgrade
RUN apt-get -y upgrade
RUN apt-get -y install bash
RUN apt-get -y install nginx
RUN apt-get -y install nginx-full
Expand Down
13 changes: 1 addition & 12 deletions EmailSender.ClientApp/nginx/nginx-http.conf
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,7 @@ http {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html$is_args$args;

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffering off;
proxy_cache_valid 200 30m;
proxy_cache_valid 404 1m;

include /etc/nginx/nginx-proxy.conf;
client_max_body_size 10m;
client_body_buffer_size 128k;
}
Expand Down
13 changes: 1 addition & 12 deletions EmailSender.ClientApp/nginx/nginx-https.conf
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,7 @@ http {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html$is_args$args;

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffering off;
proxy_cache_valid 200 30m;
proxy_cache_valid 404 1m;

include /etc/nginx/nginx-proxy.conf;
client_max_body_size 10m;
client_body_buffer_size 128k;
}
Expand Down
10 changes: 10 additions & 0 deletions EmailSender.ClientApp/nginx/nginx-proxy.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffering off;
proxy_cache_valid 200 30m;
proxy_cache_valid 404 1m;

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private async Task<ServerData> GetServerData(string address, CancellationToken c
Server = email.ServerName,
Key = email.ServerKey,
Port = email.ServerPort,
IsSSL = email.ServerSsl
IsSsl = email.ServerSsl
};
}

Expand All @@ -140,7 +140,7 @@ private async Task<ServerData> GetServerData(Guid addressId, CancellationToken c
Server = email.ServerName,
Key = email.ServerKey,
Port = email.ServerPort,
IsSSL = email.ServerSsl
IsSsl = email.ServerSsl
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public class ServerData

public int Port { get; set; }

public bool IsSSL { get; set; }
public bool IsSsl { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed class SmtpClientService : ISmtpClientService

public ServerData ServerData { get; set; } = new();

public SecureSocketOptions SslOnConnect => ServerData.IsSSL
public SecureSocketOptions SslOnConnect => ServerData.IsSsl
? SecureSocketOptions.SslOnConnect
: SecureSocketOptions.None;

Expand Down Expand Up @@ -122,9 +122,8 @@ private static bool IsFormatCorrect(string emailAddress)
{
try
{
// ReSharper disable once UnusedVariable
var address = new MailAddress(emailAddress);
return true;
return !string.IsNullOrWhiteSpace(address.Address);
}
catch (FormatException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public string GetPrivateKeyFromHeader(string headerName = "X-Private-Key")
{
return _httpContextAccessor.HttpContext is null
? string.Empty
: _httpContextAccessor.HttpContext.Request.Headers[headerName].ToString();
: _httpContextAccessor.HttpContext.Request.Headers[headerName].ToString()
?? string.Empty;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using EmailSender.Services.SmtpService;
using EmailSender.Services.SenderService;
using EmailSender.Services.SenderService.Models;
using MailKit;

namespace EmailSender.Tests.UnitTests.Services;

Expand Down Expand Up @@ -487,8 +488,34 @@ public async Task GivenValidEmailDataAndServerData_WhenInvokeSend_ShouldSucceed(
};

// Act
// Assert
await service.Send(configuration, CancellationToken.None);

// Assert
mockedSmtpClient.Verify(x => x.ConnectAsync(
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<SecureSocketOptions>(),
It.IsAny<CancellationToken>()),
Times.Once);

mockedSmtpClient.Verify(x => x.AuthenticateAsync(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<CancellationToken>()),
Times.Once);

mockedSmtpClient.Verify(x => x.SendAsync(
It.IsAny<MimeMessage>(),
It.IsAny<CancellationToken>(),
It.IsAny<ITransferProgress>()
),
Times.Once);

mockedSmtpClient.Verify(x => x.DisconnectAsync(
It.IsAny<bool>(),
It.IsAny<CancellationToken>()
),
Times.Once);
}

[Fact]
Expand Down
12 changes: 12 additions & 0 deletions EmailSender.WebApi.Dto/AddUserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

namespace EmailSender.WebApi.Dto;

/// <summary>
/// Use it when you want to register new user.
/// </summary>
[ExcludeFromCodeCoverage]
public class AddUserDto
{
/// <summary>
/// Name.
/// </summary>
public string FirstName { get; set; } = "";

/// <summary>
/// Surname.
/// </summary>
public string LastName { get; set; } = "";

/// <summary>
/// Email address.
/// </summary>
public string EmailAddress { get; set; } = "";
}
9 changes: 9 additions & 0 deletions EmailSender.WebApi.Dto/AddUserEmailDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

namespace EmailSender.WebApi.Dto;

/// <summary>
/// Use it when you want to add new user email address.
/// </summary>
[ExcludeFromCodeCoverage]
public class AddUserEmailDto
{
/// <summary>
/// User ID.
/// </summary>
public Guid? UserId { get; set; }

/// <summary>
/// Email ID.
/// </summary>
public Guid EmailId { get; set; }
}
Loading

0 comments on commit 71fc513

Please sign in to comment.