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:
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>Microsoft.eShopWeb.FunctionalTests</RootNamespace>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -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.PublicApi;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.PublicApi
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
|
||||
public class ApiTestFixture : WebApplicationFactory<Startup>
|
||||
{
|
||||
public class ApiTestFixture : 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.PublicApi
|
||||
// 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<ApiTestFixture>>();
|
||||
var logger = scopedServices
|
||||
.GetRequiredService<ILogger<ApiTestFixture>>();
|
||||
|
||||
// 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}");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,50 +1,49 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Constants;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Constants;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Api
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Api;
|
||||
|
||||
public class ApiTokenHelper
|
||||
{
|
||||
public class ApiTokenHelper
|
||||
public static string GetAdminUserToken()
|
||||
{
|
||||
public static string GetAdminUserToken()
|
||||
{
|
||||
string userName = "admin@microsoft.com";
|
||||
string[] roles = { "Administrators" };
|
||||
string userName = "admin@microsoft.com";
|
||||
string[] roles = { "Administrators" };
|
||||
|
||||
return CreateToken(userName, roles);
|
||||
return CreateToken(userName, roles);
|
||||
}
|
||||
|
||||
public static string GetNormalUserToken()
|
||||
{
|
||||
string userName = "demouser@microsoft.com";
|
||||
string[] roles = { };
|
||||
|
||||
return CreateToken(userName, roles);
|
||||
}
|
||||
|
||||
private static string CreateToken(string userName, string[] roles)
|
||||
{
|
||||
var claims = new List<Claim> { new Claim(ClaimTypes.Name, userName) };
|
||||
|
||||
foreach (var role in roles)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, role));
|
||||
}
|
||||
|
||||
public static string GetNormalUserToken()
|
||||
var key = Encoding.ASCII.GetBytes(AuthorizationConstants.JWT_SECRET_KEY);
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
string userName = "demouser@microsoft.com";
|
||||
string[] roles = { };
|
||||
|
||||
return CreateToken(userName, roles);
|
||||
}
|
||||
|
||||
private static string CreateToken(string userName, string[] roles)
|
||||
{
|
||||
var claims = new List<Claim> { new Claim(ClaimTypes.Name, userName) };
|
||||
|
||||
foreach (var role in roles)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, role));
|
||||
}
|
||||
|
||||
var key = Encoding.ASCII.GetBytes(AuthorizationConstants.JWT_SECRET_KEY);
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(claims.ToArray()),
|
||||
Expires = DateTime.UtcNow.AddHours(1),
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
return tokenHandler.WriteToken(token);
|
||||
}
|
||||
Subject = new ClaimsIdentity(claims.ToArray()),
|
||||
Expires = DateTime.UtcNow.AddHours(1),
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
return tokenHandler.WriteToken(token);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,43 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Constants;
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.PublicApi.AuthEndpoints;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Constants;
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.PublicApi.AuthEndpoints;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
|
||||
|
||||
[Collection("Sequential")]
|
||||
public class AuthenticateEndpoint : IClassFixture<ApiTestFixture>
|
||||
{
|
||||
[Collection("Sequential")]
|
||||
public class AuthenticateEndpoint : IClassFixture<ApiTestFixture>
|
||||
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
|
||||
public AuthenticateEndpoint(ApiTestFixture factory)
|
||||
{
|
||||
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
|
||||
public AuthenticateEndpoint(ApiTestFixture factory)
|
||||
public HttpClient Client { get; }
|
||||
|
||||
[Theory]
|
||||
[InlineData("demouser@microsoft.com", AuthorizationConstants.DEFAULT_PASSWORD, true)]
|
||||
[InlineData("demouser@microsoft.com", "badpassword", false)]
|
||||
[InlineData("baduser@microsoft.com", "badpassword", false)]
|
||||
public async Task ReturnsExpectedResultGivenCredentials(string testUsername, string testPassword, bool expectedResult)
|
||||
{
|
||||
var request = new AuthenticateRequest()
|
||||
{
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
Username = testUsername,
|
||||
Password = testPassword
|
||||
};
|
||||
var jsonContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
|
||||
var response = await Client.PostAsync("api/authenticate", jsonContent);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<AuthenticateResponse>();
|
||||
|
||||
public HttpClient Client { get; }
|
||||
|
||||
[Theory]
|
||||
[InlineData("demouser@microsoft.com", AuthorizationConstants.DEFAULT_PASSWORD, true)]
|
||||
[InlineData("demouser@microsoft.com", "badpassword", false)]
|
||||
[InlineData("baduser@microsoft.com", "badpassword", false)]
|
||||
public async Task ReturnsExpectedResultGivenCredentials(string testUsername, string testPassword, bool expectedResult)
|
||||
{
|
||||
var request = new AuthenticateRequest()
|
||||
{
|
||||
Username = testUsername,
|
||||
Password = testPassword
|
||||
};
|
||||
var jsonContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
|
||||
var response = await Client.PostAsync("api/authenticate", jsonContent);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<AuthenticateResponse>();
|
||||
|
||||
Assert.Equal(expectedResult, model.Result);
|
||||
}
|
||||
Assert.Equal(expectedResult, model.Result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +1,41 @@
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.Web.ViewModels;
|
||||
using System.Linq;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.Web.ViewModels;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
|
||||
|
||||
[Collection("Sequential")]
|
||||
public class ApiCatalogControllerList : IClassFixture<ApiTestFixture>
|
||||
{
|
||||
[Collection("Sequential")]
|
||||
public class ApiCatalogControllerList : IClassFixture<ApiTestFixture>
|
||||
public ApiCatalogControllerList(ApiTestFixture factory)
|
||||
{
|
||||
public ApiCatalogControllerList(ApiTestFixture factory)
|
||||
{
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
|
||||
public HttpClient Client { get; }
|
||||
public HttpClient Client { get; }
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsFirst10CatalogItems()
|
||||
{
|
||||
var response = await Client.GetAsync("/api/catalog-items?pageSize=10");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<CatalogIndexViewModel>();
|
||||
[Fact]
|
||||
public async Task ReturnsFirst10CatalogItems()
|
||||
{
|
||||
var response = await Client.GetAsync("/api/catalog-items?pageSize=10");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<CatalogIndexViewModel>();
|
||||
|
||||
Assert.Equal(10, model.CatalogItems.Count());
|
||||
}
|
||||
Assert.Equal(10, model.CatalogItems.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsLast2CatalogItemsGivenPageIndex1()
|
||||
{
|
||||
var response = await Client.GetAsync("/api/catalog-items?pageSize=10&pageIndex=1");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<CatalogIndexViewModel>();
|
||||
[Fact]
|
||||
public async Task ReturnsLast2CatalogItemsGivenPageIndex1()
|
||||
{
|
||||
var response = await Client.GetAsync("/api/catalog-items?pageSize=10&pageIndex=1");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<CatalogIndexViewModel>();
|
||||
|
||||
Assert.Equal(2, model.CatalogItems.Count());
|
||||
}
|
||||
Assert.Equal(2, model.CatalogItems.Count());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,75 +1,74 @@
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.FunctionalTests.Web.Api;
|
||||
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
|
||||
using System.Net;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.FunctionalTests.Web.Api;
|
||||
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
|
||||
|
||||
[Collection("Sequential")]
|
||||
public class CreateEndpoint : IClassFixture<ApiTestFixture>
|
||||
{
|
||||
[Collection("Sequential")]
|
||||
public class CreateEndpoint : IClassFixture<ApiTestFixture>
|
||||
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
private int _testBrandId = 1;
|
||||
private int _testTypeId = 2;
|
||||
private string _testDescription = "test description";
|
||||
private string _testName = "test name";
|
||||
private decimal _testPrice = 1.23m;
|
||||
|
||||
public CreateEndpoint(ApiTestFixture factory)
|
||||
{
|
||||
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
private int _testBrandId = 1;
|
||||
private int _testTypeId = 2;
|
||||
private string _testDescription = "test description";
|
||||
private string _testName = "test name";
|
||||
private decimal _testPrice = 1.23m;
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
|
||||
public CreateEndpoint(ApiTestFixture factory)
|
||||
public HttpClient Client { get; }
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsNotAuthorizedGivenNormalUserToken()
|
||||
{
|
||||
var jsonContent = GetValidNewItemJson();
|
||||
var token = ApiTokenHelper.GetNormalUserToken();
|
||||
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
var response = await Client.PostAsync("api/catalog-items", jsonContent);
|
||||
|
||||
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsSuccessGivenValidNewItemAndAdminUserToken()
|
||||
{
|
||||
var jsonContent = GetValidNewItemJson();
|
||||
var adminToken = ApiTokenHelper.GetAdminUserToken();
|
||||
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
|
||||
var response = await Client.PostAsync("api/catalog-items", jsonContent);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<CreateCatalogItemResponse>();
|
||||
|
||||
Assert.Equal(_testBrandId, model.CatalogItem.CatalogBrandId);
|
||||
Assert.Equal(_testTypeId, model.CatalogItem.CatalogTypeId);
|
||||
Assert.Equal(_testDescription, model.CatalogItem.Description);
|
||||
Assert.Equal(_testName, model.CatalogItem.Name);
|
||||
Assert.Equal(_testPrice, model.CatalogItem.Price);
|
||||
}
|
||||
|
||||
private StringContent GetValidNewItemJson()
|
||||
{
|
||||
var request = new CreateCatalogItemRequest()
|
||||
{
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
CatalogBrandId = _testBrandId,
|
||||
CatalogTypeId = _testTypeId,
|
||||
Description = _testDescription,
|
||||
Name = _testName,
|
||||
Price = _testPrice
|
||||
};
|
||||
var jsonContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
|
||||
|
||||
public HttpClient Client { get; }
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsNotAuthorizedGivenNormalUserToken()
|
||||
{
|
||||
var jsonContent = GetValidNewItemJson();
|
||||
var token = ApiTokenHelper.GetNormalUserToken();
|
||||
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
var response = await Client.PostAsync("api/catalog-items", jsonContent);
|
||||
|
||||
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsSuccessGivenValidNewItemAndAdminUserToken()
|
||||
{
|
||||
var jsonContent = GetValidNewItemJson();
|
||||
var adminToken = ApiTokenHelper.GetAdminUserToken();
|
||||
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
|
||||
var response = await Client.PostAsync("api/catalog-items", jsonContent);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<CreateCatalogItemResponse>();
|
||||
|
||||
Assert.Equal(_testBrandId, model.CatalogItem.CatalogBrandId);
|
||||
Assert.Equal(_testTypeId, model.CatalogItem.CatalogTypeId);
|
||||
Assert.Equal(_testDescription, model.CatalogItem.Description);
|
||||
Assert.Equal(_testName, model.CatalogItem.Name);
|
||||
Assert.Equal(_testPrice, model.CatalogItem.Price);
|
||||
}
|
||||
|
||||
private StringContent GetValidNewItemJson()
|
||||
{
|
||||
var request = new CreateCatalogItemRequest()
|
||||
{
|
||||
CatalogBrandId = _testBrandId,
|
||||
CatalogTypeId = _testTypeId,
|
||||
Description = _testDescription,
|
||||
Name = _testName,
|
||||
Price = _testPrice
|
||||
};
|
||||
var jsonContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
|
||||
|
||||
return jsonContent;
|
||||
}
|
||||
return jsonContent;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +1,47 @@
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.FunctionalTests.Web.Api;
|
||||
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
|
||||
using System.Net;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.FunctionalTests.Web.Api;
|
||||
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
|
||||
|
||||
[Collection("Sequential")]
|
||||
public class DeleteEndpoint : IClassFixture<ApiTestFixture>
|
||||
{
|
||||
[Collection("Sequential")]
|
||||
public class DeleteEndpoint : IClassFixture<ApiTestFixture>
|
||||
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
|
||||
public DeleteEndpoint(ApiTestFixture factory)
|
||||
{
|
||||
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
|
||||
public DeleteEndpoint(ApiTestFixture factory)
|
||||
{
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
public HttpClient Client { get; }
|
||||
|
||||
public HttpClient Client { get; }
|
||||
[Fact]
|
||||
public async Task ReturnsSuccessGivenValidIdAndAdminUserToken()
|
||||
{
|
||||
var adminToken = ApiTokenHelper.GetAdminUserToken();
|
||||
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
|
||||
var response = await Client.DeleteAsync("api/catalog-items/12");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<DeleteCatalogItemResponse>();
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsSuccessGivenValidIdAndAdminUserToken()
|
||||
{
|
||||
var adminToken = ApiTokenHelper.GetAdminUserToken();
|
||||
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
|
||||
var response = await Client.DeleteAsync("api/catalog-items/12");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<DeleteCatalogItemResponse>();
|
||||
Assert.Equal("Deleted", model.Status);
|
||||
}
|
||||
|
||||
Assert.Equal("Deleted", model.Status);
|
||||
}
|
||||
[Fact]
|
||||
public async Task ReturnsNotFoundGivenInvalidIdAndAdminUserToken()
|
||||
{
|
||||
var adminToken = ApiTokenHelper.GetAdminUserToken();
|
||||
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
|
||||
var response = await Client.DeleteAsync("api/catalog-items/0");
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsNotFoundGivenInvalidIdAndAdminUserToken()
|
||||
{
|
||||
var adminToken = ApiTokenHelper.GetAdminUserToken();
|
||||
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
|
||||
var response = await Client.DeleteAsync("api/catalog-items/0");
|
||||
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,42 @@
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
|
||||
using System.Net;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.FunctionalTests.PublicApi;
|
||||
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
|
||||
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
|
||||
|
||||
[Collection("Sequential")]
|
||||
public class GetByIdEndpoint : IClassFixture<ApiTestFixture>
|
||||
{
|
||||
[Collection("Sequential")]
|
||||
public class GetByIdEndpoint : IClassFixture<ApiTestFixture>
|
||||
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
|
||||
public GetByIdEndpoint(ApiTestFixture factory)
|
||||
{
|
||||
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
|
||||
public GetByIdEndpoint(ApiTestFixture factory)
|
||||
{
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
public HttpClient Client { get; }
|
||||
|
||||
public HttpClient Client { get; }
|
||||
[Fact]
|
||||
public async Task ReturnsItemGivenValidId()
|
||||
{
|
||||
var response = await Client.GetAsync("api/catalog-items/5");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<GetByIdCatalogItemResponse>();
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsItemGivenValidId()
|
||||
{
|
||||
var response = await Client.GetAsync("api/catalog-items/5");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = stringResponse.FromJson<GetByIdCatalogItemResponse>();
|
||||
Assert.Equal(5, model.CatalogItem.Id);
|
||||
Assert.Equal("Roslyn Red Sheet", model.CatalogItem.Name);
|
||||
}
|
||||
|
||||
Assert.Equal(5, model.CatalogItem.Id);
|
||||
Assert.Equal("Roslyn Red Sheet", model.CatalogItem.Name);
|
||||
}
|
||||
[Fact]
|
||||
public async Task ReturnsNotFoundGivenInvalidId()
|
||||
{
|
||||
var response = await Client.GetAsync("api/catalog-items/0");
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsNotFoundGivenInvalidId()
|
||||
{
|
||||
var response = await Client.GetAsync("api/catalog-items/0");
|
||||
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 & White Mug", stringResponse);
|
||||
// Assert
|
||||
Assert.Contains(".NET Black & 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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user