Skip to content

Commit

Permalink
Merge pull request #54 from CUMGroup/pw-48-split-environment
Browse files Browse the repository at this point in the history
Pw 48 split environment
  • Loading branch information
jas20202 authored Mar 2, 2024
2 parents 98fcddb + 43fafbc commit 796c9f0
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 65 deletions.
16 changes: 0 additions & 16 deletions PWManager.Application/Context/IApplicationEnvironment.cs

This file was deleted.

6 changes: 6 additions & 0 deletions PWManager.Application/Context/ICliEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace PWManager.Application.Context;

public interface ICliEnvironment {
public bool RunningSession { get; set; }
public string Prompt { get; }
}
5 changes: 5 additions & 0 deletions PWManager.Application/Context/ICryptEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace PWManager.Application.Context;

public interface ICryptEnvironment {
public string? EncryptionKey { get; set; }
}
5 changes: 5 additions & 0 deletions PWManager.Application/Context/IDebugEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace PWManager.Application.Context;

public interface IDebugEnvironment {
public bool IsDevelopmentMode { get; init; }
}
9 changes: 9 additions & 0 deletions PWManager.Application/Context/IUserEnvironment.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
4 changes: 2 additions & 2 deletions PWManager.Application/Services/CryptService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 6 additions & 4 deletions PWManager.CLI/ConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ internal class ConsoleRunner : IRunner {

private readonly Type?[] _controller = new Type[Enum.GetNames<AvailableCommands>().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();
}

Expand Down Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions PWManager.CLI/Controllers/HelpController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions PWManager.CLI/Controllers/InitController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions PWManager.CLI/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
9 changes: 8 additions & 1 deletion PWManager.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IApplicationEnvironment,CliEnvironment>();
// Configure Environment
var environment = new CliEnvironment();
services.AddSingleton<ICliEnvironment>(environment);
services.AddSingleton<ICryptEnvironment>(environment);
services.AddSingleton<IDebugEnvironment>(environment);
services.AddSingleton<IUserEnvironment>(environment);

// Add all services to DI
services.AddSingleton<IRunner, ConsoleRunner>();
services.AddTransient<IClipboard, Clipboard>();
Expand Down
4 changes: 2 additions & 2 deletions PWManager.Data/Repositories/GroupRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions PWManager.Data/Repositories/SettingsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 9 additions & 7 deletions PWManager.Data/Services/DatabaseInitializerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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));
}
Expand Down
18 changes: 10 additions & 8 deletions PWManager.Data/Services/LoginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion PWManager.UnitTests/Application/CryptServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void CryptService_Should_EncryptAndDecryptToGetTheMessageBack() {
var testSalt = "salt";
var testPlain = "Secret Message";

var env = Substitute.For<IApplicationEnvironment>();
var env = Substitute.For<ICryptEnvironment>();
env.EncryptionKey.Returns("f??RH!\u0016???,?@?/V??V7R???n??\u0014? Qx");
var sut = new CryptService(env);

Expand Down
13 changes: 7 additions & 6 deletions PWManager.UnitTests/Services/DatabaseInitServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class DatabaseInitServiceTest {
private DatabaseInitializerService _sut;
private IGroupRepository _groupRepo = Substitute.For<IGroupRepository>();
private ICryptService _cryptService = Substitute.For<ICryptService>();
private IApplicationEnvironment _env = Substitute.For<IApplicationEnvironment>();
private IUserEnvironment _userEnv = Substitute.For<IUserEnvironment>();
private ICryptEnvironment _cryptEnv = Substitute.For<ICryptEnvironment>();
private DataContextWrapper _wrapper = Substitute.For<DataContextWrapper>();
private IUserRepository _userRepo = Substitute.For<IUserRepository>();

Expand All @@ -26,18 +27,18 @@ public void Init_Should_SetEnvironment() {
_userRepo.AddUser(Arg.Any<string>(), Arg.Any<string>()).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<string>()).Returns(false);
_sut = new DatabaseInitializerService(_wrapper,_userRepo,_groupRepo,_env, _cryptService);
_sut = new DatabaseInitializerService(_wrapper,_userRepo,_groupRepo,_userEnv, _cryptService, _cryptEnv);

var ex = Assert.Throws<UserFeedbackException>(() => _sut.InitDatabase(".","asd$", "WhatAPassword"));

Expand All @@ -47,7 +48,7 @@ public void Init_ShouldNot_AcceptInvalidName() {
[Fact]
public void Init_ShouldNot_InitExistingDb() {
_wrapper.DatabaseExists(Arg.Any<string>()).Returns(true);
_sut = new DatabaseInitializerService(_wrapper,_userRepo,_groupRepo,_env, _cryptService);
_sut = new DatabaseInitializerService(_wrapper,_userRepo,_groupRepo,_userEnv, _cryptService, _cryptEnv);

var ex = Assert.Throws<UserFeedbackException>(() => _sut.InitDatabase(".","TestUserName", "WhatAPassword"));

Expand Down
15 changes: 8 additions & 7 deletions PWManager.UnitTests/Services/LoginServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class LoginServiceTest {
private ICryptService _cryptService = Substitute.For<ICryptService>();
private ISettingsRepository _settingsRepository = Substitute.For<ISettingsRepository>();
private DataContextWrapper _wrapper = Substitute.For<DataContextWrapper>();
private IApplicationEnvironment _env = Substitute.For<IApplicationEnvironment>();
private IUserEnvironment _userEnv = Substitute.For<IUserEnvironment>();
private ICryptEnvironment _cryptEnv = Substitute.For<ICryptEnvironment>();
private IUserRepository _userRepo = Substitute.For<IUserRepository>();

[Fact]
Expand All @@ -31,12 +32,12 @@ public void Login_Should_SetEnviroment() {
);
_groupRepo.GetGroup(Arg.Any<string>()).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]
Expand All @@ -46,7 +47,7 @@ public void Login_ShouldNot_IfDatabaseDoesntExists() {
_userRepo.CheckPasswordAttempt(Arg.Any<string>(), Arg.Any<string>()).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<UserFeedbackException>(() => _sut.Login("TestUserName", "WhatAPassword", "."));
Assert.Equal("Database not found.", ex.Message);
Expand All @@ -59,7 +60,7 @@ public void Login_ShouldNot_IfUserNotFound() {
_userRepo.CheckPasswordAttempt(Arg.Any<string>(), Arg.Any<string>()).ReturnsNull();


_sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _env);
_sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _userEnv, _cryptEnv);

var ex = Assert.Throws<UserFeedbackException>(() => _sut.Login("TestUserName", "WhatAPassword", "."));
Assert.Equal("No such user found.", ex.Message);
Expand Down

0 comments on commit 796c9f0

Please sign in to comment.