Skip to content
This repository has been archived by the owner on Sep 29, 2021. It is now read-only.

#5 Add log and initial Integration Test #10

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using ImageBase.WebApp.Controllers;
using ImageBase.WebApp.Dtos.AuthenticationDto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace ImageBase.WebApp.IntegrationTests.ControllersTests
{
public class AuthenticationControllerTests : IClassFixture<AuthenticationController>
{
private ImageBaseWebAppFactory _factory;
private HttpClient _client;

[Fact]
public void GivenARequestToTheController()
BoDmiSer marked this conversation as resolved.
Show resolved Hide resolved
{
//Act
_factory = new ImageBaseWebAppFactory();
_client = _factory.CreateClient();
}

[Theory]
[InlineData("/")]
[InlineData("/api/Authentication")]
[InlineData("/api/Authentication/register")]
public async Task AuthenticationAPIEnsure(string url)
BoDmiSer marked this conversation as resolved.
Show resolved Hide resolved
{
// Arrange
BoDmiSer marked this conversation as resolved.
Show resolved Hide resolved
var response = await _client.GetAsync(url);

//Act
response.EnsureSuccessStatusCode();

// Assert
Assert.Equal("text/html; charset=utf-8",
response.Content.Headers.ContentType.ToString());
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
}

[Fact]
public async Task RegisterUserPOSTOK()
{
// Arrange
var content = new RegisterUserDto() { Email = "[email protected]", Password = "Qwert12345", PasswordConfirm = "Qwert1234" };
var jsonContent = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");

//Act
var result = await _client.PostAsync("/api/Authentication/register", jsonContent);

// Assert
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal(HttpStatusCode.Redirect, result.StatusCode);
}

[Fact]
public async Task RegisterUserPOSTBad()
{
// Arrange
var content = new RegisterUserDto();
var jsonContent = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");

//Act
var result = await _client.PostAsync("/api/Authentication/register", jsonContent);

// Assert
Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode);
Assert.Equal(HttpStatusCode.Redirect, result.StatusCode);
BoDmiSer marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public void TearDown()
{
//Act
_client.Dispose();
_factory.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ImageBase.WebApp\ImageBase.WebApp.csproj" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions ImageBase.WebApp.IntegrationTests/ImageBaseWebAppFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.AspNetCore.Mvc.Testing;


namespace ImageBase.WebApp.IntegrationTests
{
public class ImageBaseWebAppFactory : WebApplicationFactory<Startup>
{
}
}
27 changes: 27 additions & 0 deletions ImageBase.WebApp.IntegrationTests/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:54576/",
"sslPort": 44325
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ImageBase.WebApp.IntegrationTests": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
39 changes: 0 additions & 39 deletions ImageBase.WebApp/Controllers/WeatherForecastController.cs

This file was deleted.

5 changes: 5 additions & 0 deletions ImageBase.WebApp/Dtos/AuthenticationDto/LoginUserDto.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace ImageBase.WebApp.Dtos.AuthenticationDto
{
public class LoginUserDto
{
[Required(ErrorMessage = "You have not entered your email")]
public string Email { get; set; }

[Required(ErrorMessage = "You have not entered your password")]
public string Password { get; set; }

}
}
5 changes: 4 additions & 1 deletion ImageBase.WebApp/Dtos/AuthenticationDto/RegisterUserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ namespace ImageBase.WebApp.Dtos.AuthenticationDto
public class RegisterUserDto
{
public string Email { get; set; }
BoDmiSer marked this conversation as resolved.
Show resolved Hide resolved

[MinLength(6, ErrorMessage = "String length must be more than 6 characters")]
public string Password { get; set; }
[Compare("Password", ErrorMessage = "Пароли не совпадают")]

[Compare("Password", ErrorMessage = "Passwords do not match")]
public string PasswordConfirm { get; set; }
}
}
4 changes: 4 additions & 0 deletions ImageBase.WebApp/ImageBase.WebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
</ItemGroup>

<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>


</Project>
23 changes: 22 additions & 1 deletion ImageBase.WebApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;


namespace ImageBase.WebApp
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json").Build();
BoDmiSer marked this conversation as resolved.
Show resolved Hide resolved
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration)
.CreateLogger();
try
{
Log.Information("Start web host");
CreateHostBuilder(args).Build().Run();
}
catch (Exception e)
{
Log.Fatal(e, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
Expand Down
12 changes: 5 additions & 7 deletions ImageBase.WebApp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
Expand All @@ -8,11 +7,10 @@
"sslPort": 44319
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand All @@ -21,10 +19,10 @@
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
}
3 changes: 3 additions & 0 deletions ImageBase.WebApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using ImageBase.WebApp.Models.Authentication;
using Microsoft.AspNetCore.Identity;
using Newtonsoft.Json.Serialization;
using Serilog;

namespace ImageBase.WebApp
{
Expand Down Expand Up @@ -54,6 +55,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseHttpsRedirection();

app.UseSerilogRequestLogging();
BoDmiSer marked this conversation as resolved.
Show resolved Hide resolved

app.UseRouting();

app.UseAuthentication();
Expand Down
15 changes: 0 additions & 15 deletions ImageBase.WebApp/WeatherForecast.cs

This file was deleted.

Loading