Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 16 additions & 4 deletions auth/src/main/java/com/firebase/ui/auth/AuthUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,8 @@ public PhoneBuilder setDefaultCountryIso(@NonNull String iso) {
* https://en.wikipedia.org/wiki/ISO_3166-1
* and e-164 codes here: https://en.wikipedia.org/wiki/List_of_country_calling_codes
*
* @param whitelistedCountries a case insensitive list of country codes and/or isos to
* be whitelisted
* @param whitelistedCountries a non empty case insensitive list of country codes
* and/or isos to be whitelisted
*/
public PhoneBuilder setWhitelistedCountries(
@NonNull List<String> whitelistedCountries) {
Expand All @@ -678,6 +678,12 @@ public PhoneBuilder setWhitelistedCountries(
"You can either whitelist or blacklist country codes for phone " +
"authentication.");
}

String message = "Invalid argument: The whitelist is %s. For this case, " +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of: "For this case, don't use this method" how about something like "Only non-null and non-empty whitelists are valid. To specify no whitelist, do not call this method."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better. 👍

"don't use this method.";
Preconditions.checkNotNull(whitelistedCountries, String.format(message, "null"));
Preconditions.checkArgument(!whitelistedCountries.isEmpty(), String.format(message, "empty"));

addCountriesToBundle(whitelistedCountries, ExtraConstants.WHITELISTED_COUNTRIES);
return this;
}
Expand All @@ -696,8 +702,8 @@ public PhoneBuilder setWhitelistedCountries(
* https://en.wikipedia.org/wiki/ISO_3166-1
* and e-164 codes here: https://en.wikipedia.org/wiki/List_of_country_calling_codes
*
* @param blacklistedCountries a case insensitive list of country codes and/or isos to
* be blacklisted
* @param blacklistedCountries a non empty case insensitive list of country codes
* and/or isos to be blacklisted
*/
public PhoneBuilder setBlacklistedCountries(
@NonNull List<String> blacklistedCountries) {
Expand All @@ -706,6 +712,12 @@ public PhoneBuilder setBlacklistedCountries(
"You can either whitelist or blacklist country codes for phone " +
"authentication.");
}

String message = "Invalid argument: The blacklist is %s. For this case, " +
"don't use this method.";
Preconditions.checkNotNull(blacklistedCountries, String.format(message, "null"));
Preconditions.checkArgument(!blacklistedCountries.isEmpty(), String.format(message, "empty"));

addCountriesToBundle(blacklistedCountries, ExtraConstants.BLACKLISTED_COUNTRIES);
return this;
}
Expand Down
13 changes: 13 additions & 0 deletions auth/src/main/java/com/firebase/ui/auth/util/Preconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,17 @@ public static void checkConfigured(@NonNull Context context,
}
}
}

/**
* Ensures the truth of an expression involving parameters to the calling method.
*
* @param expression a boolean expression
* @param errorMessage the exception message to use if the check fails
* @throws IllegalArgumentException if {@code expression} is false
*/
public static void checkArgument(boolean expression, String errorMessage) {
if (!expression) {
throw new IllegalArgumentException(errorMessage);
}
}
}
29 changes: 29 additions & 0 deletions auth/src/test/java/com/firebase/ui/auth/AuthUITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -175,4 +176,32 @@ public void testPhoneBuilder_setBothBlacklistedAndWhitelistedCountries_expectIll
.build();
}

@Test(expected = IllegalArgumentException.class)
public void testPhoneBuilder_passEmptyListForWhitelistedCountries_expectIllegalArgumentException() {
new IdpConfig.PhoneBuilder()
.setWhitelistedCountries(new ArrayList<String>())
.build();
}

@Test(expected = NullPointerException.class)
public void testPhoneBuilder_passNullForWhitelistedCountries_expectNullPointerException() {
new IdpConfig.PhoneBuilder()
.setWhitelistedCountries(null)
.build();
}


@Test(expected = IllegalArgumentException.class)
public void testPhoneBuilder_passEmptyListForBlacklistedCountries_expectIllegalArgumentException() {
new IdpConfig.PhoneBuilder()
.setBlacklistedCountries(new ArrayList<String>())
.build();
}

@Test(expected = NullPointerException.class)
public void testPhoneBuilder_passNullForBlacklistedCountries_expectNullPointerException() {
new IdpConfig.PhoneBuilder()
.setBlacklistedCountries(null)
.build();
}
}