public async Task <ActionResult <BankAccountDto> > CreateBankAccountWithCustomerByCustomer( [FromBody] BankAccountWithCustomerCreationByCustomerDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var generatedAccountNumber = _bankAccountNumberBuilder.GenerateBankAccountNumber(); var user = _mapper.Map <ApplicationUser>(model.Register); user.Customer = new Customer { Id = user.Id }; 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 = user.Id }; user.Customer.BankAccounts = new List <BankAccount> { bankAccount }; user.CreatedBankAccounts = new List <BankAccount> { bankAccount }; var result = await _userManager.CreateAsync(user, model.Register.User.Password); 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 CreateBankAccountWithCustomerByCustomer_Should_ReturnBadRequest_When_ModelStateIsInvalid() { // Arrange var bankAccountCreation = new BankAccountWithCustomerCreationByCustomerDto(); _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.CreateBankAccountWithCustomerByCustomer(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 CreateBankAccountWithCustomerByCustomer_Should_CreateBankAccountWithCustomer_And_ReturnBankAccountDto_When_ModelStateIsValid() { // Arrange var bankAccountCreation = new BankAccountWithCustomerCreationByCustomerDto { Register = new RegisterDto { User = new ApplicationUserCreationBySameUserDto { Name = "John", Surname = "Smith", Email = "*****@*****.**", PhoneNumber = "123456789", Password = "******" }, 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 } } }; _userManagerMock.Setup(m => m.CreateAsync(It.IsAny <ApplicationUser>(), It.IsAny <string>())) .ReturnsAsync(IdentityResult.Success).Callback <ApplicationUser, string>((user, password) => { _context.Users.Add(user); _context.SaveChanges(); }); _bankAccountNumberBuilderMock.Setup(anf => anf.GenerateBankAccountNumber(null)).Returns(bankAccountNumber); // Act var result = await _sut.CreateBankAccountWithCustomerByCustomer(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(bankAccountDto.CreatedById, bankAccountDto.Customer.Id); 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(bankAccountFromDb.CreatedById, bankAccountFromDb.Customer.Id); Assert.AreEqual(bankAccountDto.Customer.Id, bankAccountDto.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); }