Skip to content

Commit 28faee8

Browse files
committed
Fix flaky JWKS tests by mocking NetworkingClient instead of MockWebServer
1 parent 68910f1 commit 28faee8

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

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

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

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

17421752
@Test

0 commit comments

Comments
 (0)