public async Task ApiV1AccountsGetByIdReturnsAccountDetailsById() { const int accountId = 1; const string bankId = "TestBank"; const string accountNumber = "00112233"; var remoteAccountDetails = new AccountDetails { AccountNumber = accountNumber, AccountName = "Current Account", SortCode = "079046", CurrentBalance = 350, OverdraftLimit = 50 }; var accountRepository = Substitute.For <IBankAccountRepository>(); accountRepository.GetAccountById(accountId) .Returns(new BankAccount { BankId = bankId, AccountNumber = accountNumber }); var accountDataProvider = Substitute.For <IAccountDataProvider>(); accountDataProvider.GetAccountDetails(bankId, accountNumber).Returns(x => Task.FromResult( new OperationResult <AccountDetails>(remoteAccountDetails) ) ); var controller = new AccountsApiController(accountRepository, accountDataProvider); var actionResult = await controller.ApiV1AccountsGetById(accountId); var contentResult = actionResult as ObjectResult; Assert.That(contentResult, Is.Not.Null); Assert.That(contentResult.StatusCode, Is.EqualTo((int)HttpStatusCode.OK)); var accountResult = contentResult.Value as AccountDetailsViewModel; Assert.That(accountResult, Is.Not.Null); Assert.That(accountResult.BankId, Is.EqualTo(bankId)); Assert.That(accountResult.AccountNumber, Is.EqualTo(accountNumber)); Assert.That(accountResult.AccountName, Is.EqualTo(remoteAccountDetails.AccountName)); Assert.That(accountResult.SortCode, Is.EqualTo(remoteAccountDetails.SortCode)); Assert.That(accountResult.CurrentBalance, Is.EqualTo(remoteAccountDetails.CurrentBalance)); Assert.That(accountResult.OverdraftLimit, Is.EqualTo(remoteAccountDetails.OverdraftLimit)); }
public async Task ApiV1AccountsGetByIdHandlesAccountIdNotSupplied() { var controller = new AccountsApiController(null, null); var actionResult = await controller.ApiV1AccountsGetById(null); var contentResult = actionResult as ObjectResult; Assert.That(contentResult, Is.Not.Null); Assert.That(contentResult.StatusCode, Is.EqualTo((int)HttpStatusCode.BadRequest)); var accountResult = contentResult.Value as ErrorViewModel; Assert.That(accountResult, Is.Not.Null); Assert.That(accountResult.Status, Is.EqualTo((int)HttpStatusCode.BadRequest)); }
public async Task ApiV1AccountsGetByIdForwardsErrorFromAccountProvider() { const int accountId = 1; const string bankId = "TestBank"; const string accountNumber = "00112233"; const long errorCode = 1001123; var accountRepository = Substitute.For <IBankAccountRepository>(); accountRepository.GetAccountById(accountId) .Returns(new BankAccount { BankId = bankId, AccountNumber = accountNumber }); // GetAccountDetails should return a not found result even though we know this account should exist var accountDataProvider = Substitute.For <IAccountDataProvider>(); accountDataProvider.GetAccountDetails(bankId, accountNumber).Returns(x => Task.FromResult( new OperationResult <AccountDetails>( (int)HttpStatusCode.NotFound, new OperationError( errorCode, $"Account number '{accountNumber}' does not exist" ) ) ) ); var controller = new AccountsApiController(accountRepository, accountDataProvider); var actionResult = await controller.ApiV1AccountsGetById(accountId); var contentResult = actionResult as ObjectResult; Assert.That(contentResult, Is.Not.Null); Assert.That(contentResult.StatusCode, Is.EqualTo((int)HttpStatusCode.NotFound)); var errorViewModel = contentResult.Value as ErrorViewModel; Assert.That(errorViewModel, Is.Not.Null); Assert.That(errorViewModel.ErrorCode, Is.EqualTo(errorCode)); }
public async Task ApiV1AccountsGetByIdHandlesAccountNotFound() { const int requestedAccountId = 1; var accountRepository = Substitute.For <IBankAccountRepository>(); accountRepository.GetAccountById(0).ReturnsForAnyArgs((BankAccount)null); var controller = new AccountsApiController(accountRepository, null); var actionResult = await controller.ApiV1AccountsGetById(requestedAccountId); var contentResult = actionResult as ObjectResult; Assert.That(contentResult, Is.Not.Null); Assert.That(contentResult.StatusCode, Is.EqualTo((int)HttpStatusCode.NotFound)); var accountResult = contentResult.Value as ErrorViewModel; Assert.That(accountResult, Is.Not.Null); Assert.That(accountResult.Status, Is.EqualTo((int)HttpStatusCode.NotFound)); }