From c1b0fcd0d7ec06b59a9b10ad7ce9769ceba4da9a Mon Sep 17 00:00:00 2001 From: "jozsef.b@aycode.com" <9Rj@D}fVwBaN> Date: Fri, 3 May 2024 15:37:41 +0200 Subject: [PATCH] refactoring, improvements, fixes... --- AyCode.Core/Consts/AcConst.cs | 5 +- AyCode.Core/Helpers/PasswordHasher.cs | 1 + AyCode.Database/AyCode.Database.csproj | 1 + .../Logins/AcLoggedInModelServer.cs | 2 +- .../AyCode.Services.Server.Tests.csproj | 3 + .../AcLoginServiceServerTestBase.cs | 69 ++++++++++++++++++- .../AyCode.Services.Server.csproj | 1 + AyCode.Utils/Extensions/GuidExtensions.cs | 7 +- AyCode.Utils/Extensions/StringExtensions.cs | 11 +-- 9 files changed, 86 insertions(+), 14 deletions(-) diff --git a/AyCode.Core/Consts/AcConst.cs b/AyCode.Core/Consts/AcConst.cs index 02c89ab..4187a2c 100644 --- a/AyCode.Core/Consts/AcConst.cs +++ b/AyCode.Core/Consts/AcConst.cs @@ -2,6 +2,7 @@ using System.Security; using System.Text; using AyCode.Core.Extensions; +using AyCode.Utils.Extensions; namespace AyCode.Core.Consts { @@ -81,8 +82,8 @@ namespace AyCode.Core.Consts private static string _tiamProjectIdString = "684f34d1-163a-4077-918f-a9d9df5ce789"; static AcConst() { - ProjectId = Guid.Parse(_tiamProjectIdString); - ProjectSalt = GenerateProjectSalt(ProjectId.ToString("N")); + ProjectId = Guid.Parse(_tiamProjectIdString.ToLower()); + ProjectSalt = GenerateProjectSalt(ProjectId.ToString("N").ToLower()); //var anataFolder = AcDomain.IsProductVersion ? "Anata" : "AnataDev"; diff --git a/AyCode.Core/Helpers/PasswordHasher.cs b/AyCode.Core/Helpers/PasswordHasher.cs index 8875381..14c75a1 100644 --- a/AyCode.Core/Helpers/PasswordHasher.cs +++ b/AyCode.Core/Helpers/PasswordHasher.cs @@ -2,6 +2,7 @@ using System.Text; using AyCode.Core.Consts; using AyCode.Core.Extensions; +using AyCode.Utils.Extensions; using Microsoft.AspNetCore.Cryptography.KeyDerivation; namespace AyCode.Core.Helpers diff --git a/AyCode.Database/AyCode.Database.csproj b/AyCode.Database/AyCode.Database.csproj index 5830b25..da90f0b 100644 --- a/AyCode.Database/AyCode.Database.csproj +++ b/AyCode.Database/AyCode.Database.csproj @@ -7,6 +7,7 @@ + diff --git a/AyCode.Models.Server/Logins/AcLoggedInModelServer.cs b/AyCode.Models.Server/Logins/AcLoggedInModelServer.cs index ea03ca9..647ebf5 100644 --- a/AyCode.Models.Server/Logins/AcLoggedInModelServer.cs +++ b/AyCode.Models.Server/Logins/AcLoggedInModelServer.cs @@ -7,7 +7,7 @@ using AyCode.Interfaces.Server.Logins; namespace AyCode.Models.Server.Logins; -public class AcLoggedInModelServer : IAcLoggedInModelBase +public class AcLoggedInModelServer : IAcLoggedInModelBase where TUser : class, IAcUser where TUserToken : class, IAcUserTokenBase diff --git a/AyCode.Services.Server.Tests/AyCode.Services.Server.Tests.csproj b/AyCode.Services.Server.Tests/AyCode.Services.Server.Tests.csproj index f8962a6..b9c505d 100644 --- a/AyCode.Services.Server.Tests/AyCode.Services.Server.Tests.csproj +++ b/AyCode.Services.Server.Tests/AyCode.Services.Server.Tests.csproj @@ -14,6 +14,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -23,6 +24,7 @@ + @@ -32,6 +34,7 @@ + diff --git a/AyCode.Services.Server.Tests/LoginServices/AcLoginServiceServerTestBase.cs b/AyCode.Services.Server.Tests/LoginServices/AcLoginServiceServerTestBase.cs index 07f3709..f211a75 100644 --- a/AyCode.Services.Server.Tests/LoginServices/AcLoginServiceServerTestBase.cs +++ b/AyCode.Services.Server.Tests/LoginServices/AcLoginServiceServerTestBase.cs @@ -8,6 +8,10 @@ using AyCode.Interfaces.Profiles; using AyCode.Interfaces.Server.Logins; using AyCode.Interfaces.ServiceProviders; using AyCode.Interfaces.Users; +using AyCode.Core.Consts; +using AyCode.Core.Extensions; +using AyCode.Services.Server.Logins; +using AyCode.Utils.Extensions; namespace AyCode.Services.Server.Tests.LoginServices { @@ -24,10 +28,69 @@ namespace AyCode.Services.Server.Tests.LoginServices where TUserToServiceProvider : class, IAcUserToServiceProviderBase where TEmailMessage : class, IAcEmailMessageBase { - public TUser RegisterUserTest() + [DataTestMethod] + [DataRow(["", "", ""])] + public virtual async Task AcBase_RegisterUser_ReturnsUser_WhenUserExist(string[] userIdEmailPasswordStrings) { - //Activator.CreateInstance(typeof(TLoginServiceServer), ) - throw new NotImplementedException(); + var registerUserId = Guid.Parse(userIdEmailPasswordStrings[0]); + var registerEmail = userIdEmailPasswordStrings[1]; + var registerPassword = userIdEmailPasswordStrings[2]; + + await Dal.RemoveUserAsync(registerUserId); //kitöröljük a szemetet, ha korábbról bentmaradt - J. + + var loginService = Activator.CreateInstance(typeof(TLoginServiceServer), Dal, AcEnv.AppConfiguration) as TLoginServiceServer; + Assert.IsNotNull(loginService); + + var errorCode = await loginService.RegistrationAsync(registerUserId, registerEmail, registerPassword, null); + + Assert.IsTrue(errorCode == AcErrorCode.Unset); + + var user = Dal.GetUserByEmail(registerEmail, false); + + Assert.IsNotNull(user); + Assert.IsNotNull(user.Profile); + Assert.IsNotNull(user.Profile.Address); + + await Dal.RemoveUserAsync(user); //kitöröljük a szemetet - J. + } + + [DataTestMethod] + [DataRow(["", ""])] + public virtual void AcBase_LoginUser_ReturnsUser_WhenUserExist(string[] emailPasswordStrings) + { + var loginEmail = emailPasswordStrings[0]; + var loginPassword = emailPasswordStrings[1]; + + var loginService = Activator.CreateInstance(typeof(TLoginServiceServer), Dal, AcEnv.AppConfiguration) as TLoginServiceServer; + Assert.IsNotNull(loginService); + + #region Valid email+password test + var loggedInModel = loginService.Login(loginEmail, loginPassword); + + Assert.IsNotNull(loggedInModel); + Assert.IsNotNull(loggedInModel.LoggedInUser); + Assert.IsNotNull(loginService.LoggedInModel?.LoggedInUser); + + Assert.IsTrue(loggedInModel.LoginErrorCode == AcErrorCode.Unset, $"errorCode: {loggedInModel.LoginErrorCode}"); + Assert.IsTrue(loggedInModel.IsLoggedIn, $"loggedInModel.IsLoggedIn == false; errorCode: {loggedInModel.LoginErrorCode}"); + Assert.IsTrue(string.Equals(loggedInModel.LoggedInUser.EmailAddress, loginEmail, StringComparison.CurrentCultureIgnoreCase)); + #endregion Valid email+password test + + #region Wrong email test + loggedInModel = loginService.Login("gffsdgdfg@gu.hu", loginPassword); + + Assert.IsNotNull(loggedInModel); + Assert.IsFalse(loggedInModel.IsLoggedIn); + Assert.IsTrue(loggedInModel.LoginErrorCode == AcErrorCode.WrongLoginData); + #endregion Wrong email test + + #region Wrong password test + loggedInModel = loginService.Login(loginEmail, "fsdgfsdg"); + + Assert.IsNotNull(loggedInModel); + Assert.IsFalse(loggedInModel.IsLoggedIn); + Assert.IsTrue(loggedInModel.LoginErrorCode == AcErrorCode.WrongLoginData); + #endregion Wrong password test } } } \ No newline at end of file diff --git a/AyCode.Services.Server/AyCode.Services.Server.csproj b/AyCode.Services.Server/AyCode.Services.Server.csproj index 4d0a62c..e3f5e15 100644 --- a/AyCode.Services.Server/AyCode.Services.Server.csproj +++ b/AyCode.Services.Server/AyCode.Services.Server.csproj @@ -7,6 +7,7 @@ + diff --git a/AyCode.Utils/Extensions/GuidExtensions.cs b/AyCode.Utils/Extensions/GuidExtensions.cs index 94fb475..57d829f 100644 --- a/AyCode.Utils/Extensions/GuidExtensions.cs +++ b/AyCode.Utils/Extensions/GuidExtensions.cs @@ -1,4 +1,5 @@ using JetBrains.Annotations; +using System.Diagnostics.CodeAnalysis; namespace AyCode.Utils.Extensions { @@ -6,10 +7,10 @@ namespace AyCode.Utils.Extensions { //[ContractAnnotation("=> true, result: notnull; => false, result: null")] //[ContractAnnotation("true => null; false => notnull")] - public static bool IsNullOrEmpty(this Guid guid) => guid == Guid.Empty; + public static bool IsNullOrEmpty([NotNullWhen(false)] this Guid guid) => guid == Guid.Empty; //[ContractAnnotation("=> true, result: notnull; => false, result: null")] - [ContractAnnotation("guid:null => true; guid:notnull <= false")] - public static bool IsNullOrEmpty(this Guid? guid) => guid == null || guid == Guid.Empty; + //[ContractAnnotation("guid:null => true; guid:notnull <= false")] + public static bool IsNullOrEmpty([NotNullWhen(false)] this Guid? guid) => guid == null || guid == Guid.Empty; } } diff --git a/AyCode.Utils/Extensions/StringExtensions.cs b/AyCode.Utils/Extensions/StringExtensions.cs index 88bd5ae..a16df5a 100644 --- a/AyCode.Utils/Extensions/StringExtensions.cs +++ b/AyCode.Utils/Extensions/StringExtensions.cs @@ -2,16 +2,17 @@ using System.Text; using JetBrains.Annotations; -namespace AyCode.Core.Extensions +namespace AyCode.Utils.Extensions { public static class StringExtensions { //[ContractAnnotation("str:null => true; str:notnull <= false")] - [ContractAnnotation("str:null => true; str:notnull <= false")] - public static bool IsNullOrEmpty([NotNullWhen(returnValue: false)] this string str) => str == null || str.Length == 0; + //[return: NotNullIfNotNull(nameof(str))] + public static bool IsNullOrEmpty([NotNullWhen(false)] this string? str) => str == null || str.Length == 0; - [ContractAnnotation("str:null => true;")] - public static bool IsNullOrWhiteSpace([NotNullWhen(returnValue: false)] this string str) + //[ContractAnnotation("str:null => true;; str:notnull <= false")] + //[return: NotNullIfNotNull(nameof(str))] + public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? str) { if (str == null || str.Length == 0) return true; if (!char.IsWhiteSpace(str[0])) return false;