Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #119 from launchdarkly/eb/ch32176/no-user-no-event
Browse files Browse the repository at this point in the history
track or identify without a valid user shouldn't send an event
  • Loading branch information
eli-darkly authored Feb 22, 2019
2 parents 9f3eac2 + 5d208ae commit c6cee01
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/launchdarkly/client/LDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ public void track(String eventName, LDUser user, JsonElement data) {
}
if (user == null || user.getKey() == null) {
logger.warn("Track called with null user or null user key!");
} else {
eventProcessor.sendEvent(EventFactory.DEFAULT.newCustomEvent(eventName, user, data));
}
eventProcessor.sendEvent(EventFactory.DEFAULT.newCustomEvent(eventName, user, data));
}

@Override
Expand All @@ -132,8 +133,9 @@ public void track(String eventName, LDUser user) {
public void identify(LDUser user) {
if (user == null || user.getKey() == null) {
logger.warn("Identify called with null user or null user key!");
} else {
eventProcessor.sendEvent(EventFactory.DEFAULT.newIdentifyEvent(user));
}
eventProcessor.sendEvent(EventFactory.DEFAULT.newIdentifyEvent(user));
}

private void sendFlagRequestEvent(Event.FeatureRequest event) {
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/launchdarkly/client/LDClientEventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

public class LDClientEventTest {
private static final LDUser user = new LDUser("userkey");
private static final LDUser userWithNullKey = new LDUser.Builder((String)null).build();

private FeatureStore featureStore = TestUtil.initedFeatureStore();
private TestUtil.TestEventProcessor eventSink = new TestUtil.TestEventProcessor();
Expand All @@ -43,6 +44,18 @@ public void identifySendsEvent() throws Exception {
Event.Identify ie = (Event.Identify)e;
assertEquals(user.getKey(), ie.user.getKey());
}

@Test
public void identifyWithNullUserDoesNotSendEvent() {
client.identify(null);
assertEquals(0, eventSink.events.size());
}

@Test
public void identifyWithUserWithNoKeyDoesNotSendEvent() {
client.identify(userWithNullKey);
assertEquals(0, eventSink.events.size());
}

@Test
public void trackSendsEventWithoutData() throws Exception {
Expand Down Expand Up @@ -72,6 +85,18 @@ public void trackSendsEventWithData() throws Exception {
assertEquals(data, ce.data);
}

@Test
public void trackWithNullUserDoesNotSendEvent() {
client.track("eventkey", null);
assertEquals(0, eventSink.events.size());
}

@Test
public void trackWithUserWithNoKeyDoesNotSendEvent() {
client.track("eventkey", userWithNullKey);
assertEquals(0, eventSink.events.size());
}

@Test
public void boolVariationSendsEvent() throws Exception {
FeatureFlag flag = flagWithValue("key", jbool(true));
Expand Down

0 comments on commit c6cee01

Please sign in to comment.