diff --git a/PWManager.Application/Context/IApplicationEnvironment.cs b/PWManager.Application/Context/IApplicationEnvironment.cs deleted file mode 100644 index 85a6647..0000000 --- a/PWManager.Application/Context/IApplicationEnvironment.cs +++ /dev/null @@ -1,16 +0,0 @@ -using PWManager.Domain.Entities; - -namespace PWManager.Application.Context; - -public interface IApplicationEnvironment { - - public bool IsDevelopmentMode { get; init; } - - public bool RunningSession { get; set; } - - public User? CurrentUser { get; set; } - - public Group? CurrentGroup { get; set; } - - public string? EncryptionKey { get; set; } -} \ No newline at end of file diff --git a/PWManager.Application/Context/ICliEnvironment.cs b/PWManager.Application/Context/ICliEnvironment.cs new file mode 100644 index 0000000..4e72e71 --- /dev/null +++ b/PWManager.Application/Context/ICliEnvironment.cs @@ -0,0 +1,6 @@ +namespace PWManager.Application.Context; + +public interface ICliEnvironment { + public bool RunningSession { get; set; } + public string Prompt { get; } +} \ No newline at end of file diff --git a/PWManager.Application/Context/ICryptEnvironment.cs b/PWManager.Application/Context/ICryptEnvironment.cs new file mode 100644 index 0000000..08d9c1a --- /dev/null +++ b/PWManager.Application/Context/ICryptEnvironment.cs @@ -0,0 +1,5 @@ +namespace PWManager.Application.Context; + +public interface ICryptEnvironment { + public string? EncryptionKey { get; set; } +} \ No newline at end of file diff --git a/PWManager.Application/Context/IDebugEnvironment.cs b/PWManager.Application/Context/IDebugEnvironment.cs new file mode 100644 index 0000000..0311ce8 --- /dev/null +++ b/PWManager.Application/Context/IDebugEnvironment.cs @@ -0,0 +1,5 @@ +namespace PWManager.Application.Context; + +public interface IDebugEnvironment { + public bool IsDevelopmentMode { get; init; } +} \ No newline at end of file diff --git a/PWManager.Application/Context/IUserEnvironment.cs b/PWManager.Application/Context/IUserEnvironment.cs new file mode 100644 index 0000000..8300a1c --- /dev/null +++ b/PWManager.Application/Context/IUserEnvironment.cs @@ -0,0 +1,9 @@ +using PWManager.Domain.Entities; + +namespace PWManager.Application.Context; + +public interface IUserEnvironment { + public User? CurrentUser { get; set; } + + public Group? CurrentGroup { get; set; } +} \ No newline at end of file diff --git a/PWManager.Application/Services/CryptService.cs b/PWManager.Application/Services/CryptService.cs index 070a53a..2368302 100644 --- a/PWManager.Application/Services/CryptService.cs +++ b/PWManager.Application/Services/CryptService.cs @@ -6,9 +6,9 @@ namespace PWManager.Application.Services { public class CryptService : ICryptService { - private readonly IApplicationEnvironment _env; + private readonly ICryptEnvironment _env; - public CryptService(IApplicationEnvironment env) { + public CryptService(ICryptEnvironment env) { _env = env; } diff --git a/PWManager.CLI/ConsoleRunner.cs b/PWManager.CLI/ConsoleRunner.cs index 7ed9d94..94ef83e 100644 --- a/PWManager.CLI/ConsoleRunner.cs +++ b/PWManager.CLI/ConsoleRunner.cs @@ -13,12 +13,14 @@ internal class ConsoleRunner : IRunner { private readonly Type?[] _controller = new Type[Enum.GetNames().Length]; private readonly IServiceProvider _provider; - private readonly CliEnvironment _environment; + private readonly ICliEnvironment _environment; + private readonly IDebugEnvironment _debugInfo; private readonly CommandParser _commandParser; - public ConsoleRunner(IServiceProvider provider, IApplicationEnvironment environment) { + public ConsoleRunner(IServiceProvider provider, ICliEnvironment environment, IDebugEnvironment debugInfo) { _provider = provider; - _environment = (CliEnvironment)environment; + _environment = environment; + _debugInfo = debugInfo; _commandParser = new CommandParser(); } @@ -48,7 +50,7 @@ public void Run(string[] args) { } catch (Exception ex) { Console.WriteLine("An Error occured!"); - if (_environment.IsDevelopmentMode) { + if (_debugInfo.IsDevelopmentMode) { Console.WriteLine(ex); } } diff --git a/PWManager.CLI/Controllers/HelpController.cs b/PWManager.CLI/Controllers/HelpController.cs index 9e2446a..7a9d323 100644 --- a/PWManager.CLI/Controllers/HelpController.cs +++ b/PWManager.CLI/Controllers/HelpController.cs @@ -6,8 +6,8 @@ namespace PWManager.CLI.Controllers; public class HelpController : IController { - private readonly IApplicationEnvironment _env; - public HelpController(IApplicationEnvironment env) { + private readonly ICliEnvironment _env; + public HelpController(ICliEnvironment env) { _env = env; } diff --git a/PWManager.CLI/Controllers/InitController.cs b/PWManager.CLI/Controllers/InitController.cs index 0b28214..0847da1 100644 --- a/PWManager.CLI/Controllers/InitController.cs +++ b/PWManager.CLI/Controllers/InitController.cs @@ -13,9 +13,9 @@ namespace PWManager.CLI.Controllers; public class InitController : IController { private readonly IDatabaseInitializerService _dbInit; - private readonly IApplicationEnvironment _environment; + private readonly ICliEnvironment _environment; - public InitController(IDatabaseInitializerService dbInit, IApplicationEnvironment environment) { + public InitController(IDatabaseInitializerService dbInit, ICliEnvironment environment) { _dbInit = dbInit; _environment = environment; } diff --git a/PWManager.CLI/Controllers/LoginController.cs b/PWManager.CLI/Controllers/LoginController.cs index f23fa9b..0d8c9c1 100644 --- a/PWManager.CLI/Controllers/LoginController.cs +++ b/PWManager.CLI/Controllers/LoginController.cs @@ -9,9 +9,9 @@ namespace PWManager.CLI.Controllers { public class LoginController : IController { - private readonly IApplicationEnvironment _env; + private readonly ICliEnvironment _env; private readonly ILoginService _loginService; - public LoginController(IApplicationEnvironment env, ILoginService loginService) { + public LoginController(ICliEnvironment env, ILoginService loginService) { _env = env; _loginService = loginService; } diff --git a/PWManager.CLI/Abstractions/CliEnvironment.cs b/PWManager.CLI/Environment/CliEnvironment.cs similarity index 74% rename from PWManager.CLI/Abstractions/CliEnvironment.cs rename to PWManager.CLI/Environment/CliEnvironment.cs index 9a75426..af2d4f5 100644 --- a/PWManager.CLI/Abstractions/CliEnvironment.cs +++ b/PWManager.CLI/Environment/CliEnvironment.cs @@ -1,10 +1,10 @@ using PWManager.Application.Context; using PWManager.Domain.Entities; -namespace PWManager.CLI.Abstractions; - -public class CliEnvironment : IApplicationEnvironment { +namespace PWManager.CLI.Environment; +public class CliEnvironment : ICliEnvironment, IDebugEnvironment, IUserEnvironment, ICryptEnvironment { + public bool IsDevelopmentMode { get; init; } = true; public bool RunningSession { get; set; } = false; diff --git a/PWManager.CLI/Program.cs b/PWManager.CLI/Program.cs index 26bf29b..f2a2ed1 100644 --- a/PWManager.CLI/Program.cs +++ b/PWManager.CLI/Program.cs @@ -4,13 +4,20 @@ using PWManager.Application.Services.Interfaces; using PWManager.CLI; using PWManager.CLI.Abstractions; +using PWManager.CLI.Environment; using PWManager.CLI.ExtensionMethods; using PWManager.CLI.Interfaces; using PWManager.Data; var services = new ServiceCollection(); -services.AddSingleton(); +// Configure Environment +var environment = new CliEnvironment(); +services.AddSingleton(environment); +services.AddSingleton(environment); +services.AddSingleton(environment); +services.AddSingleton(environment); + // Add all services to DI services.AddSingleton(); services.AddTransient(); diff --git a/PWManager.Data/Repositories/GroupRepository.cs b/PWManager.Data/Repositories/GroupRepository.cs index 39073e7..c91dba9 100644 --- a/PWManager.Data/Repositories/GroupRepository.cs +++ b/PWManager.Data/Repositories/GroupRepository.cs @@ -12,9 +12,9 @@ public class GroupRepository : IGroupRepository { private ApplicationDbContext _dbContext => DataContext.GetDbContext(); private readonly ICryptService _cryptService; - private readonly IApplicationEnvironment _environment; + private readonly IUserEnvironment _environment; - public GroupRepository(ICryptService cryptService, IApplicationEnvironment environment) { + public GroupRepository(ICryptService cryptService, IUserEnvironment environment) { _cryptService = cryptService; _environment = environment; } diff --git a/PWManager.Data/Repositories/SettingsRepository.cs b/PWManager.Data/Repositories/SettingsRepository.cs index 3498170..c8a86d6 100644 --- a/PWManager.Data/Repositories/SettingsRepository.cs +++ b/PWManager.Data/Repositories/SettingsRepository.cs @@ -11,11 +11,11 @@ namespace PWManager.Data.Repositories; internal class SettingsRepository : ISettingsRepository { - private readonly IApplicationEnvironment _environment; + private readonly IUserEnvironment _environment; private ApplicationDbContext _dbContext => DataContext.GetDbContext(); private readonly ICryptService _cryptService; - public SettingsRepository(IApplicationEnvironment env, ICryptService cryptService) { + public SettingsRepository(IUserEnvironment env, ICryptService cryptService) { _environment = env; _cryptService = cryptService; } diff --git a/PWManager.Data/Services/DatabaseInitializerService.cs b/PWManager.Data/Services/DatabaseInitializerService.cs index 96404db..9bda2be 100644 --- a/PWManager.Data/Services/DatabaseInitializerService.cs +++ b/PWManager.Data/Services/DatabaseInitializerService.cs @@ -13,20 +13,22 @@ internal class DatabaseInitializerService : IDatabaseInitializerService { private readonly IUserRepository _userRepo; private readonly IGroupRepository _groupRepository; - private readonly IApplicationEnvironment _environment; + private readonly IUserEnvironment _userEnvironment; + private readonly ICryptEnvironment _cryptEnvironment; private readonly ICryptService _cryptService; private readonly DataContextWrapper _dataContext; internal DatabaseInitializerService(DataContextWrapper wrapper, IUserRepository userRepo, - IGroupRepository groupRepository, IApplicationEnvironment environment, ICryptService cryptService) : this( - userRepo, groupRepository, environment, cryptService) { + IGroupRepository groupRepository, IUserEnvironment environment, ICryptService cryptService, ICryptEnvironment cryptEnvironment) : this( + userRepo, groupRepository, environment, cryptService, cryptEnvironment) { _dataContext = wrapper; } - public DatabaseInitializerService(IUserRepository userRepo, IGroupRepository groupRepository, IApplicationEnvironment environment, ICryptService cryptService) { + public DatabaseInitializerService(IUserRepository userRepo, IGroupRepository groupRepository, IUserEnvironment environment, ICryptService cryptService, ICryptEnvironment cryptEnvironment) { _userRepo = userRepo; _groupRepository = groupRepository; - _environment = environment; + _userEnvironment = environment; _cryptService = cryptService; + _cryptEnvironment = cryptEnvironment; _dataContext = new DataContextWrapper(); } @@ -43,8 +45,8 @@ public void InitDatabase(string path, string username, string password) { _dataContext.InitDataContext(path); var user = _userRepo.AddUser(username, password); - _environment.CurrentUser = user; - _environment.EncryptionKey = _cryptService.DeriveKeyFrom(password, username); + _userEnvironment.CurrentUser = user; + _cryptEnvironment.EncryptionKey = _cryptService.DeriveKeyFrom(password, username); _groupRepository.AddGroup(new Group("main", user.Id)); } diff --git a/PWManager.Data/Services/LoginService.cs b/PWManager.Data/Services/LoginService.cs index 85798c9..438a86d 100644 --- a/PWManager.Data/Services/LoginService.cs +++ b/PWManager.Data/Services/LoginService.cs @@ -13,20 +13,22 @@ public class LoginService : ILoginService { private readonly IGroupRepository _groupRepository; private readonly ISettingsRepository _settingsRepository; private readonly ICryptService _cryptService; - private readonly IApplicationEnvironment _env; + private readonly IUserEnvironment _userEnv; + private readonly ICryptEnvironment _cryptEnv; private readonly DataContextWrapper _dataContext; - internal LoginService(DataContextWrapper wrapper, IUserRepository userRepository, IGroupRepository groupRepository, ICryptService cryptService, ISettingsRepository settingsRepository, IApplicationEnvironment env) : this( - userRepository, groupRepository, cryptService, settingsRepository, env) { + internal LoginService(DataContextWrapper wrapper, IUserRepository userRepository, IGroupRepository groupRepository, ICryptService cryptService, ISettingsRepository settingsRepository, IUserEnvironment userEnv, ICryptEnvironment cryptEnv) : this( + userRepository, groupRepository, cryptService, settingsRepository, userEnv, cryptEnv) { _dataContext = wrapper; } - public LoginService(IUserRepository userRepository, IGroupRepository groupRepository, ICryptService cryptService, ISettingsRepository settingsRepository, IApplicationEnvironment env) { + public LoginService(IUserRepository userRepository, IGroupRepository groupRepository, ICryptService cryptService, ISettingsRepository settingsRepository, IUserEnvironment userEnv, ICryptEnvironment cryptEnv) { _userRepository = userRepository; _groupRepository = groupRepository; _settingsRepository = settingsRepository; + _cryptEnv = cryptEnv; _cryptService = cryptService; - _env = env; + _userEnv = userEnv; _dataContext = new DataContextWrapper(); } public void Login(string username, string password, string dbPath) { @@ -41,11 +43,11 @@ public void Login(string username, string password, string dbPath) { throw new UserFeedbackException("No such user found."); } - _env.CurrentUser = user; - _env.EncryptionKey = _cryptService.DeriveKeyFrom(password, username); + _userEnv.CurrentUser = user; + _cryptEnv.EncryptionKey = _cryptService.DeriveKeyFrom(password, username); var mainGroup = _settingsRepository.GetSettings().MainGroup; - _env.CurrentGroup = _groupRepository.GetGroup(mainGroup.MainGroupIdentifier); + _userEnv.CurrentGroup = _groupRepository.GetGroup(mainGroup.MainGroupIdentifier); } } } diff --git a/PWManager.UnitTests/Application/CryptServiceTest.cs b/PWManager.UnitTests/Application/CryptServiceTest.cs index eaab6b3..d8a0a32 100644 --- a/PWManager.UnitTests/Application/CryptServiceTest.cs +++ b/PWManager.UnitTests/Application/CryptServiceTest.cs @@ -52,7 +52,7 @@ public void CryptService_Should_EncryptAndDecryptToGetTheMessageBack() { var testSalt = "salt"; var testPlain = "Secret Message"; - var env = Substitute.For(); + var env = Substitute.For(); env.EncryptionKey.Returns("f??RH!\u0016???,?@?/V??V7R???n??\u0014? Qx"); var sut = new CryptService(env); diff --git a/PWManager.UnitTests/Services/DatabaseInitServiceTest.cs b/PWManager.UnitTests/Services/DatabaseInitServiceTest.cs index e62887c..6080730 100644 --- a/PWManager.UnitTests/Services/DatabaseInitServiceTest.cs +++ b/PWManager.UnitTests/Services/DatabaseInitServiceTest.cs @@ -15,7 +15,8 @@ public class DatabaseInitServiceTest { private DatabaseInitializerService _sut; private IGroupRepository _groupRepo = Substitute.For(); private ICryptService _cryptService = Substitute.For(); - private IApplicationEnvironment _env = Substitute.For(); + private IUserEnvironment _userEnv = Substitute.For(); + private ICryptEnvironment _cryptEnv = Substitute.For(); private DataContextWrapper _wrapper = Substitute.For(); private IUserRepository _userRepo = Substitute.For(); @@ -26,18 +27,18 @@ public void Init_Should_SetEnvironment() { _userRepo.AddUser(Arg.Any(), Arg.Any()).Returns(user); - _sut = new DatabaseInitializerService(_wrapper,_userRepo,_groupRepo,_env, _cryptService); + _sut = new DatabaseInitializerService(_wrapper,_userRepo,_groupRepo,_userEnv, _cryptService, _cryptEnv); _sut.InitDatabase(".", "TestUserName", "WhatAPassword"); - Assert.Equal(user,_env.CurrentUser); - Assert.NotNull(_env.EncryptionKey); + Assert.Equal(user,_userEnv.CurrentUser); + Assert.NotNull(_cryptEnv.EncryptionKey); } [Fact] public void Init_ShouldNot_AcceptInvalidName() { _wrapper.DatabaseExists(Arg.Any()).Returns(false); - _sut = new DatabaseInitializerService(_wrapper,_userRepo,_groupRepo,_env, _cryptService); + _sut = new DatabaseInitializerService(_wrapper,_userRepo,_groupRepo,_userEnv, _cryptService, _cryptEnv); var ex = Assert.Throws(() => _sut.InitDatabase(".","asd$", "WhatAPassword")); @@ -47,7 +48,7 @@ public void Init_ShouldNot_AcceptInvalidName() { [Fact] public void Init_ShouldNot_InitExistingDb() { _wrapper.DatabaseExists(Arg.Any()).Returns(true); - _sut = new DatabaseInitializerService(_wrapper,_userRepo,_groupRepo,_env, _cryptService); + _sut = new DatabaseInitializerService(_wrapper,_userRepo,_groupRepo,_userEnv, _cryptService, _cryptEnv); var ex = Assert.Throws(() => _sut.InitDatabase(".","TestUserName", "WhatAPassword")); diff --git a/PWManager.UnitTests/Services/LoginServiceTest.cs b/PWManager.UnitTests/Services/LoginServiceTest.cs index e238825..d866dfb 100644 --- a/PWManager.UnitTests/Services/LoginServiceTest.cs +++ b/PWManager.UnitTests/Services/LoginServiceTest.cs @@ -17,7 +17,8 @@ public class LoginServiceTest { private ICryptService _cryptService = Substitute.For(); private ISettingsRepository _settingsRepository = Substitute.For(); private DataContextWrapper _wrapper = Substitute.For(); - private IApplicationEnvironment _env = Substitute.For(); + private IUserEnvironment _userEnv = Substitute.For(); + private ICryptEnvironment _cryptEnv = Substitute.For(); private IUserRepository _userRepo = Substitute.For(); [Fact] @@ -31,12 +32,12 @@ public void Login_Should_SetEnviroment() { ); _groupRepo.GetGroup(Arg.Any()).Returns(group); - _sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _env); + _sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _userEnv, _cryptEnv); _sut.Login("TestUserName", "WhatAPasswort", "."); - Assert.Equal(user, _env.CurrentUser); - Assert.NotNull(_env.EncryptionKey); - Assert.NotNull(_env.CurrentGroup); + Assert.Equal(user, _userEnv.CurrentUser); + Assert.NotNull(_cryptEnv.EncryptionKey); + Assert.NotNull(_userEnv.CurrentGroup); } [Fact] @@ -46,7 +47,7 @@ public void Login_ShouldNot_IfDatabaseDoesntExists() { _userRepo.CheckPasswordAttempt(Arg.Any(), Arg.Any()).Returns(user); - _sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _env); + _sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _userEnv, _cryptEnv); var ex = Assert.Throws(() => _sut.Login("TestUserName", "WhatAPassword", ".")); Assert.Equal("Database not found.", ex.Message); @@ -59,7 +60,7 @@ public void Login_ShouldNot_IfUserNotFound() { _userRepo.CheckPasswordAttempt(Arg.Any(), Arg.Any()).ReturnsNull(); - _sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _env); + _sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _userEnv, _cryptEnv); var ex = Assert.Throws(() => _sut.Login("TestUserName", "WhatAPassword", ".")); Assert.Equal("No such user found.", ex.Message);