Skip to content

Commit 2be39b2

Browse files
@W-21933885: [MSDK Android] App Attestation Implementation (Test Coverage For SalesforceSDKManager.updateAppAttestationClient)
1 parent 9f80004 commit 2be39b2

2 files changed

Lines changed: 54 additions & 23 deletions

File tree

libs/SalesforceSDK/src/com/salesforce/androidsdk/app/SalesforceSDKManager.kt

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ import androidx.compose.runtime.Composable
6464
import androidx.core.content.ContextCompat.RECEIVER_EXPORTED
6565
import androidx.core.content.ContextCompat.RECEIVER_NOT_EXPORTED
6666
import androidx.core.content.ContextCompat.registerReceiver
67-
import androidx.core.net.toUri
6867
import androidx.lifecycle.DefaultLifecycleObserver
6968
import androidx.lifecycle.LifecycleOwner
7069
import androidx.lifecycle.ProcessLifecycleOwner
@@ -229,29 +228,40 @@ open class SalesforceSDKManager protected constructor(
229228
val loginActivityClass: Class<out Activity> = nativeLoginActivity ?: webViewLoginActivityClass
230229

231230
/**
232-
* The Google Cloud Project ID used to for the client implementation of the
233-
* Salesforce App Attestation External Client App Plugin. When using App
231+
* The client side implementation of the Salesforce App Attestation External
232+
* Client App Plugin or null with app attestation is disabled.
233+
*/
234+
var appAttestationClient: AppAttestationClient? = null
235+
236+
/**
237+
* Updates the Salesforce App Attestation ECA Plugin Client for the selected
238+
* login server and matching Google Cloud Project ID. When using App
234239
* Attestation, this value must match the linked Google Cloud Project ID
235240
* for the app in Google Play Console's Play Integrity API and provided to
236241
* the Salesforce App Attestation External Client App Plugin.
237242
*
238-
* When null, App Attestation and Google Play Integrity will be ignored by
239-
* the Salesforce Mobile SDK.
240-
*/
241-
var appAttestationGoogleCloudProjectId: Long? = null
242-
set(value) {
243-
field = value
244-
245-
val loginHost = loginServerManager.selectedLoginServer?.url?.toUri()?.host
246-
if (loginHost == null) {
247-
w(javaClass.name, "Cannot initialize Salesforce App Attestation Client since the selected login server URL doesn't have a host. Authentication may malfunction.")
248-
return
249-
}
243+
* @param selectedLoginServerHost The selected login server configured with
244+
* the Salesforce App Attestation ECA Plugin
245+
* @param googleCloudProjectId The Google Cloud Project ID or null to
246+
* disable Salesforce App Attestation
247+
*/
248+
fun updateAppAttestationClient(
249+
selectedLoginServerHost: String,
250+
googleCloudProjectId: Long? = null
251+
) {
252+
// TODO: Needs Coverage x4. ECJ20260417
253+
// val loginHost = selectedLoginServer.url.toUri().host
254+
// TODO: Needs Coverage x1. ECJ20260417
255+
// if (loginHost == null) {
256+
// w(javaClass.name, "Cannot initialize Salesforce App Attestation Client since the selected login server URL doesn't have a host. Authentication may malfunction.")
257+
// return
258+
// }
250259

251-
appAttestationClient = field?.let { appAttestationGoogleCloudProjectId ->
260+
// TODO: Needs Coverage x2. ECJ20260417
261+
appAttestationClient = googleCloudProjectId?.let { appAttestationGoogleCloudProjectId ->
252262
AppAttestationClient(
253263
context = appContext,
254-
apiHostName = loginHost,
264+
apiHostName = selectedLoginServerHost,
255265
deviceId = deviceId,
256266
googleCloudProjectId = appAttestationGoogleCloudProjectId,
257267
remoteAccessConsumerKey = getBootConfig(getInstance().appContext).remoteAccessConsumerKey,
@@ -260,12 +270,6 @@ open class SalesforceSDKManager protected constructor(
260270
}
261271
}
262272

263-
/**
264-
* The client side implementation of the Salesforce App Attestation External
265-
* Client App Plugin or null with app attestation is disabled.
266-
*/
267-
var appAttestationClient: AppAttestationClient? = null
268-
269273
/**
270274
* ViewModel Factory the SDK will use in LoginActivity and composable functions. Setting this will allow for
271275
* visual customization without overriding LoginActivity.

libs/test/SalesforceSDKTest/src/com/salesforce/androidsdk/app/SalesforceSDKManagerTests.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.junit.After
2222
import org.junit.Assert.assertEquals
2323
import org.junit.Assert.assertFalse
2424
import org.junit.Assert.assertNotNull
25+
import org.junit.Assert.assertNull
2526
import org.junit.Assert.assertTrue
2627
import org.junit.Before
2728
import org.junit.Test
@@ -288,4 +289,30 @@ class SalesforceSDKManagerTests {
288289
assertNotNull(devActions["Show dev info"])
289290
assertNotNull(devActions["Login Options"])
290291
}
292+
293+
@Test
294+
fun salesforceSdkManager_setAppAttestationGoogleCloudProjectId_updatesAppAttestationClient() {
295+
296+
SalesforceSDKManager.getInstance().updateAppAttestationClient(
297+
selectedLoginServerHost = "login.example.com",
298+
googleCloudProjectId = 123456
299+
)
300+
301+
assertEquals(123456L, SalesforceSDKManager.getInstance().appAttestationClient?.googleCloudProjectId)
302+
assertEquals("login.example.com", SalesforceSDKManager.getInstance().appAttestationClient?.apiHostName)
303+
assertNotNull(SalesforceSDKManager.getInstance().appAttestationClient?.deviceId)
304+
assertEquals("__CONSUMER_KEY__", SalesforceSDKManager.getInstance().appAttestationClient?.remoteAccessConsumerKey)
305+
assertNotNull(SalesforceSDKManager.getInstance().appAttestationClient?.restClient)
306+
}
307+
308+
@Test
309+
fun salesforceSdkManager_setAppAttestationGoogleCloudProjectId_doesNotSetAppAttestationClientWhenGoogleCloudProjectIdIsNull() {
310+
311+
SalesforceSDKManager.getInstance().updateAppAttestationClient(
312+
selectedLoginServerHost = "login.example.com",
313+
googleCloudProjectId = null
314+
)
315+
316+
assertNull(SalesforceSDKManager.getInstance().appAttestationClient)
317+
}
291318
}

0 commit comments

Comments
 (0)