Skip to content

Commit f14c30f

Browse files
committed
Exisiting test classess updated
1 parent 75a4241 commit f14c30f

File tree

6 files changed

+119
-9
lines changed

6 files changed

+119
-9
lines changed

auth0/src/main/java/com/auth0/android/dpop/DPoPProvider.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public object DPoPProvider {
6767
* @throws DPoPException if there is an error generating the DPoP proof or accessing the key pair.
6868
*/
6969
@Throws(DPoPException::class)
70+
@JvmStatic
7071
public fun generateProof(
7172
httpUrl: String,
7273
httpMethod: String,
@@ -133,6 +134,7 @@ public object DPoPProvider {
133134
* @throws DPoPException if there is an error deleting the key pair.
134135
*/
135136
@Throws(DPoPException::class)
137+
@JvmStatic
136138
public fun clearKeyPair() {
137139
keyStore.deleteKeyPair()
138140
}
@@ -155,6 +157,7 @@ public object DPoPProvider {
155157
* @throws DPoPException if there is an error accessing the key pair.
156158
*/
157159
@Throws(DPoPException::class)
160+
@JvmStatic
158161
public fun getPublicKeyJWK(): String? {
159162
if (!keyStore.hasKeyPair()) {
160163
Log.e(TAG, "getPublicKeyJWK: Key pair is not present to generate JWK")
@@ -188,6 +191,7 @@ public object DPoPProvider {
188191
* @throws DPoPException if there is an error generating the key pair or accessing the keystore.
189192
*/
190193
@Throws(DPoPException::class)
194+
@JvmStatic
191195
public fun generateKeyPair(context: Context) {
192196
if (keyStore.hasKeyPair()) {
193197
return
@@ -227,6 +231,7 @@ public object DPoPProvider {
227231
* @throws DPoPException if there is an error generating the DPoP proof or accessing the key pair
228232
*/
229233
@Throws(DPoPException::class)
234+
@JvmStatic
230235
public fun getHeaderData(
231236
httpMethod: String,
232237
httpUrl: String,
@@ -255,6 +260,7 @@ public object DPoPProvider {
255260
* @param response The HTTP response to check for nonce requirement.
256261
* @return True if the response indicates that a nonce is required, false otherwise.
257262
*/
263+
@JvmStatic
258264
public fun isNonceRequiredError(response: Response): Boolean {
259265
return (response.code == 400 && response.getErrorBody().errorCode == NONCE_REQUIRED_ERROR) ||
260266
(response.code == 401 && isResourceServerNonceError(response))
@@ -275,6 +281,7 @@ public object DPoPProvider {
275281
*
276282
* @param response The HTTP response containing the nonce header.
277283
*/
284+
@JvmStatic
278285
public fun storeNonce(response: Response) {
279286
auth0Nonce = response.headers[NONCE_HEADER]
280287
}

auth0/src/test/java/com/auth0/android/authentication/AuthenticationAPIClientTest.kt

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import android.content.Context
44
import android.content.res.Resources
55
import com.auth0.android.Auth0
66
import com.auth0.android.authentication.ParameterBuilder.Companion.newBuilder
7+
import com.auth0.android.dpop.DPoPKeyStore
8+
import com.auth0.android.dpop.DPoPProvider
9+
import com.auth0.android.dpop.FakeECPrivateKey
10+
import com.auth0.android.dpop.FakeECPublicKey
711
import com.auth0.android.provider.JwtTestUtils
812
import com.auth0.android.request.HttpMethod
913
import com.auth0.android.request.NetworkingClient
@@ -44,6 +48,7 @@ import org.junit.After
4448
import org.junit.Before
4549
import org.junit.Test
4650
import org.junit.runner.RunWith
51+
import org.mockito.Mockito.`when`
4752
import org.robolectric.RobolectricTestRunner
4853
import org.robolectric.annotation.Config
4954
import org.robolectric.shadows.ShadowLooper
@@ -59,13 +64,16 @@ public class AuthenticationAPIClientTest {
5964
private lateinit var client: AuthenticationAPIClient
6065
private lateinit var gson: Gson
6166
private lateinit var mockAPI: AuthenticationAPIMockServer
67+
private lateinit var mockKeyStore: DPoPKeyStore
6268

6369
@Before
6470
public fun setUp() {
6571
mockAPI = AuthenticationAPIMockServer()
72+
mockKeyStore = mock()
6673
val auth0 = auth0
6774
client = AuthenticationAPIClient(auth0)
6875
gson = GsonBuilder().serializeNulls().create()
76+
DPoPProvider.keyStore = mockKeyStore
6977
}
7078

7179
@After
@@ -193,8 +201,10 @@ public class AuthenticationAPIClientTest {
193201
val callback = MockAuthenticationCallback<Credentials>()
194202
val auth0 = auth0
195203
val client = AuthenticationAPIClient(auth0)
196-
client.signinWithPasskey("auth-session", mock<PublicKeyCredentials>(), MY_CONNECTION,
197-
"testOrganisation")
204+
client.signinWithPasskey(
205+
"auth-session", mock<PublicKeyCredentials>(), MY_CONNECTION,
206+
"testOrganisation"
207+
)
198208
.start(callback)
199209
ShadowLooper.idleMainLooper()
200210
assertThat(
@@ -592,7 +602,7 @@ public class AuthenticationAPIClientTest {
592602
public fun shouldFetchUserInfo() {
593603
mockAPI.willReturnUserInfo()
594604
val callback = MockAuthenticationCallback<UserProfile>()
595-
client.userInfo("ACCESS_TOKEN","Bearer")
605+
client.userInfo("ACCESS_TOKEN", "Bearer")
596606
.start(callback)
597607
ShadowLooper.idleMainLooper()
598608
assertThat(
@@ -617,7 +627,7 @@ public class AuthenticationAPIClientTest {
617627
public fun shouldFetchUserInfoSync() {
618628
mockAPI.willReturnUserInfo()
619629
val profile = client
620-
.userInfo("ACCESS_TOKEN","Bearer")
630+
.userInfo("ACCESS_TOKEN", "Bearer")
621631
.execute()
622632
assertThat(profile, Matchers.`is`(Matchers.notNullValue()))
623633
val request = mockAPI.takeRequest()
@@ -638,7 +648,7 @@ public class AuthenticationAPIClientTest {
638648
public fun shouldAwaitFetchUserInfo(): Unit = runTest {
639649
mockAPI.willReturnUserInfo()
640650
val profile = client
641-
.userInfo("ACCESS_TOKEN","Bearer")
651+
.userInfo("ACCESS_TOKEN", "Bearer")
642652
.await()
643653
assertThat(profile, Matchers.`is`(Matchers.notNullValue()))
644654
val request = mockAPI.takeRequest()
@@ -2470,6 +2480,40 @@ public class AuthenticationAPIClientTest {
24702480
)
24712481
}
24722482

2483+
@Test
2484+
public fun shouldRenewAuthWithDpopHeaderIfDpopEnabled() {
2485+
`when`(mockKeyStore.hasKeyPair()).thenReturn(true)
2486+
`when`(mockKeyStore.getKeyPair()).thenReturn(Pair(FakeECPrivateKey(), FakeECPublicKey()))
2487+
val auth0 = auth0
2488+
val client = AuthenticationAPIClient(auth0)
2489+
mockAPI.willReturnSuccessfulLogin()
2490+
val callback = MockAuthenticationCallback<Credentials>()
2491+
client.renewAuth("refreshToken")
2492+
.start(callback)
2493+
ShadowLooper.idleMainLooper()
2494+
val request = mockAPI.takeRequest()
2495+
assertThat(
2496+
request.getHeader("Accept-Language"), Matchers.`is`(
2497+
defaultLocale
2498+
)
2499+
)
2500+
assertThat(
2501+
request.getHeader("DPoP"),
2502+
Matchers.notNullValue()
2503+
)
2504+
assertThat(request.path, Matchers.equalTo("/oauth/token"))
2505+
val body = bodyFromRequest<String>(request)
2506+
assertThat(body, Matchers.not(Matchers.hasKey("scope")))
2507+
assertThat(body, Matchers.hasEntry("client_id", CLIENT_ID))
2508+
assertThat(body, Matchers.hasEntry("refresh_token", "refreshToken"))
2509+
assertThat(body, Matchers.hasEntry("grant_type", "refresh_token"))
2510+
assertThat(
2511+
callback, AuthenticationCallbackMatcher.hasPayloadOfType(
2512+
Credentials::class.java
2513+
)
2514+
)
2515+
}
2516+
24732517
@Test
24742518
public fun shouldRenewAuthWithOAuthTokenSync() {
24752519
val auth0 = auth0
@@ -2566,8 +2610,9 @@ public class AuthenticationAPIClientTest {
25662610
val auth0 = auth0
25672611
val client = AuthenticationAPIClient(auth0)
25682612
mockAPI.willReturnSuccessfulLogin()
2569-
val credentials = client.renewAuth(refreshToken = "refreshToken", scope = "openid read:data")
2570-
.execute()
2613+
val credentials =
2614+
client.renewAuth(refreshToken = "refreshToken", scope = "openid read:data")
2615+
.execute()
25712616
val request = mockAPI.takeRequest()
25722617
assertThat(
25732618
request.getHeader("Accept-Language"), Matchers.`is`(

auth0/src/test/java/com/auth0/android/dpop/DPoPProviderTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class DPoPProviderTest {
3737
private val testHttpMethod = "POST"
3838
private val testAccessToken = "test-access-token"
3939
private val testNonce = "test-nonce"
40-
private val fakePrivateKey = FakeEcPrivateKey()
40+
private val fakePrivateKey = FakeECPrivateKey()
4141
private val fakePublicKey = FakeECPublicKey()
4242
private val testEncodedAccessToken = "WXSA1LYsphIZPxnnP-TMOtF_C_nPwWp8v0tQZBMcSAU"
4343
private val testPublicJwkHash = "KQ-r0YQMCm0yVnGippcsZK4zO7oGIjOkNRbvILjjBAo"

auth0/src/test/java/com/auth0/android/dpop/Fakes.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import java.security.spec.ECPoint
1111
/**
1212
* Fake Private key used for testing DPoP
1313
*/
14-
public class FakeEcPrivateKey : ECPrivateKey {
14+
public class FakeECPrivateKey : ECPrivateKey {
1515

1616
private companion object {
1717
private val S =

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.auth0.android.provider
22

33
import android.app.Activity
4+
import android.content.Context
45
import android.content.Intent
56
import android.net.Uri
67
import android.os.Parcelable
@@ -11,6 +12,7 @@ import com.auth0.android.authentication.AuthenticationException
1112
import com.auth0.android.callback.Callback
1213
import com.auth0.android.dpop.DPoPKeyStore
1314
import com.auth0.android.dpop.DPoPProvider
15+
import com.auth0.android.dpop.FakeECPublicKey
1416
import com.auth0.android.provider.WebAuthProvider.login
1517
import com.auth0.android.provider.WebAuthProvider.logout
1618
import com.auth0.android.provider.WebAuthProvider.resume
@@ -317,6 +319,52 @@ public class WebAuthProviderTest {
317319
)
318320
}
319321

322+
//jwk
323+
324+
@Test
325+
public fun enablingDPoPWillGenerateNEwKEyPairIfOneDoesNotExist() {
326+
`when`(mockKeyStore.hasKeyPair()).thenReturn(false)
327+
val context: Context = mock()
328+
WebAuthProvider.enableDPoP(context)
329+
login(account)
330+
.start(activity, callback)
331+
verify(mockKeyStore).generateKeyPair(context)
332+
}
333+
334+
@Test
335+
public fun shouldNotHaveDpopJwkOnLoginIfDPoPIsDisabled() {
336+
login(account)
337+
.start(activity, callback)
338+
verify(activity).startActivity(intentCaptor.capture())
339+
val uri =
340+
intentCaptor.firstValue.getParcelableExtra<Uri>(AuthenticationActivity.EXTRA_AUTHORIZE_URI)
341+
assertThat(uri, `is`(notNullValue()))
342+
assertThat(
343+
uri,
344+
not(
345+
UriMatchers.hasParamWithName("dpop_jkt")
346+
)
347+
)
348+
}
349+
350+
@Test
351+
public fun shouldNotHaveDpopJwkOnLoginIfDPoPIsEnabled() {
352+
`when`(mockKeyStore.hasKeyPair()).thenReturn(true)
353+
`when`(mockKeyStore.getKeyPair()).thenReturn(Pair(mock(), FakeECPublicKey()))
354+
355+
WebAuthProvider.enableDPoP(mock())
356+
login(account)
357+
.start(activity, callback)
358+
verify(activity).startActivity(intentCaptor.capture())
359+
val uri =
360+
intentCaptor.firstValue.getParcelableExtra<Uri>(AuthenticationActivity.EXTRA_AUTHORIZE_URI)
361+
assertThat(uri, `is`(notNullValue()))
362+
assertThat(
363+
uri,
364+
UriMatchers.hasParamWithValue("dpop_jkt", "KQ-r0YQMCm0yVnGippcsZK4zO7oGIjOkNRbvILjjBAo")
365+
)
366+
}
367+
320368
//scope
321369
@Test
322370
public fun shouldHaveDefaultScopeOnLogin() {

auth0/src/test/java/com/auth0/android/request/DefaultClientTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ public class DefaultClientTest {
8282

8383
@Test
8484
public fun shouldHaveLoggingDisabledByDefault() {
85+
val netClient = DefaultClient(enableLogging = false)
86+
assertThat(DefaultClient().okHttpClient.interceptors, hasSize(1))
87+
val interceptor: Interceptor = netClient.okHttpClient.interceptors[0]
88+
assert(
89+
interceptor is RetryInterceptor,
90+
)
91+
}
92+
93+
@Test
94+
public fun shouldHaveRetryInterceptorEnabled() {
8595
assertThat(DefaultClient().okHttpClient.interceptors, hasSize(1))
8696
}
8797

0 commit comments

Comments
 (0)