Základní backend: migrace, autentizace JWT, role, Swagger
- Helpdesk.Migrator (DbUp) aplikuje db/migrations, evidence v schema_version - Domain/Application/Infrastructure/Api podle Clean Architecture - registrace, ověření e-mailu, login, refresh s rotací, logout, reset hesla - JWT podle RFC 7519 evidované jen podle jti v auth_token - správa uživatelů a rolí, číselníky, ProblemDetails, Swagger UI - Docker Compose (db, migrator, api), oddělené DB účty - notifikace mají cizí klíče na issue a proposal - unit testy domény a zabezpečení Closes #1, closes #2, closes #3, closes #4, closes #5, closes #9
This commit is contained in:
1 parent
fc76be7d69
commit
a51d386278
64 files changed
+4394
-1
No files matched your search
@@ -0,0 +1,102 @@
|
||||
using Helpdesk.Application.Auth;
|
||||
using Helpdesk.Domain;
|
||||
using Helpdesk.Domain.Auth;
|
||||
using Helpdesk.Domain.Users;
|
||||
|
||||
namespace Helpdesk.UnitTests;
|
||||
|
||||
public class EnumCodeTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(UserRole.Student, "STUDENT")]
|
||||
[InlineData(UserRole.FacilityManager, "FACILITY_MANAGER")]
|
||||
[InlineData(UserRole.Management, "MANAGEMENT")]
|
||||
public void UserRole_ma_kod_odpovidajici_check_omezeni(UserRole role, string code)
|
||||
{
|
||||
Assert.Equal(code, role.ToCode());
|
||||
Assert.Equal(role, EnumCode.Parse<UserRole>(code));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(TokenType.EmailConfirm, "EMAIL_CONFIRM")]
|
||||
[InlineData(TokenType.PasswordReset, "PASSWORD_RESET")]
|
||||
[InlineData(RevokeReason.ReuseDetected, "REUSE_DETECTED")]
|
||||
[InlineData(RevokeReason.LogoutAll, "LOGOUT_ALL")]
|
||||
public void Kody_tokenu_odpovidaji_check_omezeni(object value, string code)
|
||||
{
|
||||
var actual = value switch
|
||||
{
|
||||
TokenType t => t.ToCode(),
|
||||
RevokeReason r => r.ToCode(),
|
||||
_ => throw new ArgumentException(null, nameof(value)),
|
||||
};
|
||||
Assert.Equal(code, actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Neznamy_kod_neprojde() => Assert.False(EnumCode.TryParse<UserRole>("SUPERUSER", out _));
|
||||
}
|
||||
|
||||
public class UserLockoutTests
|
||||
{
|
||||
private static readonly DateTime Now = new(2026, 10, 29, 10, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
private static User NewUser() => User.Create("[email protected]", "hash", "Jan", "Novák", UserRole.Student);
|
||||
|
||||
[Fact]
|
||||
public void Ctyri_neuspesne_pokusy_ucet_nezamknou()
|
||||
{
|
||||
var user = NewUser();
|
||||
for (var i = 0; i < User.MaxFailedLogins - 1; i++)
|
||||
user.RegisterFailedLogin(Now);
|
||||
|
||||
Assert.False(user.IsLockedOut(Now));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Paty_neuspesny_pokus_zamkne_ucet_na_15_minut()
|
||||
{
|
||||
var user = NewUser();
|
||||
for (var i = 0; i < User.MaxFailedLogins; i++)
|
||||
user.RegisterFailedLogin(Now);
|
||||
|
||||
Assert.True(user.IsLockedOut(Now.AddMinutes(14)));
|
||||
Assert.False(user.IsLockedOut(Now.AddMinutes(15)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Uspesne_prihlaseni_vynuluje_pocitadlo()
|
||||
{
|
||||
var user = NewUser();
|
||||
user.RegisterFailedLogin(Now);
|
||||
user.RegisterSuccessfulLogin();
|
||||
|
||||
Assert.Equal(0, user.FailedLoginCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Email_se_normalizuje_pro_jednoznacne_vyhledani() =>
|
||||
Assert.Equal("[email protected]", NewUser().EmailNormalized);
|
||||
}
|
||||
|
||||
public class RegistrationRoleTests
|
||||
{
|
||||
private readonly RegistrationOptions _options = new()
|
||||
{
|
||||
StudentEmailDomains = ["student.univerzita.cz"],
|
||||
TeacherEmailDomains = ["univerzita.cz"],
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[InlineData("[email protected]", UserRole.Student)]
|
||||
[InlineData("[email protected]", UserRole.Teacher)]
|
||||
[InlineData("[email protected]", UserRole.Teacher)]
|
||||
public void Role_se_urci_podle_domeny(string email, UserRole expected) =>
|
||||
Assert.Equal(expected, _options.ResolveRole(email));
|
||||
|
||||
[Theory]
|
||||
[InlineData("[email protected]")]
|
||||
[InlineData("[email protected]")]
|
||||
[InlineData("bez-zavinace")]
|
||||
public void Cizi_domena_registraci_neumozni(string email) => Assert.Null(_options.ResolveRole(email));
|
||||
}
|
||||
Reference in new issue
Block a user