Skip to content

Commit 64dee5e

Browse files
committed
Added test cases for call token exchange
1 parent abdeee8 commit 64dee5e

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

auth0/src/main/java/com/auth0/android/authentication/AuthenticationAPIClient.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe
714714
* Example usage:
715715
*
716716
* ```
717-
* client.callTokenExchange("{subject token type}", "{subject token}")
717+
* client.customTokenExchange("{subject token type}", "{subject token}")
718718
* .validateClaims() //mandatory
719719
* .setScope("{scope}")
720720
* .setAudience("{audience}")
@@ -728,7 +728,7 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe
728728
* @param subjectToken the subject token, typically obtained through the Identity Provider's SDK
729729
* @return a request to configure and start that will yield [Credentials]
730730
*/
731-
public fun callTokenExchange(
731+
public fun customTokenExchange(
732732
subjectTokenType: String,
733733
subjectToken: String,
734734
): AuthenticationRequest {

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,90 @@ public class AuthenticationAPIClientTest {
22722272
assertThat(body, Matchers.hasEntry("token", "refreshToken"))
22732273
}
22742274

2275+
@Test
2276+
public fun shouldCustomTokenExchange() {
2277+
mockAPI.willReturnSuccessfulLogin()
2278+
val callback = MockAuthenticationCallback<Credentials>()
2279+
client.customTokenExchange( "subject-token-type","subject-token")
2280+
.setScope("openid profile email")
2281+
.start(callback)
2282+
ShadowLooper.idleMainLooper()
2283+
val request = mockAPI.takeRequest()
2284+
assertThat(
2285+
request.getHeader("Accept-Language"), Matchers.`is`(
2286+
defaultLocale
2287+
)
2288+
)
2289+
assertThat(request.path, Matchers.equalTo("/oauth/token"))
2290+
val body = bodyFromRequest<String>(request)
2291+
assertThat(body, Matchers.hasEntry("client_id", CLIENT_ID))
2292+
assertThat(
2293+
body,
2294+
Matchers.hasEntry("grant_type", ParameterBuilder.GRANT_TYPE_TOKEN_EXCHANGE)
2295+
)
2296+
assertThat(body, Matchers.hasEntry("subject_token", "subject-token"))
2297+
assertThat(body, Matchers.hasEntry("subject_token_type", "subject-token-type"))
2298+
assertThat(body, Matchers.hasEntry("scope", "openid profile email"))
2299+
assertThat(
2300+
callback, AuthenticationCallbackMatcher.hasPayloadOfType(
2301+
Credentials::class.java
2302+
)
2303+
)
2304+
}
2305+
2306+
@Test
2307+
public fun shouldCustomTokenExchangeSync() {
2308+
mockAPI.willReturnSuccessfulLogin()
2309+
val credentials = client
2310+
.customTokenExchange("subject-token-type", "subject-token")
2311+
.setScope("openid profile email")
2312+
.execute()
2313+
val request = mockAPI.takeRequest()
2314+
assertThat(
2315+
request.getHeader("Accept-Language"), Matchers.`is`(
2316+
defaultLocale
2317+
)
2318+
)
2319+
assertThat(request.path, Matchers.equalTo("/oauth/token"))
2320+
val body = bodyFromRequest<String>(request)
2321+
assertThat(body, Matchers.hasEntry("client_id", CLIENT_ID))
2322+
assertThat(
2323+
body,
2324+
Matchers.hasEntry("grant_type", ParameterBuilder.GRANT_TYPE_TOKEN_EXCHANGE)
2325+
)
2326+
assertThat(body, Matchers.hasEntry("subject_token", "subject-token"))
2327+
assertThat(body, Matchers.hasEntry("subject_token_type", "subject-token-type"))
2328+
assertThat(body, Matchers.hasEntry("scope", "openid profile email"))
2329+
assertThat(credentials, Matchers.`is`(Matchers.notNullValue()))
2330+
}
2331+
2332+
@Test
2333+
@ExperimentalCoroutinesApi
2334+
public fun shouldAwaitCustomTokenExchnage(): Unit = runTest {
2335+
mockAPI.willReturnSuccessfulLogin()
2336+
val credentials = client
2337+
.customTokenExchange("subject-token-type", "subject-token")
2338+
.setScope("openid profile email")
2339+
.await()
2340+
val request = mockAPI.takeRequest()
2341+
assertThat(
2342+
request.getHeader("Accept-Language"), Matchers.`is`(
2343+
defaultLocale
2344+
)
2345+
)
2346+
assertThat(request.path, Matchers.equalTo("/oauth/token"))
2347+
val body = bodyFromRequest<String>(request)
2348+
assertThat(body, Matchers.hasEntry("client_id", CLIENT_ID))
2349+
assertThat(
2350+
body,
2351+
Matchers.hasEntry("grant_type", ParameterBuilder.GRANT_TYPE_TOKEN_EXCHANGE)
2352+
)
2353+
assertThat(body, Matchers.hasEntry("subject_token", "subject-token"))
2354+
assertThat(body, Matchers.hasEntry("subject_token_type", "subject-token-type"))
2355+
assertThat(body, Matchers.hasEntry("scope", "openid profile email"))
2356+
assertThat(credentials, Matchers.`is`(Matchers.notNullValue()))
2357+
}
2358+
22752359
@Test
22762360
public fun shouldRenewAuthWithOAuthToken() {
22772361
val auth0 = auth0

0 commit comments

Comments
 (0)