Base template solution with tests, authentication, support for admin accounts, prepared tests for auth, basic error handling system used in auth.

This commit is contained in:
Matěj Kubíček
2026-04-18 17:37:38 +02:00
parent 5a2e76fefd
commit f50bec167f
101 changed files with 83814 additions and 0 deletions
+115
View File
@@ -0,0 +1,115 @@
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Moq;
using TemplateWeb.Data;
using TemplateWeb.Models;
using TemplateWeb.Models.DbModels;
using TemplateWeb.Services;
namespace WebTests;
[TestClass]
public class AuthServiceTests
{
private AppDbContext _context = null!;
private Mock<ILogger<AuthService>> _loggerMock = null!;
private Mock<IHttpContextAccessor> _httpContextAccessorMock = null!;
private AuthService _authService = null!;
[TestInitialize]
public void Initialize()
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
_context = new AppDbContext(options);
_loggerMock = new Mock<ILogger<AuthService>>();
_httpContextAccessorMock = new Mock<IHttpContextAccessor>();
_authService = new AuthService(_context, _loggerMock.Object, _httpContextAccessorMock.Object);
}
[TestCleanup]
public void Cleanup()
{
_context.Database.EnsureDeleted();
_context.Dispose();
}
[TestMethod]
public async Task RegisterAsync_ShouldReturnSuccess_WhenUserIsNew()
{
// Arrange
var model = new AuthRegisterViewModel
{
UserName = "testuser",
Email = "test@example.com",
Password = "password123",
ConfirmPassword = "password123"
};
// Act
var result = await _authService.RegisterAsync(model);
// Assert
Assert.IsTrue(result.Success);
Assert.IsNotNull(result.Data);
Assert.AreEqual("testuser", result.Data.UserName);
Assert.AreEqual("test@example.com", result.Data.Email);
var userInDb = await _context.Users.FirstOrDefaultAsync(u => u.UserName == "testuser");
Assert.IsNotNull(userInDb);
}
[TestMethod]
public async Task RegisterAsync_ShouldReturnFailure_WhenUserAlreadyExists()
{
// Arrange
var existingUser = new UserModel { UserName = "existing", Email = "existing@example.com", Password = "hashedpassword" };
_context.Users.Add(existingUser);
await _context.SaveChangesAsync();
var model = new AuthRegisterViewModel
{
UserName = "existing",
Email = "new@example.com",
Password = "password123",
ConfirmPassword = "password123"
};
// Act
var result = await _authService.RegisterAsync(model);
// Assert
Assert.IsFalse(result.Success);
Assert.AreEqual("Username or Email already exists.", result.Message);
}
[TestMethod]
public async Task FindUserByEmailOrUsernameAsync_ShouldReturnUser_WhenUsernameMatches()
{
// Arrange
var user = new UserModel { UserName = "testuser", Email = "test@example.com", Password = "password" };
_context.Users.Add(user);
await _context.SaveChangesAsync();
// Act
var result = await _authService.FindUserByEmailOrUsernameAsync("TESTUSER");
// Assert
Assert.IsTrue(result.Success);
Assert.IsNotNull(result.Data);
Assert.AreEqual("testuser", result.Data.UserName);
}
[TestMethod]
public async Task FindUserByEmailOrUsernameAsync_ShouldReturnFailure_WhenUserNotFound()
{
// Act
var result = await _authService.FindUserByEmailOrUsernameAsync("nonexistent");
// Assert
Assert.IsFalse(result.Success);
Assert.AreEqual("User not found.", result.Message);
}
}
+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);
}
}
+25
View File
@@ -0,0 +1,25 @@
<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.EntityFrameworkCore.InMemory" 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>