Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pw 55 out of session attrib #56

Merged
merged 5 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions PWManager.CLI/Attributes/NoSessionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace PWManager.CLI.Attributes;

[AttributeUsage(AttributeTargets.Class)]
public class NoSessionAttribute : Attribute {

}
7 changes: 6 additions & 1 deletion PWManager.CLI/ConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,13 @@ public void MapCommand<TCommand>(AvailableCommands command) {
}

private bool IsSessionLocked(MemberInfo controllerType) {
return Attribute.GetCustomAttribute(controllerType, typeof(SessionOnlyAttribute)) is not null
var sessionOnly = Attribute.GetCustomAttribute(controllerType, typeof(SessionOnlyAttribute)) is not null
&& !_environment.RunningSession;

var noSessionOnly = Attribute.GetCustomAttribute(controllerType, typeof(NoSessionAttribute)) is not null
&& _environment.RunningSession;

return sessionOnly || noSessionOnly;
}
}
}
13 changes: 5 additions & 8 deletions PWManager.CLI/Controllers/InitController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@
using PWManager.Application.Exceptions;
using PWManager.Application.Services.Interfaces;
using PWManager.CLI.Abstractions;
using PWManager.CLI.Attributes;
using PWManager.CLI.Enums;
using PWManager.CLI.Interfaces;
using Sharprompt;

namespace PWManager.CLI.Controllers;

