generated from egmont11/ASP.Net-Core-MVC-Template
InitEshop+ddd+ca
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
using Eshop.Application.Auth;
|
||||
using Eshop.Infrastructure;
|
||||
using Eshop.Infrastructure.Persistence;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace WebTests;
|
||||
|
||||
[TestClass]
|
||||
public class AuthServiceTests
|
||||
{
|
||||
private ServiceProvider _serviceProvider = null!;
|
||||
private IAuthenticationService _authenticationService = null!;
|
||||
private IUserAccountService _userAccountService = null!;
|
||||
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
|
||||
services.AddLogging();
|
||||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||
|
||||
services.AddDbContext<AppDbContext>(options =>
|
||||
options.UseInMemoryDatabase(Guid.NewGuid().ToString()));
|
||||
|
||||
services.AddIdentity<UserEntity, IdentityRole>()
|
||||
.AddEntityFrameworkStores<AppDbContext>()
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
services.AddInfrastructure();
|
||||
|
||||
_serviceProvider = services.BuildServiceProvider();
|
||||
|
||||
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
|
||||
_serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext = httpContext;
|
||||
|
||||
var roleManager = _serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
|
||||
foreach (var role in new[] { "Admin", "User" })
|
||||
{
|
||||
if (!roleManager.RoleExistsAsync(role).GetAwaiter().GetResult())
|
||||
roleManager.CreateAsync(new IdentityRole(role)).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
_authenticationService = _serviceProvider.GetRequiredService<IAuthenticationService>();
|
||||
_userAccountService = _serviceProvider.GetRequiredService<IUserAccountService>();
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void Cleanup()
|
||||
{
|
||||
_serviceProvider.Dispose();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task RegisterAsync_ShouldReturnSuccess_WhenUserIsNew()
|
||||
{
|
||||
var result = await _userAccountService.RegisterAsync("testuser", "test@example.com", "Password123!");
|
||||
|
||||
Assert.IsTrue(result.Success);
|
||||
|
||||
var profile = await _userAccountService.GetProfileAsync(
|
||||
(await _serviceProvider.GetRequiredService<UserManager<UserEntity>>()
|
||||
.FindByNameAsync("testuser"))!.Id);
|
||||
|
||||
Assert.IsNotNull(profile);
|
||||
Assert.AreEqual("testuser", profile.UserName);
|
||||
Assert.AreEqual("test@example.com", profile.Email);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task RegisterAsync_ShouldReturnFailure_WhenUsernameAlreadyExists()
|
||||
{
|
||||
await _userAccountService.RegisterAsync("existing", "existing@example.com", "Password123!");
|
||||
|
||||
var result = await _userAccountService.RegisterAsync("existing", "other@example.com", "Password123!");
|
||||
|
||||
Assert.IsFalse(result.Success);
|
||||
Assert.IsTrue(result.Errors.Any(e => e.Contains("already taken", StringComparison.OrdinalIgnoreCase)));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task SignInAsync_ShouldReturnSuccess_WithValidCredentials()
|
||||
{
|
||||
await _userAccountService.RegisterAsync("testuser", "test@example.com", "Password123!");
|
||||
|
||||
var result = await _authenticationService.SignInAsync("testuser", "Password123!", rememberMe: false);
|
||||
|
||||
Assert.IsTrue(result.Succeeded);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task SignInAsync_ShouldReturnFailure_WithInvalidCredentials()
|
||||
{
|
||||
await _userAccountService.RegisterAsync("testuser", "test@example.com", "Password123!");
|
||||
|
||||
var result = await _authenticationService.SignInAsync("testuser", "WrongPassword!", rememberMe: false);
|
||||
|
||||
Assert.IsFalse(result.Succeeded);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task SignInAsync_ShouldReturnFailure_WhenUserNotFound()
|
||||
{
|
||||
var result = await _authenticationService.SignInAsync("nonexistent", "Password123!", rememberMe: false);
|
||||
|
||||
Assert.IsFalse(result.Succeeded);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
|
||||
@@ -0,0 +1,45 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Eshop.Infrastructure;
|
||||
|
||||
namespace WebTests;
|
||||
|
||||
[TestClass]
|
||||
public sealed class TestClassExample
|
||||
{
|
||||
private AppDbContext _context;
|
||||
// private Mock<ILogger<ExampleService>> _loggerMock;
|
||||
// private ExampleService _exampleService;
|
||||
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<AppDbContext>()
|
||||
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
|
||||
.Options;
|
||||
_context = new AppDbContext(options);
|
||||
// _exampleService = new ExampleService(_context);
|
||||
SeedDatabase();
|
||||
}
|
||||
|
||||
private void SeedDatabase()
|
||||
{
|
||||
// var user1 = new UserModel { Id = 1, UserName = "User1", Email = "user1@example.com", Password = "password123" };
|
||||
// var user2 = new UserModel { Id = 2, UserName = "User2", Email = "user2@example.com", Password = "password123" };
|
||||
// var user3 = new UserModel { Id = 3, UserName = "User3", Email = "user3@example.com", Password = "password123" };
|
||||
// _context.Users.AddRange(user1, user2, user3);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void Cleanup()
|
||||
{
|
||||
_context.Database.EnsureDeleted();
|
||||
_context.Dispose();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ExampleTest()
|
||||
{
|
||||
Assert.IsTrue(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.10" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.10" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="MSTest" Version="4.3.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Eshop.Infrastructure\Eshop.Infrastructure.csproj" />
|
||||
<ProjectReference Include="..\Eshop.Web\Eshop.Web.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user