Skip to content

Commit

Permalink
Added AccountService Test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMi-Ha committed Apr 10, 2024
1 parent 6d12d4a commit 8a04e68
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .github/workflows/test_on_pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ jobs:
- name: Run Unit Tests
run: |
cd PWManager.UnitTests
dotnet test --logger "console;verbosity=detailed"
rm -rf coverage
rm -rf TestResults
dotnet tool install -g dotnet-reportgenerator-globaltool
dotnet test --logger "console;verbosity=detailed" --collect:"XPlat Code Coverage"
reportgenerator "-reports:TestResults/*/coverage*" "-targetdir:coverage" "-reporttypes:TextSummary"
cat coverage/Summary.txt
rm -rf coverage
rm -rf TestResults
1 change: 1 addition & 0 deletions PWManager.Application/Services/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void DeleteAccount(string identifier) {
throw new UserFeedbackException(MessageStrings.ACCOUNT_NOT_FOUND);
}

_environment.CurrentGroup.RemoveAccount(acc);
_groupRepo.DeleteAccountInGroup(acc, _environment.CurrentGroup);
}

Expand Down
126 changes: 126 additions & 0 deletions PWManager.UnitTests/Services/AccountServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using NSubstitute;
using PWManager.Application.Context;
using PWManager.Application.Services;
using PWManager.Application.Services.Interfaces;
using PWManager.Domain.Entities;
using PWManager.Domain.Repositories;
using PWManager.Domain.Services.Interfaces;

namespace PWManager.UnitTests.Services;

public class AccountServiceTest {

private AccountService _sut;

[Fact]
public void AccountService_Should_GetAllNames() {
var envWithGroup = MockUserEnvironmentWithGroup();

_sut = new AccountService(envWithGroup, null, null, null);

var groups = _sut.GetCurrentAccountNames();

Assert.Equal(2, groups.Count);
Assert.Equal("AccountId", groups[0]);
Assert.Equal("AccountId2", groups[1]);
}

[Fact]
public void AccountService_Should_AddNewAccount() {
var env = MockUserEnvironmentWithGroup();
var groupRepo = MockGroupRepo();

_sut = new AccountService(env, groupRepo, null, null);

_sut.AddNewAccount("NewIdentifier", "NewName", "NewPassword");

Assert.Equal(3, env.CurrentGroup.Accounts.Count);
Assert.Equal("NewIdentifier", env.CurrentGroup.Accounts[2].Identifier);
Assert.Equal("NewName", env.CurrentGroup.Accounts[2].LoginName);
Assert.Equal("NewPassword", env.CurrentGroup.Accounts[2].Password);

groupRepo.Received().AddAccountToGroup(Arg.Any<Account>(), Arg.Any<Group>());

}

[Fact]
public void AccountService_Should_GetAccountByIdentifier() {
var env = MockUserEnvironmentWithGroup();

_sut = new AccountService(env, null, null, null);
var account = _sut.GetAccountByIdentifier("AccountId");

Assert.Equal(env.CurrentGroup.Accounts[0], account);
}

[Fact]
public void AccountService_Should_CopyPassword() {
var env = MockUserEnvironmentWithGroup();
var clipboard = Substitute.For<IClipboard>();

_sut = new AccountService(env, null, clipboard, null);

_sut.CopyPasswordToClipboard("AccountId");

clipboard.Received().WriteClipboard(Arg.Is<string>(e => e == "Password1"));
}

[Fact]
public void AccountService_Should_CopyLoginName() {
var env = MockUserEnvironmentWithGroup();
var clipboard = Substitute.For<IClipboard>();

_sut = new AccountService(env, null, clipboard, null);

_sut.CopyLoginnameToClipboard("AccountId");

clipboard.Received().WriteClipboard(Arg.Is<string>(e => e == "Name1"));
}

[Fact]
public void AccountService_Should_RegeneratePassword() {
var env = MockUserEnvironmentWithGroup();
var newPassword = "NewPasswordGenerated";
var passGen = MockPassGenReturns(newPassword);
var groupRepo = MockGroupRepo();

_sut = new AccountService(env, groupRepo, null, passGen);

_sut.RegeneratePassword("AccountId");

Assert.Equal(newPassword, env.CurrentGroup.Accounts[0].Password);
groupRepo.Received().UpdateAccountInGroup(Arg.Any<Account>(), Arg.Any<Group>());
}

[Fact]
public void AccountService_Should_DeleteAccount() {
var env = MockUserEnvironmentWithGroup();
var groupRepo = MockGroupRepo();

_sut = new AccountService(env, groupRepo, null, null);

_sut.DeleteAccount("AccountId");

Assert.Null(env.CurrentGroup.FindByIdentifier("AccountId"));
groupRepo.Received().DeleteAccountInGroup(Arg.Any<Account>(), Arg.Any<Group>());
}

private IGroupRepository MockGroupRepo() {
var groupRepo = Substitute.For<IGroupRepository>();
groupRepo.AddAccountToGroup(Arg.Any<Account>(), Arg.Any<Group>()).Returns(true);
groupRepo.UpdateAccountInGroup(Arg.Any<Account>(), Arg.Any<Group>()).Returns(true);
groupRepo.DeleteAccountInGroup(Arg.Any<Account>(), Arg.Any<Group>()).Returns(true);
return groupRepo;
}
private IUserEnvironment MockUserEnvironmentWithGroup() {
var env = Substitute.For<IUserEnvironment>();
env.CurrentGroup.Returns(new Group("GroupId", new List<Account>() {new Account("AccountId", "Name1", "Password1"), new Account("AccountId2", "Name2", "Password2")} ));
return env;
}

private IPasswordGeneratorService MockPassGenReturns(string pw) {
var pwGen = Substitute.For<IPasswordGeneratorService>();
pwGen.GeneratePassword().Returns(pw);
return pwGen;
}
}

0 comments on commit 8a04e68

Please sign in to comment.