[NoSession]
public class InitController : IController {

private readonly IDatabaseInitializerService _dbInit;
private readonly ICliEnvironment _environment;

public InitController(IDatabaseInitializerService dbInit, ICliEnvironment environment) {

public InitController(IDatabaseInitializerService dbInit) {

_dbInit = dbInit;
_environment = environment;
}

public ExitCondition Handle(string[] args) {
if (_environment.RunningSession) {
throw new UserFeedbackException("Command not available in a session!");
}

var path = Prompt.Input<string>("Where do you want to create your database file?");
while(!Path.Exists(path)) {
Console.WriteLine("The given path does not exist.");
Expand Down
18 changes: 8 additions & 10 deletions PWManager.CLI/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
using PWManager.Application.Exceptions;
using PWManager.Application.Services.Interfaces;
using PWManager.CLI.Abstractions;
using PWManager.CLI.Attributes;
using PWManager.CLI.Enums;
using PWManager.CLI.Interfaces;
using Sharprompt;
using System.IO;

namespace PWManager.CLI.Controllers {

[NoSession]
public class LoginController : IController {
private readonly ICliEnvironment _env;

private readonly ILoginService _loginService;
public LoginController(ICliEnvironment env, ILoginService loginService) {
_env = env;
public LoginController(ILoginService loginService) {

_loginService = loginService;
}
public ExitCondition Handle(string[] args) {
if (_env.RunningSession) {
throw new UserFeedbackException("Command not available in a session!");
}


public ExitCondition Handle(string[] args) {
(var username, var path) = ParseArgs(args);

var lastUser = ConfigFileHandler.ReadDefaultFile();
Expand All @@ -35,8 +35,6 @@ public ExitCondition Handle(string[] args) {

ConfigFileHandler.WriteDefaultFile(username, path);
Console.WriteLine($"Welcome {username} :)");

_env.RunningSession = true;
return ExitCondition.CONTINUE;
}

Expand Down
10 changes: 7 additions & 3 deletions PWManager.Data/Services/LoginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@ public class LoginService : ILoginService {
private readonly IGroupRepository _groupRepository;
private readonly ISettingsRepository _settingsRepository;
private readonly ICryptService _cryptService;
private readonly ICliEnvironment _cliEnv;
private readonly IUserEnvironment _userEnv;
private readonly ICryptEnvironment _cryptEnv;

private readonly DataContextWrapper _dataContext;

internal LoginService(DataContextWrapper wrapper, IUserRepository userRepository, IGroupRepository groupRepository, ICryptService cryptService, ISettingsRepository settingsRepository, IUserEnvironment userEnv, ICryptEnvironment cryptEnv) : this(
userRepository, groupRepository, cryptService, settingsRepository, userEnv, cryptEnv) {
internal LoginService(DataContextWrapper wrapper, IUserRepository userRepository, IGroupRepository groupRepository, ICryptService cryptService, ISettingsRepository settingsRepository, ICliEnvironment cliEnv, IUserEnvironment userEnv, ICryptEnvironment cryptEnv) : this(
userRepository, groupRepository, cryptService, settingsRepository, cliEnv, userEnv, cryptEnv) {
_dataContext = wrapper;
}
public LoginService(IUserRepository userRepository, IGroupRepository groupRepository, ICryptService cryptService, ISettingsRepository settingsRepository, IUserEnvironment userEnv, ICryptEnvironment cryptEnv) {
public LoginService(IUserRepository userRepository, IGroupRepository groupRepository, ICryptService cryptService, ISettingsRepository settingsRepository, ICliEnvironment cliEnv, IUserEnvironment userEnv, ICryptEnvironment cryptEnv) {
_userRepository = userRepository;
_groupRepository = groupRepository;
_settingsRepository = settingsRepository;
_cryptEnv = cryptEnv;
_cryptService = cryptService;
_userEnv = userEnv;
_cliEnv = cliEnv;
_dataContext = new DataContextWrapper();
}
public void Login(string username, string password, string dbPath) {
Expand All @@ -47,6 +49,8 @@ public void Login(string username, string password, string dbPath) {
_cryptEnv.EncryptionKey = _cryptService.DeriveKeyFrom(password, username);

var mainGroup = _settingsRepository.GetSettings().MainGroup;

_cliEnv.RunningSession = true;
_userEnv.CurrentGroup = _groupRepository.GetGroup(mainGroup.MainGroupIdentifier);
}
}
Expand Down
14 changes: 1 addition & 13 deletions PWManager.UnitTests/Cli/LoginControllerTest.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
using NSubstitute;
using PWManager.Application.Context;
using PWManager.Application.Services.Interfaces;
using PWManager.CLI.Controllers;
using PWManager.Data.Repositories;
using PWManager.Data.Services;
using PWManager.Domain.Entities;
using PWManager.Domain.Repositories;
using PWManager.Domain.ValueObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PWManager.UnitTests.Cli {
public class LoginControllerTest {
Expand All @@ -20,7 +8,7 @@ public class LoginControllerTest {

public LoginControllerTest()
{
_sut = Substitute.ForPartsOf<LoginController>(null, null);
_sut = Substitute.ForPartsOf<LoginController>(new object[] {null});
_sut.AskForInput(Arg.Any<string>()).Returns("Test");
}

Expand Down
8 changes: 5 additions & 3 deletions PWManager.UnitTests/Services/LoginServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class LoginServiceTest {
private ICryptService _cryptService = Substitute.For<ICryptService>();
private ISettingsRepository _settingsRepository = Substitute.For<ISettingsRepository>();
private DataContextWrapper _wrapper = Substitute.For<DataContextWrapper>();
private ICliEnvironment _cliEnv = Substitute.For<ICliEnvironment>();
private IUserEnvironment _userEnv = Substitute.For<IUserEnvironment>();
private ICryptEnvironment _cryptEnv = Substitute.For<ICryptEnvironment>();
private IUserRepository _userRepo = Substitute.For<IUserRepository>();
Expand All @@ -32,12 +33,13 @@ public void Login_Should_SetEnviroment() {
);
_groupRepo.GetGroup(Arg.Any<string>()).Returns(group);

_sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _userEnv, _cryptEnv);
_sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _cliEnv, _userEnv, _cryptEnv);
_sut.Login("TestUserName", "WhatAPasswort", ".");

Assert.Equal(user, _userEnv.CurrentUser);
Assert.NotNull(_cryptEnv.EncryptionKey);
Assert.NotNull(_userEnv.CurrentGroup);
Assert.True(_cliEnv.RunningSession);
}

[Fact]
Expand All @@ -47,7 +49,7 @@ public void Login_ShouldNot_IfDatabaseDoesntExists() {
_userRepo.CheckPasswordAttempt(Arg.Any<string>(), Arg.Any<string>()).Returns(user);


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

var ex = Assert.Throws<UserFeedbackException>(() => _sut.Login("TestUserName", "WhatAPassword", "."));
Assert.Equal("Database not found.", ex.Message);
Expand All @@ -60,7 +62,7 @@ public void Login_ShouldNot_IfUserNotFound() {
_userRepo.CheckPasswordAttempt(Arg.Any<string>(), Arg.Any<string>()).ReturnsNull();


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

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