Skip to content

Add updateRequest method to Tenant class and add unit tests. #361

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 7 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 97 additions & 56 deletions src/main/java/com/google/firebase/auth/Tenant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -26,7 +31,7 @@
*/
public final class Tenant {

@Key("tenantId")
@Key("name")
private String tenantId;

@Key("displayName")
Expand Down Expand Up @@ -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.
*
* <p>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<String,Object> 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.
* <p>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<String, Object> 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.
*
* <p>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<String,Object> 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.
*
* <p>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<String, Object> getProperties() {
return ImmutableMap.copyOf(properties);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/google/firebase/auth/UserRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
97 changes: 97 additions & 0 deletions src/test/java/com/google/firebase/auth/TenantTest.java
Original file line number Diff line number Diff line change
@@ -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<String,Object> 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<String,Object> 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<String,Object> properties = createRequest.getProperties();
assertEquals(properties.size(), 3);
assertEquals("DISPLAY_NAME", (String) properties.get("displayName"));
assertFalse((boolean) properties.get("allowPasswordSignup"));
assertTrue((boolean) properties.get("enableEmailLinkSignin"));
}
}