Skip to content

Commit 5939e2b

Browse files
msohailhussainaliabbasrizvi
authored andcommitted
bot filtering (#79)
1 parent da13545 commit 5939e2b

File tree

14 files changed

+1363
-619
lines changed

14 files changed

+1363
-619
lines changed

OptimizelySDK.Net35/OptimizelySDK.Net35.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@
147147
</Compile>
148148
<Compile Include="..\OptimizelySDK\Utils\Validator.cs">
149149
<Link>Utils\Validator.cs</Link>
150+
</Compile>
151+
<Compile Include="..\OptimizelySDK\Utils\ControlAttributes.cs">
152+
<Link>Utils\ControlAttributes.cs</Link>
150153
</Compile>
151154
<Compile Include="Properties\AssemblyInfo.cs" />
152155
<Compile Include="..\OptimizelySDK\Bucketing\Bucketer.cs">

OptimizelySDK.Net40/OptimizelySDK.Net40.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@
148148
</Compile>
149149
<Compile Include="..\OptimizelySDK\Utils\Validator.cs">
150150
<Link>Utils\Validator.cs</Link>
151+
</Compile>
152+
<Compile Include="..\OptimizelySDK\Utils\ControlAttributes.cs">
153+
<Link>Utils\ControlAttributes.cs</Link>
151154
</Compile>
152155
<Compile Include="Properties\AssemblyInfo.cs" />
153156
<Compile Include="..\OptimizelySDK\Bucketing\Bucketer.cs">

OptimizelySDK.NetStandard16/OptimizelySDK.NetStandard16.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<Compile Include="..\OptimizelySDK\Utils\Validator.cs" />
4949
<Compile Include="..\OptimizelySDK\Utils\ConfigParser.cs" />
5050
<Compile Include="..\OptimizelySDK\Utils\Schema.cs" />
51+
<Compile Include="..\OptimizelySDK\Utils\ControlAttributes.cs" />
5152

5253
<Compile Include="..\OptimizelySDK\Bucketing\Bucketer.cs" />
5354
<Compile Include="..\OptimizelySDK\Bucketing\Decision.cs" />

OptimizelySDK.Tests/DecisionServiceTest.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using OptimizelySDK.Entity;
2323
using NUnit.Framework;
2424
using OptimizelySDK.Bucketing;
25+
using OptimizelySDK.Utils;
2526

2627
namespace OptimizelySDK.Tests
2728
{
@@ -388,13 +389,13 @@ public void TestGetVariationWithBucketingId()
388389
{"device_type", "iPhone"},
389390
{"company", "Optimizely"},
390391
{"location", "San Francisco"},
391-
{DecisionService.RESERVED_ATTRIBUTE_KEY_BUCKETING_ID, testBucketingIdVariation}
392+
{ControlAttributes.BUCKETING_ID_ATTRIBUTE, testBucketingIdVariation}
392393
};
393394

394395
var invalidUserAttributesWithBucketingId = new UserAttributes
395396
{
396397
{"company", "Optimizely"},
397-
{DecisionService.RESERVED_ATTRIBUTE_KEY_BUCKETING_ID, testBucketingIdControl}
398+
{ControlAttributes.BUCKETING_ID_ATTRIBUTE, testBucketingIdControl}
398399
};
399400

400401
var optlyObject = new Optimizely(TestData.Datafile, new ValidEventDispatcher(), LoggerMock.Object);

OptimizelySDK.Tests/EventTests/EventBuilderTest.cs

+490-22
Large diffs are not rendered by default.

OptimizelySDK.Tests/OptimizelyTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using OptimizelySDK.Bucketing;
2929
using OptimizelySDK.Notifications;
3030
using OptimizelySDK.Tests.NotificationTests;
31+
using OptimizelySDK.Utils;
3132

3233
namespace OptimizelySDK.Tests
3334
{
@@ -516,8 +517,7 @@ public void TestTrackInvalidAttributes()
516517

517518
Optimizely.Track("purchase", TestUserId, attributes);
518519

519-
//LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Provided attributes are in an invalid format."), Times.Once);
520-
ErrorHandlerMock.Verify(e => e.HandleError(It.IsAny<InvalidAttributeException>()), Times.Once);
520+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, @"Attribute key ""abc"" is not in datafile."), Times.Once);
521521
}
522522

523523
[Test]
@@ -1112,7 +1112,7 @@ public void TestGetVariationBucketingIdAttribute()
11121112
{ "device_type", "iPhone" },
11131113
{ "company", "Optimizely" },
11141114
{ "location", "San Francisco" },
1115-
{ DecisionService.RESERVED_ATTRIBUTE_KEY_BUCKETING_ID, testBucketingIdVariation }
1115+
{ ControlAttributes.BUCKETING_ID_ATTRIBUTE, testBucketingIdVariation }
11161116
};
11171117

