Skip to content

Commit d926ce9

Browse files
PM-32721: bug: Sort password history before persisting (#6709)
1 parent 6332081 commit d926ce9

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

app/src/main/kotlin/com/x8bit/bitwarden/ui/vault/feature/vault/util/VaultAddItemStateExtensions.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,16 @@ private fun VaultAddEditState.ViewState.Content.toPasswordHistory(
163163
)
164164

165165
return listOf(
166-
common.originalCipher?.passwordHistory.orEmpty(),
167166
newPasswordHistory,
167+
common.originalCipher?.passwordHistory.orEmpty(),
168168
newHiddenFieldHistory,
169169
)
170170
.flatten()
171+
// Ensure that they are in the correct order before saving.
172+
.sortedByDescending { it.lastUsedDate }
173+
// Only persist the 5 most recent items.
174+
.take(5)
171175
.ifEmpty { null }
172-
?.takeLast(5)
173176
}
174177

175178
private fun getPasswordHistory(

app/src/test/kotlin/com/x8bit/bitwarden/ui/vault/feature/vault/util/VaultAddItemStateExtensionsTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockSdkFido2Cre
1818
import com.x8bit.bitwarden.ui.vault.feature.addedit.VaultAddEditState
1919
import com.x8bit.bitwarden.ui.vault.feature.addedit.model.UriItem
2020
import com.x8bit.bitwarden.ui.vault.model.VaultCardBrand
21-
import com.x8bit.bitwarden.ui.vault.model.VaultCollection
2221
import com.x8bit.bitwarden.ui.vault.model.VaultCardExpirationMonth
22+
import com.x8bit.bitwarden.ui.vault.model.VaultCollection
2323
import com.x8bit.bitwarden.ui.vault.model.VaultIdentityTitle
2424
import com.x8bit.bitwarden.ui.vault.model.VaultLinkedFieldType
2525
import org.junit.jupiter.api.Assertions.assertEquals
@@ -202,11 +202,11 @@ class VaultAddItemStateExtensionsTest {
202202
),
203203
passwordHistory = listOf(
204204
PasswordHistoryView(
205-
password = "old_password",
205+
password = "password",
206206
lastUsedDate = FIXED_CLOCK.instant(),
207207
),
208208
PasswordHistoryView(
209-
password = "password",
209+
password = "old_password",
210210
lastUsedDate = FIXED_CLOCK.instant(),
211211
),
212212
PasswordHistoryView(
@@ -1104,11 +1104,11 @@ class VaultAddItemStateExtensionsTest {
11041104
fields = emptyList(),
11051105
passwordHistory = listOf(
11061106
PasswordHistoryView(
1107-
password = "old_password",
1107+
password = "password",
11081108
lastUsedDate = FIXED_CLOCK.instant(),
11091109
),
11101110
PasswordHistoryView(
1111-
password = "password",
1111+
password = "old_password",
11121112
lastUsedDate = FIXED_CLOCK.instant(),
11131113
),
11141114
PasswordHistoryView(

0 commit comments

Comments
 (0)