diff --git a/src/main/java/com/google/firebase/auth/Tenant.java b/src/main/java/com/google/firebase/auth/Tenant.java index f64d93a78..366d9e997 100644 --- a/src/main/java/com/google/firebase/auth/Tenant.java +++ b/src/main/java/com/google/firebase/auth/Tenant.java @@ -16,8 +16,13 @@ package com.google.firebase.auth; +import static com.google.common.base.Preconditions.checkArgument; + import com.google.api.client.util.Key; -import com.google.auto.value.AutoValue; +import com.google.common.base.Strings; +import com.google.common.collect.ImmutableMap; +import java.util.HashMap; +import java.util.Map; /** * Contains metadata associated with a Firebase tenant. @@ -26,7 +31,7 @@ */ public final class Tenant { - @Key("tenantId") + @Key("name") private String tenantId; @Key("displayName") @@ -55,100 +60,136 @@ public boolean isEmailLinkSignInEnabled() { } /** - * Class used to hold the information needs to make a tenant create request. + * Returns a new {@link UpdateRequest}, which can be used to update the attributes + * of this tenant. + * + * @return a non-null Tenant.UpdateRequest instance. */ - @AutoValue - public abstract static class CreateRequest { + public UpdateRequest updateRequest() { + return new UpdateRequest(getTenantId()); + } + + /** + * A specification class for creating new user accounts. + * + *

Set the initial attributes of the new tenant by calling various setter methods available in + * this class. None of the attributes are required. + */ + public static final class CreateRequest { + + private final Map properties = new HashMap<>(); /** - * Returns the display name of this tenant. + * Creates a new {@link CreateRequest}, which can be used to create a new tenant. * - * @return a non-empty display name string. + *

The returned object should be passed to {@link TenantManager#createTenant(CreateRequest)} + * to register the tenant information persistently. */ - public abstract String getDisplayName(); + public CreateRequest() { } /** - * Returns whether to allow email/password user authentication. + * Sets the display name for the new tenant. * - * @return true if a user can be authenticated using an email and password, and false otherwise. + * @param displayName a non-null, non-empty display name string. */ - public abstract boolean isPasswordSignInAllowed(); + public CreateRequest setDisplayName(String displayName) { + checkArgument(!Strings.isNullOrEmpty(displayName), "display name must not be null or empty"); + properties.put("displayName", displayName); + return this; + } /** - * Returns whether to enable email link user authentication. + * Sets whether to allow email/password user authentication. * - * @return true if a user can be authenticated using an email link, and false otherwise. + * @param passwordSignInAllowed a boolean indicating whether users can be authenticated using + * an email and password, and false otherwise. */ - public abstract boolean isEmailLinkSignInEnabled(); + public CreateRequest setPasswordSignInAllowed(boolean passwordSignInAllowed) { + properties.put("allowPasswordSignup", passwordSignInAllowed); + return this; + } /** - * Returns a builder for a tenant create request. + * Sets whether to enable email link user authentication. + * + * @param emailLinkSignInEnabled a boolean indicating whether users can be authenticated using + * an email link, and false otherwise. */ - public static Builder newBuilder() { - return new AutoValue_Tenant_CreateRequest.Builder(); + public CreateRequest setEmailLinkSignInEnabled(boolean emailLinkSignInEnabled) { + properties.put("enableEmailLinkSignin", emailLinkSignInEnabled); + return this; } - /** - * Builder class used to construct a create request. - */ - @AutoValue.Builder - abstract static class Builder { - public abstract Builder setDisplayName(String displayName); - - public abstract Builder setPasswordSignInAllowed(boolean allowPasswordSignIn); - - public abstract Builder setEmailLinkSignInEnabled(boolean enableEmailLinkSignIn); - - public abstract CreateRequest build(); + Map getProperties() { + return ImmutableMap.copyOf(properties); } } /** - * Class used to hold the information needs to make a tenant update request. + * A class for updating the attributes of an existing tenant. + * + *

An instance of this class can be obtained via a {@link Tenant} object, or from a tenant ID + * string. Specify the changes to be made to the tenant by calling the various setter methods + * available in this class. */ - @AutoValue - public abstract static class UpdateRequest { + public static final class UpdateRequest { + + private final Map properties = new HashMap<>(); /** - * Returns the display name of this tenant. + * Creates a new {@link UpdateRequest}, which can be used to update the attributes of the + * of the tenant identified by the specified tenant ID. + * + *

This method allows updating attributes of a tenant account, without first having to call + * {@link TenantManager#getTenant(String)}. * - * @return a non-empty display name string. + * @param tenantId a non-null, non-empty tenant ID string. + * @throws IllegalArgumentException If the tenant ID is null or empty. */ - public abstract String getDisplayName(); + public UpdateRequest(String tenantId) { + checkArgument(!Strings.isNullOrEmpty(tenantId), "tenant ID must not be null or empty"); + properties.put("name", tenantId); + } + + String getTenantId() { + return (String) properties.get("name"); + } /** - * Returns whether to allow email/password user authentication. + * Sets the display name of the existingtenant. * - * @return true if a user can be authenticated using an email and password, and false otherwise. + * @param displayName a non-null, non-empty display name string. */ - public abstract boolean isPasswordSignInAllowed(); + public UpdateRequest setDisplayName(String displayName) { + checkArgument(!Strings.isNullOrEmpty(displayName), "display name must not be null or empty"); + properties.put("displayName", displayName); + return this; + } /** - * Returns whether to enable email link user authentication. + * Sets whether to allow email/password user authentication. * - * @return true if a user can be authenticated using an email link, and false otherwise. + * @param passwordSignInAllowed a boolean indicating whether users can be authenticated using + * an email and password, and false otherwise. */ - public abstract boolean isEmailLinkSignInEnabled(); + public UpdateRequest setPasswordSignInAllowed(boolean passwordSignInAllowed) { + properties.put("allowPasswordSignup", passwordSignInAllowed); + return this; + } /** - * Returns a builder for a tenant update request. + * Sets whether to enable email link user authentication. + * + * @param emailLinkSignInEnabled a boolean indicating whether users can be authenticated using + * an email link, and false otherwise. */ - public static Builder newBuilder() { - return new AutoValue_Tenant_UpdateRequest.Builder(); + public UpdateRequest setEmailLinkSignInEnabled(boolean emailLinkSignInEnabled) { + properties.put("enableEmailLinkSignin", emailLinkSignInEnabled); + return this; } - /** - * Builder class used to construct a update request. - */ - @AutoValue.Builder - abstract static class Builder { - public abstract Builder setDisplayName(String displayName); - - public abstract Builder setPasswordSignInAllowed(boolean allowPasswordSignIn); - - public abstract Builder setEmailLinkSignInEnabled(boolean enableEmailLinkSignIn); - - public abstract UpdateRequest build(); + Map getProperties() { + return ImmutableMap.copyOf(properties); } } } diff --git a/src/main/java/com/google/firebase/auth/UserRecord.java b/src/main/java/com/google/firebase/auth/UserRecord.java index e00450079..1e199e015 100644 --- a/src/main/java/com/google/firebase/auth/UserRecord.java +++ b/src/main/java/com/google/firebase/auth/UserRecord.java @@ -357,10 +357,10 @@ public CreateRequest setEmailVerified(boolean emailVerified) { /** * Sets the display name for the new user. * - * @param displayName a non-null, non-empty display name string. + * @param displayName a non-null display name string. */ public CreateRequest setDisplayName(String displayName) { - checkNotNull(displayName, "displayName cannot be null or empty"); + checkNotNull(displayName, "displayName cannot be null"); properties.put("displayName", displayName); return this; } diff --git a/src/test/java/com/google/firebase/auth/ListTenantsPageTest.java b/src/test/java/com/google/firebase/auth/ListTenantsPageTest.java index 227e3dca4..c1d09bc52 100644 --- a/src/test/java/com/google/firebase/auth/ListTenantsPageTest.java +++ b/src/test/java/com/google/firebase/auth/ListTenantsPageTest.java @@ -321,7 +321,7 @@ public void testInvalidMaxResults() throws IOException { private static Tenant newTenant(String tenantId) throws IOException { return Utils.getDefaultJsonFactory().fromString( - String.format("{\"tenantId\":\"%s\"}", tenantId), Tenant.class); + String.format("{\"name\":\"%s\"}", tenantId), Tenant.class); } private static class TestTenantSource implements ListTenantsPage.TenantSource { diff --git a/src/test/java/com/google/firebase/auth/TenantTest.java b/src/test/java/com/google/firebase/auth/TenantTest.java new file mode 100644 index 000000000..22209e8ad --- /dev/null +++ b/src/test/java/com/google/firebase/auth/TenantTest.java @@ -0,0 +1,97 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.firebase.auth; + +import static com.google.firebase.auth.Tenant.UpdateRequest; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import com.google.api.client.googleapis.util.Utils; +import com.google.api.client.json.JsonFactory; +import java.io.IOException; +import java.util.Map; +import org.junit.Test; + +public class TenantTest { + + private static final JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); + + private static final String TENANT_JSON_STRING = + "{" + + "\"name\":\"TENANT_ID\"," + + "\"displayName\":\"DISPLAY_NAME\"," + + "\"allowPasswordSignup\":true," + + "\"enableEmailLinkSignin\":false" + + "}"; + + @Test + public void testJsonSerialization() throws IOException { + Tenant tenant = jsonFactory.fromString(TENANT_JSON_STRING, Tenant.class); + + assertEquals(tenant.getTenantId(), "TENANT_ID"); + assertEquals(tenant.getDisplayName(), "DISPLAY_NAME"); + assertTrue(tenant.isPasswordSignInAllowed()); + assertFalse(tenant.isEmailLinkSignInEnabled()); + } + + @Test + public void testUpdateRequestFromTenant() throws IOException { + Tenant tenant = jsonFactory.fromString(TENANT_JSON_STRING, Tenant.class); + + Tenant.UpdateRequest updateRequest = tenant.updateRequest(); + + assertEquals("TENANT_ID", updateRequest.getTenantId()); + Map properties = updateRequest.getProperties(); + assertEquals(properties.size(), 1); + assertEquals("TENANT_ID", (String) properties.get("name")); + } + + @Test + public void testUpdateRequestFromTenantId() throws IOException { + Tenant.UpdateRequest updateRequest = new Tenant.UpdateRequest("TENANT_ID"); + updateRequest + .setDisplayName("DISPLAY_NAME") + .setPasswordSignInAllowed(false) + .setEmailLinkSignInEnabled(true); + + assertEquals("TENANT_ID", updateRequest.getTenantId()); + Map properties = updateRequest.getProperties(); + assertEquals(properties.size(), 4); + assertEquals("TENANT_ID", (String) properties.get("name")); + assertEquals("DISPLAY_NAME", (String) properties.get("displayName")); + assertFalse((boolean) properties.get("allowPasswordSignup")); + assertTrue((boolean) properties.get("enableEmailLinkSignin")); + } + + @Test + public void testCreateRequest() throws IOException { + Tenant.CreateRequest createRequest = new Tenant.CreateRequest(); + createRequest + .setDisplayName("DISPLAY_NAME") + .setPasswordSignInAllowed(false) + .setEmailLinkSignInEnabled(true); + + Map properties = createRequest.getProperties(); + assertEquals(properties.size(), 3); + assertEquals("DISPLAY_NAME", (String) properties.get("displayName")); + assertFalse((boolean) properties.get("allowPasswordSignup")); + assertTrue((boolean) properties.get("enableEmailLinkSignin")); + } +} +