Initial commit

This commit is contained in:
2026-07-14 20:40:37 +02:00
commit ffd8a087c8
119 changed files with 85507 additions and 0 deletions
+97
View File
@@ -0,0 +1,97 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using TemplateWeb.Data;
using TemplateWeb.Entities;
namespace WebTests;
[TestClass]
public class AuthServiceTests
{
private ServiceProvider _serviceProvider = null!;
private UserManager<UserEntity> _userManager = null!;
[TestInitialize]
public void Initialize()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddDbContext<AppDbContext>(options =>
options.UseInMemoryDatabase(Guid.NewGuid().ToString()));
services.AddIdentity<UserEntity, IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
_serviceProvider = services.BuildServiceProvider();
_userManager = _serviceProvider.GetRequiredService<UserManager<UserEntity>>();
}
[TestCleanup]
public void Cleanup()
{
_serviceProvider.Dispose();
}
[TestMethod]
public async Task CreateAsync_ShouldReturnSuccess_WhenUserIsNew()
{
// Arrange
var user = new UserEntity { UserName = "testuser", Email = "test@example.com" };
// Act
var result = await _userManager.CreateAsync(user, "Password123!");
// Assert
Assert.IsTrue(result.Succeeded);
var userInDb = await _userManager.FindByNameAsync("testuser");
Assert.IsNotNull(userInDb);
Assert.AreEqual("test@example.com", userInDb.Email);
}
[TestMethod]
public async Task CreateAsync_ShouldReturnFailure_WhenUsernameAlreadyExists()
{
// Arrange
var existing = new UserEntity { UserName = "existing", Email = "existing@example.com" };
await _userManager.CreateAsync(existing, "Password123!");
var duplicate = new UserEntity { UserName = "existing", Email = "other@example.com" };
// Act
var result = await _userManager.CreateAsync(duplicate, "Password123!");
// Assert
Assert.IsFalse(result.Succeeded);
Assert.IsTrue(result.Errors.Any(e => e.Code == "DuplicateUserName"));
}
[TestMethod]
public async Task FindByNameAsync_ShouldReturnUser_CaseInsensitive()
{
// Arrange
var user = new UserEntity { UserName = "testuser", Email = "test@example.com" };
await _userManager.CreateAsync(user, "Password123!");
// Act — Identity normalizes usernames, so lookup is always case-insensitive
var found = await _userManager.FindByNameAsync("TESTUSER");
// Assert
Assert.IsNotNull(found);
Assert.AreEqual("testuser", found.UserName);
}
[TestMethod]
public async Task FindByNameAsync_ShouldReturnNull_WhenUserNotFound()
{
// Act
var result = await _userManager.FindByNameAsync("nonexistent");
// Assert
Assert.IsNull(result);
}
}
+1
View File
@@ -0,0 +1 @@
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
+45
View File
@@ -0,0 +1,45 @@
using Microsoft.EntityFrameworkCore;
using TemplateWeb.Data;
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);
}
}
+27
View File
@@ -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="dotenv.net" Version="4.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.6" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.6" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="MSTest" Version="4.0.1"/>
</ItemGroup>
<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TemplateWeb\TemplateWeb.csproj" />
</ItemGroup>
</Project>