Skip to content

Commit

Permalink
add tests for setting metadata filters by default
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Mar 1, 2018
1 parent dfc4c12 commit ffa0809
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
41 changes: 41 additions & 0 deletions sdk/src/androidTest/java/com/bugsnag/android/MetaDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand All @@ -17,13 +18,21 @@
import java.util.Map;

import static com.bugsnag.android.BugsnagTestUtils.streamableToJson;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class MetaDataTest {

private Client client;

@Before
public void setUp() throws Exception {
client = BugsnagTestUtils.generateClient();
}

@Test
public void testBasicSerialization() throws JSONException, IOException {
MetaData metaData = new MetaData();
Expand Down Expand Up @@ -188,4 +197,36 @@ public void testNestedFiltering() throws JSONException, IOException {
assertEquals("[FILTERED]", sensitiveMapJson.getString("confirm_password"));
assertEquals("safe", sensitiveMapJson.getString("normal"));
}

@Test
public void testFilterConstructor() throws Exception {
MetaData metaData = client.getMetaData();
metaData.addToTab("foo", "password", "abc123");
JSONObject jsonObject = streamableToJson(metaData);

assertArrayEquals(new String[]{"password"}, metaData.getFilters());
assertEquals("[FILTERED]", jsonObject.getJSONObject("foo").get("password"));
}

@Test
public void testFilterSetter() throws Exception {
MetaData metaData = new MetaData();
client.setMetaData(metaData);
assertArrayEquals(new String[]{"password"}, metaData.getFilters());
}

@Test
public void testFilterOverride() throws Exception {
MetaData metaData = client.getMetaData();
client.setFilters("test", "another");
assertArrayEquals(new String[]{"test", "another"}, metaData.getFilters());
}

@Test
public void testFilterMetadataOverride() throws Exception {
MetaData data = new MetaData();
data.setFilters("CUSTOM");
client.setMetaData(data);
assertArrayEquals(new String[]{"CUSTOM"}, data.getFilters());
}
}
13 changes: 10 additions & 3 deletions sdk/src/main/java/com/bugsnag/android/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class Configuration extends Observable implements Observer {
private String endpoint = "https://notify.bugsnag.com";
private String sessionEndpoint = "https://sessions.bugsnag.com";

private String[] filters = new String[]{"password"};
private String[] ignoreClasses;
@Nullable
private String[] notifyReleaseStages = null;
Expand Down Expand Up @@ -64,6 +63,7 @@ public class Configuration extends Observable implements Observer {
public Configuration(@NonNull String apiKey) {
this.apiKey = apiKey;
this.metaData = new MetaData();
setDefaultMetaDataFilters();
this.metaData.addObserver(this);
}

Expand Down Expand Up @@ -190,7 +190,7 @@ public void setBuildUUID(String buildUuid) {
* @return Filters
*/
public String[] getFilters() {
return filters;
return metaData.getFilters();
}

/**
Expand All @@ -207,7 +207,6 @@ public String[] getFilters() {
* @param filters a list of keys to filter from metaData
*/
public void setFilters(String[] filters) {
this.filters = filters;
this.metaData.setFilters(filters);
}

Expand Down Expand Up @@ -389,6 +388,10 @@ protected void setMetaData(@NonNull MetaData metaData) {
this.metaData = metaData;
}

if (this.metaData.getFilters() == null) {
setDefaultMetaDataFilters();
}

this.metaData.addObserver(this);
notifyBugsnagObservers(NotifyType.META);
}
Expand Down Expand Up @@ -577,6 +580,10 @@ protected boolean inProject(@NonNull String className) {
return false;
}

private void setDefaultMetaDataFilters() {
metaData.setFilters("password");
}

private void notifyBugsnagObservers(@NonNull NotifyType type) {
setChanged();
super.notifyObservers(type.getValue());
Expand Down
4 changes: 4 additions & 0 deletions sdk/src/main/java/com/bugsnag/android/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ void setFilters(String... filters) {
notifyBugsnagObservers(NotifyType.FILTERS);
}

String[] getFilters() {
return filters;
}

@NonNull
static MetaData merge(@NonNull MetaData... metaDataList) {
List<Map<String, Object>> stores = new ArrayList<>();
Expand Down

0 comments on commit ffa0809

Please sign in to comment.