Skip to content

Commit

Permalink
filter social connections by name instead of strategy. hide those wit…
Browse files Browse the repository at this point in the history
…hout connections
  • Loading branch information
lbalmaceda committed Sep 6, 2016
1 parent e234fd9 commit 14267ed
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
7 changes: 4 additions & 3 deletions lib/src/main/java/com/auth0/android/lock/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,13 @@ private Strategy filterStrategy(Strategy strategy, Set<String> connections) {
}

private List<Strategy> filterSocialStrategies(List<Strategy> strategies, Set<String> connections) {
if (strategies == null || connections.isEmpty()) {
return strategies;
if (strategies == null) {
return null;
}
List<Strategy> filtered = new ArrayList<>(strategies.size());
for (Strategy strategy : strategies) {
if (connections.contains(strategy.getName())) {
final boolean allowed = connections.isEmpty() || connections.contains(strategy.getDefaultConnectionName());
if (allowed && strategy.getDefaultConnectionName() != null) {
filtered.add(strategy);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public boolean isActiveFlowEnabled() {
}

/**
* Returns the name of the first connection found in this strategy. When no connections are available,
* it will return the strategy name if this is a social connection or return null in any other case.
* Returns the name of the first connection found in this strategy. When no connections
* are available it will return null.
*
* @return the first connection found or null if no connections available.
*/
Expand All @@ -86,6 +86,6 @@ public String getDefaultConnectionName() {
if (!connections.isEmpty()) {
return connections.get(0).getName();
}
return strategyMetadata.getType() == Strategies.Type.SOCIAL ? this.name : null;
return null;
}
}
16 changes: 14 additions & 2 deletions lib/src/test/java/com/auth0/android/lock/ConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import static com.auth0.android.lock.utils.Strategies.GoogleApps;
import static com.auth0.android.lock.utils.Strategies.GooglePlus;
import static com.auth0.android.lock.utils.Strategies.Instagram;
import static com.auth0.android.lock.utils.Strategies.Linkedin;
import static com.auth0.android.lock.utils.Strategies.SMS;
import static com.auth0.android.lock.utils.Strategies.Twitter;
import static com.auth0.android.lock.utils.Strategies.Yahoo;
Expand All @@ -66,6 +67,8 @@
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
Expand Down Expand Up @@ -502,10 +505,19 @@ public void shouldReturnEmptyPasswordlessStrategiesIfNoneMatch() throws Exceptio
}

@Test
public void shouldReturnUnfilteredSocialStrategies() throws Exception {
public void shouldReturnUnfilteredSocialStrategiesWithConnections() throws Exception {
configuration = unfilteredConfig();
final List<Strategy> strategies = configuration.getSocialStrategies();
assertThat(strategies, containsInAnyOrder(isStrategy(Facebook), isStrategy(Twitter), isStrategy(Instagram), isStrategy(GooglePlus)));
assertThat(strategies, hasItems(isStrategy(Facebook), isStrategy(Twitter), isStrategy(Instagram), isStrategy(GooglePlus)));
assertThat(strategies, not(hasItem(isStrategy(Linkedin))));
}

@Test
public void shouldNotReturnFilteredSocialStrategiesWithoutConnections() throws Exception {
configuration = filteredConfigBy(Facebook.getName(), Linkedin.getName());
final List<Strategy> strategies = configuration.getSocialStrategies();
assertThat(strategies, hasItem(isStrategy(Facebook)));
assertThat(strategies, not(hasItem(isStrategy(Linkedin))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public void shouldReturnTheFirstConnectionName() throws Exception {
}

@Test
public void shouldReturnStrategyNameWhenNoConnectionsAndTypeSocial() throws Exception {
public void shouldReturnNullStrategyNameWhenNoConnectionsAndTypeSocial() throws Exception {
Strategy strategy = new Strategy("facebook", Collections.<Connection>emptyList());
assertThat(strategy.getDefaultConnectionName(), is("facebook"));
assertThat(strategy.getDefaultConnectionName(), is(nullValue()));
assertThat(strategy.getConnections().isEmpty(), is(true));
}

Expand Down
3 changes: 3 additions & 0 deletions lib/src/test/resources/appinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,8 @@
"connections": [{
"name": "twitter"
}]
}, {
"name": "linkedin",
"connections": []
}]
}

0 comments on commit 14267ed

Please sign in to comment.