Skip to content

Commit 6885e4c

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 2a880c4 commit 6885e4c

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
@@ -334,32 +334,39 @@ class SiteSqlUtils
334334
fun updateApplicationPasswordCredentials(localId: Int, usernamePlain: String, passwordPlain: String): Int {
335335
val username = encryptionUtils.encrypt(usernamePlain)
336336
val password = encryptionUtils.encrypt(passwordPlain)
337-
return WellSql.update(SiteModel::class.java)
338-
.whereId(localId)
339-
.put(localId, { _ ->
340-
val cv = ContentValues()
341-
cv.put(SiteModelTable.API_REST_USERNAME, username.first)
342-
cv.put(SiteModelTable.API_REST_USERNAME_IV, username.second)
343-
cv.put(SiteModelTable.API_REST_PASSWORD, password.first)
344-
cv.put(SiteModelTable.API_REST_PASSWORD_IV, password.second)
345-
cv
346-
}).execute()
337+
return writeApplicationPasswordCredentialColumns(
338+
localId, username.first, username.second, password.first, password.second
339+
)
347340
}
348341

349342
/**
350343
* Clears the application-password credential columns (encrypted username/password and their IVs) for the
351344
* given local id. Companion to [updateApplicationPasswordCredentials] for the sign-out / revoke paths,
352345
* since the generic update path no longer touches these columns.
353346
*/
354-
fun clearApplicationPasswordCredentials(localId: Int): Int {
347+
fun clearApplicationPasswordCredentials(localId: Int): Int =
348+
writeApplicationPasswordCredentialColumns(localId, "", "", "", "")
349+
350+
/**
351+
* The single place that maps the four application-password credential columns to their values, so the
352+
* column set lives in one spot. Callers pass already-encrypted values (or empty strings to clear); each
353+
* IV always travels with its ciphertext, so a row can never be left holding one without the other.
354+
*/
355+
private fun writeApplicationPasswordCredentialColumns(
356+
localId: Int,
357+
usernameCipher: String,
358+
usernameIv: String,
359+
passwordCipher: String,
360+
passwordIv: String
361+
): Int {
355362
return WellSql.update(SiteModel::class.java)
356363
.whereId(localId)
357364
.put(localId, { _ ->
358365
val cv = ContentValues()
359-
cv.put(SiteModelTable.API_REST_USERNAME, "")
360-
cv.put(SiteModelTable.API_REST_USERNAME_IV, "")
361-
cv.put(SiteModelTable.API_REST_PASSWORD, "")
362-
cv.put(SiteModelTable.API_REST_PASSWORD_IV, "")
366+
cv.put(SiteModelTable.API_REST_USERNAME, usernameCipher)
367+
cv.put(SiteModelTable.API_REST_USERNAME_IV, usernameIv)
368+
cv.put(SiteModelTable.API_REST_PASSWORD, passwordCipher)
369+
cv.put(SiteModelTable.API_REST_PASSWORD_IV, passwordIv)
363370
cv
364371
}).execute()
365372
}

0 commit comments

Comments
 (0)