-
Notifications
You must be signed in to change notification settings - Fork 289
Add deleteTenant operation to TenantManager. #372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a75bfbc
Add deleteTenant operation to TenantManager.
micahstairs c03dd36
Address pull request feedback
micahstairs c042c0f
Work on testing error handling of deleteTenant.
micahstairs aa2467b
Fix error handling test for deleteTenant and do some refactoring.
micahstairs d22031f
Introduce local variable for HttpRequest
micahstairs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -74,6 +76,8 @@ public class FirebaseUserManagerTest { | |
.build(); | ||
private static final Map<String, Object> 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() { | ||
|
@@ -114,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()); | ||
} | ||
|
@@ -137,7 +141,7 @@ public void testGetUserByEmailWithNotFoundError() throws Exception { | |
FirebaseAuth.getInstance().getUserByEmailAsync("[email protected]").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()); | ||
} | ||
|
@@ -160,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()); | ||
} | ||
|
@@ -476,6 +480,32 @@ public void testListZeroTenants() throws Exception { | |
checkRequestHeaders(interceptor); | ||
} | ||
|
||
@Test | ||
public void testDeleteTenant() throws Exception { | ||
TestResponseInterceptor interceptor = initializeAppForUserManagement("{}"); | ||
|
||
FirebaseAuth.getInstance().getTenantManager().deleteTenantAsync("TENANT_1").get(); | ||
|
||
checkRequestHeaders(interceptor); | ||
micahstairs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
checkUrl(interceptor, "DELETE", TENANTS_BASE_URL + "/TENANT_1"); | ||
} | ||
|
||
@Test | ||
public void testDeleteTenantWithNotFoundError() throws Exception { | ||
TestResponseInterceptor interceptor = | ||
initializeAppForUserManagementWithStatusCode(404, | ||
"{\"error\": {\"message\": \"TENANT_NOT_FOUND\"}}"); | ||
try { | ||
FirebaseAuth.getInstance().getTenantManager().deleteTenantAsync("UNKNOWN").get(); | ||
fail("No error thrown for invalid response"); | ||
} catch (ExecutionException e) { | ||
assertThat(e.getCause(), instanceOf(FirebaseAuthException.class)); | ||
FirebaseAuthException authException = (FirebaseAuthException) e.getCause(); | ||
assertEquals(FirebaseUserManager.TENANT_NOT_FOUND_ERROR, authException.getErrorCode()); | ||
} | ||
checkUrl(interceptor, "DELETE", TENANTS_BASE_URL + "/UNKNOWN"); | ||
} | ||
|
||
@Test | ||
public void testCreateSessionCookie() throws Exception { | ||
TestResponseInterceptor interceptor = initializeAppForUserManagement( | ||
|
@@ -615,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()); | ||
} | ||
} | ||
|
@@ -633,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()); | ||
} | ||
} | ||
|
@@ -649,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()); | ||
|
@@ -1219,47 +1239,46 @@ 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, "[email protected]", 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, "[email protected]", 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 initializeAppForUserManagement(String ...responses) { | ||
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<MockLowLevelHttpResponse> mocks = new ArrayList<>(); | ||
for (String response : responses) { | ||
mocks.add(new MockLowLevelHttpResponse().setContent(response)); | ||
|
@@ -1270,10 +1289,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; | ||
} | ||
|
||
|
@@ -1326,6 +1343,12 @@ private static void checkRequestHeaders(TestResponseInterceptor interceptor) { | |
assertEquals(clientVersion, headers.getFirstHeaderStringValue("X-Client-Version")); | ||
} | ||
|
||
private static void checkUrl(TestResponseInterceptor interceptor, String method, String url) { | ||
HttpRequest request = interceptor.getResponse().getRequest(); | ||
assertEquals(method, request.getRequestMethod()); | ||
assertEquals(url, request.getUrl().toString()); | ||
} | ||
|
||
private interface UserManagerOp { | ||
void call(FirebaseAuth auth) throws Exception; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.