Skip to content

Commit fb89734

Browse files
committed
De-duplicate the credential-column writer; tighten a test
updateApplicationPasswordCredentials and clearApplicationPasswordCredentials hand-wrote the same four-column ContentValues, so the column set lived in two places — a future column change has to touch both or clear leaves a stale column. Route both through one private writeApplicationPasswordCredentialColumns. ApplicationPasswordViewModelSliceTest: the xmlRpc-rediscovery-success assertion was pre-satisfied by @before seeding siteTest.xmlRpcUrl to the same value; reset it to null first so the assertion proves rediscovery assigned it.
1 parent 17a2c01 commit fb89734

2 files changed

Lines changed: 24 additions & 15 deletions

File tree

WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/applicationpassword/ApplicationPasswordViewModelSliceTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ class ApplicationPasswordViewModelSliceTest : BaseUnitTest() {
377377
@Test
378378
fun `given xmlRpc rediscovery and auth check succeed, then persist the discovered xmlRpcUrl`() =
379379
runTest {
380+
// @Before seeds siteTest.xmlRpcUrl; clear it so the final assertion proves rediscovery set it.
381+
siteTest.xmlRpcUrl = null
380382
val xmlRpcUrl = "https://www.test.com/xmlrpc.php"
381383
whenever(
382384
selfHostedEndpointFinder

libs/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/SiteSqlUtils.kt

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -343,32 +343,39 @@ class SiteSqlUtils
343343
fun updateApplicationPasswordCredentials(localId: Int, usernamePlain: String, passwordPlain: String): Int {
344344
val username = encryptionUtils.encrypt(usernamePlain)
345345
val password = encryptionUtils.encrypt(passwordPlain)
346-
return WellSql.update(SiteModel::class.java)
347-
.whereId(localId)
348-
.put(localId, { _ ->
349-
val cv = ContentValues()
350-
cv.put(SiteModelTable.API_REST_USERNAME, username.first)
351-
cv.put(SiteModelTable.API_REST_USERNAME_IV, username.second)
352-
cv.put(SiteModelTable.API_REST_PASSWORD, password.first)
353-
cv.put(SiteModelTable.API_REST_PASSWORD_IV, password.second)
354-
cv
355-
}).execute()
346+
return writeApplicationPasswordCredentialColumns(
347+
localId, username.first, username.second, password.first, password.second
348+
)
356349
}
357350

358351
/**
359352
* Clears the application-password credential columns (encrypted username/password and their IVs) for the
360353
* given local id. Companion to [updateApplicationPasswordCredentials] for the sign-out / revoke paths,
361354
* since the generic update path no longer touches these columns.
362355
*/
363-
fun clearApplicationPasswordCredentials(localId: Int): Int {
356+
fun clearApplicationPasswordCredentials(localId: Int): Int =
357+
writeApplicationPasswordCredentialColumns(localId, "", "", "", "")
358+
359+
/**
360+
* The single place that maps the four application-password credential columns to their values, so the
361+
* column set lives in one spot. Callers pass already-encrypted values (or empty strings to clear); each
362+
* IV always travels with its ciphertext, so a row can never be left holding one without the other.
363+
*/
364+
private fun writeApplicationPasswordCredentialColumns(
365+
localId: Int,
366+
usernameCipher: String,
367+
usernameIv: String,
368+
passwordCipher: String,
369+
passwordIv: String
370+
): Int {
364371
return WellSql.update(SiteModel::class.java)
365372
.whereId(localId)
366373
.put(localId, { _ ->
367374
val cv = ContentValues()
368-
cv.put(SiteModelTable.API_REST_USERNAME, "")
369-
cv.put(SiteModelTable.API_REST_USERNAME_IV, "")
370-
cv.put(SiteModelTable.API_REST_PASSWORD, "")
371-
cv.put(SiteModelTable.API_REST_PASSWORD_IV, "")
375+
cv.put(SiteModelTable.API_REST_USERNAME, usernameCipher)
376+
cv.put(SiteModelTable.API_REST_USERNAME_IV, usernameIv)
377+
cv.put(SiteModelTable.API_REST_PASSWORD, passwordCipher)
378+
cv.put(SiteModelTable.API_REST_PASSWORD_IV, passwordIv)
372379
cv
373380
}).execute()
374381
}

0 commit comments

Comments
 (0)