public async Task Given_ExistingLastPeriod_When_EndDateUpdatedWithDateBeforeStart_Then_ResultIsFailure() { // Arrange. var lastPeriod = new Period( new DateTime(2020, 2, 1), new DateTime(2020, 2, 29)); AccountingManager testObject = AccountingManagerFactory.Create(out IAccountingDataAccess accountingDataAccess); accountingDataAccess .GetLastPeriod() .Returns(GetResult <Period> .CreateSuccess(lastPeriod)); var updateEndDate = new UpdateLastPeriodEndDateDto(lastPeriod.Start.AddDays(-1)); accountingDataAccess .UpdateLastPeriodEndDate(updateEndDate.NewEnd) .Returns(ActionResult.CreateSuccess()); // Act. ActionResult result = await testObject.UpdateLastPeriodEndDate(updateEndDate); // Assert. Assert.IsFalse(result.IsSuccess); }
private async Task <ActionResult <string[]> > VerifyNewAccount(NewAccount newAccount) { var errors = new List <string>(); Check(string.IsNullOrWhiteSpace(newAccount.AccountName), "Empty account name is not allowed"); Check(newAccount.Password.Length == 0, "Empty password is not allowed"); Check(await AccountNameIsUsed(newAccount.AccountName), "Account name has already been used"); if (errors.Any()) { return(ActionResult <string[]> .CreateError(errors.ToArray())); } else { return(ActionResult <string[]> .CreateSuccess()); } void Check(bool check, string error) { if (check) { errors.Add(error); } } }
public async Task Given_ExistingPeriodWhichIsLastPeriod_When_EndDateUpdated_Then_ResultIsSuccess() { // Arrange. var period = new Period( new DateTime(2020, 1, 1), new DateTime(2020, 1, 31)); AccountingManager testObject = AccountingManagerFactory.Create(out IAccountingDataAccess accountingDataAccess); accountingDataAccess .GetLastPeriod() .Returns(GetResult <Period> .CreateSuccess(period)); var updateEndDate = new UpdateLastPeriodEndDateDto(period.End.AddDays(1)); accountingDataAccess .UpdateLastPeriodEndDate(updateEndDate.NewEnd) .Returns(ActionResult.CreateSuccess()); // Act. ActionResult result = await testObject.UpdateLastPeriodEndDate(updateEndDate); // Assert. Assert.IsTrue(result.IsSuccess); }
public Task <ActionResult <string> > UpdatePassword(string name, string newPassword) { lock (_lock) { if (!_accounts.TryGetValue(name, out var account)) { return(Task.FromResult(ActionResult <string> .CreateError("Unknown account"))); } account.Password = newPassword; return(Task.FromResult(ActionResult <string> .CreateSuccess())); } }
public async Task Given_PeriodWithEndBeforeStart_When_Adding_Then_ResultIsFailure() { // Arrange. AccountingManager testObject = AccountingManagerFactory.Create(out IAccountingDataAccess accountingDataAccess); accountingDataAccess .GetLastPeriod() .Returns(GetResult <Period> .CreateSuccess(null)); accountingDataAccess.AddPeriod(Arg.Any <Period>()).Returns(ActionResult.CreateSuccess()); var period = new PeriodDto(DateTime.Now, DateTime.Now.AddDays(-1)); // Act. ActionResult result = await testObject.AddPeriod(period); // Assert. Assert.IsFalse(result.IsSuccess); }
public async Task Given_NoExistingPeriods_When_Added_Then_ResultIsSuccess() { // Arrange. AccountingManager testObject = AccountingManagerFactory.Create(out IAccountingDataAccess accountingDataAccess); accountingDataAccess .GetLastPeriod() .Returns(GetResult <Period> .CreateFailure("No periods found")); accountingDataAccess .AddPeriod(Arg.Any <Period>()) .Returns(ActionResult.CreateSuccess()); var period = new PeriodDto(DateTime.Now, DateTime.Now); // Act. ActionResult result = await testObject.AddPeriod(period); // Assert. Assert.IsTrue(result.IsSuccess); }
public async Task <ActionResult <string[]> > CreateAccount(NewAccount newAccount) { _logger.LogInformation("Creating account {AccountName}", newAccount.AccountName); var verifyResult = await VerifyNewAccount(newAccount); if (!verifyResult.Success) { return(verifyResult); } var salt = Guid.NewGuid().ToString(); await _accountStore.CreateAccount(new UserAccount { Name = newAccount.AccountName, Password = PasswordHasher.Hash(newAccount.Password, salt), PasswordSalt = salt }); _logger.LogInformation("Account created: {AccountName}", newAccount.AccountName); return(ActionResult <string[]> .CreateSuccess()); }