Shady nagy/net6 (#614)

* udated to .net6

* used the .net6 version RC2

* added editconfig.

* App core new Scoped Namespaces style.

* BlazorAdmin new Scoped Namespaces style.

* Blazor Shared new Scoped Namespaces style.

* Infra new Scoped Namespaces style.

* public api new Scoped Namespaces style.

* web new Scoped Namespaces style.

* FunctionalTests new Scoped Namespaces style.

* Integrational tests new Scoped Namespaces style.

* unit tests new Scoped Namespaces style.

* update github action.

* update github action.

* change the global.
This commit is contained in:
Shady Nagy
2021-11-06 01:55:48 +02:00
committed by GitHub
parent 64f150dc07
commit 9db2feb930
252 changed files with 6307 additions and 6413 deletions

View File

@@ -1,89 +1,88 @@
using Microsoft.AspNetCore.Mvc.Testing;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
[Collection("Sequential")]
public class AccountControllerSignIn : IClassFixture<WebTestFixture>
{
[Collection("Sequential")]
public class AccountControllerSignIn : IClassFixture<WebTestFixture>
public AccountControllerSignIn(WebTestFixture factory)
{
public AccountControllerSignIn(WebTestFixture factory)
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
}
AllowAutoRedirect = false
});
}
public HttpClient Client { get; }
public HttpClient Client { get; }
[Fact]
public async Task ReturnsSignInScreenOnGet()
{
var response = await Client.GetAsync("/identity/account/login");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
[Fact]
public async Task ReturnsSignInScreenOnGet()
{
var response = await Client.GetAsync("/identity/account/login");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
Assert.Contains("demouser@microsoft.com", stringResponse);
}
Assert.Contains("demouser@microsoft.com", stringResponse);
}
[Fact]
public void RegexMatchesValidRequestVerificationToken()
{
// TODO: Move to a unit test
// TODO: Move regex to a constant in test project
var input = @"<input name=""__RequestVerificationToken"" type=""hidden"" value=""CfDJ8Obhlq65OzlDkoBvsSX0tgxFUkIZ_qDDSt49D_StnYwphIyXO4zxfjopCWsygfOkngsL6P0tPmS2HTB1oYW-p_JzE0_MCFb7tF9Ol_qoOg_IC_yTjBNChF0qRgoZPmKYOIJigg7e2rsBsmMZDTdbnGo"" /><input name=""RememberMe"" type=""hidden"" value=""false"" /></form>";
string regexpression = @"name=""__RequestVerificationToken"" type=""hidden"" value=""([-A-Za-z0-9+=/\\_]+?)""";
var regex = new Regex(regexpression);
var match = regex.Match(input);
var group = match.Groups.Values.LastOrDefault();
Assert.NotNull(group);
Assert.True(group.Value.Length > 50);
}
[Fact]
public void RegexMatchesValidRequestVerificationToken()
{
// TODO: Move to a unit test
// TODO: Move regex to a constant in test project
var input = @"<input name=""__RequestVerificationToken"" type=""hidden"" value=""CfDJ8Obhlq65OzlDkoBvsSX0tgxFUkIZ_qDDSt49D_StnYwphIyXO4zxfjopCWsygfOkngsL6P0tPmS2HTB1oYW-p_JzE0_MCFb7tF9Ol_qoOg_IC_yTjBNChF0qRgoZPmKYOIJigg7e2rsBsmMZDTdbnGo"" /><input name=""RememberMe"" type=""hidden"" value=""false"" /></form>";
string regexpression = @"name=""__RequestVerificationToken"" type=""hidden"" value=""([-A-Za-z0-9+=/\\_]+?)""";
var regex = new Regex(regexpression);
var match = regex.Match(input);
var group = match.Groups.Values.LastOrDefault();
Assert.NotNull(group);
Assert.True(group.Value.Length > 50);
}
[Fact]
public async Task ReturnsFormWithRequestVerificationToken()
{
var response = await Client.GetAsync("/identity/account/login");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
[Fact]
public async Task ReturnsFormWithRequestVerificationToken()
{
var response = await Client.GetAsync("/identity/account/login");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
string token = GetRequestVerificationToken(stringResponse);
Assert.True(token.Length > 50);
}
string token = GetRequestVerificationToken(stringResponse);
Assert.True(token.Length > 50);
}
private string GetRequestVerificationToken(string input)
{
string regexpression = @"name=""__RequestVerificationToken"" type=""hidden"" value=""([-A-Za-z0-9+=/\\_]+?)""";
var regex = new Regex(regexpression);
var match = regex.Match(input);
return match.Groups.Values.LastOrDefault().Value;
}
private string GetRequestVerificationToken(string input)
{
string regexpression = @"name=""__RequestVerificationToken"" type=""hidden"" value=""([-A-Za-z0-9+=/\\_]+?)""";
var regex = new Regex(regexpression);
var match = regex.Match(input);
return match.Groups.Values.LastOrDefault().Value;
}
[Fact]
public async Task ReturnsSuccessfulSignInOnPostWithValidCredentials()
{
var getResponse = await Client.GetAsync("/identity/account/login");
getResponse.EnsureSuccessStatusCode();
var stringResponse1 = await getResponse.Content.ReadAsStringAsync();
string token = GetRequestVerificationToken(stringResponse1);
[Fact]
public async Task ReturnsSuccessfulSignInOnPostWithValidCredentials()
{
var getResponse = await Client.GetAsync("/identity/account/login");
getResponse.EnsureSuccessStatusCode();
var stringResponse1 = await getResponse.Content.ReadAsStringAsync();
string token = GetRequestVerificationToken(stringResponse1);
var keyValues = new List<KeyValuePair<string, string>>();
keyValues.Add(new KeyValuePair<string, string>("Email", "demouser@microsoft.com"));
keyValues.Add(new KeyValuePair<string, string>("Password", "Pass@word1"));
var keyValues = new List<KeyValuePair<string, string>>();
keyValues.Add(new KeyValuePair<string, string>("Email", "demouser@microsoft.com"));
keyValues.Add(new KeyValuePair<string, string>("Password", "Pass@word1"));
keyValues.Add(new KeyValuePair<string, string>("__RequestVerificationToken", token));
var formContent = new FormUrlEncodedContent(keyValues);
keyValues.Add(new KeyValuePair<string, string>("__RequestVerificationToken", token));
var formContent = new FormUrlEncodedContent(keyValues);
var postResponse = await Client.PostAsync("/identity/account/login", formContent);
Assert.Equal(HttpStatusCode.Redirect, postResponse.StatusCode);
Assert.Equal(new System.Uri("/", UriKind.Relative), postResponse.Headers.Location);
}
var postResponse = await Client.PostAsync("/identity/account/login", formContent);
Assert.Equal(HttpStatusCode.Redirect, postResponse.StatusCode);
Assert.Equal(new System.Uri("/", UriKind.Relative), postResponse.Headers.Location);
}
}

