16
16
import org .robolectric .RuntimeEnvironment ;
17
17
18
18
import java .io .IOException ;
19
+ import java .net .UnknownHostException ;
19
20
20
21
import static org .junit .Assert .assertEquals ;
21
22
import static org .junit .Assert .assertNotNull ;
22
23
import static org .junit .Assert .assertNull ;
23
24
import static org .junit .Assert .fail ;
24
25
import static org .mockito .ArgumentMatchers .any ;
25
- import static org .mockito .ArgumentMatchers .anyInt ;
26
26
import static org .mockito .ArgumentMatchers .anyString ;
27
27
import static org .mockito .ArgumentMatchers .argThat ;
28
+ import static org .mockito .ArgumentMatchers .eq ;
28
29
import static org .mockito .Mockito .mock ;
29
30
import static org .mockito .Mockito .times ;
30
31
import static org .mockito .Mockito .verify ;
@@ -41,7 +42,7 @@ public class OAuthAuthenticatorTest {
41
42
private AccountAuthenticatorResponse response ;
42
43
43
44
@ Before
44
- public void setUp () throws Exception {
45
+ public void setUp () {
45
46
am = AccountManager .get (RuntimeEnvironment .application );
46
47
47
48
response = mock (AccountAuthenticatorResponse .class );
@@ -51,9 +52,7 @@ public void setUp() throws Exception {
51
52
}
52
53
53
54
@ Test
54
- public void accessTokenReturnedImmediately ()
55
- throws NetworkErrorException , AuthenticatorException , OperationCanceledException ,
56
- IOException {
55
+ public void accessTokenReturnedImmediately () {
57
56
am .addAccountExplicitly (account , null , null );
58
57
final String accessToken = "access1" ;
59
58
am .setAuthToken (account , tokenType , accessToken );
@@ -67,23 +66,21 @@ public void accessTokenReturnedImmediately()
67
66
}
68
67
69
68
@ Test
70
- public void errorOnInvalidRefreshToken ()
71
- throws NetworkErrorException , AuthenticatorException , OperationCanceledException ,
72
- IOException {
69
+ public void errorOnInvalidRefreshToken () throws IOException , TokenRefreshError {
73
70
am .addAccountExplicitly (account , null , null );
74
71
am .setPassword (account , "invalid" );
75
72
76
73
withServiceResponse (
77
74
callback -> {
78
- throw new RuntimeException ();
75
+ throw new UnknownHostException ();
79
76
});
80
77
81
78
// when
82
79
Bundle result = getAuthTokenWithResponse ();
83
80
84
81
// then
85
82
assertNull (result );
86
- verify (response ).onError (anyInt ( ), any ());
83
+ verify (response ).onError (eq ( AccountManager . ERROR_CODE_NETWORK_ERROR ), any ());
87
84
}
88
85
89
86
@ Test
@@ -95,8 +92,8 @@ public void noLoginIntentProvided() throws NetworkErrorException {
95
92
96
93
@ Test
97
94
public void accessTokenReturnedAfterRefresh ()
98
- throws NetworkErrorException , AuthenticatorException , OperationCanceledException ,
99
- IOException {
95
+ throws AuthenticatorException , OperationCanceledException , IOException ,
96
+ TokenRefreshError {
100
97
am .addAccountExplicitly (account , null , null );
101
98
final String accessToken = "access1" ;
102
99
am .setPassword (account , "refresh1" );
@@ -113,9 +110,7 @@ public void accessTokenReturnedAfterRefresh()
113
110
}
114
111
115
112
@ Test
116
- public void multipleRequestsTriggerASingleRefresh ()
117
- throws NetworkErrorException , AuthenticatorException , OperationCanceledException ,
118
- IOException {
113
+ public void multipleRequestsTriggerASingleRefresh () throws IOException , TokenRefreshError {
119
114
am .addAccountExplicitly (account , null , null );
120
115
final String accessToken = "access1" ;
121
116
am .setPassword (account , "refresh1" );
@@ -148,9 +143,7 @@ public void multipleRequestsTriggerASingleRefresh()
148
143
}
149
144
150
145
@ Test
151
- public void multipleUserRequestsTriggerRunConcurrently ()
152
- throws NetworkErrorException , AuthenticatorException , OperationCanceledException ,
153
- IOException {
146
+ public void multipleUserRequestsTriggerRunConcurrently () throws IOException , TokenRefreshError {
154
147
155
148
// given some complicated setup... simulate "concurrency" :/
156
149
Account [] users =
@@ -204,11 +197,34 @@ public void multipleUserRequestsTriggerRunConcurrently()
204
197
}
205
198
}
206
199
207
- private void withServiceResponse (Function0 <TokenPair > action ) throws IOException {
200
+ @ Test
201
+ public void returnCustomError () throws IOException , TokenRefreshError {
202
+ am .addAccountExplicitly (account , null , null );
203
+ am .setPassword (account , "invalid" );
204
+
205
+ final int errCode = AccountManager .ERROR_CODE_BAD_AUTHENTICATION ;
206
+ final String errMessage = "unauthorized" ;
207
+
208
+ withServiceResponse (
209
+ callback -> {
210
+ throw new TokenRefreshError (errCode , errMessage );
211
+ });
212
+
213
+ // when
214
+ Bundle result = getAuthTokenWithResponse ();
215
+
216
+ // then
217
+ assertNull (result );
218
+ verify (response ).onError (errCode , errMessage );
219
+ }
220
+
221
+ private void withServiceResponse (Function0 <TokenPair > action )
222
+ throws TokenRefreshError , IOException {
208
223
withServiceResponse ((obj1 ) -> action .run ());
209
224
}
210
225
211
- private void withServiceResponse (Function1 <String , TokenPair > action ) throws IOException {
226
+ private void withServiceResponse (Function1 <String , TokenPair > action )
227
+ throws TokenRefreshError , IOException {
212
228
Mockito .doAnswer (
213
229
invocation -> {
214
230
String refreshToken = (String ) invocation .getArguments ()[0 ];
0 commit comments