Skip to content

Commit ab2af33

Browse files
@W-21933885: [MSDK Android] App Attestation Implementation (Automated "Don't Repeat Yourself" Code Review In New Tests)
1 parent 1304062 commit ab2af33

3 files changed

Lines changed: 275 additions & 484 deletions

File tree

libs/test/SalesforceSDKTest/src/com/salesforce/androidsdk/auth/AppAttestationChallengeApiClientTest.kt

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import com.salesforce.androidsdk.rest.RestClient
3333
import com.salesforce.androidsdk.rest.RestResponse
3434
import io.mockk.every
3535
import io.mockk.mockk
36+
import org.junit.Assert.assertEquals
3637
import org.junit.Assert.assertThrows
3738
import org.junit.Test
3839
import org.junit.runner.RunWith
@@ -41,68 +42,82 @@ import org.junit.runner.RunWith
4142
class AppAttestationChallengeApiClientTest {
4243

4344
@Test
44-
fun appAttestationChallengeApiClient_fetchChallenge_throwsWhenRestResponseIsNotSuccess() {
45+
fun appAttestationChallengeApiClient_fetchChallenge_returnsChallengeOnSuccess() {
4546

46-
val restResponse = mockk<RestResponse>(relaxed = true)
47-
every { restResponse.asString() } returns "__TEST_CHALLENGE_VALUE__"
48-
every { restResponse.isSuccess } returns false
49-
val restClient = mockk<RestClient>(relaxed = true)
50-
every { restClient.sendSync(any()) } returns restResponse
47+
val client = createClient(body = TEST_CHALLENGE_VALUE, success = true)
5148

52-
val appAttestationChallengeApiClient = AppAttestationChallengeApiClient(
53-
apiHostName = "https://www.example.com",
54-
restClient = restClient
49+
val result = client.fetchChallenge(
50+
attestationId = TEST_ATTESTATION_ID,
51+
remoteConsumerKey = TEST_REMOTE_CONSUMER_KEY,
5552
)
5653

54+
assertEquals(TEST_CHALLENGE_VALUE, result)
55+
}
56+
57+
@Test
58+
fun appAttestationChallengeApiClient_fetchChallenge_throwsWhenRestResponseIsNotSuccess() {
59+
60+
val client = createClient(body = TEST_CHALLENGE_VALUE, success = false)
61+
5762
assertThrows(AppAttestationChallengeApiException::class.java) {
58-
appAttestationChallengeApiClient.fetchChallenge(
59-
attestationId = "__ATTESTATION_ID__",
60-
remoteConsumerKey = "__REMOTE_CONSUMER_KEY__"
63+
client.fetchChallenge(
64+
attestationId = TEST_ATTESTATION_ID,
65+
remoteConsumerKey = TEST_REMOTE_CONSUMER_KEY,
6166
)
6267
}
6368
}
6469

6570
@Test
6671
fun appAttestationChallengeApiClient_fetchChallenge_throwsWhenRestResponseBodyStringIsNull() {
6772

68-
val restResponse = mockk<RestResponse>(relaxed = true)
69-
every { restResponse.asString() } returns null
70-
every { restResponse.isSuccess } returns true
71-
val restClient = mockk<RestClient>(relaxed = true)
72-
every { restClient.sendSync(any()) } returns restResponse
73-
74-
val appAttestationChallengeApiClient = AppAttestationChallengeApiClient(
75-
apiHostName = "https://www.example.com",
76-
restClient = restClient
77-
)
73+
val client = createClient(body = null, success = true)
7874

7975
assertThrows(AppAttestationChallengeApiException::class.java) {
80-
appAttestationChallengeApiClient.fetchChallenge(
81-
attestationId = "__ATTESTATION_ID__",
82-
remoteConsumerKey = "__REMOTE_CONSUMER_KEY__"
76+
client.fetchChallenge(
77+
attestationId = TEST_ATTESTATION_ID,
78+
remoteConsumerKey = TEST_REMOTE_CONSUMER_KEY,
8379
)
8480
}
8581
}
8682

8783
@Test
8884
fun appAttestationChallengeApiClient_fetchChallenge_throwsWhenRestResponseIsNotSuccessAndBodyStringIsNull() {
8985

90-
val restResponse = mockk<RestResponse>(relaxed = true)
91-
every { restResponse.asString() } returns null
92-
every { restResponse.isSuccess } returns false
93-
val restClient = mockk<RestClient>(relaxed = true)
94-
every { restClient.sendSync(any()) } returns restResponse
95-
96-
val appAttestationChallengeApiClient = AppAttestationChallengeApiClient(
97-
apiHostName = "https://www.example.com",
98-
restClient = restClient
99-
)
86+
val client = createClient(body = null, success = false)
10087

10188
assertThrows(AppAttestationChallengeApiException::class.java) {
102-
appAttestationChallengeApiClient.fetchChallenge(
103-
attestationId = "__ATTESTATION_ID__",
104-
remoteConsumerKey = "__REMOTE_CONSUMER_KEY__"
89+
client.fetchChallenge(
90+
attestationId = TEST_ATTESTATION_ID,
91+
remoteConsumerKey = TEST_REMOTE_CONSUMER_KEY,
10592
)
10693
}
10794
}
95+
96+
// region Helpers
97+
98+
private fun createClient(
99+
body: String?,
100+
success: Boolean,
101+
): AppAttestationChallengeApiClient {
102+
val restResponse = mockk<RestResponse>(relaxed = true).apply {
103+
every { asString() } returns body
104+
every { isSuccess } returns success
105+
}
106+
val restClient = mockk<RestClient>(relaxed = true).apply {
107+
every { sendSync(any()) } returns restResponse
108+
}
109+
return AppAttestationChallengeApiClient(
110+
apiHostName = TEST_API_HOST_NAME,
111+
restClient = restClient,
112+
)
113+
}
114+
115+
// endregion Helpers
116+
117+
private companion object {
118+
const val TEST_API_HOST_NAME = "https://www.example.com"
119+
const val TEST_ATTESTATION_ID = "__ATTESTATION_ID__"
120+
const val TEST_REMOTE_CONSUMER_KEY = "__REMOTE_CONSUMER_KEY__"
121+
const val TEST_CHALLENGE_VALUE = "__TEST_CHALLENGE_VALUE__"
122+
}
108123
}

0 commit comments

Comments
 (0)