From a75bfbca9bc710562cfe7a89072e444d80ae244b Mon Sep 17 00:00:00 2001 From: Micah Stairs Date: Thu, 20 Feb 2020 16:10:50 -0500 Subject: [PATCH 1/5] Add deleteTenant operation to TenantManager. --- .../firebase/auth/FirebaseUserManager.java | 11 +++++- .../google/firebase/auth/TenantManager.java | 38 +++++++++++++++++++ .../auth/FirebaseUserManagerTest.java | 8 ++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/firebase/auth/FirebaseUserManager.java b/src/main/java/com/google/firebase/auth/FirebaseUserManager.java index eec3a81cc..d1b83feac 100644 --- a/src/main/java/com/google/firebase/auth/FirebaseUserManager.java +++ b/src/main/java/com/google/firebase/auth/FirebaseUserManager.java @@ -226,6 +226,15 @@ UserImportResult importUsers(UserImportRequest request) throws FirebaseAuthExcep return new UserImportResult(request.getUsersCount(), response); } + void deleteTenant(String tenantId) throws FirebaseAuthException { + GenericUrl url = new GenericUrl(tenantMgtBaseUrl + "/tenants:delete"); + url.put("name", tenantId); + GenericJson response = sendRequest("DELETE", url, null, GenericJson.class); + if (response == null) { + throw new FirebaseAuthException(INTERNAL_ERROR, "Failed to delete tenant: " + tenantId); + } + } + ListTenantsResponse listTenants(int maxResults, String pageToken) throws FirebaseAuthException { ImmutableMap.Builder builder = ImmutableMap.builder() @@ -287,7 +296,7 @@ private T post(String path, Object content, Class clazz) throws FirebaseA private T sendRequest( String method, GenericUrl url, - @Nullable Object content, Class clazz) throws FirebaseAuthException { + Object content, Class clazz) throws FirebaseAuthException { checkArgument(!Strings.isNullOrEmpty(method), "method must not be null or empty"); checkNotNull(url, "url must not be null"); diff --git a/src/main/java/com/google/firebase/auth/TenantManager.java b/src/main/java/com/google/firebase/auth/TenantManager.java index 62acd1b64..1d1948c30 100644 --- a/src/main/java/com/google/firebase/auth/TenantManager.java +++ b/src/main/java/com/google/firebase/auth/TenantManager.java @@ -16,10 +16,12 @@ package com.google.firebase.auth; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import com.google.api.client.json.JsonFactory; import com.google.api.core.ApiFuture; +import com.google.common.base.Strings; import com.google.firebase.FirebaseApp; import com.google.firebase.auth.ListTenantsPage.DefaultTenantSource; import com.google.firebase.auth.ListTenantsPage.PageFactory; @@ -115,4 +117,40 @@ protected ListTenantsPage execute() throws FirebaseAuthException { } }; } + + /** + * Deletes the tenant identified by the specified tenant ID. + * + * @param tenantId A tenant ID string. + * @throws IllegalArgumentException If the tenant ID string is null or empty. + * @throws FirebaseAuthException If an error occurs while deleting the tenant. + */ + public void deleteTenant(@NonNull String tenantId) throws FirebaseAuthException { + deleteTenantOp(tenantId).call(); + } + + /** + * Similar to {@link #deleteTenant(String)} but performs the operation asynchronously. + * + * @param tenantId A tenant ID string. + * @return An {@code ApiFuture} which will complete successfully when the specified tenant account + * has been deleted. If an error occurs while deleting the tenant account, the future throws a + * {@link FirebaseAuthException}. + * @throws IllegalArgumentException If the tenant ID string is null or empty. + */ + public ApiFuture deleteTenantAsync(String tenantId) { + return deleteTenantOp(tenantId).callAsync(firebaseApp); + } + + private CallableOperation deleteTenantOp(final String tenantId) { + // TODO(micahstairs): Add a check to make sure the app has not been destroyed yet. + checkArgument(!Strings.isNullOrEmpty(tenantId), "tenantId must not be null or empty"); + return new CallableOperation() { + @Override + protected Void execute() throws FirebaseAuthException { + userManager.deleteTenant(tenantId); + return null; + } + }; + } } diff --git a/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java b/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java index fa7de0c8e..bea765090 100644 --- a/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java +++ b/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java @@ -476,6 +476,14 @@ public void testListZeroTenants() throws Exception { checkRequestHeaders(interceptor); } + @Test + public void testDeleteTenant() throws Exception { + TestResponseInterceptor interceptor = initializeAppForUserManagement("{}"); + // should not throw + FirebaseAuth.getInstance().getTenantManager().deleteTenantAsync("TENANT_1").get(); + checkRequestHeaders(interceptor); + } + @Test public void testCreateSessionCookie() throws Exception { TestResponseInterceptor interceptor = initializeAppForUserManagement( From c03dd3627955d6d8316ea5ceca17902da7b899a1 Mon Sep 17 00:00:00 2001 From: Micah Stairs Date: Thu, 27 Feb 2020 12:03:09 -0500 Subject: [PATCH 2/5] Address pull request feedback --- .../java/com/google/firebase/auth/FirebaseUserManager.java | 3 +-- .../com/google/firebase/auth/FirebaseUserManagerTest.java | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/firebase/auth/FirebaseUserManager.java b/src/main/java/com/google/firebase/auth/FirebaseUserManager.java index d1b83feac..808637f33 100644 --- a/src/main/java/com/google/firebase/auth/FirebaseUserManager.java +++ b/src/main/java/com/google/firebase/auth/FirebaseUserManager.java @@ -227,8 +227,7 @@ UserImportResult importUsers(UserImportRequest request) throws FirebaseAuthExcep } void deleteTenant(String tenantId) throws FirebaseAuthException { - GenericUrl url = new GenericUrl(tenantMgtBaseUrl + "/tenants:delete"); - url.put("name", tenantId); + GenericUrl url = new GenericUrl(tenantMgtBaseUrl + "/tenants/" + tenantId); GenericJson response = sendRequest("DELETE", url, null, GenericJson.class); if (response == null) { throw new FirebaseAuthException(INTERNAL_ERROR, "Failed to delete tenant: " + tenantId); diff --git a/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java b/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java index bea765090..50441cde8 100644 --- a/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java +++ b/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java @@ -482,6 +482,8 @@ public void testDeleteTenant() throws Exception { // should not throw FirebaseAuth.getInstance().getTenantManager().deleteTenantAsync("TENANT_1").get(); checkRequestHeaders(interceptor); + checkUrl(interceptor, + "https://identitytoolkit.googleapis.com/v2/projects/test-project-id/tenants/TENANT_1"); } @Test @@ -1334,6 +1336,10 @@ private static void checkRequestHeaders(TestResponseInterceptor interceptor) { assertEquals(clientVersion, headers.getFirstHeaderStringValue("X-Client-Version")); } + private static void checkUrl(TestResponseInterceptor interceptor, String expectedUrl) { + assertEquals(expectedUrl, interceptor.getResponse().getRequest().getUrl().toString()); + } + private interface UserManagerOp { void call(FirebaseAuth auth) throws Exception; } From c042c0fc0391c5a2f490ae7b28fe4cb41dbe3e95 Mon Sep 17 00:00:00 2001 From: Micah Stairs Date: Thu, 27 Feb 2020 15:53:32 -0500 Subject: [PATCH 3/5] Work on testing error handling of deleteTenant. --- .../firebase/auth/FirebaseUserManager.java | 5 ++- .../auth/FirebaseUserManagerTest.java | 44 +++++++++++++++---- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/google/firebase/auth/FirebaseUserManager.java b/src/main/java/com/google/firebase/auth/FirebaseUserManager.java index 808637f33..5ad00acb4 100644 --- a/src/main/java/com/google/firebase/auth/FirebaseUserManager.java +++ b/src/main/java/com/google/firebase/auth/FirebaseUserManager.java @@ -65,6 +65,7 @@ */ class FirebaseUserManager { + static final String TENANT_NOT_FOUND_ERROR = "tenant-not-found"; static final String USER_NOT_FOUND_ERROR = "user-not-found"; static final String INTERNAL_ERROR = "internal-error"; @@ -82,6 +83,7 @@ class FirebaseUserManager { .put("INVALID_PAGE_SELECTION", "invalid-page-token") .put("INVALID_PHONE_NUMBER", "invalid-phone-number") .put("PHONE_NUMBER_EXISTS", "phone-number-already-exists") + .put("TENANT_NOT_FOUND", TENANT_NOT_FOUND_ERROR) .put("PROJECT_NOT_FOUND", "project-not-found") .put("USER_NOT_FOUND", USER_NOT_FOUND_ERROR) .put("WEAK_PASSWORD", "invalid-password") @@ -230,7 +232,8 @@ void deleteTenant(String tenantId) throws FirebaseAuthException { GenericUrl url = new GenericUrl(tenantMgtBaseUrl + "/tenants/" + tenantId); GenericJson response = sendRequest("DELETE", url, null, GenericJson.class); if (response == null) { - throw new FirebaseAuthException(INTERNAL_ERROR, "Failed to delete tenant: " + tenantId); + throw new FirebaseAuthException(TENANT_NOT_FOUND_ERROR, + "Failed to delete tenant: " + tenantId); } } diff --git a/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java b/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java index 50441cde8..f90777b03 100644 --- a/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java +++ b/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java @@ -74,6 +74,8 @@ public class FirebaseUserManagerTest { .build(); private static final Map ACTION_CODE_SETTINGS_MAP = ACTION_CODE_SETTINGS.getProperties(); + private static final String TENANTS_BASE_URL = + "https://identitytoolkit.googleapis.com/v2/projects/test-project-id/tenants"; @After public void tearDown() { @@ -479,11 +481,36 @@ public void testListZeroTenants() throws Exception { @Test public void testDeleteTenant() throws Exception { TestResponseInterceptor interceptor = initializeAppForUserManagement("{}"); - // should not throw + FirebaseAuth.getInstance().getTenantManager().deleteTenantAsync("TENANT_1").get(); + checkRequestHeaders(interceptor); - checkUrl(interceptor, - "https://identitytoolkit.googleapis.com/v2/projects/test-project-id/tenants/TENANT_1"); + checkUrl(interceptor, "DELETE", TENANTS_BASE_URL + "/TENANT_1"); + } + + @Test + public void testDeleteTenantWithNotFoundError() throws Exception { + FirebaseApp.initializeApp(new FirebaseOptions.Builder() + .setCredentials(credentials) + .setHttpTransport(new MultiRequestMockHttpTransport(ImmutableList.of( + new MockLowLevelHttpResponse() + .setContent("{}") + .setStatusCode(404)))) + .setProjectId("test-project-id") + .build()); + TestResponseInterceptor interceptor = new TestResponseInterceptor(); + FirebaseAuth auth = FirebaseAuth.getInstance(); + auth.getUserManager().setInterceptor(interceptor); + + try { + auth.getTenantManager().deleteTenantAsync("UNKNOWN").get(); + fail("No error thrown for invalid response"); + } catch (ExecutionException e) { + assertTrue(e.getCause() instanceof FirebaseAuthException); + FirebaseAuthException authException = (FirebaseAuthException) e.getCause(); + assertEquals(FirebaseUserManager.TENANT_NOT_FOUND_ERROR, authException.getErrorCode()); + } + checkUrl(interceptor, "DELETE", TENANTS_BASE_URL + "/UNKNOWN"); } @Test @@ -1269,7 +1296,7 @@ public void testUnexpectedHttpError() { } } - private static TestResponseInterceptor initializeAppForUserManagement(String ...responses) { + private static TestResponseInterceptor initializeAppForUserManagement(String... responses) { List mocks = new ArrayList<>(); for (String response : responses) { mocks.add(new MockLowLevelHttpResponse().setContent(response)); @@ -1280,10 +1307,8 @@ private static TestResponseInterceptor initializeAppForUserManagement(String ... .setHttpTransport(transport) .setProjectId("test-project-id") .build()); - FirebaseAuth auth = FirebaseAuth.getInstance(); - FirebaseUserManager userManager = auth.getUserManager(); TestResponseInterceptor interceptor = new TestResponseInterceptor(); - userManager.setInterceptor(interceptor); + FirebaseAuth.getInstance().getUserManager().setInterceptor(interceptor); return interceptor; } @@ -1336,8 +1361,9 @@ private static void checkRequestHeaders(TestResponseInterceptor interceptor) { assertEquals(clientVersion, headers.getFirstHeaderStringValue("X-Client-Version")); } - private static void checkUrl(TestResponseInterceptor interceptor, String expectedUrl) { - assertEquals(expectedUrl, interceptor.getResponse().getRequest().getUrl().toString()); + private static void checkUrl(TestResponseInterceptor interceptor, String method, String url) { + assertEquals(method, interceptor.getResponse().getRequest().getRequestMethod()); + assertEquals(url, interceptor.getResponse().getRequest().getUrl().toString()); } private interface UserManagerOp { From aa2467b0a0ea84906d059d7df5b219f79292d82c Mon Sep 17 00:00:00 2001 From: Micah Stairs Date: Thu, 27 Feb 2020 17:08:09 -0500 Subject: [PATCH 4/5] Fix error handling test for deleteTenant and do some refactoring. --- .../firebase/auth/FirebaseUserManager.java | 2 +- .../auth/FirebaseUserManagerTest.java | 98 ++++++++----------- 2 files changed, 41 insertions(+), 59 deletions(-) diff --git a/src/main/java/com/google/firebase/auth/FirebaseUserManager.java b/src/main/java/com/google/firebase/auth/FirebaseUserManager.java index 5ad00acb4..4bab926e3 100644 --- a/src/main/java/com/google/firebase/auth/FirebaseUserManager.java +++ b/src/main/java/com/google/firebase/auth/FirebaseUserManager.java @@ -298,7 +298,7 @@ private T post(String path, Object content, Class clazz) throws FirebaseA private T sendRequest( String method, GenericUrl url, - Object content, Class clazz) throws FirebaseAuthException { + @Nullable Object content, Class clazz) throws FirebaseAuthException { checkArgument(!Strings.isNullOrEmpty(method), "method must not be null or empty"); checkNotNull(url, "url must not be null"); diff --git a/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java b/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java index f90777b03..5a460de2c 100644 --- a/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java +++ b/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java @@ -16,10 +16,12 @@ package com.google.firebase.auth; +import static org.hamcrest.core.IsInstanceOf.instanceOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -116,7 +118,7 @@ public void testGetUserWithNotFoundError() throws Exception { FirebaseAuth.getInstance().getUserAsync("testuser").get(); fail("No error thrown for invalid response"); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof FirebaseAuthException); + assertThat(e.getCause(), instanceOf(FirebaseAuthException.class)); FirebaseAuthException authException = (FirebaseAuthException) e.getCause(); assertEquals(FirebaseUserManager.USER_NOT_FOUND_ERROR, authException.getErrorCode()); } @@ -139,7 +141,7 @@ public void testGetUserByEmailWithNotFoundError() throws Exception { FirebaseAuth.getInstance().getUserByEmailAsync("testuser@example.com").get(); fail("No error thrown for invalid response"); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof FirebaseAuthException); + assertThat(e.getCause(), instanceOf(FirebaseAuthException.class)); FirebaseAuthException authException = (FirebaseAuthException) e.getCause(); assertEquals(FirebaseUserManager.USER_NOT_FOUND_ERROR, authException.getErrorCode()); } @@ -162,7 +164,7 @@ public void testGetUserByPhoneNumberWithNotFoundError() throws Exception { FirebaseAuth.getInstance().getUserByPhoneNumberAsync("+1234567890").get(); fail("No error thrown for invalid response"); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof FirebaseAuthException); + assertThat(e.getCause(), instanceOf(FirebaseAuthException.class)); FirebaseAuthException authException = (FirebaseAuthException) e.getCause(); assertEquals(FirebaseUserManager.USER_NOT_FOUND_ERROR, authException.getErrorCode()); } @@ -490,23 +492,14 @@ public void testDeleteTenant() throws Exception { @Test public void testDeleteTenantWithNotFoundError() throws Exception { - FirebaseApp.initializeApp(new FirebaseOptions.Builder() - .setCredentials(credentials) - .setHttpTransport(new MultiRequestMockHttpTransport(ImmutableList.of( - new MockLowLevelHttpResponse() - .setContent("{}") - .setStatusCode(404)))) - .setProjectId("test-project-id") - .build()); - TestResponseInterceptor interceptor = new TestResponseInterceptor(); - FirebaseAuth auth = FirebaseAuth.getInstance(); - auth.getUserManager().setInterceptor(interceptor); - + TestResponseInterceptor interceptor = + initializeAppForUserManagementWithStatusCode(404, + "{\"error\": {\"message\": \"TENANT_NOT_FOUND\"}}"); try { - auth.getTenantManager().deleteTenantAsync("UNKNOWN").get(); + FirebaseAuth.getInstance().getTenantManager().deleteTenantAsync("UNKNOWN").get(); fail("No error thrown for invalid response"); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof FirebaseAuthException); + assertThat(e.getCause(), instanceOf(FirebaseAuthException.class)); FirebaseAuthException authException = (FirebaseAuthException) e.getCause(); assertEquals(FirebaseUserManager.TENANT_NOT_FOUND_ERROR, authException.getErrorCode()); } @@ -652,11 +645,11 @@ public void call(FirebaseAuth auth) throws Exception { operation.call(FirebaseAuth.getInstance()); fail("No error thrown for HTTP error: " + code); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof FirebaseAuthException); + assertThat(e.getCause(), instanceOf(FirebaseAuthException.class)); FirebaseAuthException authException = (FirebaseAuthException) e.getCause(); String msg = String.format("Unexpected HTTP response with status: %d; body: {}", code); assertEquals(msg, authException.getMessage()); - assertTrue(authException.getCause() instanceof HttpResponseException); + assertThat(authException.getCause(), instanceOf(HttpResponseException.class)); assertEquals(FirebaseUserManager.INTERNAL_ERROR, authException.getErrorCode()); } } @@ -670,10 +663,10 @@ public void call(FirebaseAuth auth) throws Exception { operation.call(FirebaseAuth.getInstance()); fail("No error thrown for HTTP error"); } catch (ExecutionException e) { - assertTrue(e.getCause().toString(), e.getCause() instanceof FirebaseAuthException); + assertThat(e.getCause().toString(), e.getCause(), instanceOf(FirebaseAuthException.class)); FirebaseAuthException authException = (FirebaseAuthException) e.getCause(); assertEquals("User management service responded with an error", authException.getMessage()); - assertTrue(authException.getCause() instanceof HttpResponseException); + assertThat(authException.getCause(), instanceOf(HttpResponseException.class)); assertEquals(FirebaseUserManager.USER_NOT_FOUND_ERROR, authException.getErrorCode()); } } @@ -686,33 +679,23 @@ public void testGetUserMalformedJsonError() throws Exception { FirebaseAuth.getInstance().getUserAsync("testuser").get(); fail("No error thrown for JSON error"); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof FirebaseAuthException); + assertThat(e.getCause(), instanceOf(FirebaseAuthException.class)); FirebaseAuthException authException = (FirebaseAuthException) e.getCause(); - assertTrue(authException.getCause() instanceof IOException); + assertThat(authException.getCause(), instanceOf(IOException.class)); assertEquals(FirebaseUserManager.INTERNAL_ERROR, authException.getErrorCode()); } } @Test public void testGetUserUnexpectedHttpError() throws Exception { - MockLowLevelHttpResponse response = new MockLowLevelHttpResponse(); - response.setContent("{\"not\" json}"); - response.setStatusCode(500); - MockHttpTransport transport = new MockHttpTransport.Builder() - .setLowLevelHttpResponse(response) - .build(); - FirebaseApp.initializeApp(new FirebaseOptions.Builder() - .setCredentials(credentials) - .setProjectId("test-project-id") - .setHttpTransport(transport) - .build()); + initializeAppForUserManagementWithStatusCode(500, "{\"not\" json}"); try { FirebaseAuth.getInstance().getUserAsync("testuser").get(); fail("No error thrown for JSON error"); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof FirebaseAuthException); + assertThat(e.getCause(), instanceOf(FirebaseAuthException.class)); FirebaseAuthException authException = (FirebaseAuthException) e.getCause(); - assertTrue(authException.getCause() instanceof HttpResponseException); + assertThat(authException.getCause(), instanceOf(HttpResponseException.class)); assertEquals("Unexpected HTTP response with status: 500; body: {\"not\" json}", authException.getMessage()); assertEquals(FirebaseUserManager.INTERNAL_ERROR, authException.getErrorCode()); @@ -1256,46 +1239,45 @@ public void testGenerateSignInWithEmailLinkWithSettings() throws Exception { @Test public void testHttpErrorWithCode() { - FirebaseApp.initializeApp(new FirebaseOptions.Builder() - .setCredentials(credentials) - .setHttpTransport(new MultiRequestMockHttpTransport(ImmutableList.of( - new MockLowLevelHttpResponse() - .setContent("{\"error\": {\"message\": \"UNAUTHORIZED_DOMAIN\"}}") - .setStatusCode(500)))) - .setProjectId("test-project-id") - .build()); - FirebaseAuth auth = FirebaseAuth.getInstance(); - FirebaseUserManager userManager = auth.getUserManager(); + initializeAppForUserManagementWithStatusCode(500, + "{\"error\": {\"message\": \"UNAUTHORIZED_DOMAIN\"}}"); + FirebaseUserManager userManager = FirebaseAuth.getInstance().getUserManager(); try { userManager.getEmailActionLink(EmailLinkType.PASSWORD_RESET, "test@example.com", null); fail("No exception thrown for HTTP error"); } catch (FirebaseAuthException e) { assertEquals("unauthorized-continue-uri", e.getErrorCode()); - assertTrue(e.getCause() instanceof HttpResponseException); + assertThat(e.getCause(), instanceOf(HttpResponseException.class)); } } @Test public void testUnexpectedHttpError() { - FirebaseApp.initializeApp(new FirebaseOptions.Builder() - .setCredentials(credentials) - .setHttpTransport(new MultiRequestMockHttpTransport(ImmutableList.of( - new MockLowLevelHttpResponse() - .setContent("{}") - .setStatusCode(500)))) - .setProjectId("test-project-id") - .build()); - FirebaseAuth auth = FirebaseAuth.getInstance(); - FirebaseUserManager userManager = auth.getUserManager(); + initializeAppForUserManagementWithStatusCode(500, "{}"); + FirebaseUserManager userManager = FirebaseAuth.getInstance().getUserManager(); try { userManager.getEmailActionLink(EmailLinkType.PASSWORD_RESET, "test@example.com", null); fail("No exception thrown for HTTP error"); } catch (FirebaseAuthException e) { assertEquals("internal-error", e.getErrorCode()); - assertTrue(e.getCause() instanceof HttpResponseException); + assertThat(e.getCause(), instanceOf(HttpResponseException.class)); } } + private static TestResponseInterceptor initializeAppForUserManagementWithStatusCode( + int statusCode, String response) { + FirebaseApp.initializeApp(new FirebaseOptions.Builder() + .setCredentials(credentials) + .setHttpTransport( + MockHttpTransport.builder().setLowLevelHttpResponse( + new MockLowLevelHttpResponse().setContent(response).setStatusCode(statusCode)).build()) + .setProjectId("test-project-id") + .build()); + TestResponseInterceptor interceptor = new TestResponseInterceptor(); + FirebaseAuth.getInstance().getUserManager().setInterceptor(interceptor); + return interceptor; + } + private static TestResponseInterceptor initializeAppForUserManagement(String... responses) { List mocks = new ArrayList<>(); for (String response : responses) { From d22031fe61042d7b37e3022b426b4f834f752fb5 Mon Sep 17 00:00:00 2001 From: Micah Stairs Date: Thu, 27 Feb 2020 18:05:46 -0500 Subject: [PATCH 5/5] Introduce local variable for HttpRequest --- .../com/google/firebase/auth/FirebaseUserManagerTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java b/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java index 5a460de2c..c1ef26c86 100644 --- a/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java +++ b/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java @@ -1344,8 +1344,9 @@ private static void checkRequestHeaders(TestResponseInterceptor interceptor) { } private static void checkUrl(TestResponseInterceptor interceptor, String method, String url) { - assertEquals(method, interceptor.getResponse().getRequest().getRequestMethod()); - assertEquals(url, interceptor.getResponse().getRequest().getUrl().toString()); + HttpRequest request = interceptor.getResponse().getRequest(); + assertEquals(method, request.getRequestMethod()); + assertEquals(url, request.getUrl().toString()); } private interface UserManagerOp {