Skip to content

Commit 1843b00

Browse files
committed
Fix flaky JWKS tests by mocking NetworkingClient instead of MockWebServer
1 parent 1258b26 commit 1843b00

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

auth0/src/test/java/com/auth0/android/provider/WebAuthProviderTest.kt

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,11 +1544,11 @@ public class WebAuthProviderTest {
15441544
public fun shouldFailToResumeLoginWhenRSAKeyIsMissingFromJWKSet() {
15451545
val pkce = Mockito.mock(PKCE::class.java)
15461546
`when`(pkce.codeChallenge).thenReturn("challenge")
1547-
val mockAPI = AuthenticationAPIMockServer()
1548-
mockAPI.willReturnEmptyJsonWebKeys()
1547+
val networkingClient: NetworkingClient = Mockito.spy(DefaultClient())
15491548
val authCallback = mock<Callback<Credentials, AuthenticationException>>()
1550-
val proxyAccount: Auth0 = Auth0.getInstance(JwtTestUtils.EXPECTED_AUDIENCE, mockAPI.domain)
1551-
proxyAccount.networkingClient = SSLTestUtils.testClient
1549+
val proxyAccount =
1550+
Auth0.getInstance(JwtTestUtils.EXPECTED_AUDIENCE, JwtTestUtils.EXPECTED_BASE_DOMAIN)
1551+
proxyAccount.networkingClient = networkingClient
15521552
login(proxyAccount)
15531553
.withState("1234567890")
15541554
.withNonce(JwtTestUtils.EXPECTED_NONCE)
@@ -1585,11 +1585,17 @@ public class WebAuthProviderTest {
15851585
callbackCaptor.firstValue.onSuccess(codeCredentials)
15861586
null
15871587
}.`when`(pkce).getToken(eq("1234"), callbackCaptor.capture())
1588+
// Mock JWKS response with empty keys (no matching RSA key for kid)
1589+
val emptyJwksJson = """{"keys": []}"""
1590+
val jwksInputStream: InputStream = ByteArrayInputStream(emptyJwksJson.toByteArray())
1591+
val jwksResponse = ServerResponse(200, jwksInputStream, emptyMap())
1592+
Mockito.doReturn(jwksResponse).`when`(networkingClient).load(
1593+
eq(proxyAccount.getDomainUrl() + ".well-known/jwks.json"),
1594+
any()
1595+
)
15881596
Assert.assertTrue(resume(intent))
1589-
mockAPI.takeRequest()
15901597
ShadowLooper.idleMainLooper()
1591-
// Use Mockito timeout to handle async JWKS response processing on slower CI environments
1592-
verify(authCallback, Mockito.timeout(5000)).onFailure(authExceptionCaptor.capture())
1598+
verify(authCallback).onFailure(authExceptionCaptor.capture())
15931599
val error = authExceptionCaptor.firstValue
15941600
assertThat(error, `is`(notNullValue()))
15951601
assertThat(
@@ -1603,7 +1609,6 @@ public class WebAuthProviderTest {
16031609
error.cause?.message,
16041610
`is`("Could not find a public key for kid \"key123\"")
16051611
)
1606-
mockAPI.shutdown()
16071612
}
16081613

16091614
@Test
@@ -1679,11 +1684,11 @@ public class WebAuthProviderTest {
16791684
public fun shouldFailToResumeLoginWhenKeyIdIsMissingFromIdTokenHeader() {
16801685
val pkce = Mockito.mock(PKCE::class.java)
16811686
`when`(pkce.codeChallenge).thenReturn("challenge")
1682-
val mockAPI = AuthenticationAPIMockServer()
1683-
mockAPI.willReturnValidJsonWebKeys()
1687+
val networkingClient: NetworkingClient = Mockito.spy(DefaultClient())
16841688
val authCallback = mock<Callback<Credentials, AuthenticationException>>()
1685-
val proxyAccount: Auth0 = Auth0.getInstance(JwtTestUtils.EXPECTED_AUDIENCE, mockAPI.domain)
1686-
proxyAccount.networkingClient = SSLTestUtils.testClient
1689+
val proxyAccount =
1690+
Auth0.getInstance(JwtTestUtils.EXPECTED_AUDIENCE, JwtTestUtils.EXPECTED_BASE_DOMAIN)
1691+
proxyAccount.networkingClient = networkingClient
16871692
login(proxyAccount)
16881693
.withState("1234567890")
16891694
.withNonce("abcdefg")
@@ -1719,11 +1724,17 @@ public class WebAuthProviderTest {
17191724
callbackCaptor.firstValue.onSuccess(codeCredentials)
17201725
null
17211726
}.`when`(pkce).getToken(eq("1234"), callbackCaptor.capture())
1727+
// Mock JWKS response with valid keys
1728+
val encoded = Files.readAllBytes(Paths.get("src/test/resources/rsa_jwks.json"))
1729+
val jwksInputStream: InputStream = ByteArrayInputStream(encoded)
1730+
val jwksResponse = ServerResponse(200, jwksInputStream, emptyMap())
1731+
Mockito.doReturn(jwksResponse).`when`(networkingClient).load(
1732+
eq(proxyAccount.getDomainUrl() + ".well-known/jwks.json"),
1733+
any()
1734+
)
17221735
Assert.assertTrue(resume(intent))
1723-
mockAPI.takeRequest()
17241736
ShadowLooper.idleMainLooper()
1725-
// Use Mockito timeout to handle async JWKS response processing on slower CI environments
1726-
verify(authCallback, Mockito.timeout(5000)).onFailure(authExceptionCaptor.capture())
1737+
verify(authCallback).onFailure(authExceptionCaptor.capture())
17271738
val error = authExceptionCaptor.firstValue
17281739
assertThat(error, `is`(notNullValue()))
17291740
assertThat(
@@ -1737,7 +1748,6 @@ public class WebAuthProviderTest {
17371748
error.cause?.message,
17381749
`is`("Could not find a public key for kid \"null\"")
17391750
)
1740-
mockAPI.shutdown()
17411751
}
17421752

17431753
@Test

0 commit comments

Comments
 (0)