Skip to content

Commit 65c078b

Browse files
yasirfolio3jaeopt
authored andcommitted
Fixed get_variation validation issues. (#441)
1 parent 138fff5 commit 65c078b

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

OptimizelySDKCore/OptimizelySDKCore/OPTLYLoggerMessages.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2017-2019, Optimizely, Inc. and contributors *
2+
* Copyright 2017-2020, Optimizely, Inc. and contributors *
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. *
@@ -31,6 +31,8 @@ extern NSString *const OPTLYLoggerMessagesConversionFailure;
3131
extern NSString *const OPTLYLoggerMessagesUserIdInvalid;
3232
extern NSString *const OPTLYLoggerMessagesActivateExperimentKeyEmpty;
3333
extern NSString *const OPTLYLoggerMessagesActivateExperimentKeyInvalid;
34+
extern NSString *const OPTLYLoggerMessagesGetVariationExperimentKeyEmpty;
35+
extern NSString *const OPTLYLoggerMessagesGetVariationExperimentKeyInvalid;
3436
extern NSString *const OPTLYLoggerMessagesTrackEventKeyEmpty;
3537
extern NSString *const OPTLYLoggerMessagesTrackEventKeyInvalid;
3638
extern NSString *const OPTLYLoggerMessagesTrackEventNoAssociation;

OptimizelySDKCore/OptimizelySDKCore/OPTLYLoggerMessages.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2017-2019, Optimizely, Inc. and contributors *
2+
* Copyright 2017-2020, Optimizely, Inc. and contributors *
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. *
@@ -27,6 +27,8 @@
2727
NSString *const OPTLYLoggerMessagesUserIdInvalid = @"[OPTIMIZELY] User ID cannot be nil or an empty string.";
2828
NSString *const OPTLYLoggerMessagesActivateExperimentKeyEmpty = @"[OPTIMIZELY] Experiment Key cannot be nil or an empty string.";
2929
NSString *const OPTLYLoggerMessagesActivateExperimentKeyInvalid = @"[OPTIMIZELY] Experiment not found for Key %@.";
30+
NSString *const OPTLYLoggerMessagesGetVariationExperimentKeyEmpty = @"[OPTIMIZELY] Experiment Key cannot be nil or an empty string.";
31+
NSString *const OPTLYLoggerMessagesGetVariationExperimentKeyInvalid = @"[OPTIMIZELY] Experiment not found for Key %@.";
3032
NSString *const OPTLYLoggerMessagesTrackEventKeyEmpty = @"[OPTIMIZELY] Event Key cannot be nil or an empty string.";
3133
NSString *const OPTLYLoggerMessagesTrackEventKeyInvalid = @"[OPTIMIZELY] Event not found for Key %@.";
3234
NSString *const OPTLYLoggerMessagesTrackEventNoAssociation = @"[OPTIMIZELY] Event key %@ is not associated with any experiment.";

OptimizelySDKCore/OptimizelySDKCore/Optimizely.m

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2017-2019, Optimizely, Inc. and contributors *
2+
* Copyright 2017-2020, Optimizely, Inc. and contributors *
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. *
@@ -172,11 +172,31 @@ - (OPTLYVariation *)variation:(NSString *)experimentKey
172172
userId:(NSString *)userId
173173
attributes:(NSDictionary<NSString *, id> *)attributes
174174
{
175+
176+
NSMutableDictionary<NSString *, NSString *> *inputValues = [[NSMutableDictionary alloc] initWithDictionary:@{
177+
OPTLYNotificationUserIdKey:[self ObjectOrNull:userId],
178+
OPTLYNotificationExperimentKey:[self ObjectOrNull:experimentKey]}];
179+
NSDictionary <NSString *, NSString *> *logs = @{
180+
OPTLYNotificationUserIdKey:OPTLYLoggerMessagesUserIdInvalid,
181+
OPTLYNotificationExperimentKey:OPTLYLoggerMessagesGetVariationExperimentKeyEmpty};
182+
183+
if (![self validateStringInputs:inputValues logs:logs]) {
184+
return nil;
185+
}
186+
187+
// get experiment
188+
OPTLYExperiment *experiment = [self.config getExperimentForKey:experimentKey];
189+
190+
if (!experiment) {
191+
NSString *logMessage = [NSString stringWithFormat:OPTLYLoggerMessagesGetVariationExperimentKeyInvalid, experimentKey];
192+
[self.logger logMessage:logMessage withLevel:OptimizelyLogLevelError];
193+
return nil;
194+
}
195+
175196
OPTLYVariation *bucketedVariation = [self.config getVariationForExperiment:experimentKey
176197
userId:userId
177198
attributes:attributes
178199
bucketer:self.bucketer];
179-
OPTLYExperiment *experiment = [self.config getExperimentForKey:experimentKey];
180200
NSString *decisionType = [self.config isFeatureExperiment:experiment.experimentId] ? OPTLYDecisionTypeFeatureTest : OPTLYDecisionTypeABTest;
181201

182202
NSMutableDictionary *args = [[NSMutableDictionary alloc] init];

OptimizelySDKCore/OptimizelySDKCoreTests/OptimizelyTest.m

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016-2019, Optimizely, Inc. and contributors *
2+
* Copyright 2016-2020, Optimizely, Inc. and contributors *
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. *
@@ -255,28 +255,63 @@ - (void)testDecisionNotificationForBasicGetVariation {
255255
XCTAssertNotNil(experiment);
256256
OPTLYVariation *variation;
257257

258-
// test just experiment key
258+
__block NSString *decisionNotificationExperimentKey = nil;
259+
__block NSString *decisionNotificationVariationKey = nil;
260+
261+
[self.optimizely.notificationCenter addDecisionNotificationListener:^(NSString * _Nonnull type, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nonnull decisionInfo) {
262+
decisionNotificationExperimentKey = decisionInfo[ExperimentDecisionInfo.ExperimentKey];
263+
decisionNotificationVariationKey = decisionInfo[ExperimentDecisionInfo.VariationKey];
264+
}];
265+
259266
variation = [self.optimizely variation:experimentKey userId:kUserId];
260267
XCTAssertNotNil(variation);
261268
XCTAssertTrue([variation.variationKey isEqualToString:@"control"]);
262269
XCTAssertTrue([variation.variationId isEqualToString:@"6384330451"]);
270+
XCTAssertEqualObjects(decisionNotificationExperimentKey, experimentKey);
271+
XCTAssertEqualObjects(decisionNotificationVariationKey, variation.variationKey);
272+
}
273+
274+
- (void)testDecisionNotificationForBadExperimentKey {
275+
__block NSString *decisionNotificationExperimentKey = nil;
276+
__block NSString *decisionNotificationVariationKey = nil;
277+
NSString *experimentKey = @"badKey";
278+
OPTLYExperiment *experiment = [self.optimizely.config getExperimentForKey:experimentKey];
263279

264-
// test with bad experiment key
280+
XCTAssertNil(experiment);
281+
OPTLYVariation *variation;
265282

283+
[self.optimizely.notificationCenter addDecisionNotificationListener:^(NSString * _Nonnull type, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nonnull decisionInfo) {
284+
decisionNotificationExperimentKey = decisionInfo[ExperimentDecisionInfo.ExperimentKey];
285+
decisionNotificationVariationKey = decisionInfo[ExperimentDecisionInfo.VariationKey];
286+
}];
287+
288+
variation = [self.optimizely variation:experimentKey userId:kUserId];
289+
XCTAssertNil(variation);
290+
XCTAssertNil(decisionNotificationExperimentKey);
291+
XCTAssertNil(decisionNotificationVariationKey);
292+
}
293+
294+
- (void)testDecisionNotificationForEmptyExperimentKey {
266295
__block NSString *decisionNotificationExperimentKey = nil;
267296
__block NSString *decisionNotificationVariationKey = nil;
297+
NSString *experimentKey = @"";
298+
OPTLYExperiment *experiment = [self.optimizely.config getExperimentForKey:experimentKey];
299+
300+
XCTAssertNil(experiment);
301+
OPTLYVariation *variation;
268302

269303
[self.optimizely.notificationCenter addDecisionNotificationListener:^(NSString * _Nonnull type, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nonnull decisionInfo) {
270304
decisionNotificationExperimentKey = decisionInfo[ExperimentDecisionInfo.ExperimentKey];
271305
decisionNotificationVariationKey = decisionInfo[ExperimentDecisionInfo.VariationKey];
272306
}];
273307

274-
variation = [self.optimizely variation:@"bad" userId:kUserId];
308+
variation = [self.optimizely variation:experimentKey userId:kUserId];
275309
XCTAssertNil(variation);
276-
XCTAssertEqualObjects(decisionNotificationExperimentKey, @"bad");
277-
XCTAssertEqualObjects(decisionNotificationVariationKey, [NSNull null]);
310+
XCTAssertNil(decisionNotificationExperimentKey);
311+
XCTAssertNil(decisionNotificationVariationKey);
278312
}
279313

314+
280315
// Test whitelisting works with get variation
281316
- (void)testDecisionNotificationForVariationWhitelisting {
282317
NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kBucketerTestDatafileName];

0 commit comments

Comments
 (0)