Skip to content

Commit

Permalink
sn-2950
Browse files Browse the repository at this point in the history
  • Loading branch information
arvifox committed Oct 10, 2023
1 parent 7ffb95b commit 18b32d0
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MigrationManager @Inject constructor(
credentialsRepository.saveMnemonic(mnemonic, soraAccount)
val soraKeys = credentialsRepository.retrieveKeyPair(emptySoraAccount)
credentialsRepository.saveKeyPair(soraKeys, soraAccount)
userRepository.insertSoraAccount(soraAccount)
userRepository.insertSoraAccount(soraAccount, false)
userRepository.setCurSoraAccount(soraAccount)
FirebaseWrapper.log("Migration ma done")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface UserRepository {

suspend fun soraAccountsList(): List<SoraAccount>

suspend fun insertSoraAccount(soraAccount: SoraAccount)
suspend fun insertSoraAccount(soraAccount: SoraAccount, newAccount: Boolean)

suspend fun updateAccountName(soraAccount: SoraAccount, newName: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,19 @@ class UserRepositoryImpl(
return db.accountDao().getAccountsCount()
}

override suspend fun insertSoraAccount(soraAccount: SoraAccount) {
override suspend fun insertSoraAccount(soraAccount: SoraAccount, newAccount: Boolean) {
db.withTransaction {
db.accountDao().insertSoraAccount(
SoraAccountMapper.map(soraAccount)
)
db.cardsHubDao().insert(
CardHubType.values()
CardHubType.entries
.filter { it.boundToAccount }
.mapIndexed { _, type ->
CardHubLocal(
cardId = type.hubName,
accountAddress = soraAccount.substrateAddress,
visibility = true,
visibility = if (type == CardHubType.BACKUP) newAccount else true,
sortOrder = type.order,
collapsed = false,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ import jp.co.soramitsu.core_db.model.GlobalCardHubLocal
object TestData {

val CARD_HUB_LOCAL = listOf(
CardHubLocal(
cardId = CardHubType.BACKUP.hubName,
accountAddress = "accountAddress",
visibility = true,
sortOrder = CardHubType.BACKUP.order,
collapsed = false
),
CardHubLocal(
cardId = CardHubType.ASSETS.hubName,
accountAddress = "accountAddress",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class UserRepositoryTest {
coEvery { hubDao.insert(any()) } returns Unit

val soraAccount = SoraAccount("accountAddress", "accountName")
userRepository.insertSoraAccount(soraAccount)
userRepository.insertSoraAccount(soraAccount, true)

coVerify {
accountDao.insertSoraAccount(
Expand All @@ -271,14 +271,14 @@ class UserRepositoryTest {
coEvery { hubDao.insert(TestData.CARD_HUB_LOCAL) } returns Unit
mockkStatic("androidx.room.RoomDatabaseKt")
mockkStatic(CardHubType::class)
every { CardHubType.values() } returns arrayOf(CardHubType.GET_SORA_CARD, CardHubType.ASSETS, CardHubType.POOLS)
//every { CardHubType.entries } returns arrayOf(CardHubType.GET_SORA_CARD, CardHubType.ASSETS, CardHubType.POOLS)
val lambda = slot<suspend () -> R>()
coEvery { db.withTransaction(capture(lambda)) } coAnswers {
lambda.captured.invoke()
}

val soraAccount = SoraAccount("accountAddress", "accountName")
userRepository.insertSoraAccount(soraAccount)
userRepository.insertSoraAccount(soraAccount, true)

coVerify { hubDao.insert(TestData.CARD_HUB_LOCAL) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class MultiaccountInteractor @Inject constructor(
suspend fun accountExists(address: String) = userRepository.accountExists(address)

suspend fun continueRecoverFlow(soraAccount: SoraAccount) {
insertAndSetCurAccount(soraAccount)
insertAndSetCurAccount(soraAccount, false)
userRepository.saveRegistrationState(OnboardingState.REGISTRATION_FINISHED)
}

Expand All @@ -89,8 +89,8 @@ class MultiaccountInteractor @Inject constructor(
return credentialsRepository.retrieveMnemonic(soraAccount ?: userRepository.getCurSoraAccount())
}

private suspend fun insertAndSetCurAccount(soraAccount: SoraAccount) {
userRepository.insertSoraAccount(soraAccount)
private suspend fun insertAndSetCurAccount(soraAccount: SoraAccount, newAccount: Boolean) {
userRepository.insertSoraAccount(soraAccount, newAccount)
userRepository.setCurSoraAccount(soraAccount)
}

Expand All @@ -107,7 +107,7 @@ class MultiaccountInteractor @Inject constructor(
}

suspend fun createUser(soraAccount: SoraAccount) {
insertAndSetCurAccount(soraAccount)
insertAndSetCurAccount(soraAccount, true)
userRepository.saveRegistrationState(OnboardingState.INITIAL)
userRepository.saveNeedsMigration(false, soraAccount)
userRepository.saveIsMigrationFetched(true, soraAccount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class MultiaccountInteractorTest {
fun setup() {
coEvery { credentialsRepository.isMnemonicValid(any()) } returns true
coEvery { credentialsRepository.isRawSeedValid(any()) } returns true
coEvery { userRepository.insertSoraAccount(any()) } returns Unit
coEvery { userRepository.insertSoraAccount(any(), any()) } returns Unit
coEvery { userRepository.setCurSoraAccount(any()) } returns Unit
coEvery { userRepository.saveRegistrationState(any()) } returns Unit
coEvery { credentialsRepository.restoreUserCredentialsFromMnemonic(any(), any()) } returns account
Expand Down Expand Up @@ -128,7 +128,7 @@ class MultiaccountInteractorTest {
@Test
fun `continueRecoverFlow is called`() = runTest {
multiaccountInteractor.continueRecoverFlow(account)
coVerify { userRepository.insertSoraAccount(account) }
coVerify { userRepository.insertSoraAccount(account, false) }
coVerify { userRepository.setCurSoraAccount(account) }
coVerify { userRepository.saveRegistrationState(OnboardingState.REGISTRATION_FINISHED) }
}
Expand All @@ -152,7 +152,7 @@ class MultiaccountInteractorTest {
@Test
fun `createUser is called`() = runTest {
multiaccountInteractor.createUser(account)
coVerify { userRepository.insertSoraAccount(account) }
coVerify { userRepository.insertSoraAccount(account, true) }
coVerify { userRepository.setCurSoraAccount(account) }
coVerify { userRepository.saveRegistrationState(OnboardingState.INITIAL) }
coVerify { userRepository.saveNeedsMigration(false, account) }
Expand Down

0 comments on commit 18b32d0

Please sign in to comment.