|
| 1 | +package com.x8bit.bitwarden.data.platform.manager.sdk.repository |
| 2 | + |
| 3 | +import com.bitwarden.core.LocalUserDataKeyState |
| 4 | +import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson |
| 5 | +import com.x8bit.bitwarden.data.auth.datasource.disk.util.FakeAuthDiskSource |
| 6 | +import io.mockk.mockk |
| 7 | +import kotlinx.coroutines.test.runTest |
| 8 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 9 | +import org.junit.jupiter.api.Assertions.assertFalse |
| 10 | +import org.junit.jupiter.api.Assertions.assertNull |
| 11 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 12 | +import org.junit.jupiter.api.Test |
| 13 | + |
| 14 | +class SdkLocalUserDataKeyStateRepositoryTest { |
| 15 | + |
| 16 | + private val fakeAuthDiskSource = FakeAuthDiskSource() |
| 17 | + |
| 18 | + private val repository = SdkLocalUserDataKeyStateRepository( |
| 19 | + authDiskSource = fakeAuthDiskSource, |
| 20 | + ) |
| 21 | + |
| 22 | + @Test |
| 23 | + fun `get should return null when no key is stored for the given id`() = runTest { |
| 24 | + assertNull(repository.get(id = USER_ID)) |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + fun `get should return LocalUserDataKeyState when key is stored for the given id`() = runTest { |
| 29 | + fakeAuthDiskSource.storeLocalUserDataKey(userId = USER_ID, wrappedKey = WRAPPED_KEY) |
| 30 | + |
| 31 | + assertEquals( |
| 32 | + LocalUserDataKeyState(wrappedKey = WRAPPED_KEY), |
| 33 | + repository.get(id = USER_ID), |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun `has should return false when no key is stored for the given id`() = runTest { |
| 39 | + assertFalse(repository.has(id = USER_ID)) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun `has should return true when a key is stored for the given id`() = runTest { |
| 44 | + fakeAuthDiskSource.storeLocalUserDataKey(userId = USER_ID, wrappedKey = WRAPPED_KEY) |
| 45 | + |
| 46 | + assertTrue(repository.has(id = USER_ID)) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `list should return empty list when userState is null`() = runTest { |
| 51 | + fakeAuthDiskSource.userState = null |
| 52 | + |
| 53 | + assertEquals(emptyList<LocalUserDataKeyState>(), repository.list()) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun `list should return empty list when no keys are stored for any account`() = runTest { |
| 58 | + fakeAuthDiskSource.userState = UserStateJson( |
| 59 | + activeUserId = USER_ID, |
| 60 | + accounts = mapOf(USER_ID to mockk()), |
| 61 | + ) |
| 62 | + |
| 63 | + assertEquals(emptyList<LocalUserDataKeyState>(), repository.list()) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `list should return LocalUserDataKeyState for each account that has a stored key`() = |
| 68 | + runTest { |
| 69 | + fakeAuthDiskSource.userState = UserStateJson( |
| 70 | + activeUserId = USER_ID, |
| 71 | + accounts = mapOf( |
| 72 | + USER_ID to mockk(), |
| 73 | + USER_ID_2 to mockk(), |
| 74 | + ), |
| 75 | + ) |
| 76 | + fakeAuthDiskSource.storeLocalUserDataKey(userId = USER_ID, wrappedKey = WRAPPED_KEY) |
| 77 | + fakeAuthDiskSource.storeLocalUserDataKey(userId = USER_ID_2, wrappedKey = WRAPPED_KEY_2) |
| 78 | + |
| 79 | + assertEquals( |
| 80 | + listOf( |
| 81 | + LocalUserDataKeyState(wrappedKey = WRAPPED_KEY), |
| 82 | + LocalUserDataKeyState(wrappedKey = WRAPPED_KEY_2), |
| 83 | + ), |
| 84 | + repository.list(), |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun `list should omit accounts that have no stored key`() = runTest { |
| 90 | + fakeAuthDiskSource.userState = UserStateJson( |
| 91 | + activeUserId = USER_ID, |
| 92 | + accounts = mapOf(USER_ID to mockk(), USER_ID_2 to mockk()), |
| 93 | + ) |
| 94 | + fakeAuthDiskSource.storeLocalUserDataKey(userId = USER_ID, wrappedKey = WRAPPED_KEY) |
| 95 | + |
| 96 | + assertEquals( |
| 97 | + listOf(LocalUserDataKeyState(wrappedKey = WRAPPED_KEY)), |
| 98 | + repository.list(), |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + fun `remove should clear the stored key for the given id`() = runTest { |
| 104 | + fakeAuthDiskSource.storeLocalUserDataKey(userId = USER_ID, wrappedKey = WRAPPED_KEY) |
| 105 | + |
| 106 | + repository.remove(id = USER_ID) |
| 107 | + |
| 108 | + assertNull(fakeAuthDiskSource.getLocalUserDataKey(userId = USER_ID)) |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + fun `removeAll should clear the stored key for all accounts`() = runTest { |
| 113 | + fakeAuthDiskSource.userState = UserStateJson( |
| 114 | + activeUserId = USER_ID, |
| 115 | + accounts = mapOf( |
| 116 | + USER_ID to mockk(), |
| 117 | + USER_ID_2 to mockk(), |
| 118 | + ), |
| 119 | + ) |
| 120 | + fakeAuthDiskSource.storeLocalUserDataKey(userId = USER_ID, wrappedKey = WRAPPED_KEY) |
| 121 | + fakeAuthDiskSource.storeLocalUserDataKey(userId = USER_ID_2, wrappedKey = WRAPPED_KEY_2) |
| 122 | + |
| 123 | + repository.removeAll() |
| 124 | + |
| 125 | + assertNull(fakeAuthDiskSource.getLocalUserDataKey(userId = USER_ID)) |
| 126 | + assertNull(fakeAuthDiskSource.getLocalUserDataKey(userId = USER_ID_2)) |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + fun `removeAll should do nothing when userState is null`() = runTest { |
| 131 | + fakeAuthDiskSource.userState = null |
| 132 | + |
| 133 | + repository.removeAll() |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + fun `removeBulk should clear the stored key for each given id`() = runTest { |
| 138 | + fakeAuthDiskSource.storeLocalUserDataKey(userId = USER_ID, wrappedKey = WRAPPED_KEY) |
| 139 | + fakeAuthDiskSource.storeLocalUserDataKey(userId = USER_ID_2, wrappedKey = WRAPPED_KEY_2) |
| 140 | + |
| 141 | + repository.removeBulk(keys = listOf(USER_ID, USER_ID_2)) |
| 142 | + |
| 143 | + assertNull(fakeAuthDiskSource.getLocalUserDataKey(userId = USER_ID)) |
| 144 | + assertNull(fakeAuthDiskSource.getLocalUserDataKey(userId = USER_ID_2)) |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + fun `set should store the wrapped key for the given id`() = runTest { |
| 149 | + repository.set(id = USER_ID, value = LocalUserDataKeyState(wrappedKey = WRAPPED_KEY)) |
| 150 | + |
| 151 | + assertEquals(WRAPPED_KEY, fakeAuthDiskSource.getLocalUserDataKey(userId = USER_ID)) |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + fun `setBulk should store the wrapped key for each given id`() = runTest { |
| 156 | + repository.setBulk( |
| 157 | + values = mapOf( |
| 158 | + USER_ID to LocalUserDataKeyState(wrappedKey = WRAPPED_KEY), |
| 159 | + USER_ID_2 to LocalUserDataKeyState(wrappedKey = WRAPPED_KEY_2), |
| 160 | + ), |
| 161 | + ) |
| 162 | + |
| 163 | + assertEquals(WRAPPED_KEY, fakeAuthDiskSource.getLocalUserDataKey(userId = USER_ID)) |
| 164 | + assertEquals(WRAPPED_KEY_2, fakeAuthDiskSource.getLocalUserDataKey(userId = USER_ID_2)) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +private const val USER_ID: String = "userId" |
| 169 | +private const val USER_ID_2: String = "userId2" |
| 170 | +private const val WRAPPED_KEY: String = "wrappedKey" |
| 171 | +private const val WRAPPED_KEY_2: String = "wrappedKey2" |
0 commit comments