Skip to content

Commit 466feef

Browse files
mfahadahmedkellyroach-optimizely
authored andcommitted
Input validation in Activate, Track and GetVariation methods. (#76)
* Input validation in Activate, Track and GetVariation methods. * Replaced InputType enum with string constants. * Updates unit tests.
1 parent cd99d97 commit 466feef

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

OptimizelySDK.Tests/OptimizelyTest.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,5 +1812,51 @@ public void TestGetEnabledFeaturesWithSomeFeaturesEnabledForUser()
18121812
}
18131813

18141814
#endregion // Test GetEnabledFeatures
1815+
1816+
#region Test ValidateStringInputs
1817+
1818+
[Test]
1819+
public void TestActivateValidateInputValues()
1820+
{
1821+
// Verify that ValidateStringInputs does not log error for valid values.
1822+
var variation = Optimizely.Activate("test_experiment", "test_user");
1823+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided User Id is in invalid format."), Times.Never);
1824+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided Experiment Key is in invalid format."), Times.Never);
1825+
1826+
// Verify that ValidateStringInputs logs error for invalid values.
1827+
variation = Optimizely.Activate("", null);
1828+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided User Id is in invalid format."), Times.Once);
1829+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided Experiment Key is in invalid format."), Times.Once);
1830+
}
1831+
1832+
[Test]
1833+
public void TestGetVariationValidateInputValues()
1834+
{
1835+
// Verify that ValidateStringInputs does not log error for valid values.
1836+
var variation = Optimizely.GetVariation("test_experiment", "test_user");
1837+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided User Id is in invalid format."), Times.Never);
1838+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided Experiment Key is in invalid format."), Times.Never);
1839+
1840+
// Verify that ValidateStringInputs logs error for invalid values.
1841+
variation = Optimizely.GetVariation("", null);
1842+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided User Id is in invalid format."), Times.Once);
1843+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided Experiment Key is in invalid format."), Times.Once);
1844+
}
1845+
1846+
[Test]
1847+
public void TestTrackValidateInputValues()
1848+
{
1849+
// Verify that ValidateStringInputs does not log error for valid values.
1850+
Optimizely.Track("purchase", "test_user");
1851+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided User Id is in invalid format."), Times.Never);
1852+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided Event Key is in invalid format."), Times.Never);
1853+
1854+
// Verify that ValidateStringInputs logs error for invalid values.
1855+
Optimizely.Track("", null);
1856+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided User Id is in invalid format."), Times.Once);
1857+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided Event Key is in invalid format."), Times.Once);
1858+
}
1859+
1860+
#endregion // Test ValidateStringInputs
18151861
}
18161862
}

OptimizelySDK/Optimizely.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public static String SDK_TYPE {
7171
}
7272
}
7373

74+
public const string USER_ID = "User Id";
75+
public const string EXPERIMENT_KEY = "Experiment Key";
76+
public const string EVENT_KEY = "Event Key";
77+
7478
/// <summary>
7579
/// Optimizely constructor for managing Full Stack .NET projects.
7680
/// </summary>
@@ -158,6 +162,15 @@ public Variation Activate(string experimentKey, string userId, UserAttributes us
158162
return null;
159163
}
160164

165+
var inputValues = new Dictionary<string, string>
166+
{
167+
{ USER_ID, userId },
168+
{ EXPERIMENT_KEY, experimentKey }
169+
};
170+
171+
if (!ValidateStringInputs(inputValues))
172+
return null;
173+
161174
var experiment = Config.GetExperimentFromKey(experimentKey);
162175

163176
if (experiment.Key == null)
@@ -210,6 +223,15 @@ public void Track(string eventKey, string userId, UserAttributes userAttributes
210223
return;
211224
}
212225

226+
var inputValues = new Dictionary<string, string>
227+
{
228+
{ USER_ID, userId },
229+
{ EVENT_KEY, eventKey }
230+
};
231+
232+
if (!ValidateStringInputs(inputValues))
233+
return;
234+
213235
var eevent = Config.GetEvent(eventKey);
214236

215237
if (eevent.Key == null)
@@ -290,6 +312,15 @@ public Variation GetVariation(string experimentKey, string userId, UserAttribute
290312
return null;
291313
}
292314

315+
var inputValues = new Dictionary<string, string>
316+
{
317+
{ USER_ID, userId },
318+
{ EXPERIMENT_KEY, experimentKey }
319+
};
320+
321+
if (!ValidateStringInputs(inputValues))
322+
return null;
323+
293324
Experiment experiment = Config.GetExperimentFromKey(experimentKey);
294325
if (experiment.Key == null)
295326
return null;
@@ -595,5 +626,25 @@ public List<string> GetEnabledFeatures(string userId, UserAttributes userAttribu
595626
}
596627

597628
#endregion // FeatureFlag APIs
629+
630+
/// <summary>
631+
/// Validate all string inputs are not null or empty.
632+
/// </summary>
633+
/// <param name="inputs">Array Hash input types and values</param>
634+
/// <returns>True if all values are valid, false otherwise</returns>
635+
private bool ValidateStringInputs(Dictionary<string, string> inputs)
636+
{
637+
bool isValid = true;
638+
foreach(var input in inputs)
639+
{
640+
if (string.IsNullOrEmpty(input.Value))
641+
{
642+
Logger.Log(LogLevel.ERROR, $"Provided {input.Key} is in invalid format.");
643+
isValid = false;
644+
}
645+
}
646+
647+
return isValid;
648+
}
598649
}
599650
}

0 commit comments

Comments
 (0)