Skip to content

Commit

Permalink
Merge pull request #340 from auth0/fix-social-connections-filter
Browse files Browse the repository at this point in the history
Filter social strategies by connection name
  • Loading branch information
hzalaz authored Sep 9, 2016
2 parents f0dc721 + 3a7b68c commit e03c682
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
11 changes: 7 additions & 4 deletions lib/src/main/java/com/auth0/android/lock/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,16 @@ 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())) {
filtered.add(strategy);
for (Connection connection : strategy.getConnections()) {
if (connections.isEmpty() || connections.contains(connection.getName())) {
filtered.add(strategy);
break;
}
}
}
return filtered;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,13 @@ 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.
*/
@Nullable
public String getDefaultConnectionName() {
if (!connections.isEmpty()) {
return connections.get(0).getName();
}
return strategyMetadata.getType() == Strategies.Type.SOCIAL ? this.name : null;
return !connections.isEmpty() ? connections.get(0).getName() : null;
}
}
24 changes: 22 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,27 @@ public void shouldReturnEmptyPasswordlessStrategiesIfNoneMatch() throws Exceptio
}

@Test
public void shouldReturnUnfilteredSocialStrategies() throws Exception {
public void shouldNotReturnDuplicatedSocialStrategies() throws Exception {
configuration = filteredConfigBy("twitter", "twitter-dev");
final List<Strategy> strategies = configuration.getSocialStrategies();
assertThat(strategies, hasItems(isStrategy(Twitter)));
assertThat(strategies, hasSize(1));
}

@Test
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
6 changes: 6 additions & 0 deletions lib/src/test/resources/appinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@
"name": "twitter",
"connections": [{
"name": "twitter"
},
{
"name": "twitter-dev"
}]
}, {
"name": "linkedin",
"connections": []
}]
}

0 comments on commit e03c682

Please sign in to comment.