public void TestTransferPost_UnauthorizedSourceAccountOwner() { #region ASSIGN TestRepository tRepo = new TestRepository(); AccountsController tController = null; AccountTransferVM tVM = new AccountTransferVM() { DestinationID = 1, SourceID = 3, Amount = 1.0, }; tController = new AccountsController(tRepo) { ControllerContext = UtilityFunctions.GenerateMockControllerContext("UserA"), }; #endregion #region ACT var tResult = tController.Transfer(tVM); #endregion #region ASSERT Assert.IsTrue(tResult is RedirectToActionResult); Assert.AreEqual((tResult as RedirectToActionResult).ActionName, "Index"); #endregion }
public void TestTransferPost_SameAccountSelection() { #region ASSIGN TestRepository tRepo = new TestRepository(); AccountsController tController = null; AccountTransferVM tVM = new AccountTransferVM() { DestinationID = 0, SourceID = 0, Amount = 1.0, }; tController = new AccountsController(tRepo) { ControllerContext = UtilityFunctions.GenerateMockControllerContext("UserA"), }; #endregion #region ACT var tResult = tController.Transfer(tVM); #endregion #region ASSERT Assert.IsTrue(tResult is ViewResult); Assert.IsNotNull((tResult as ViewResult).ViewData["ErrorMessage"]); #endregion }
// GET: Accounts/Delete/5 //public async Task<IActionResult> Delete(int? id) //{ // if (id == null) // { // return NotFound(); // } // var account = await _repo.Accounts // .Include(a => a.AccountType) // .Include(a => a.Customer) // .FirstOrDefaultAsync(m => m.ID == id); // if (account == null) // { // return NotFound(); // } // return View(account); //} // POST: Accounts/Delete/5 //[HttpPost, ActionName("Delete")] //[ValidateAntiForgeryToken] //public async Task<IActionResult> DeleteConfirmed(int id) //{ // var account = await _repo.Accounts.FindAsync(id); // _repo.Accounts.Remove(account); // await _repo.SaveChangesAsync(); // return RedirectToAction(nameof(Index)); //} //private bool AccountExists(int id) //{ // return _repo.Accounts.Any(e => e.ID == id); //} public IActionResult Transfer() { AccountTransferVM transfer = null; try { string guid = GetUserGuID(); Customer currentCustomer = _repo.GetCustomer(guid); if (_repo.CanTransferBalance(currentCustomer.ID)) { transfer = new AccountTransferVM { Amount = 0, }; // Get all withdrawable accounts. transfer.SourceAccounts = _repo.GetWithdrawAccounts(currentCustomer.ID); transfer.SourceID = transfer.SourceAccounts[0].ID; // Get all depositable accounts. transfer.DestinationAccounts = _repo.GetDepositAccounts(currentCustomer.ID); transfer.DestinationID = transfer.DestinationAccounts[0].ID; } else { return(RedirectToAction(nameof(Index))); } } catch (Exception WTF) { Console.WriteLine(WTF); return(RedirectToAction(nameof(Index))); } if (transfer == null) { return(RedirectToAction(nameof(Index))); } return(View(transfer)); }
public void TestTransferPost_Valid() { #region ASSIGN TestRepository tRepo = new TestRepository(); AccountsController tController = null; AccountTransferVM tVM = new AccountTransferVM() { DestinationID = 1, SourceID = 0, Amount = 500.0, }; Account tData1 = tRepo.GetAccountInformation(0, 0); Account tData2 = tRepo.GetAccountInformation(0, 1); tController = new AccountsController(tRepo) { ControllerContext = UtilityFunctions.GenerateMockControllerContext("UserA"), }; #endregion #region ACT var tResult = tController.Transfer(tVM); #endregion #region ASSERT Assert.IsTrue(tResult is RedirectToActionResult); Assert.AreEqual((tResult as RedirectToActionResult).ActionName, "Index"); Assert.AreEqual((tResult as RedirectToActionResult).ControllerName, "Customers"); Assert.AreEqual(tData1.AccountBalance, 500.0); Assert.AreEqual(tData2.AccountBalance, 1000.0); #endregion }
public void TestTransferPost_OverdraftError() { #region ASSIGN TestRepository tRepo = new TestRepository(); AccountsController tController = null; AccountTransferVM tVM = new AccountTransferVM() { DestinationID = 1, SourceID = 0, Amount = 5000.0, }; Account tData1 = tRepo.GetAccountInformation(0, 0); Account tData2 = tRepo.GetAccountInformation(0, 1); tController = new AccountsController(tRepo) { ControllerContext = UtilityFunctions.GenerateMockControllerContext("UserA"), }; #endregion #region ACT var tResult = tController.Transfer(tVM); #endregion #region ASSERT Assert.IsTrue(tResult is ViewResult); Assert.IsNotNull((tResult as ViewResult).ViewData["ErrorMessage"]); Assert.IsTrue((tResult as ViewResult).ViewData["ErrorMessage"].ToString().Contains("Overdraft")); Assert.AreEqual(tData1.AccountBalance, 1000.0); Assert.AreEqual(tData2.AccountBalance, 500.0); #endregion }
public IActionResult Transfer([Bind("SourceID,DestinationID,Amount,SourceAccounts,DestinationAccounts")] AccountTransferVM transferPost) { AccountTransferVM transfer = null; try { // Check if user is valid for each account. string guid = GetUserGuID(); Customer currentCustomer = _repo.GetCustomer(guid); transfer = new AccountTransferVM { Amount = 0, }; // Get all withdrawable accounts. transfer.SourceAccounts = _repo.GetWithdrawAccounts(currentCustomer.ID); transfer.SourceID = transfer.SourceAccounts[0].ID; // Get all depositable accounts. transfer.DestinationAccounts = _repo.GetDepositAccounts(currentCustomer.ID); transfer.DestinationID = transfer.DestinationAccounts[0].ID; Account sourceAccount = _repo.GetAccountInformation(currentCustomer.ID, transferPost.SourceID); Account destinationAccount = _repo.GetAccountInformation(currentCustomer.ID, transferPost.DestinationID); // Check if source account and destination account are not the same. if (sourceAccount.ID != destinationAccount.ID) { // Check if withdraw amount is valid. sourceAccount = _repo.Withdraw(currentCustomer.ID, sourceAccount.ID, transferPost.Amount); // Deposit amount to destination account. destinationAccount = _repo.Deposit(currentCustomer.ID, destinationAccount.ID, transferPost.Amount); return(RedirectToAction(nameof(Index), "Customers")); } } catch (OverdraftProtectionException WTF) { Console.WriteLine(WTF); ViewData["ErrorMessage"] = "Overdraft Protection! Withdrawal amount exceeded current balance of source account!"; if (transfer == null) { return(RedirectToAction(nameof(Index))); } return(View(transfer)); } catch (UnauthorizedAccessException WTF) { Console.WriteLine(WTF); return(RedirectToAction(nameof(Index))); } catch (Exception WTF) { Console.WriteLine(WTF); return(RedirectToAction(nameof(Index))); } ViewData["ErrorMessage"] = "Same account can NOT be selected for both source and destination of a transfer!"; return(View(transfer)); }