View File

@@ -2,28 +2,27 @@
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
[Collection("Sequential")]
public class CatalogControllerIndex : IClassFixture<WebTestFixture>
{
[Collection("Sequential")]
public class CatalogControllerIndex : IClassFixture<WebTestFixture>
public CatalogControllerIndex(WebTestFixture factory)
{
public CatalogControllerIndex(WebTestFixture factory)
{
Client = factory.CreateClient();
}
Client = factory.CreateClient();
}
public HttpClient Client { get; }
public HttpClient Client { get; }
[Fact]
public async Task ReturnsHomePageWithProductListing()
{
// Arrange & Act
var response = await Client.GetAsync("/");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
[Fact]
public async Task ReturnsHomePageWithProductListing()
{
// Arrange & Act
var response = await Client.GetAsync("/");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
// Assert
Assert.Contains(".NET Bot Black Sweatshirt", stringResponse);
}
// Assert
Assert.Contains(".NET Bot Black Sweatshirt", stringResponse);
}
}

View File

@@ -1,32 +1,31 @@
using Microsoft.AspNetCore.Mvc.Testing;
using System.Net;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
[Collection("Sequential")]
public class OrderIndexOnGet : IClassFixture<WebTestFixture>
{
[Collection("Sequential")]
public class OrderIndexOnGet : IClassFixture<WebTestFixture>
public OrderIndexOnGet(WebTestFixture factory)
{
public OrderIndexOnGet(WebTestFixture factory)
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
}
AllowAutoRedirect = false
});
}
public HttpClient Client { get; }
public HttpClient Client { get; }
[Fact]
public async Task ReturnsRedirectGivenAnonymousUser()
{
var response = await Client.GetAsync("/order/my-orders");
var redirectLocation = response.Headers.Location.OriginalString;
[Fact]
public async Task ReturnsRedirectGivenAnonymousUser()
{
var response = await Client.GetAsync("/order/my-orders");
var redirectLocation = response.Headers.Location.OriginalString;
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Contains("/Account/Login", redirectLocation);
}
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Contains("/Account/Login", redirectLocation);
}
}

