Skip to content

Commit 71bc2d3

Browse files
committed
Added test cases to check that exception is thrown in the scenario when intent extras are null
1 parent e8a6f09 commit 71bc2d3

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

auth0/src/main/java/com/auth0/android/provider/WebAuthProvider.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public object WebAuthProvider {
8282
}
8383

8484
internal fun failure(exception: AuthenticationException) {
85+
if (managerInstance == null) {
86+
Log.w(TAG, "There is no previous instance of this provider.")
87+
return
88+
}
8589
managerInstance!!.failure(exception)
8690
}
8791

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package com.auth0.android.provider
22

33
import android.content.Context
44
import android.content.Intent
5+
import com.auth0.android.authentication.AuthenticationException
56

67
public class AuthenticationActivityMock : AuthenticationActivity() {
78
internal var customTabsController: CustomTabsController? = null
89
public var deliveredIntent: Intent? = null
910
private set
11+
public var deliveredException: AuthenticationException? = null
12+
private set
1013

1114
override fun createCustomTabsController(
1215
context: Context,
@@ -19,4 +22,9 @@ public class AuthenticationActivityMock : AuthenticationActivity() {
1922
deliveredIntent = result
2023
super.deliverAuthenticationResult(result)
2124
}
25+
26+
override fun deliverAuthenticationFailure(ex: AuthenticationException) {
27+
deliveredException = ex
28+
super.deliverAuthenticationFailure(ex)
29+
}
2230
}

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

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import androidx.test.espresso.intent.matcher.IntentMatchers
99
import com.auth0.android.authentication.AuthenticationException
1010
import com.auth0.android.callback.RunnableTask
1111
import com.auth0.android.provider.AuthenticationActivity
12+
import com.auth0.android.provider.AuthenticationActivity.Companion.EXTRA_AUTHORIZE_URI
13+
import com.auth0.android.provider.AuthenticationActivity.Companion.EXTRA_CT_OPTIONS
14+
import com.auth0.android.provider.AuthenticationActivity.Companion.EXTRA_LAUNCH_AS_TWA
1215
import com.auth0.android.provider.CustomTabsOptions
1316
import com.nhaarman.mockitokotlin2.any
1417
import org.hamcrest.CoreMatchers
@@ -92,7 +95,12 @@ public class AuthenticationActivityTest {
9295
createActivity(intentCaptor.value)
9396
activityController.create().start().resume()
9497
Mockito.verify(customTabsController).bindService()
95-
Mockito.verify(customTabsController).launchUri(uriCaptor.capture(), launchAsTwaCaptor.capture(), any(), failureCallbackCaptor.capture())
98+
Mockito.verify(customTabsController).launchUri(
99+
uriCaptor.capture(),
100+
launchAsTwaCaptor.capture(),
101+
any(),
102+
failureCallbackCaptor.capture()
103+
)
96104
MatcherAssert.assertThat(uriCaptor.value, Is.`is`(Matchers.notNullValue()))
97105
MatcherAssert.assertThat(uriCaptor.value, Is.`is`(uri))
98106
MatcherAssert.assertThat(activity.deliveredIntent, Is.`is`(Matchers.nullValue()))
@@ -123,7 +131,12 @@ public class AuthenticationActivityTest {
123131
createActivity(intentCaptor.value)
124132
activityController.create().start().resume()
125133
Mockito.verify(customTabsController).bindService()
126-
Mockito.verify(customTabsController).launchUri(uriCaptor.capture(), launchAsTwaCaptor.capture(), any(), failureCallbackCaptor.capture())
134+
Mockito.verify(customTabsController).launchUri(
135+
uriCaptor.capture(),
136+
launchAsTwaCaptor.capture(),
137+
any(),
138+
failureCallbackCaptor.capture()
139+
)
127140
MatcherAssert.assertThat(uriCaptor.value, Is.`is`(Matchers.notNullValue()))
128141
MatcherAssert.assertThat(uriCaptor.value, Is.`is`(uri))
129142
MatcherAssert.assertThat(activity.deliveredIntent, Is.`is`(Matchers.nullValue()))
@@ -154,7 +167,12 @@ public class AuthenticationActivityTest {
154167
createActivity(intentCaptor.value)
155168
activityController.create().start().resume()
156169
Mockito.verify(customTabsController).bindService()
157-
Mockito.verify(customTabsController).launchUri(uriCaptor.capture(), launchAsTwaCaptor.capture(), any(), failureCallbackCaptor.capture())
170+
Mockito.verify(customTabsController).launchUri(
171+
uriCaptor.capture(),
172+
launchAsTwaCaptor.capture(),
173+
any(),
174+
failureCallbackCaptor.capture()
175+
)
158176
MatcherAssert.assertThat(uriCaptor.value, Is.`is`(Matchers.notNullValue()))
159177
MatcherAssert.assertThat(uriCaptor.value, Is.`is`(uri))
160178
MatcherAssert.assertThat(launchAsTwaCaptor.value, Is.`is`(Matchers.notNullValue()))
@@ -184,7 +202,12 @@ public class AuthenticationActivityTest {
184202
createActivity(intentCaptor.value)
185203
activityController.create().start().resume()
186204
Mockito.verify(customTabsController).bindService()
187-
Mockito.verify(customTabsController).launchUri(uriCaptor.capture(), launchAsTwaCaptor.capture(), any(), failureCallbackCaptor.capture())
205+
Mockito.verify(customTabsController).launchUri(
206+
uriCaptor.capture(),
207+
launchAsTwaCaptor.capture(),
208+
any(),
209+
failureCallbackCaptor.capture()
210+
)
188211
MatcherAssert.assertThat(uriCaptor.value, Is.`is`(Matchers.notNullValue()))
189212
MatcherAssert.assertThat(uriCaptor.value, Is.`is`(uri))
190213
MatcherAssert.assertThat(launchAsTwaCaptor.value, Is.`is`(Matchers.notNullValue()))
@@ -243,6 +266,47 @@ public class AuthenticationActivityTest {
243266
MatcherAssert.assertThat(controller, Is.`is`(Matchers.notNullValue()))
244267
}
245268

269+
270+
@Test
271+
public fun shouldThrowExceptionIfAuthorizeUriIsNullInIntent() {
272+
AuthenticationActivity.authenticateUsingBrowser(
273+
callerActivity,
274+
uri,
275+
false,
276+
customTabsOptions
277+
)
278+
Mockito.verify(callerActivity).startActivity(intentCaptor.capture())
279+
createActivity(intentCaptor.value)
280+
activity.intent = Intent().apply {
281+
putExtra(EXTRA_LAUNCH_AS_TWA, false)
282+
putExtra(EXTRA_CT_OPTIONS, customTabsOptions)
283+
}
284+
activityController.create().start().resume()
285+
MatcherAssert.assertThat(activity.deliveredException, Is.`is`(Matchers.notNullValue()))
286+
MatcherAssert.assertThat(activity.isFinishing, Is.`is`(true))
287+
activityController.destroy()
288+
}
289+
290+
@Test
291+
public fun shouldThrowExceptionIfCustomTabsIsNullInIntent() {
292+
AuthenticationActivity.authenticateUsingBrowser(
293+
callerActivity,
294+
uri,
295+
false,
296+
customTabsOptions
297+
)
298+
Mockito.verify(callerActivity).startActivity(intentCaptor.capture())
299+
createActivity(intentCaptor.value)
300+
activity.intent = Intent().apply {
301+
putExtra(EXTRA_LAUNCH_AS_TWA, false)
302+
putExtra(EXTRA_AUTHORIZE_URI, uri)
303+
}
304+
activityController.create().start().resume()
305+
MatcherAssert.assertThat(activity.deliveredException, Is.`is`(Matchers.notNullValue()))
306+
MatcherAssert.assertThat(activity.isFinishing, Is.`is`(true))
307+
activityController.destroy()
308+
}
309+
246310
private fun recreateAndCallNewIntent(data: Intent) {
247311
val outState = Bundle()
248312
activityController.saveInstanceState(outState)

0 commit comments

Comments
 (0)