-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created and run unit Tests - all green
- Loading branch information
Showing
4 changed files
with
197 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
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 { | ||
|
||
private LoginController _sut; | ||
|
||
public LoginControllerTest() | ||
{ | ||
_sut = Substitute.ForPartsOf<LoginController>(null, null); | ||
_sut.AskForInput(Arg.Any<string>()).Returns("Test"); | ||
} | ||
|
||
[Fact] | ||
public void Arguments_ShouldBe_Empty_WithoutArgs() { | ||
string[] args = { }; | ||
|
||
(var username, var path) = _sut.ParseArgs(args); | ||
|
||
Assert.Equal(0, username.Length); | ||
Assert.Equal(0, path.Length); | ||
} | ||
|
||
[Fact] | ||
public void Arguments_Should_Return_Path() { | ||
string[] args = { "-d", "TestPath" }; | ||
|
||
(var username, var path) = _sut.ParseArgs(args); | ||
|
||
Assert.Equal(0, username.Length); | ||
Assert.Equal("TestPath", path); | ||
} | ||
|
||
[Fact] | ||
public void Arguments_Should_Return_Username() { | ||
string[] args = { "-u", "TestUserName" }; | ||
|
||
(var username, var path) = _sut.ParseArgs(args); | ||
|
||
Assert.Equal("TestUserName", username); | ||
Assert.Equal(0, path.Length); | ||
} | ||
|
||
|
||
[Fact] | ||
public void Arguments_Should_Return_Path_And_PromptName() { | ||
string[] args = { "-d", "TestPath", "-u" }; | ||
|
||
(var username, var path) = _sut.ParseArgs(args); | ||
|
||
Assert.Equal("Test", username); | ||
Assert.Equal("TestPath", path); | ||
} | ||
|
||
[Fact] | ||
public void Arguments_Should_Return_Username_And_PromptPath() { | ||
string[] args = { "-d", "-u", "TestUserName" }; | ||
|
||
(var username, var path) = _sut.ParseArgs(args); | ||
|
||
Assert.Equal("TestUserName", username); | ||
Assert.Equal("Test", path); | ||
} | ||
|
||
[Fact] | ||
public void Arguments_Should_Return_From_Prompts() { | ||
string[] args = { "-d", "-u" }; | ||
|
||
(var username, var path) = _sut.ParseArgs(args); | ||
|
||
Assert.Equal("Test", username); | ||
Assert.Equal("Test", path); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using NSubstitute; | ||
using NSubstitute.ReturnsExtensions; | ||
using PWManager.Application.Context; | ||
using PWManager.Application.Exceptions; | ||
using PWManager.Data.Abstraction; | ||
using PWManager.Data.Services; | ||
using PWManager.Domain.Entities; | ||
using PWManager.Domain.Repositories; | ||
using PWManager.Domain.Services.Interfaces; | ||
using PWManager.Domain.ValueObjects; | ||
|
||
namespace PWManager.UnitTests.Services { | ||
public class LoginServiceTest { | ||
|
||
private LoginService _sut; | ||
private IGroupRepository _groupRepo = Substitute.For<IGroupRepository>(); | ||
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 IUserRepository _userRepo = Substitute.For<IUserRepository>(); | ||
|
||
[Fact] | ||
public void Login_Should_SetEnviroment() { | ||
_wrapper.DatabaseExists(Arg.Any<string>()).Returns(true); | ||
var user = new User(Guid.NewGuid().ToString(), DateTimeOffset.Now, DateTimeOffset.Now, "TestUserName"); | ||
_userRepo.CheckPasswordAttempt(Arg.Any<string>(), Arg.Any<string>()).Returns(user); | ||
var group = new Group("TestGroup", user.Id); | ||
_settingsRepository.GetSettings().Returns( | ||
new Settings(user.Id, null, null, new MainGroupSetting(group.Identifier)) | ||
); | ||
_groupRepo.GetGroup(Arg.Any<string>()).Returns(group); | ||
|
||
_sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _env); | ||
_sut.Login("TestUserName", "WhatAPasswort", "."); | ||
|
||
Assert.Equal(user, _env.CurrentUser); | ||
Assert.NotNull(_env.EncryptionKey); | ||
Assert.NotNull(_env.CurrentGroup); | ||
} | ||
|
||
[Fact] | ||
public void Login_ShouldNot_IfDatabaseDoesntExists() { | ||
_wrapper.DatabaseExists(Arg.Any<string>()).Returns(false); | ||
var user = new User(Guid.NewGuid().ToString(), DateTimeOffset.Now, DateTimeOffset.Now, "TestUserName"); | ||
_userRepo.CheckPasswordAttempt(Arg.Any<string>(), Arg.Any<string>()).Returns(user); | ||
|
||
|
||
_sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _env); | ||
|
||
var ex = Assert.Throws<UserFeedbackException>(() => _sut.Login("TestUserName", "WhatAPassword", ".")); | ||
Assert.Equal("Database not found.", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void Login_ShouldNot_IfUserNotFound() { | ||
_wrapper.DatabaseExists(Arg.Any<string>()).Returns(true); | ||
var user = new User(Guid.NewGuid().ToString(), DateTimeOffset.Now, DateTimeOffset.Now, "TestUserName"); | ||
_userRepo.CheckPasswordAttempt(Arg.Any<string>(), Arg.Any<string>()).ReturnsNull(); | ||
|
||
|
||
_sut = new LoginService(_wrapper, _userRepo, _groupRepo, _cryptService, _settingsRepository, _env); | ||
|
||
var ex = Assert.Throws<UserFeedbackException>(() => _sut.Login("TestUserName", "WhatAPassword", ".")); | ||
Assert.Equal("No such user found.", ex.Message); | ||
} | ||
|
||
} | ||
} |