Skip to content

Commit c3ef972

Browse files
authored
Add getTenant operation to TenantManager. (#371)
Added getTenant to the TenantManager class. Also added the relevant unit tests to FirebaseUserManagerTest. This is part of the initiative to adding multi-tenancy support (see issue #332).
1 parent a04fcfa commit c3ef972

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

src/main/java/com/google/firebase/auth/FirebaseUserManager.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ class FirebaseUserManager {
8383
.put("INVALID_PAGE_SELECTION", "invalid-page-token")
8484
.put("INVALID_PHONE_NUMBER", "invalid-phone-number")
8585
.put("PHONE_NUMBER_EXISTS", "phone-number-already-exists")
86-
.put("TENANT_NOT_FOUND", TENANT_NOT_FOUND_ERROR)
8786
.put("PROJECT_NOT_FOUND", "project-not-found")
87+
.put("TENANT_NOT_FOUND", TENANT_NOT_FOUND_ERROR)
8888
.put("USER_NOT_FOUND", USER_NOT_FOUND_ERROR)
8989
.put("WEAK_PASSWORD", "invalid-password")
9090
.put("UNAUTHORIZED_DOMAIN", "unauthorized-continue-uri")
@@ -228,6 +228,15 @@ UserImportResult importUsers(UserImportRequest request) throws FirebaseAuthExcep
228228
return new UserImportResult(request.getUsersCount(), response);
229229
}
230230

231+
Tenant getTenant(String tenantId) throws FirebaseAuthException {
232+
GenericUrl url = new GenericUrl(tenantMgtBaseUrl + "/tenants/" + tenantId);
233+
Tenant response = sendRequest("GET", url, null, Tenant.class);
234+
if (Strings.isNullOrEmpty(response.getTenantId())) {
235+
throw new FirebaseAuthException(TENANT_NOT_FOUND_ERROR, "Failed to get tenant.");
236+
}
237+
return response;
238+
}
239+
231240
void deleteTenant(String tenantId) throws FirebaseAuthException {
232241
GenericUrl url = new GenericUrl(tenantMgtBaseUrl + "/tenants/" + tenantId);
233242
GenericJson response = sendRequest("DELETE", url, null, GenericJson.class);

src/main/java/com/google/firebase/auth/TenantManager.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
* This class can be used to perform a variety of tenant-related operations, including creating,
3737
* updating, and listing tenants.
3838
*
39-
* <p>TODO(micahstairs): Implement the following methods: getAuthForTenant(), getTenant(),
40-
* deleteTenant(), createTenant(), and updateTenant().
39+
* <p>TODO(micahstairs): Implement the following methods: getAuthForTenant(), createTenant(), and
40+
* updateTenant().
4141
*/
4242
public final class TenantManager {
4343

@@ -49,6 +49,42 @@ public final class TenantManager {
4949
this.userManager = userManager;
5050
}
5151

52+
/**
53+
* Gets the tenant corresponding to the specified tenant ID.
54+
*
55+
* @param tenantId A tenant ID string.
56+
* @return A {@link Tenant} instance.
57+
* @throws IllegalArgumentException If the tenant ID string is null or empty.
58+
* @throws FirebaseAuthException If an error occurs while retrieving user data.
59+
*/
60+
public Tenant getTenant(@NonNull String tenantId) throws FirebaseAuthException {
61+
return getTenantOp(tenantId).call();
62+
}
63+
64+
/**
65+
* Similar to {@link #getTenant(String)} but performs the operation asynchronously.
66+
*
67+
* @param tenantId A tenantId string.
68+
* @return An {@code ApiFuture} which will complete successfully with a {@link Tenant} instance
69+
* If an error occurs while retrieving tenant data or if the specified tenant ID does not
70+
* exist, the future throws a {@link FirebaseAuthException}.
71+
* @throws IllegalArgumentException If the tenant ID string is null or empty.
72+
*/
73+
public ApiFuture<Tenant> getTenantAsync(@NonNull String tenantId) {
74+
return getTenantOp(tenantId).callAsync(firebaseApp);
75+
}
76+
77+
private CallableOperation<Tenant, FirebaseAuthException> getTenantOp(final String tenantId) {
78+
// TODO(micahstairs): Add a check to make sure the app has not been destroyed yet.
79+
checkArgument(!Strings.isNullOrEmpty(tenantId), "tenantId must not be null or empty");
80+
return new CallableOperation<Tenant, FirebaseAuthException>() {
81+
@Override
82+
protected Tenant execute() throws FirebaseAuthException {
83+
return userManager.getTenant(tenantId);
84+
}
85+
};
86+
}
87+
5288
/**
5389
* Gets a page of tenants starting from the specified {@code pageToken}. Page size will be limited
5490
* to 1000 tenants.

src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,32 @@ public void testImportUsersLargeList() {
434434
}
435435
}
436436

437+
@Test
438+
public void testGetTenant() throws Exception {
439+
TestResponseInterceptor interceptor = initializeAppForUserManagement(
440+
TestUtils.loadResource("getTenant.json"));
441+
Tenant tenant = FirebaseAuth.getInstance().getTenantManager().getTenantAsync("TENANT_1").get();
442+
checkTenant(tenant, "TENANT_1");
443+
checkRequestHeaders(interceptor);
444+
checkUrl(interceptor, "GET", TENANTS_BASE_URL + "/TENANT_1");
445+
}
446+
447+
@Test
448+
public void testGetTenantWithNotFoundError() throws Exception {
449+
TestResponseInterceptor interceptor =
450+
initializeAppForUserManagementWithStatusCode(404,
451+
"{\"error\": {\"message\": \"TENANT_NOT_FOUND\"}}");
452+
try {
453+
FirebaseAuth.getInstance().getTenantManager().getTenantAsync("UNKNOWN").get();
454+
fail("No error thrown for invalid response");
455+
} catch (ExecutionException e) {
456+
assertThat(e.getCause(), instanceOf(FirebaseAuthException.class));
457+
FirebaseAuthException authException = (FirebaseAuthException) e.getCause();
458+
assertEquals(FirebaseUserManager.TENANT_NOT_FOUND_ERROR, authException.getErrorCode());
459+
}
460+
checkUrl(interceptor, "GET", TENANTS_BASE_URL + "/UNKNOWN");
461+
}
462+
437463
@Test
438464
public void testListTenants() throws Exception {
439465
final TestResponseInterceptor interceptor = initializeAppForUserManagement(

src/test/resources/getTenant.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name" : "TENANT_1",
3+
"displayName" : "DISPLAY_NAME",
4+
"allowPasswordSignup" : true,
5+
"enableEmailLinkSignin" : false,
6+
"disableAuth" : true,
7+
"enableAnonymousUser" : false
8+
}

0 commit comments

Comments
 (0)