public void TestAddAccountFromFi() { // Mock setup for DataService var mockAccountSet = new Mock<DbSet<Account>>(); var mockFiSet = new Mock<DbSet<FinancialInstitution>>(); var mockFiUserSet = new Mock<DbSet<FinancialInstitutionUser>>(); var mockContext = new Mock<SoCashDbContext>(); mockContext.Setup(m => m.Accounts).Returns(mockAccountSet.Object); mockContext.Setup(m => m.FinancialInstitutions).Returns(mockFiSet.Object); mockContext.Setup(m => m.FinancialInstitutionUsers).Returns(mockFiUserSet.Object); // Account to add var newAccount = new Account { AccountName = "Test Account", AccountType = AccountType.Checking.ToString(), Currency = "USD" }; // Dummy FI var financialInstitution = new OFX.Types.FinancialInstitution("Test FI", new Uri("http://test.com/"), "Test Org ID", "Test Unit ID"); // Dummy FI User var financialInstitutionUser = new FinancialInstitutionUser { UserId = "Test User", Password = "******" }; // Add the account in a transaction using (var service = new DataService(mockContext.Object)) { service.AddAccount(newAccount, financialInstitution, financialInstitutionUser); } // Verify that the service added 1 account, 1 fi and 1 fi user mockAccountSet.Verify(m => m.Add(newAccount), Times.Once()); mockFiSet.Verify(m => m.Add(It.IsAny<FinancialInstitution>()), Times.Once()); mockFiUserSet.Verify(m => m.Add(financialInstitutionUser), Times.Once()); // Verify that the transaction ended properly mockContext.Verify(m => m.SaveChanges(), Times.Once()); }
public void TestUpdateFiUser() { // FiUser for test var fiUser = new FinancialInstitutionUser {UserId = "OriginalId", Password = "******"}; // Mock setup for DataService var mockFiUserSet = new Mock<DbSet<FinancialInstitutionUser>>(); var mockContext = new Mock<SoCashDbContext>(); mockContext.Setup(m => m.Set<FinancialInstitutionUser>()).Returns(mockFiUserSet.Object); // Update the user using (var service = new DataService(mockContext.Object)) service.UpdateFiUser(fiUser); // Verify that the service attached the user on the mock db exactly once mockContext.Verify(m => m.SetModified(fiUser), Times.Once()); // Verify that the transaction ended properly mockContext.Verify(m => m.SaveChanges(), Times.Once()); }
/// <summary> /// Add an account to the database - Using OFX data specification /// </summary> /// <param name="account">Populated account object to add to database</param> /// <param name="financialInstitution">OFX financial institution to link in database. Will be created if necessary.</param> /// <param name="fiUser">User credentials for the financial institution</param> /// <returns>Created account</returns> public Account AddAccount(Account account, OFX.Types.FinancialInstitution financialInstitution, FinancialInstitutionUser fiUser) { // TODO: See if there's an existing FI or user with this info already // Look for existing FI entry with the same name FinancialInstitution fi; try { fi = DbContext.FinancialInstitutions.First(i => i.Name == financialInstitution.Name); } catch (Exception ex) { // Can result in InvalidOperationException or NullReferenceException depending on provider if (ex is InvalidOperationException || ex is NullReferenceException) { // FI Doesn't exist, add a new one fi = new FinancialInstitution { Name = financialInstitution.Name, OfxFinancialUnitId = financialInstitution.FinancialId, OfxOrganizationId = financialInstitution.OrganizationId, OfxUpdateUrl = financialInstitution.ServiceEndpoint.ToString() }; DbContext.FinancialInstitutions.Add(fi); } else throw; // Unhandled } // Look for existing user under this FI with same userId try { fiUser = fi.Users.First(u => u.UserId == fiUser.UserId && u.Password == fiUser.Password); } catch (Exception ex) { // Can result in InvalidOperationException or NullReferenceException depending on provider if (ex is InvalidOperationException || ex is NullReferenceException) { // User doesn't exist, add as new fi.Users.Add(fiUser); DbContext.FinancialInstitutionUsers.Add(fiUser); } else throw; // Unhandled } fiUser.Accounts.Add(account); DbContext.Accounts.Add(account); return account; }
/// <summary> /// Update a FI user in the database with the provided data /// </summary> /// <param name="user">FI User data to update</param> public void UpdateFiUser(FinancialInstitutionUser user) { // Attach to context and mark as modified DbContext.SetModified(user); }