Skip to content

Commit

Permalink
Added more account testts
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMi-Ha committed Apr 10, 2024
1 parent 8a04e68 commit ab162e4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions PWManager.Application/Services/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void AddNewAccount(string identifier, string loginname, string password)

var saved = _groupRepo.AddAccountToGroup(account, _environment.CurrentGroup);
if (!saved) {
_environment.CurrentGroup.RemoveAccount(account);
throw new UserFeedbackException(MessageStrings.FAILED_ADDING_ACCOUNT);
}
}
Expand Down
64 changes: 64 additions & 0 deletions PWManager.UnitTests/Services/AccountServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NSubstitute;
using PWManager.Application.Context;
using PWManager.Application.Exceptions;
using PWManager.Application.Services;
using PWManager.Application.Services.Interfaces;
using PWManager.Domain.Entities;
Expand Down Expand Up @@ -43,6 +44,29 @@ public void AccountService_Should_AddNewAccount() {

}

[Fact]
public void AccountService_ShouldNot_AddExistingAccount() {
var env = MockUserEnvironmentWithGroup();
_sut = new AccountService(env, null, null, null);

var ex = Assert.Throws<UserFeedbackException>(() => _sut.AddNewAccount("AccountId", "Name", "password"));

Assert.Equal(MessageStrings.AccountAlreadyExist("AccountId"), ex.Message);
}

[Fact]
public void AccountService_ShouldNot_AddWhenFailed() {
var env = MockUserEnvironmentWithGroup();
var groupRepo = MockGroupRepo();
groupRepo.AddAccountToGroup(Arg.Any<Account>(), Arg.Any<Group>()).Returns(false);
_sut = new AccountService(env, groupRepo, null, null);

var ex = Assert.Throws<UserFeedbackException>(() => _sut.AddNewAccount("NewAccountId", "Name", "password"));

Assert.Equal(MessageStrings.FAILED_ADDING_ACCOUNT, ex.Message);
Assert.Null(env.CurrentGroup.FindByIdentifier("NewAccountId"));
}

[Fact]
public void AccountService_Should_GetAccountByIdentifier() {
var env = MockUserEnvironmentWithGroup();
Expand All @@ -65,6 +89,26 @@ public void AccountService_Should_CopyPassword() {
clipboard.Received().WriteClipboard(Arg.Is<string>(e => e == "Password1"));
}

[Fact]
public void AccountService_ShouldNot_CopyNonExistingPassword() {
var env = MockUserEnvironmentWithGroup();
_sut = new AccountService(env, null, null, null);

var ex = Assert.Throws<UserFeedbackException>(() => _sut.CopyPasswordToClipboard("AccountIdNotExists"));

Assert.Equal(MessageStrings.ACCOUNT_NOT_FOUND, ex.Message);
}

[Fact]
public void AccountService_ShouldNot_CopyNonExistingLoginName() {
var env = MockUserEnvironmentWithGroup();
_sut = new AccountService(env, null, null, null);

var ex = Assert.Throws<UserFeedbackException>(() => _sut.CopyLoginnameToClipboard("AccountIdNotExists"));

Assert.Equal(MessageStrings.ACCOUNT_NOT_FOUND, ex.Message);
}

[Fact]
public void AccountService_Should_CopyLoginName() {
var env = MockUserEnvironmentWithGroup();
Expand All @@ -91,6 +135,16 @@ public void AccountService_Should_RegeneratePassword() {
Assert.Equal(newPassword, env.CurrentGroup.Accounts[0].Password);
groupRepo.Received().UpdateAccountInGroup(Arg.Any<Account>(), Arg.Any<Group>());
}

[Fact]
public void AccountService_ShouldNot_RegenerateNonExistingPassword() {
var env = MockUserEnvironmentWithGroup();
_sut = new AccountService(env, null, null, null);

var ex = Assert.Throws<UserFeedbackException>(() => _sut.RegeneratePassword("AccountIdNotExists"));

Assert.Equal(MessageStrings.ACCOUNT_NOT_FOUND, ex.Message);
}

[Fact]
public void AccountService_Should_DeleteAccount() {
Expand All @@ -105,6 +159,16 @@ public void AccountService_Should_DeleteAccount() {
groupRepo.Received().DeleteAccountInGroup(Arg.Any<Account>(), Arg.Any<Group>());
}

[Fact]
public void AccountService_ShouldNot_DeleteNonExistingAccount() {
var env = MockUserEnvironmentWithGroup();
_sut = new AccountService(env, null, null, null);

var ex = Assert.Throws<UserFeedbackException>(() => _sut.DeleteAccount("AccountIdNotExists"));

Assert.Equal(MessageStrings.ACCOUNT_NOT_FOUND, ex.Message);
}

private IGroupRepository MockGroupRepo() {
var groupRepo = Substitute.For<IGroupRepository>();
groupRepo.AddAccountToGroup(Arg.Any<Account>(), Arg.Any<Group>()).Returns(true);
Expand Down

0 comments on commit ab162e4

Please sign in to comment.