View File

@@ -1,70 +1,69 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.eShopWeb.FunctionalTests.Web;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.eShopWeb.FunctionalTests.Web;
using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.WebRazorPages
namespace Microsoft.eShopWeb.FunctionalTests.WebRazorPages;
[Collection("Sequential")]
public class BasketPageCheckout : IClassFixture<WebTestFixture>
{
[Collection("Sequential")]
public class BasketPageCheckout : IClassFixture<WebTestFixture>
public BasketPageCheckout(WebTestFixture factory)
{
public BasketPageCheckout(WebTestFixture factory)
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = true
});
}
AllowAutoRedirect = true
});
}
public HttpClient Client { get; }
public HttpClient Client { get; }
private string GetRequestVerificationToken(string input)
{
string regexpression = @"name=""__RequestVerificationToken"" type=""hidden"" value=""([-A-Za-z0-9+=/\\_]+?)""";
var regex = new Regex(regexpression);
var match = regex.Match(input);
return match.Groups.Values.LastOrDefault().Value;
}
private string GetRequestVerificationToken(string input)
{
string regexpression = @"name=""__RequestVerificationToken"" type=""hidden"" value=""([-A-Za-z0-9+=/\\_]+?)""";
var regex = new Regex(regexpression);
var match = regex.Match(input);
return match.Groups.Values.LastOrDefault().Value;
}
[Fact]
public async Task RedirectsToLoginIfNotAuthenticated()
{
// Arrange & Act
[Fact]
public async Task RedirectsToLoginIfNotAuthenticated()
{
// Arrange & Act
// Load Home Page
var response = await Client.GetAsync("/");
response.EnsureSuccessStatusCode();
var stringResponse1 = await response.Content.ReadAsStringAsync();
// Load Home Page
var response = await Client.GetAsync("/");
response.EnsureSuccessStatusCode();
var stringResponse1 = await response.Content.ReadAsStringAsync();
string token = GetRequestVerificationToken(stringResponse1);
string token = GetRequestVerificationToken(stringResponse1);
// Add Item to Cart
var keyValues = new List<KeyValuePair<string, string>>();
keyValues.Add(new KeyValuePair<string, string>("id", "2"));
keyValues.Add(new KeyValuePair<string, string>("name", "shirt"));
// Add Item to Cart
var keyValues = new List<KeyValuePair<string, string>>();
keyValues.Add(new KeyValuePair<string, string>("id", "2"));
keyValues.Add(new KeyValuePair<string, string>("name", "shirt"));
keyValues.Add(new KeyValuePair<string, string>("price", "19.49"));
keyValues.Add(new KeyValuePair<string, string>("__RequestVerificationToken", token));
keyValues.Add(new KeyValuePair<string, string>("price", "19.49"));
keyValues.Add(new KeyValuePair<string, string>("__RequestVerificationToken", token));
var formContent = new FormUrlEncodedContent(keyValues);
var formContent = new FormUrlEncodedContent(keyValues);
var postResponse = await Client.PostAsync("/basket/index", formContent);
postResponse.EnsureSuccessStatusCode();
var stringResponse = await postResponse.Content.ReadAsStringAsync();
var postResponse = await Client.PostAsync("/basket/index", formContent);
postResponse.EnsureSuccessStatusCode();
var stringResponse = await postResponse.Content.ReadAsStringAsync();
// Assert
Assert.Contains(".NET Black &amp; White Mug", stringResponse);
// Assert
Assert.Contains(".NET Black &amp; White Mug", stringResponse);
keyValues.Clear();
keyValues.Add(new KeyValuePair<string, string>("__RequestVerificationToken", token));
keyValues.Clear();
keyValues.Add(new KeyValuePair<string, string>("__RequestVerificationToken", token));
formContent = new FormUrlEncodedContent(keyValues);
var postResponse2 = await Client.PostAsync("/Basket/Checkout", formContent);
Assert.Contains("/Identity/Account/Login", postResponse2.RequestMessage.RequestUri.ToString());
}
formContent = new FormUrlEncodedContent(keyValues);
var postResponse2 = await Client.PostAsync("/Basket/Checkout", formContent);
Assert.Contains("/Identity/Account/Login", postResponse2.RequestMessage.RequestUri.ToString());
}
}

