16
16
17
17
package com .google .firebase .auth ;
18
18
19
+ import static org .hamcrest .core .IsInstanceOf .instanceOf ;
19
20
import static org .junit .Assert .assertEquals ;
20
21
import static org .junit .Assert .assertFalse ;
21
22
import static org .junit .Assert .assertNotNull ;
22
23
import static org .junit .Assert .assertNull ;
24
+ import static org .junit .Assert .assertThat ;
23
25
import static org .junit .Assert .assertTrue ;
24
26
import static org .junit .Assert .fail ;
25
27
@@ -74,6 +76,8 @@ public class FirebaseUserManagerTest {
74
76
.build ();
75
77
private static final Map <String , Object > ACTION_CODE_SETTINGS_MAP =
76
78
ACTION_CODE_SETTINGS .getProperties ();
79
+ private static final String TENANTS_BASE_URL =
80
+ "https://identitytoolkit.googleapis.com/v2/projects/test-project-id/tenants" ;
77
81
78
82
@ After
79
83
public void tearDown () {
@@ -114,7 +118,7 @@ public void testGetUserWithNotFoundError() throws Exception {
114
118
FirebaseAuth .getInstance ().getUserAsync ("testuser" ).get ();
115
119
fail ("No error thrown for invalid response" );
116
120
} catch (ExecutionException e ) {
117
- assertTrue (e .getCause () instanceof FirebaseAuthException );
121
+ assertThat (e .getCause (), instanceOf ( FirebaseAuthException . class ) );
118
122
FirebaseAuthException authException = (FirebaseAuthException ) e .getCause ();
119
123
assertEquals (FirebaseUserManager .USER_NOT_FOUND_ERROR , authException .getErrorCode ());
120
124
}
@@ -137,7 +141,7 @@ public void testGetUserByEmailWithNotFoundError() throws Exception {
137
141
FirebaseAuth .getInstance ().getUserByEmailAsync ("testuser@example.com" ).get ();
138
142
fail ("No error thrown for invalid response" );
139
143
} catch (ExecutionException e ) {
140
- assertTrue (e .getCause () instanceof FirebaseAuthException );
144
+ assertThat (e .getCause (), instanceOf ( FirebaseAuthException . class ) );
141
145
FirebaseAuthException authException = (FirebaseAuthException ) e .getCause ();
142
146
assertEquals (FirebaseUserManager .USER_NOT_FOUND_ERROR , authException .getErrorCode ());
143
147
}
@@ -160,7 +164,7 @@ public void testGetUserByPhoneNumberWithNotFoundError() throws Exception {
160
164
FirebaseAuth .getInstance ().getUserByPhoneNumberAsync ("+1234567890" ).get ();
161
165
fail ("No error thrown for invalid response" );
162
166
} catch (ExecutionException e ) {
163
- assertTrue (e .getCause () instanceof FirebaseAuthException );
167
+ assertThat (e .getCause (), instanceOf ( FirebaseAuthException . class ) );
164
168
FirebaseAuthException authException = (FirebaseAuthException ) e .getCause ();
165
169
assertEquals (FirebaseUserManager .USER_NOT_FOUND_ERROR , authException .getErrorCode ());
166
170
}
@@ -476,6 +480,32 @@ public void testListZeroTenants() throws Exception {
476
480
checkRequestHeaders (interceptor );
477
481
}
478
482
483
+ @ Test
484
+ public void testDeleteTenant () throws Exception {
485
+ TestResponseInterceptor interceptor = initializeAppForUserManagement ("{}" );
486
+
487
+ FirebaseAuth .getInstance ().getTenantManager ().deleteTenantAsync ("TENANT_1" ).get ();
488
+
489
+ checkRequestHeaders (interceptor );
490
+ checkUrl (interceptor , "DELETE" , TENANTS_BASE_URL + "/TENANT_1" );
491
+ }
492
+
493
+ @ Test
494
+ public void testDeleteTenantWithNotFoundError () throws Exception {
495
+ TestResponseInterceptor interceptor =
496
+ initializeAppForUserManagementWithStatusCode (404 ,
497
+ "{\" error\" : {\" message\" : \" TENANT_NOT_FOUND\" }}" );
498
+ try {
499
+ FirebaseAuth .getInstance ().getTenantManager ().deleteTenantAsync ("UNKNOWN" ).get ();
500
+ fail ("No error thrown for invalid response" );
501
+ } catch (ExecutionException e ) {
502
+ assertThat (e .getCause (), instanceOf (FirebaseAuthException .class ));
503
+ FirebaseAuthException authException = (FirebaseAuthException ) e .getCause ();
504
+ assertEquals (FirebaseUserManager .TENANT_NOT_FOUND_ERROR , authException .getErrorCode ());
505
+ }
506
+ checkUrl (interceptor , "DELETE" , TENANTS_BASE_URL + "/UNKNOWN" );
507
+ }
508
+
479
509
@ Test
480
510
public void testCreateSessionCookie () throws Exception {
481
511
TestResponseInterceptor interceptor = initializeAppForUserManagement (
@@ -615,11 +645,11 @@ public void call(FirebaseAuth auth) throws Exception {
615
645
operation .call (FirebaseAuth .getInstance ());
616
646
fail ("No error thrown for HTTP error: " + code );
617
647
} catch (ExecutionException e ) {
618
- assertTrue (e .getCause () instanceof FirebaseAuthException );
648
+ assertThat (e .getCause (), instanceOf ( FirebaseAuthException . class ) );
619
649
FirebaseAuthException authException = (FirebaseAuthException ) e .getCause ();
620
650
String msg = String .format ("Unexpected HTTP response with status: %d; body: {}" , code );
621
651
assertEquals (msg , authException .getMessage ());
622
- assertTrue (authException .getCause () instanceof HttpResponseException );
652
+ assertThat (authException .getCause (), instanceOf ( HttpResponseException . class ) );
623
653
assertEquals (FirebaseUserManager .INTERNAL_ERROR , authException .getErrorCode ());
624
654
}
625
655
}
@@ -633,10 +663,10 @@ public void call(FirebaseAuth auth) throws Exception {
633
663
operation .call (FirebaseAuth .getInstance ());
634
664
fail ("No error thrown for HTTP error" );
635
665
} catch (ExecutionException e ) {
636
- assertTrue (e .getCause ().toString (), e .getCause () instanceof FirebaseAuthException );
666
+ assertThat (e .getCause ().toString (), e .getCause (), instanceOf ( FirebaseAuthException . class ) );
637
667
FirebaseAuthException authException = (FirebaseAuthException ) e .getCause ();
638
668
assertEquals ("User management service responded with an error" , authException .getMessage ());
639
- assertTrue (authException .getCause () instanceof HttpResponseException );
669
+ assertThat (authException .getCause (), instanceOf ( HttpResponseException . class ) );
640
670
assertEquals (FirebaseUserManager .USER_NOT_FOUND_ERROR , authException .getErrorCode ());
641
671
}
642
672
}
@@ -649,33 +679,23 @@ public void testGetUserMalformedJsonError() throws Exception {
649
679
FirebaseAuth .getInstance ().getUserAsync ("testuser" ).get ();
650
680
fail ("No error thrown for JSON error" );
651
681
} catch (ExecutionException e ) {
652
- assertTrue (e .getCause () instanceof FirebaseAuthException );
682
+ assertThat (e .getCause (), instanceOf ( FirebaseAuthException . class ) );
653
683
FirebaseAuthException authException = (FirebaseAuthException ) e .getCause ();
654
- assertTrue (authException .getCause () instanceof IOException );
684
+ assertThat (authException .getCause (), instanceOf ( IOException . class ) );
655
685
assertEquals (FirebaseUserManager .INTERNAL_ERROR , authException .getErrorCode ());
656
686
}
657
687
}
658
688
659
689
@ Test
660
690
public void testGetUserUnexpectedHttpError () throws Exception {
661
- MockLowLevelHttpResponse response = new MockLowLevelHttpResponse ();
662
- response .setContent ("{\" not\" json}" );
663
- response .setStatusCode (500 );
664
- MockHttpTransport transport = new MockHttpTransport .Builder ()
665
- .setLowLevelHttpResponse (response )
666
- .build ();
667
- FirebaseApp .initializeApp (new FirebaseOptions .Builder ()
668
- .setCredentials (credentials )
669
- .setProjectId ("test-project-id" )
670
- .setHttpTransport (transport )
671
- .build ());
691
+ initializeAppForUserManagementWithStatusCode (500 , "{\" not\" json}" );
672
692
try {
673
693
FirebaseAuth .getInstance ().getUserAsync ("testuser" ).get ();
674
694
fail ("No error thrown for JSON error" );
675
695
} catch (ExecutionException e ) {
676
- assertTrue (e .getCause () instanceof FirebaseAuthException );
696
+ assertThat (e .getCause (), instanceOf ( FirebaseAuthException . class ) );
677
697
FirebaseAuthException authException = (FirebaseAuthException ) e .getCause ();
678
- assertTrue (authException .getCause () instanceof HttpResponseException );
698
+ assertThat (authException .getCause (), instanceOf ( HttpResponseException . class ) );
679
699
assertEquals ("Unexpected HTTP response with status: 500; body: {\" not\" json}" ,
680
700
authException .getMessage ());
681
701
assertEquals (FirebaseUserManager .INTERNAL_ERROR , authException .getErrorCode ());
@@ -1219,47 +1239,46 @@ public void testGenerateSignInWithEmailLinkWithSettings() throws Exception {
1219
1239
1220
1240
@ Test
1221
1241
public void testHttpErrorWithCode () {
1222
- FirebaseApp .initializeApp (new FirebaseOptions .Builder ()
1223
- .setCredentials (credentials )
1224
- .setHttpTransport (new MultiRequestMockHttpTransport (ImmutableList .of (
1225
- new MockLowLevelHttpResponse ()
1226
- .setContent ("{\" error\" : {\" message\" : \" UNAUTHORIZED_DOMAIN\" }}" )
1227
- .setStatusCode (500 ))))
1228
- .setProjectId ("test-project-id" )
1229
- .build ());
1230
- FirebaseAuth auth = FirebaseAuth .getInstance ();
1231
- FirebaseUserManager userManager = auth .getUserManager ();
1242
+ initializeAppForUserManagementWithStatusCode (500 ,
1243
+ "{\" error\" : {\" message\" : \" UNAUTHORIZED_DOMAIN\" }}" );
1244
+ FirebaseUserManager userManager = FirebaseAuth .getInstance ().getUserManager ();
1232
1245
try {
1233
1246
userManager .getEmailActionLink (EmailLinkType .PASSWORD_RESET , "test@example.com" , null );
1234
1247
fail ("No exception thrown for HTTP error" );
1235
1248
} catch (FirebaseAuthException e ) {
1236
1249
assertEquals ("unauthorized-continue-uri" , e .getErrorCode ());
1237
- assertTrue (e .getCause () instanceof HttpResponseException );
1250
+ assertThat (e .getCause (), instanceOf ( HttpResponseException . class ) );
1238
1251
}
1239
1252
}
1240
1253
1241
1254
@ Test
1242
1255
public void testUnexpectedHttpError () {
1243
- FirebaseApp .initializeApp (new FirebaseOptions .Builder ()
1244
- .setCredentials (credentials )
1245
- .setHttpTransport (new MultiRequestMockHttpTransport (ImmutableList .of (
1246
- new MockLowLevelHttpResponse ()
1247
- .setContent ("{}" )
1248
- .setStatusCode (500 ))))
1249
- .setProjectId ("test-project-id" )
1250
- .build ());
1251
- FirebaseAuth auth = FirebaseAuth .getInstance ();
1252
- FirebaseUserManager userManager = auth .getUserManager ();
1256
+ initializeAppForUserManagementWithStatusCode (500 , "{}" );
1257
+ FirebaseUserManager userManager = FirebaseAuth .getInstance ().getUserManager ();
1253
1258
try {
1254
1259
userManager .getEmailActionLink (EmailLinkType .PASSWORD_RESET , "test@example.com" , null );
1255
1260
fail ("No exception thrown for HTTP error" );
1256
1261
} catch (FirebaseAuthException e ) {
1257
1262
assertEquals ("internal-error" , e .getErrorCode ());
1258
- assertTrue (e .getCause () instanceof HttpResponseException );
1263
+ assertThat (e .getCause (), instanceOf ( HttpResponseException . class ) );
1259
1264
}
1260
1265
}
1261
1266
1262
- private static TestResponseInterceptor initializeAppForUserManagement (String ...responses ) {
1267
+ private static TestResponseInterceptor initializeAppForUserManagementWithStatusCode (
1268
+ int statusCode , String response ) {
1269
+ FirebaseApp .initializeApp (new FirebaseOptions .Builder ()
1270
+ .setCredentials (credentials )
1271
+ .setHttpTransport (
1272
+ MockHttpTransport .builder ().setLowLevelHttpResponse (
1273
+ new MockLowLevelHttpResponse ().setContent (response ).setStatusCode (statusCode )).build ())
1274
+ .setProjectId ("test-project-id" )
1275
+ .build ());
1276
+ TestResponseInterceptor interceptor = new TestResponseInterceptor ();
1277
+ FirebaseAuth .getInstance ().getUserManager ().setInterceptor (interceptor );
1278
+ return interceptor ;
1279
+ }
1280
+
1281
+ private static TestResponseInterceptor initializeAppForUserManagement (String ... responses ) {
1263
1282
List <MockLowLevelHttpResponse > mocks = new ArrayList <>();
1264
1283
for (String response : responses ) {
1265
1284
mocks .add (new MockLowLevelHttpResponse ().setContent (response ));
@@ -1270,10 +1289,8 @@ private static TestResponseInterceptor initializeAppForUserManagement(String ...
1270
1289
.setHttpTransport (transport )
1271
1290
.setProjectId ("test-project-id" )
1272
1291
.build ());
1273
- FirebaseAuth auth = FirebaseAuth .getInstance ();
1274
- FirebaseUserManager userManager = auth .getUserManager ();
1275
1292
TestResponseInterceptor interceptor = new TestResponseInterceptor ();
1276
- userManager .setInterceptor (interceptor );
1293
+ FirebaseAuth . getInstance (). getUserManager () .setInterceptor (interceptor );
1277
1294
return interceptor ;
1278
1295
}
1279
1296
@@ -1326,6 +1343,12 @@ private static void checkRequestHeaders(TestResponseInterceptor interceptor) {
1326
1343
assertEquals (clientVersion , headers .getFirstHeaderStringValue ("X-Client-Version" ));
1327
1344
}
1328
1345
1346
+ private static void checkUrl (TestResponseInterceptor interceptor , String method , String url ) {
1347
+ HttpRequest request = interceptor .getResponse ().getRequest ();
1348
+ assertEquals (method , request .getRequestMethod ());
1349
+ assertEquals (url , request .getUrl ().toString ());
1350
+ }
1351
+
1329
1352
private interface UserManagerOp {
1330
1353
void call (FirebaseAuth auth ) throws Exception ;
1331
1354
}
0 commit comments