diff --git a/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala b/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala index b9402cf16..e21f3edb5 100644 --- a/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala +++ b/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala @@ -1303,11 +1303,12 @@ def restoreSomeSessions(): Unit = { cbsRemovedBankAccountId <- cbsRemovedBankAccountIds bankId = cbsRemovedBankAccountId.bankId accountId = cbsRemovedBankAccountId.accountId - _ = Views.views.vend.revokeAllAccountAccesses(bankId, accountId, user) + _ = Views.views.vend.revokeAccountAccessesByUser(bankId, accountId, user) _ = AccountHolders.accountHolders.vend.deleteAccountHolder(user,cbsRemovedBankAccountId) cbsAccount = accountsHeld.find(cbsAccount =>cbsAccount.bankId == bankId.value && cbsAccount.accountId == accountId.value) viewId <- cbsAccount.map(_.viewsToGenerate).getOrElse(List.empty[String]) } yield { + UserRefreshes.UserRefreshes.vend.createOrUpdateRefreshUser(user.userId) Views.views.vend.removeCustomView(ViewId(viewId), cbsRemovedBankAccountId) } @@ -1321,13 +1322,14 @@ def restoreSomeSessions(): Unit = { viewId <- newBankAccount.map(_.viewsToGenerate).getOrElse(List.empty[String]) view <- Views.views.vend.getOrCreateAccountView(newBankAccountId, viewId)//this method will return both system views and custom views back. } yield { + UserRefreshes.UserRefreshes.vend.createOrUpdateRefreshUser(user.userId) if (view.isSystem)//if the view is a system view, we will call `grantAccessToSystemView` Views.views.vend.grantAccessToSystemView(bankId, accountId, view, user) else //otherwise, we will call `grantAccessToCustomView` Views.views.vend.grantAccessToCustomView(view.uid, user) } - UserRefreshes.UserRefreshes.vend.createOrUpdateRefreshUser(user.userId) + } else { } } diff --git a/obp-api/src/main/scala/code/views/MapperViews.scala b/obp-api/src/main/scala/code/views/MapperViews.scala index cfa6f40f7..53688a61b 100644 --- a/obp-api/src/main/scala/code/views/MapperViews.scala +++ b/obp-api/src/main/scala/code/views/MapperViews.scala @@ -545,8 +545,10 @@ object MapperViews extends Views with MdcLoggable { getOrCreateSystemView(SYSTEM_ACCOUNTANT_VIEW_ID) else if (auditorsView) getOrCreateSystemView(SYSTEM_AUDITOR_VIEW_ID) - else + else { + logger.error(ViewIdNotSupported+ s"Your input viewId is :$viewId") Failure(ViewIdNotSupported+ s"Your input viewId is :$viewId") + } logger.debug(s"-->getOrCreateAccountView.${viewId } : ${theView} ") diff --git a/obp-api/src/test/scala/code/connector/MockedJune2017Connector.scala b/obp-api/src/test/scala/code/connector/MockedJune2017Connector.scala index 8fe721be0..f6fffd102 100644 --- a/obp-api/src/test/scala/code/connector/MockedJune2017Connector.scala +++ b/obp-api/src/test/scala/code/connector/MockedJune2017Connector.scala @@ -24,15 +24,15 @@ object MockedJune2017Connector extends ServerSetup implicit override val nameOfConnector = "MockedCardConnector" //These bank id and account ids are real data over adapter - val bankIdAccountId = BankIdAccountId(BankId("obp-bank-x-gh"),AccountId("KOa4M8UfjUuWPIXwPXYPpy5FoFcTUwpfHgXC1qpSluc")) - val bankIdAccountId2 = BankIdAccountId(BankId("obp-bank-x-gh"),AccountId("tKWSUBy6sha3Vhxc/vw9OK96a0RprtoxUuObMYR29TI")) + val bankIdAccountId1 = BankIdAccountId(BankId("obp-bank-id1"),AccountId("KOa4M8UfjUuWPIXwPXYPpy5FoFcTUwpfH-accountId1")) + val bankIdAccountId2 = BankIdAccountId(BankId("obp-bank-id2"),AccountId("KOa4M8UfjUuWPIXwPXYPpy5FoFcTUwpfH-accountId2")) override def getBankAccountsForUser(username: String, callContext: Option[CallContext])= Future{ Full( (InboundAccountCommons( - bankId = bankIdAccountId.bankId.value, - accountId = bankIdAccountId.accountId.value, + bankId = bankIdAccountId1.bankId.value, + accountId = bankIdAccountId1.accountId.value, viewsToGenerate = "Owner" :: "_Public" :: "Accountant" :: "Auditor" :: Nil, branchId = "", accountNumber = "", diff --git a/obp-api/src/test/scala/code/model/AuthUserTest.scala b/obp-api/src/test/scala/code/model/AuthUserTest.scala index b7e2a5ef5..9b24a8cdc 100644 --- a/obp-api/src/test/scala/code/model/AuthUserTest.scala +++ b/obp-api/src/test/scala/code/model/AuthUserTest.scala @@ -1,13 +1,17 @@ package code.model +import code.UserRefreshes.MappedUserRefreshes import code.accountholders.MapperAccountHolders import code.bankconnectors.Connector import code.connector.MockedJune2017Connector import code.model.dataAccess.{AuthUser, ViewImpl, ViewPrivileges} -import code.setup.{DefaultUsers, ServerSetup} +import code.setup.{DefaultUsers, PropsReset, ServerSetup} import code.views.MapperViews import code.views.system.{AccountAccess, ViewDefinition} -import java.util.concurrent.TimeUnit +import com.openbankproject.commons.model.{InboundAccount, InboundAccountCommons} +import net.liftweb.mapper.{By, PreCache} + +import scala.collection.immutable.List import scala.concurrent.Await import scala.concurrent.duration.Duration @@ -21,6 +25,8 @@ class AuthUserTest extends ServerSetup with DefaultUsers { Connector.connector.default.set(MockedJune2017Connector) ViewDefinition.bulkDelete_!!() MapperAccountHolders.bulkDelete_!!() + AccountAccess.bulkDelete_!!() + MappedUserRefreshes.bulkDelete_!!() } override def afterEach() = { @@ -28,29 +34,131 @@ class AuthUserTest extends ServerSetup with DefaultUsers { Connector.connector.default.set(Connector.buildOne) ViewDefinition.bulkDelete_!!() MapperAccountHolders.bulkDelete_!!() + AccountAccess.bulkDelete_!!() + MappedUserRefreshes.bulkDelete_!!() } - val bankIdAccountId = MockedJune2017Connector.bankIdAccountId + val bankIdAccountId1 = MockedJune2017Connector.bankIdAccountId1 val bankIdAccountId2 = MockedJune2017Connector.bankIdAccountId2 + + def account1Access = AccountAccess.findAll( + By(AccountAccess.user_fk, resourceUser1.userPrimaryKey.value), + By(AccountAccess.bank_id, bankIdAccountId1.bankId.value), + By(AccountAccess.account_id, bankIdAccountId1.accountId.value), + ) + + def account2Access = AccountAccess.findAll( + By(AccountAccess.user_fk, resourceUser1.userPrimaryKey.value), + By(AccountAccess.bank_id, bankIdAccountId2.bankId.value), + By(AccountAccess.account_id, bankIdAccountId2.accountId.value), + ) + + def accountholder1 = MapperAccountHolders.getAccountHolders(bankIdAccountId1.bankId, bankIdAccountId1.accountId) + def accountholder2 = MapperAccountHolders.getAccountHolders(bankIdAccountId2.bankId, bankIdAccountId2.accountId) + + def allViewsForAccount1 = MapperViews.availableViewsForAccount(bankIdAccountId1) + def allViewsForAccount2 = MapperViews.availableViewsForAccount(bankIdAccountId2) + + def mappedUserRefreshesLength= MappedUserRefreshes.findAll().length + + + val accountsHeldEmpty = List() + + val account1Held = List( + InboundAccountCommons( + bankId = bankIdAccountId1.bankId.value, + accountId = bankIdAccountId1.accountId.value, + viewsToGenerate = "Owner" :: Nil, + branchId = "", + accountNumber = "", + accountType = "", + balanceAmount = "", + balanceCurrency = "", + owners = List(""), + bankRoutingScheme = "", + bankRoutingAddress = "", + branchRoutingScheme = "", + branchRoutingAddress = "", + accountRoutingScheme = "", + accountRoutingAddress = "" + ) + ) + + val account2Held = List( + InboundAccountCommons( + bankId = bankIdAccountId2.bankId.value, + accountId = bankIdAccountId2.accountId.value, + viewsToGenerate = "Owner" :: Nil, + branchId = "", + accountNumber = "", + accountType = "", + balanceAmount = "", + balanceCurrency = "", + owners = List(""), + bankRoutingScheme = "", + bankRoutingAddress = "", + branchRoutingScheme = "", + branchRoutingAddress = "", + accountRoutingScheme = "", + accountRoutingAddress = "" + ) + ) + + val twoAccountsHeld = List( + InboundAccountCommons( + bankId = bankIdAccountId1.bankId.value, + accountId = bankIdAccountId1.accountId.value, + viewsToGenerate = "Owner" :: Nil, + branchId = "", + accountNumber = "", + accountType = "", + balanceAmount = "", + balanceCurrency = "", + owners = List(""), + bankRoutingScheme = "", + bankRoutingAddress = "", + branchRoutingScheme = "", + branchRoutingAddress = "", + accountRoutingScheme = "", + accountRoutingAddress = "" + ), + InboundAccountCommons( + bankId = bankIdAccountId2.bankId.value, + accountId = bankIdAccountId2.accountId.value, + viewsToGenerate = "Owner" :: Nil, + branchId = "", + accountNumber = "", + accountType = "", + balanceAmount = "", + balanceCurrency = "", + owners = List(""), + bankRoutingScheme = "", + bankRoutingAddress = "", + branchRoutingScheme = "", + branchRoutingAddress = "", + accountRoutingScheme = "", + accountRoutingAddress = "" + ) + ) - feature("Test the updateUserAccountViews method") { + feature("Test the refreshUser method") { scenario("we fake the output from getBankAccounts(), and check the functions there") { When("We call the method use resourceUser1") val result = Await.result(AuthUser.refreshUser(resourceUser1, None), Duration.Inf) Then("We check the accountHolders") - var accountholder1 = MapperAccountHolders.getAccountHolders(bankIdAccountId.bankId, bankIdAccountId.accountId) + var accountholder1 = MapperAccountHolders.getAccountHolders(bankIdAccountId1.bankId, bankIdAccountId1.accountId) var accountholder2 = MapperAccountHolders.getAccountHolders(bankIdAccountId2.bankId, bankIdAccountId2.accountId) var accountholders = MapperAccountHolders.findAll() accountholder1.head.userPrimaryKey should equal(resourceUser1.userPrimaryKey) accountholder2.head.userPrimaryKey should equal(resourceUser1.userPrimaryKey) accountholders.length should equal(2) - Then("We check the views") //"Owner"::"_Public" :: "Accountant" :: "Auditor" - val allViewsForAccount1 = MapperViews.availableViewsForAccount(bankIdAccountId) - val allViewsForAccount2 = MapperViews.availableViewsForAccount(bankIdAccountId) + Then("We check the views") + val allViewsForAccount1 = MapperViews.availableViewsForAccount(bankIdAccountId1) + val allViewsForAccount2 = MapperViews.availableViewsForAccount(bankIdAccountId2) val allViews = ViewDefinition.findAll() allViewsForAccount1.toString().contains("owner") should equal(true) allViewsForAccount1.toString().contains("_public") should equal(true) @@ -67,7 +175,152 @@ class AuthUserTest extends ServerSetup with DefaultUsers { numberOfAccountAccesses should equal(8) } + } + + feature("Test the refreshViewsAccountAccessAndHolders method") { + scenario("Test one account views,account access and account holder") { + When("1rd Step: no accounts in the List") + AuthUser.refreshViewsAccountAccessAndHolders(resourceUser1, accountsHeldEmpty) + + Then("We check the accountHolders") + accountholder1.size should be(0) + accountholder2.size should be(0) + + Then("We check the views, only support the system views") + allViewsForAccount1.map(_.viewId.value) should equal(List()) + allViewsForAccount2.map(_.viewId.value) should equal(List()) + + Then("We check the AccountAccesses") + account1Access.length should equal(0) + account2Access.length should equal(0) + + Then("We check the MappedUserRefreshes table") + MappedUserRefreshes.findAll().length should be (0) + + Then("2rd Step: there is 1st account in the List") + AuthUser.refreshViewsAccountAccessAndHolders(resourceUser1, account1Held) + + Then("We check the accountHolders") + accountholder1.size should be(1) + accountholder2.size should be(0) + + Then("We check the views, only support the system view. both accounts should have the `owner` view.") + allViewsForAccount1.map(_.viewId.value) should equal(List("owner")) //TODO. check this, only one account in the list, why two `owner` here. + allViewsForAccount2.map(_.viewId.value) should equal(List("owner")) + + Then("We check the AccountAccesses") + account1Access.length should equal(1) + account2Access.length should equal(0) + + Then("We check the MappedUserRefreshes table") + MappedUserRefreshes.findAll().length should be (1) + + //3rd: we remove the accounts + Then("we delete the account") + val accountsHeld = List() + AuthUser.refreshViewsAccountAccessAndHolders(resourceUser1, accountsHeld) + + Then("We check the accountHolders") + accountholder1.size should be(0) + accountholder2.size should be(0) + + Then("We check the views, only support the system view. both accounts should have the `owner` view.") + allViewsForAccount1.map(_.viewId.value) should equal(List("owner")) + allViewsForAccount2.map(_.viewId.value) should equal(List("owner")) + + Then("We check the AccountAccesses") + account1Access.length should equal(0) + account2Access.length should equal(0) + + Then("We check the MappedUserRefreshes table") + MappedUserRefreshes.findAll().length should be (1) + + } + + + + scenario("Test two accounts views,account access and account holder") { + + //1st block, we prepare one account + When("first we have 1st new account in the accountsHeld") + AuthUser.refreshViewsAccountAccessAndHolders(resourceUser1, account1Held) + + Then("We check the accountHolders") + accountholder1.size should be(1) + accountholder2.size should be(0) + + Then("We check the views, only support the system views") + allViewsForAccount1.map(_.viewId.value) should equal(List("owner")) + allViewsForAccount2.map(_.viewId.value) should equal(List("owner")) + + Then("We check the AccountAccesses") + account1Access.length should equal(1) + account2Access.length should equal(0) + + Then("We check the MappedUserRefreshes table") + MappedUserRefreshes.findAll().length should be (1) + + //2rd block, we prepare second account + Then("first we have two accounts in the accountsHeld") + AuthUser.refreshViewsAccountAccessAndHolders(resourceUser1, twoAccountsHeld) + + Then("We check the accountHolders") + accountholder1.size should be(1) + accountholder2.size should be(1) + + Then("We check the views, only support the system views") + allViewsForAccount1.map(_.viewId.value) should equal(List("owner")) + allViewsForAccount2.map(_.viewId.value) should equal(List("owner")) + + Then("We check the AccountAccesses") + account1Access.length should equal(1) + account2Access.length should equal(1) + + Then("We check the MappedUserRefreshes table") + MappedUserRefreshes.findAll().length should be (1) + + + //3rd block, we removed the 2rd account, only have 1st account there. + When("we delete 2rd account, only have 1st account in the accountsHeld") + AuthUser.refreshViewsAccountAccessAndHolders(resourceUser1, account1Held) + + Then("We check the accountHolders") + accountholder1.size should be(1) + accountholder2.size should be(0) + + Then("We check the views, only support the system views") + allViewsForAccount1.map(_.viewId.value) should equal(List("owner")) + allViewsForAccount2.map(_.viewId.value) should equal(List("owner")) + + Then("We check the AccountAccesses") + account1Access.length should equal(1) + account2Access.length should equal(0) + + Then("We check the MappedUserRefreshes table") + MappedUserRefreshes.findAll().length should be (1) + + + //4th, we do not have any accounts + When("we delete all accounts, no account in the accountsHeld") + AuthUser.refreshViewsAccountAccessAndHolders(resourceUser1, accountsHeldEmpty) + + Then("We check the accountHolders") + accountholder1.size should be(0) + accountholder2.size should be(0) + + Then("We check the views, only support the system views") + allViewsForAccount1.map(_.viewId.value) should equal(List("owner")) + allViewsForAccount2.map(_.viewId.value) should equal(List("owner")) + + Then("We check the AccountAccesses") + account1Access.length should equal(0) + account2Access.length should equal(0) + + Then("We check the MappedUserRefreshes table") + MappedUserRefreshes.findAll().length should be (1) + + } } }