11181118
// confirm that a valid variation is bucketed without the bucketing ID

OptimizelySDK.Tests/ProjectConfigTest.cs

+56-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017, Optimizely
2+
* Copyright 2017-2018, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,9 @@
2222
using OptimizelySDK.Exceptions;
2323
using NUnit.Framework;
2424
using OptimizelySDK.Entity;
25+
using Newtonsoft.Json;
26+
using OptimizelySDK.Event.Builder;
27+
using OptimizelySDK.Utils;
2528

2629
namespace OptimizelySDK.Tests
2730
{
@@ -51,14 +54,14 @@ public static Dictionary<string, object> CreateDictionary(string name, object en
5154
public void TestInit()
5255
{
5356
// Check Version
54-
Assert.AreEqual(4, Config.Version);
57+
Assert.AreEqual("4", Config.Version);
5558

5659
// Check Account ID
5760
Assert.AreEqual("1592310167", Config.AccountId);
5861
// Check Project ID
5962
Assert.AreEqual("7720880029", Config.ProjectId);
6063
// Check Revision
61-
Assert.AreEqual(15, Config.Revision);
64+
Assert.AreEqual("15", Config.Revision);
6265

6366
// Check Group ID Map
6467
var expectedGroupId = CreateDictionary("7722400015", Config.GetGroup("7722400015"));
@@ -810,5 +813,55 @@ public void TestVariationFeatureEnabledProperty()
810813
var variation = Config.GetVariationFromKey("test_experiment", "control");
811814
Assert.IsFalse(variation.IsFeatureEnabled);
812815
}
816+
817+
[Test]
818+
public void TestBotFilteringValues()
819+
{
820+
// Verify that bot filtering value is true as defined in Config data.
821+
Assert.True(Config.BotFiltering.GetValueOrDefault());
822+
823+
// Remove botFilering node and verify returned value in null.
824+
JObject projConfig = JObject.Parse(TestData.Datafile);
825+
if (projConfig.TryGetValue("botFiltering", out JToken token))
826+
{
827+
projConfig.Property("botFiltering").Remove();
828+
var configWithoutBotFilter = ProjectConfig.Create(JsonConvert.SerializeObject(projConfig),
829+
LoggerMock.Object, ErrorHandlerMock.Object);
830+
831+
// Verify that bot filtering is null when not defined in datafile.
832+
Assert.Null(configWithoutBotFilter.BotFiltering);
833+
}
834+
}
835+
836+
[Test]
837+
public void TestGetAttributeIdWithReservedPrefix()
838+
{
839+
// Verify that attribute key is returned for reserved attribute key.
840+
Assert.AreEqual(Config.GetAttributeId(ControlAttributes.USER_AGENT_ATTRIBUTE), ControlAttributes.USER_AGENT_ATTRIBUTE);
841+
842+
// Verify that attribute Id is returned for attribute key with reserved prefix that does not exist in datafile.
843+
Assert.AreEqual(Config.GetAttributeId("$opt_reserved_prefix_attribute"), "$opt_reserved_prefix_attribute");
844+
845+
// Create config file copy with additional resered prefix attribute.
846+
string reservedPrefixAttrKey = "$opt_user_defined_attribute";
847+
JObject projConfig = JObject.Parse(TestData.Datafile);
848+
var attributes = (JArray)projConfig["attributes"];
849+
850+
var reservedAttr = new Entity.Attribute { Id = "7723348204", Key = reservedPrefixAttrKey };
851+
attributes.Add((JObject)JToken.FromObject(reservedAttr));
852+
853+
// Verify that attribute Id is returned and warning is logged for attribute key with reserved prefix that exists in datafile.
854+
var reservedAttrConfig = ProjectConfig.Create(JsonConvert.SerializeObject(projConfig), LoggerMock.Object, ErrorHandlerMock.Object);
855+
Assert.AreEqual(reservedAttrConfig.GetAttributeId(reservedPrefixAttrKey), reservedAttrConfig.GetAttribute(reservedPrefixAttrKey).Id);
856+
LoggerMock.Verify(l => l.Log(LogLevel.WARN, $@"Attribute {reservedPrefixAttrKey} unexpectedly has reserved prefix {ProjectConfig.RESERVED_ATTRIBUTE_PREFIX}; using attribute ID instead of reserved attribute name."));
857+
}
858+
859+
[Test]
860+
public void TestGetAttributeIdWithInvalidAttributeKey()
861+
{
862+
// Verify that null is returned when provided attribute key is invalid.
863+
Assert.Null(Config.GetAttributeId("invalid_attribute"));
864+
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, @"Attribute key ""invalid_attribute"" is not in datafile."));
865+
}
813866
}
814867
}

0 commit comments

Comments
 (0)