View File

@@ -1,30 +1,29 @@
using Microsoft.eShopWeb.FunctionalTests.Web;
using System.Net.Http;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.eShopWeb.FunctionalTests.Web;
using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.WebRazorPages
namespace Microsoft.eShopWeb.FunctionalTests.WebRazorPages;
[Collection("Sequential")]
public class HomePageOnGet : IClassFixture<WebTestFixture>
{
[Collection("Sequential")]
public class HomePageOnGet : IClassFixture<WebTestFixture>
public HomePageOnGet(WebTestFixture factory)
{
public HomePageOnGet(WebTestFixture factory)
{
Client = factory.CreateClient();
}
Client = factory.CreateClient();
}
public HttpClient Client { get; }
public HttpClient Client { get; }
[Fact]
public async Task ReturnsHomePageWithProductListing()
{
// Arrange & Act
var response = await Client.GetAsync("/");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
[Fact]
public async Task ReturnsHomePageWithProductListing()
{
// Arrange & Act
var response = await Client.GetAsync("/");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
// Assert
Assert.Contains(".NET Bot Black Sweatshirt", stringResponse);
}
// Assert
Assert.Contains(".NET Bot Black Sweatshirt", stringResponse);
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Hosting;
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
@@ -7,38 +8,37 @@ using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.eShopWeb.Web;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
namespace Microsoft.eShopWeb.FunctionalTests.Web
namespace Microsoft.eShopWeb.FunctionalTests.Web;
public class WebTestFixture : WebApplicationFactory<Startup>
{
public class WebTestFixture : WebApplicationFactory<Startup>
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Testing");
builder.UseEnvironment("Testing");
builder.ConfigureServices(services =>
{
services.AddEntityFrameworkInMemoryDatabase();
builder.ConfigureServices(services =>
{
services.AddEntityFrameworkInMemoryDatabase();
// Create a new service provider.
var provider = services
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
// Add a database context (ApplicationDbContext) using an in-memory
// database for testing.
services.AddDbContext<CatalogContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
options.UseInternalServiceProvider(provider);
});
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
options.UseInternalServiceProvider(provider);
});
services.AddDbContext<AppIdentityDbContext>(options =>
{
options.UseInMemoryDatabase("Identity");
options.UseInternalServiceProvider(provider);
});
services.AddDbContext<AppIdentityDbContext>(options =>
{
options.UseInMemoryDatabase("Identity");
options.UseInternalServiceProvider(provider);
});
// Build the service provider.
var sp = services.BuildServiceProvider();
@@ -46,34 +46,33 @@ namespace Microsoft.eShopWeb.FunctionalTests.Web
// Create a scope to obtain a reference to the database
// context (ApplicationDbContext).
using (var scope = sp.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var db = scopedServices.GetRequiredService<CatalogContext>();
var loggerFactory = scopedServices.GetRequiredService<ILoggerFactory>();
{
var scopedServices = scope.ServiceProvider;
var db = scopedServices.GetRequiredService<CatalogContext>();
var loggerFactory = scopedServices.GetRequiredService<ILoggerFactory>();
var logger = scopedServices
.GetRequiredService<ILogger<WebTestFixture>>();
var logger = scopedServices
.GetRequiredService<ILogger<WebTestFixture>>();
// Ensure the database is created.
db.Database.EnsureCreated();
try
{
try
{
// Seed the database with test data.
CatalogContextSeed.SeedAsync(db, loggerFactory).Wait();
// seed sample user data
var userManager = scopedServices.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = scopedServices.GetRequiredService<RoleManager<IdentityRole>>();
AppIdentityDbContextSeed.SeedAsync(userManager, roleManager).Wait();
}
catch (Exception ex)
{
logger.LogError(ex, $"An error occurred seeding the " +
"database with test messages. Error: {ex.Message}");
}
var roleManager = scopedServices.GetRequiredService<RoleManager<IdentityRole>>();
AppIdentityDbContextSeed.SeedAsync(userManager, roleManager).Wait();
}
});
}
catch (Exception ex)
{
logger.LogError(ex, $"An error occurred seeding the " +
"database with test messages. Error: {ex.Message}");
}
}
});
}
}