Store organization keys during sync request (#367)

This commit is contained in:
Brian Yencho
2023-12-11 20:38:22 -06:00
committed by GitHub
parent 64a3caf354
commit 25bad1ec78
8 changed files with 194 additions and 19 deletions

View File

@@ -192,6 +192,64 @@ class AuthDiskSourceTest {
actual,
)
}
@Test
fun `getOrganizationKeys should pull from SharedPreferences`() {
val organizationKeysBaseKey = "bwPreferencesStorage:encOrgKeys"
val mockUserId = "mockUserId"
val mockOrganizationKeys = mapOf(
"organizationId1" to "organizationKey1",
"organizationId2" to "organizationKey2",
)
fakeSharedPreferences
.edit()
.putString(
"${organizationKeysBaseKey}_$mockUserId",
"""
{
"organizationId1": "organizationKey1",
"organizationId2": "organizationKey2"
}
"""
.trimIndent(),
)
.apply()
val actual = authDiskSource.getOrganizationKeys(userId = mockUserId)
assertEquals(
mockOrganizationKeys,
actual,
)
}
@Test
fun `putOrganizationKeys should update SharedPreferences`() {
val organizationKeysBaseKey = "bwPreferencesStorage:encOrgKeys"
val mockUserId = "mockUserId"
val mockOrganizationKeys = mapOf(
"organizationId1" to "organizationKey1",
"organizationId2" to "organizationKey2",
)
authDiskSource.storeOrganizationKeys(
userId = mockUserId,
organizationKeys = mockOrganizationKeys,
)
val actual = fakeSharedPreferences.getString(
"${organizationKeysBaseKey}_$mockUserId",
null,
)
assertEquals(
json.parseToJsonElement(
"""
{
"organizationId1": "organizationKey1",
"organizationId2": "organizationKey2"
}
"""
.trimIndent(),
),
json.parseToJsonElement(requireNotNull(actual)),
)
}
}
private const val USER_STATE_JSON = """

View File

@@ -8,10 +8,21 @@ import kotlinx.coroutines.flow.onSubscription
import org.junit.Assert.assertEquals
class FakeAuthDiskSource : AuthDiskSource {
override val uniqueAppId: String = "testUniqueAppId"
override var rememberedEmailAddress: String? = null
private val mutableUserStateFlow =
MutableSharedFlow<UserStateJson?>(
replay = 1,
extraBufferCapacity = Int.MAX_VALUE,
)
private val storedUserKeys = mutableMapOf<String, String?>()
private val storedPrivateKeys = mutableMapOf<String, String?>()
private val storedOrganizationKeys = mutableMapOf<String, Map<String, String>?>()
override var userState: UserStateJson? = null
set(value) {
field = value
@@ -33,15 +44,16 @@ class FakeAuthDiskSource : AuthDiskSource {
storedPrivateKeys[userId] = privateKey
}
private val mutableUserStateFlow =
MutableSharedFlow<UserStateJson?>(
replay = 1,
extraBufferCapacity = Int.MAX_VALUE,
)
override fun getOrganizationKeys(
userId: String,
): Map<String, String>? = storedOrganizationKeys[userId]
private val storedUserKeys = mutableMapOf<String, String?>()
private val storedPrivateKeys = mutableMapOf<String, String?>()
override fun storeOrganizationKeys(
userId: String,
organizationKeys: Map<String, String>?,
) {
storedOrganizationKeys[userId] = organizationKeys
}
/**
* Assert that the given [userState] matches the currently tracked value.
@@ -63,4 +75,11 @@ class FakeAuthDiskSource : AuthDiskSource {
fun assertPrivateKey(userId: String, privateKey: String?) {
assertEquals(privateKey, storedPrivateKeys[userId])
}
/**
* Assert the the [organizationKeys] was stored successfully using the [userId].
*/
fun assertOrganizationKeys(userId: String, organizationKeys: Map<String, String>?) {
assertEquals(organizationKeys, storedOrganizationKeys[userId])
}
}

View File

@@ -957,6 +957,20 @@ class AuthRepositoryTest {
environmentUrlData = EnvironmentUrlDataJson.DEFAULT_US,
)
} returns SINGLE_USER_STATE_1
fakeAuthDiskSource.apply {
storeUserKey(
userId = USER_ID_1,
userKey = PUBLIC_KEY,
)
storePrivateKey(
userId = USER_ID_1,
privateKey = PRIVATE_KEY,
)
storeOrganizationKeys(
userId = USER_ID_1,
organizationKeys = ORGANIZATION_KEYS,
)
}
repository.login(email = EMAIL, password = PASSWORD, captchaToken = null)
@@ -979,6 +993,10 @@ class AuthRepositoryTest {
userId = USER_ID_1,
userKey = null,
)
fakeAuthDiskSource.assertOrganizationKeys(
userId = USER_ID_1,
organizationKeys = null,
)
verify { vaultRepository.clearUnlockedData() }
verify { vaultRepository.lockVaultIfNecessary(userId = USER_ID_1) }
}
@@ -1021,6 +1039,20 @@ class AuthRepositoryTest {
environmentUrlData = EnvironmentUrlDataJson.DEFAULT_US,
)
} returns MULTI_USER_STATE
fakeAuthDiskSource.apply {
storeUserKey(
userId = USER_ID_2,
userKey = PUBLIC_KEY,
)
storePrivateKey(
userId = USER_ID_2,
privateKey = PRIVATE_KEY,
)
storeOrganizationKeys(
userId = USER_ID_2,
organizationKeys = ORGANIZATION_KEYS,
)
}
repository.login(email = EMAIL, password = PASSWORD, captchaToken = null)
@@ -1043,6 +1075,10 @@ class AuthRepositoryTest {
userId = USER_ID_1,
userKey = null,
)
fakeAuthDiskSource.assertOrganizationKeys(
userId = USER_ID_1,
organizationKeys = null,
)
verify { vaultRepository.clearUnlockedData() }
verify { vaultRepository.lockVaultIfNecessary(userId = USER_ID_1) }
}
@@ -1056,6 +1092,20 @@ class AuthRepositoryTest {
accounts = initialUserState.accounts.filter { it.key != USER_ID_2 },
)
fakeAuthDiskSource.userState = initialUserState
fakeAuthDiskSource.apply {
storeUserKey(
userId = USER_ID_2,
userKey = PUBLIC_KEY,
)
storePrivateKey(
userId = USER_ID_2,
privateKey = PRIVATE_KEY,
)
storeOrganizationKeys(
userId = USER_ID_2,
organizationKeys = ORGANIZATION_KEYS,
)
}
assertEquals(initialUserState, fakeAuthDiskSource.userState)
@@ -1075,6 +1125,10 @@ class AuthRepositoryTest {
userId = USER_ID_2,
userKey = null,
)
fakeAuthDiskSource.assertOrganizationKeys(
userId = USER_ID_2,
organizationKeys = null,
)
verify(exactly = 0) { vaultRepository.clearUnlockedData() }
verify { vaultRepository.lockVaultIfNecessary(userId = USER_ID_2) }
}
@@ -1295,6 +1349,7 @@ class AuthRepositoryTest {
private const val USER_ID_1 = "2a135b23-e1fb-42c9-bec3-573857bc8181"
private const val USER_ID_2 = "b9d32ec0-6497-4582-9798-b350f53bfa02"
private const val USER_ID_3 = "3816ef34-0747-4133-9b7a-ba35d3768a68"
private val ORGANIZATION_KEYS = mapOf("organizationId1" to "organizationKey1")
private val PRE_LOGIN_SUCCESS = PreLoginResponseJson(
kdfParams = PreLoginResponseJson.KdfParams.Pbkdf2(iterations = 1u),
)

View File

@@ -106,6 +106,10 @@ class VaultRepositoryTest {
userId = "mockId-1",
privateKey = "mockPrivateKey-1",
)
fakeAuthDiskSource.assertOrganizationKeys(
userId = "mockId-1",
organizationKeys = mapOf("mockId-1" to "mockKey-1"),
)
assertEquals(
DataState.Loaded(
data = VaultData(