11package com .auth0 .android .provider ;
22
3+ import android .net .Uri ;
4+ import com .auth0 .android .Auth0 ;
35import com .auth0 .android .authentication .AuthenticationException ;
6+ import com .auth0 .android .callback .Callback ;
7+ import com .auth0 .android .request .NetworkingClient ;
8+ import com .auth0 .android .util .Auth0UserAgent ;
9+ import com .auth0 .android .result .Credentials ;
410
11+ import org .junit .After ;
512import org .junit .Assert ;
13+ import org .junit .Before ;
614import org .junit .Test ;
715import org .junit .runner .RunWith ;
16+ import org .mockito .Mock ;
17+ import org .mockito .Mockito ;
18+ import org .mockito .MockitoAnnotations ;
819import org .robolectric .RobolectricTestRunner ;
920
21+ import java .lang .reflect .Method ;
22+ import java .util .Collections ;
23+ import java .util .Map ;
24+
1025
1126@ RunWith (RobolectricTestRunner .class )
1227public class OAuthManagerTest {
1328
29+ @ Mock
30+ private Auth0 mockAccount ;
31+ @ Mock
32+ private Callback <Credentials , AuthenticationException > mockCallback ;
33+ @ Mock
34+ private CustomTabsOptions mockCtOptions ;
35+ @ Mock
36+ private OAuthManagerState mockState ;
37+ @ Mock
38+ private PKCE mockPkce ;
39+ @ Mock
40+ private NetworkingClient mockNetworkingClient ;
41+ @ Mock
42+ private Auth0UserAgent mockUserAgent ;
43+
44+ @ Before
45+ public void setUp () {
46+ MockitoAnnotations .openMocks (this );
47+ Mockito .when (mockAccount .getNetworkingClient ()).thenReturn (mockNetworkingClient );
48+ Mockito .when (mockAccount .getClientId ()).thenReturn ("test-client-id" );
49+ Mockito .when (mockAccount .getDomainUrl ()).thenReturn ("https://test.domain.com/" );
50+ Mockito .when (mockAccount .getAuth0UserAgent ()).thenReturn (mockUserAgent );
51+ Mockito .when (mockUserAgent .getValue ()).thenReturn ("test-user-agent/1.0" );
52+ Mockito .when (mockState .getAuth0 ()).thenReturn (mockAccount );
53+ Mockito .when (mockState .getCtOptions ()).thenReturn (mockCtOptions );
54+ Mockito .when (mockState .getParameters ()).thenReturn (Collections .emptyMap ());
55+ Mockito .when (mockState .getHeaders ()).thenReturn (Collections .emptyMap ());
56+ Mockito .when (mockState .getPkce ()).thenReturn (mockPkce );
57+ Mockito .when (mockState .getIdTokenVerificationIssuer ()).thenReturn ("default-issuer" );
58+ Mockito .when (mockState .getIdTokenVerificationLeeway ()).thenReturn (60 );
59+ }
60+
61+ @ After
62+ public void tearDown () {
63+ }
64+
1465 @ Test
1566 public void shouldHaveValidState () {
1667 OAuthManager .assertValidState ("1234567890" , "1234567890" );
@@ -25,4 +76,80 @@ public void shouldHaveInvalidState() {
2576 public void shouldHaveInvalidStateWhenOneIsNull () {
2677 Assert .assertThrows (AuthenticationException .class , () -> OAuthManager .assertValidState ("0987654321" , null ));
2778 }
79+
80+ @ Test
81+ public void buildAuthorizeUriShouldUseDefaultUrlWhenCustomUrlIsNull () throws Exception {
82+ final String defaultUrl = "https://default.auth0.com/authorize" ;
83+ final Map <String , String > parameters = Collections .singletonMap ("param1" , "value1" );
84+ Mockito .when (mockAccount .getAuthorizeUrl ()).thenReturn (defaultUrl );
85+ OAuthManager manager = new OAuthManager (mockAccount , mockCallback , parameters , mockCtOptions , false , null );
86+ Uri resultUri = callBuildAuthorizeUri (manager );
87+ Assert .assertNotNull (resultUri );
88+ Assert .assertEquals ("https" , resultUri .getScheme ());
89+ Assert .assertEquals ("default.auth0.com" , resultUri .getHost ());
90+ Assert .assertEquals ("/authorize" , resultUri .getPath ());
91+ Assert .assertEquals ("value1" , resultUri .getQueryParameter ("param1" ));
92+ Mockito .verify (mockAccount ).getAuthorizeUrl ();
93+ }
94+
95+ @ Test
96+ public void buildAuthorizeUriShouldUseCustomUrlWhenProvided () throws Exception {
97+ final String defaultUrl = "https://default.auth0.com/authorize" ;
98+ final String customUrl = "https://custom.example.com/custom_auth" ;
99+ final Map <String , String > parameters = Collections .singletonMap ("param1" , "value1" );
100+ Mockito .when (mockAccount .getAuthorizeUrl ()).thenReturn (defaultUrl );
101+ OAuthManager manager = new OAuthManager (mockAccount , mockCallback , parameters , mockCtOptions , false , customUrl );
102+ Uri resultUri = callBuildAuthorizeUri (manager );
103+ Assert .assertNotNull (resultUri );
104+ Assert .assertEquals ("https" , resultUri .getScheme ());
105+ Assert .assertEquals ("custom.example.com" , resultUri .getHost ());
106+ Assert .assertEquals ("/custom_auth" , resultUri .getPath ());
107+ Assert .assertEquals ("value1" , resultUri .getQueryParameter ("param1" ));
108+ Mockito .verify (mockAccount , Mockito .never ()).getAuthorizeUrl ();
109+ }
110+
111+ @ Test
112+ public void managerRestoredFromStateShouldUseRestoredCustomAuthorizeUrl () throws Exception {
113+ final String restoredCustomUrl = "https://restored.com/custom_auth" ;
114+ final String defaultUrl = "https://should-not-be-used.com/authorize" ;
115+
116+ Mockito .when (mockState .getCustomAuthorizeUrl ()).thenReturn (restoredCustomUrl );
117+ Mockito .when (mockAccount .getAuthorizeUrl ()).thenReturn (defaultUrl );
118+
119+ OAuthManager restoredManager = new OAuthManager (
120+ mockState .getAuth0 (), mockCallback , mockState .getParameters (),
121+ mockState .getCtOptions (), false , mockState .getCustomAuthorizeUrl ()
122+ );
123+ Uri resultUri = callBuildAuthorizeUri (restoredManager );
124+ Assert .assertNotNull (resultUri );
125+ Assert .assertEquals ("https" , resultUri .getScheme ());
126+ Assert .assertEquals ("restored.com" , resultUri .getHost ());
127+ Assert .assertEquals ("/custom_auth" , resultUri .getPath ());
128+ Mockito .verify (mockAccount , Mockito .never ()).getAuthorizeUrl ();
129+ }
130+
131+ @ Test
132+ public void managerRestoredFromStateShouldHandleNullCustomAuthorizeUrl () throws Exception {
133+ final String defaultUrl = "https://default.auth0.com/authorize" ;
134+
135+ Mockito .when (mockState .getCustomAuthorizeUrl ()).thenReturn (null );
136+ Mockito .when (mockAccount .getAuthorizeUrl ()).thenReturn (defaultUrl );
137+ OAuthManager restoredManager = new OAuthManager (
138+ mockState .getAuth0 (), mockCallback , mockState .getParameters (),
139+ mockState .getCtOptions (), false , mockState .getCustomAuthorizeUrl ()
140+ );
141+ Uri resultUri = callBuildAuthorizeUri (restoredManager );
142+ Assert .assertNotNull (resultUri );
143+ Assert .assertEquals ("https" , resultUri .getScheme ());
144+ Assert .assertEquals ("default.auth0.com" , resultUri .getHost ());
145+ Assert .assertEquals ("/authorize" , resultUri .getPath ());
146+ Mockito .verify (mockAccount ).getAuthorizeUrl ();
147+ }
148+
149+ private Uri callBuildAuthorizeUri (OAuthManager instance ) throws Exception {
150+ Method method = OAuthManager .class .getDeclaredMethod ("buildAuthorizeUri" );
151+ method .setAccessible (true );
152+ return (Uri ) method .invoke (instance );
153+ }
154+
28155}
0 commit comments