public async Task CreateBankAccountWithCustomerByWorker_Should_ReturnBadRequest_When_ModelStateIsInvalid() { // Arrange var bankAccountCreation = new BankAccountWithCustomerCreationByWorkerDto(); _sut.ModelState.AddModelError(nameof(bankAccountCreation.Register), $"The {nameof(bankAccountCreation.Register)} field is required."); _sut.ModelState.AddModelError(nameof(bankAccountCreation.BankAccount), $"The {nameof(bankAccountCreation.BankAccount)} field is required."); // Act var result = await _sut.CreateBankAccountWithCustomerByWorker(bankAccountCreation); // Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result.Result, typeof(BadRequestObjectResult)); var badRequestResult = result.Result as BadRequestObjectResult; Assert.IsNotNull(badRequestResult); Assert.IsInstanceOfType(badRequestResult.Value, typeof(SerializableError)); var error = badRequestResult.Value as SerializableError; Assert.IsNotNull(error); Assert.IsTrue(error.ContainsKey(nameof(bankAccountCreation.Register))); Assert.IsTrue(error.ContainsKey(nameof(bankAccountCreation.BankAccount))); var registerErrorValues = error[nameof(bankAccountCreation.Register)] as string[]; Assert.IsNotNull(registerErrorValues); Assert.IsTrue(registerErrorValues.Single() == $"The {nameof(bankAccountCreation.Register)} field is required."); var bankAccountErrorValues = error[nameof(bankAccountCreation.BankAccount)] as string[]; Assert.IsNotNull(bankAccountErrorValues); Assert.IsTrue(bankAccountErrorValues.Single() == $"The {nameof(bankAccountCreation.BankAccount)} field is required."); }
public async Task <ActionResult <BankAccountDto> > CreateBankAccountWithCustomerByWorker( [FromBody] BankAccountWithCustomerCreationByWorkerDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var currentUserId = int.Parse(User.Claims.Single(c => c.Type == CustomClaimTypes.UserId).Value); var currentUser = _context.Users.SingleOrDefault(u => u.Id == currentUserId); if (currentUser == null) { ModelState.AddModelError(nameof(currentUser), $"User with id {currentUserId} found in claims doesn't exist in database."); return(BadRequest(ModelState)); } int?workerBranchId = null; if (await _userManager.IsInRoleAsync(currentUser, UserRole.Teller.ToString())) { await _context.Tellers.Where(t => t.Id == currentUser.Id).LoadAsync(); if (currentUser.Teller.WorkAtId == null) { ModelState.AddModelError(nameof(currentUser.Teller), $"Worker with id {currentUserId} is currently not assigned to any branch."); return(BadRequest(ModelState)); } workerBranchId = currentUser.Teller.WorkAtId; } else if (await _userManager.IsInRoleAsync(currentUser, UserRole.Manager.ToString())) { await _context.Managers.Where(m => m.Id == currentUser.Id).LoadAsync(); if (currentUser.Manager.WorkAtId == null) { ModelState.AddModelError(nameof(currentUser.Manager), $"Worker with id {currentUserId} is currently not assigned to any branch."); return(BadRequest(ModelState)); } workerBranchId = currentUser.Manager.WorkAtId; } var generatedAccountNumber = _bankAccountNumberBuilder.GenerateBankAccountNumber(workerBranchId); var user = _mapper.Map <ApplicationUser>(model.Register); user.Customer = new Customer { Id = user.Id }; user.CreatedById = currentUserId; var bankAccount = new BankAccount { AccountType = (AccountType)model.BankAccount.AccountType, Currency = (Currency)model.BankAccount.Currency, CountryCode = generatedAccountNumber.CountryCode, CheckDigits = generatedAccountNumber.CheckDigits, NationalBankCode = generatedAccountNumber.NationalBankCode, BranchCode = generatedAccountNumber.BranchCode, NationalCheckDigit = generatedAccountNumber.NationalCheckDigit, AccountNumber = generatedAccountNumber.AccountNumber, AccountNumberText = generatedAccountNumber.AccountNumberText, Iban = generatedAccountNumber.Iban, IbanSeparated = generatedAccountNumber.IbanSeparated, OpenedDate = DateTime.UtcNow, CustomerId = user.Id, CreatedById = currentUserId }; user.Customer.BankAccounts = new List <BankAccount> { bankAccount }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, UserRole.Customer.ToString()); } else { return(BadRequest(result.Errors)); } var bankAccountDto = _mapper.Map <BankAccount, BankAccountDto>(bankAccount); return(CreatedAtRoute("GetBankAccount", new { bankAccountId = bankAccountDto.Id }, bankAccountDto)); }
public async Task CreateBankAccountWithCustomerByWorker_Should_ReturnBadRequest_When_ManagerIsNotAssignedToBranch() { // Arrange var bankAccountCreation = new BankAccountWithCustomerCreationByWorkerDto { Register = new RegisterByAnotherUserDto { User = new ApplicationUserCreationByAnotherUserDto { Name = "John", Surname = "Smith", Email = "*****@*****.**", PhoneNumber = "123456789" }, Address = new AddressCreationDto { Country = "United States", City = "New York", Street = "Glenwood Ave", HouseNumber = "10", ApartmentNumber = "11", PostalCode = "10028" } }, BankAccount = new Dtos.BankAccount.WithCustomerCreation.BankAccountCreationDto { AccountType = AccountType.Checking, Currency = Currency.Eur } }; var currentUser = new ApplicationUser { Id = 4 }; var claims = new List <Claim> { new Claim(CustomClaimTypes.UserId, currentUser.Id.ToString()) }; var identity = new ClaimsIdentity(claims); var claimsPrincipal = new ClaimsPrincipal(identity); var context = new ControllerContext { HttpContext = new DefaultHttpContext { User = claimsPrincipal } }; _sut.ControllerContext = context; _userManagerMock.Setup(m => m.IsInRoleAsync(It.IsAny <ApplicationUser>(), UserRole.Manager.ToString())) .ReturnsAsync(() => true); // Act var result = await _sut.CreateBankAccountWithCustomerByWorker(bankAccountCreation); // Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result.Result, typeof(BadRequestObjectResult)); var badRequestResult = result.Result as BadRequestObjectResult; Assert.IsNotNull(badRequestResult); Assert.IsInstanceOfType(badRequestResult.Value, typeof(SerializableError)); var error = badRequestResult.Value as SerializableError; Assert.IsNotNull(error); Assert.IsTrue(error.ContainsKey(nameof(currentUser.Manager))); var currentUserErrorValues = error[nameof(currentUser.Manager)] as string[]; Assert.IsNotNull(currentUserErrorValues); Assert.IsTrue(currentUserErrorValues.Single() == $"Worker with id {currentUser.Id} is currently not assigned to any branch."); }
public async Task CreateBankAccountWithCustomerByWorker_Should_CreateBankAccountWithCustomer_And_ReturnBankAccountDto_When_ModelStateIsValid() { // Arrange var bankAccountCreation = new BankAccountWithCustomerCreationByWorkerDto { Register = new RegisterByAnotherUserDto { User = new ApplicationUserCreationByAnotherUserDto { Name = "John", Surname = "Smith", Email = "*****@*****.**", PhoneNumber = "123456789" }, Address = new AddressCreationDto { Country = "United States", City = "New York", Street = "Glenwood Ave", HouseNumber = "10", ApartmentNumber = "11", PostalCode = "10028" } }, BankAccount = new Dtos.BankAccount.WithCustomerCreation.BankAccountCreationDto { AccountType = AccountType.Checking, Currency = Currency.Eur } }; var bankAccountNumber = new BankAccountNumber { CountryCode = "PL", CheckDigits = "61", NationalBankCode = "1080", BranchCode = "000", NationalCheckDigit = 1, AccountNumber = 0, AccountNumberText = "0000000000000000", Iban = "PL61108000010000000000000000", IbanSeparated = "PL 61 1080 0001 0000 0000 0000 0000" }; var expectedBankAccount = new BankAccountDto { AccountType = (AccountType)bankAccountCreation.BankAccount.AccountType, Currency = (Currency)bankAccountCreation.BankAccount.Currency, CountryCode = bankAccountNumber.CountryCode, CheckDigits = bankAccountNumber.CheckDigits, NationalBankCode = bankAccountNumber.NationalBankCode, BranchCode = bankAccountNumber.BranchCode, NationalCheckDigit = bankAccountNumber.NationalCheckDigit, AccountNumber = bankAccountNumber.AccountNumber, AccountNumberText = bankAccountNumber.AccountNumberText, Iban = bankAccountNumber.Iban, IbanSeparated = bankAccountNumber.IbanSeparated, Balance = 0, DebitLimit = 0, Customer = new CustomerDto { ApplicationUser = new ApplicationUserDto { Name = bankAccountCreation.Register.User.Name, Surname = bankAccountCreation.Register.User.Surname, Email = bankAccountCreation.Register.User.Email, PhoneNumber = bankAccountCreation.Register.User.PhoneNumber } } }; var currentUser = new ApplicationUser { Id = 2 }; var claims = new List <Claim> { new Claim(CustomClaimTypes.UserId, currentUser.Id.ToString()) }; var identity = new ClaimsIdentity(claims); var claimsPrincipal = new ClaimsPrincipal(identity); var context = new ControllerContext { HttpContext = new DefaultHttpContext { User = claimsPrincipal } }; _sut.ControllerContext = context; _userManagerMock.Setup(m => m.IsInRoleAsync(It.IsAny <ApplicationUser>(), It.IsAny <string>())) .ReturnsAsync(() => true); _userManagerMock.Setup(m => m.CreateAsync(It.IsAny <ApplicationUser>())) .ReturnsAsync(IdentityResult.Success).Callback <ApplicationUser>(user => { _context.Users.Add(user); _context.SaveChanges(); }); _bankAccountNumberBuilderMock.Setup(anf => anf.GenerateBankAccountNumber(It.IsAny <int>())).Returns(bankAccountNumber); // Act var result = await _sut.CreateBankAccountWithCustomerByWorker(bankAccountCreation); // Assert _userManagerMock.Verify(m => m.AddToRoleAsync(It.IsAny <ApplicationUser>(), UserRole.Customer.ToString()), Times.Once); Assert.IsNotNull(result); Assert.IsInstanceOfType(result.Result, typeof(CreatedAtRouteResult)); var createdAtRouteResult = result.Result as CreatedAtRouteResult; Assert.IsNotNull(createdAtRouteResult); Assert.IsInstanceOfType(createdAtRouteResult.Value, typeof(BankAccountDto)); var bankAccountDto = createdAtRouteResult.Value as BankAccountDto; Assert.IsNotNull(bankAccountDto); Assert.AreEqual(expectedBankAccount.AccountType, bankAccountDto.AccountType); Assert.AreEqual(expectedBankAccount.Currency, bankAccountDto.Currency); Assert.AreEqual(expectedBankAccount.CountryCode, bankAccountDto.CountryCode); Assert.AreEqual(expectedBankAccount.CheckDigits, bankAccountDto.CheckDigits); Assert.AreEqual(expectedBankAccount.NationalBankCode, bankAccountDto.NationalBankCode); Assert.AreEqual(expectedBankAccount.BranchCode, bankAccountDto.BranchCode); Assert.AreEqual(expectedBankAccount.NationalCheckDigit, bankAccountDto.NationalCheckDigit); Assert.AreEqual(expectedBankAccount.AccountNumber, bankAccountDto.AccountNumber); Assert.AreEqual(expectedBankAccount.AccountNumberText, bankAccountDto.AccountNumberText); Assert.AreEqual(expectedBankAccount.Iban, bankAccountDto.Iban); Assert.AreEqual(expectedBankAccount.IbanSeparated, bankAccountDto.IbanSeparated); Assert.AreEqual(expectedBankAccount.Balance, bankAccountDto.Balance); Assert.AreEqual(expectedBankAccount.DebitLimit, bankAccountDto.DebitLimit); Assert.AreNotEqual(DateTime.MinValue, bankAccountDto.OpenedDate); Assert.AreEqual(currentUser.Id, bankAccountDto.CreatedById); Assert.AreEqual(bankAccountDto.Customer.Id, bankAccountDto.Customer.ApplicationUser.Id); Assert.AreEqual(expectedBankAccount.Customer.ApplicationUser.Name, bankAccountDto.Customer.ApplicationUser.Name); Assert.AreEqual(expectedBankAccount.Customer.ApplicationUser.Surname, bankAccountDto.Customer.ApplicationUser.Surname); Assert.AreEqual(expectedBankAccount.Customer.ApplicationUser.Email, bankAccountDto.Customer.ApplicationUser.Email); Assert.AreEqual(expectedBankAccount.Customer.ApplicationUser.PhoneNumber, bankAccountDto.Customer.ApplicationUser.PhoneNumber); var bankAccountFromDb = _context.BankAccounts.SingleOrDefault(ba => ba.Id == bankAccountDto.Id); Assert.IsNotNull(bankAccountFromDb); Assert.AreEqual(expectedBankAccount.AccountType, bankAccountFromDb.AccountType); Assert.AreEqual(expectedBankAccount.Currency, bankAccountFromDb.Currency); Assert.AreEqual(expectedBankAccount.CountryCode, bankAccountFromDb.CountryCode); Assert.AreEqual(expectedBankAccount.CheckDigits, bankAccountFromDb.CheckDigits); Assert.AreEqual(expectedBankAccount.NationalBankCode, bankAccountFromDb.NationalBankCode); Assert.AreEqual(expectedBankAccount.BranchCode, bankAccountFromDb.BranchCode); Assert.AreEqual(expectedBankAccount.NationalCheckDigit, bankAccountFromDb.NationalCheckDigit); Assert.AreEqual(expectedBankAccount.AccountNumber, bankAccountFromDb.AccountNumber); Assert.AreEqual(expectedBankAccount.AccountNumberText, bankAccountFromDb.AccountNumberText); Assert.AreEqual(expectedBankAccount.Iban, bankAccountFromDb.Iban); Assert.AreEqual(expectedBankAccount.IbanSeparated, bankAccountFromDb.IbanSeparated); Assert.AreEqual(expectedBankAccount.Balance, bankAccountFromDb.Balance); Assert.AreEqual(expectedBankAccount.DebitLimit, bankAccountFromDb.DebitLimit); Assert.AreNotEqual(DateTime.MinValue, bankAccountFromDb.OpenedDate); Assert.AreEqual(currentUser.Id, bankAccountFromDb.CreatedById); Assert.AreEqual(bankAccountFromDb.Customer.Id, bankAccountFromDb.Customer.ApplicationUser.Id); Assert.AreEqual(expectedBankAccount.Customer.ApplicationUser.Name, bankAccountFromDb.Customer.ApplicationUser.Name); Assert.AreEqual(expectedBankAccount.Customer.ApplicationUser.Surname, bankAccountFromDb.Customer.ApplicationUser.Surname); Assert.AreEqual(expectedBankAccount.Customer.ApplicationUser.Email, bankAccountFromDb.Customer.ApplicationUser.Email); Assert.AreEqual(expectedBankAccount.Customer.ApplicationUser.PhoneNumber, bankAccountFromDb.Customer.ApplicationUser.PhoneNumber); }