@@ -25,6 +25,8 @@ import (
25
25
"net/http/httptest"
26
26
"testing"
27
27
28
+ "github.com/optimizely/go-sdk/pkg/decision"
29
+
28
30
"github.com/optimizely/sidedoor/pkg/api/middleware"
29
31
"github.com/optimizely/sidedoor/pkg/api/models"
30
32
@@ -65,7 +67,11 @@ func (o *UserMW) UserCtx(next http.Handler) http.Handler {
65
67
// Setup Mux
66
68
func (suite * UserTestSuite ) SetupTest () {
67
69
testClient := optimizelytest .NewClient ()
68
- optlyClient := & optimizely.OptlyClient {testClient .OptimizelyClient , nil }
70
+ optlyClient := & optimizely.OptlyClient {
71
+ OptimizelyClient : testClient .OptimizelyClient ,
72
+ ConfigManager : nil ,
73
+ ForcedVariations : testClient .ForcedVariations ,
74
+ }
69
75
70
76
mux := chi .NewMux ()
71
77
userAPI := new (UserHandler )
@@ -80,6 +86,8 @@ func (suite *UserTestSuite) SetupTest() {
80
86
81
87
mux .Get ("/experiments/{experimentKey}" , userAPI .GetVariation )
82
88
mux .Post ("/experiments/{experimentKey}" , userAPI .ActivateExperiment )
89
+ mux .Put ("/experiments/{experimentKey}/variations/{variationKey}" , userAPI .SetForcedVariation )
90
+ mux .Delete ("/experiments/{experimentKey}/variations" , userAPI .RemoveForcedVariation )
83
91
84
92
suite .mux = mux
85
93
suite .tc = testClient
@@ -246,6 +254,74 @@ func (suite *UserTestSuite) TestTrackEventEmptyKey() {
246
254
suite .assertError (rec , "missing required path parameter: eventKey" , http .StatusBadRequest )
247
255
}
248
256
257
+ func (suite * UserTestSuite ) TestSetForcedVariation () {
258
+ feature := entities.Feature {Key : "my_feat" }
259
+ suite .tc .ProjectConfig .AddMultiVariationFeatureTest (feature , "variation_disabled" , "variation_enabled" )
260
+ featureExp := suite .tc .ProjectConfig .FeatureMap ["my_feat" ].FeatureExperiments [0 ]
261
+
262
+ req := httptest .NewRequest ("PUT" , "/experiments/" + featureExp .Key + "/variations/variation_enabled" , nil )
263
+ rec := httptest .NewRecorder ()
264
+ suite .mux .ServeHTTP (rec , req )
265
+ suite .Equal (http .StatusCreated , rec .Code )
266
+
267
+ req = httptest .NewRequest ("GET" , "/features/my_feat" , nil )
268
+ rec = httptest .NewRecorder ()
269
+ suite .mux .ServeHTTP (rec , req )
270
+ var actual models.Feature
271
+ json .Unmarshal (rec .Body .Bytes (), & actual )
272
+ suite .True (actual .Enabled )
273
+
274
+ req = httptest .NewRequest ("PUT" , "/experiments/" + featureExp .Key + "/variations/variation_enabled" , nil )
275
+ rec = httptest .NewRecorder ()
276
+ suite .mux .ServeHTTP (rec , req )
277
+ suite .Equal (http .StatusNoContent , rec .Code )
278
+
279
+ req = httptest .NewRequest ("GET" , "/features/my_feat" , nil )
280
+ rec = httptest .NewRecorder ()
281
+ suite .mux .ServeHTTP (rec , req )
282
+ var actualRepeated models.Feature
283
+ json .Unmarshal (rec .Body .Bytes (), & actualRepeated )
284
+ suite .True (actualRepeated .Enabled )
285
+ }
286
+
287
+ func (suite * UserTestSuite ) TestSetForcedVariationEmptyExperimentKey () {
288
+ req := httptest .NewRequest ("PUT" , "/experiments//variations/variation_enabled" , nil )
289
+ rec := httptest .NewRecorder ()
290
+ suite .mux .ServeHTTP (rec , req )
291
+ suite .Equal (http .StatusBadRequest , rec .Code )
292
+ }
293
+
294
+ func (suite * UserTestSuite ) TestRemoveForcedVariation () {
295
+ feature := entities.Feature {Key : "my_feat" }
296
+ suite .tc .ProjectConfig .AddMultiVariationFeatureTest (feature , "variation_disabled" , "variation_enabled" )
297
+ featureExp := suite .tc .ProjectConfig .FeatureMap ["my_feat" ].FeatureExperiments [0 ]
298
+
299
+ suite .tc .ForcedVariations .SetVariation (decision.ExperimentOverrideKey {
300
+ ExperimentKey : featureExp .Key ,
301
+ UserID : "testUser" ,
302
+ }, "variation_enabled" )
303
+
304
+ req := httptest .NewRequest ("DELETE" , "/experiments/" + featureExp .Key + "/variations" , nil )
305
+ rec := httptest .NewRecorder ()
306
+ suite .mux .ServeHTTP (rec , req )
307
+ suite .Equal (http .StatusNoContent , rec .Code )
308
+
309
+ req = httptest .NewRequest ("GET" , "/features/my_feat" , nil )
310
+ rec = httptest .NewRecorder ()
311
+ suite .mux .ServeHTTP (rec , req )
312
+ suite .Equal (http .StatusOK , rec .Code )
313
+ var actual models.Feature
314
+ json .Unmarshal (rec .Body .Bytes (), & actual )
315
+ suite .False (actual .Enabled )
316
+ }
317
+
318
+ func (suite * UserTestSuite ) TestRemoveForcedVariationEmptyExperimentKey () {
319
+ req := httptest .NewRequest ("DELETE" , "/experiments//variations" , nil )
320
+ rec := httptest .NewRecorder ()
321
+ suite .mux .ServeHTTP (rec , req )
322
+ suite .Equal (http .StatusBadRequest , rec .Code )
323
+ }
324
+
249
325
func (suite * UserTestSuite ) TestGetVariation () {
250
326
testVariation := suite .tc .ProjectConfig .CreateVariation ("variation_a" )
251
327
suite .tc .AddExperiment ("one" , []entities.Variation {testVariation })
@@ -356,6 +432,8 @@ func TestUserMissingOptlyCtx(t *testing.T) {
356
432
userHandler .GetVariation ,
357
433
userHandler .TrackFeature ,
358
434
userHandler .TrackEvent ,
435
+ userHandler .SetForcedVariation ,
436
+ userHandler .RemoveForcedVariation ,
359
437
}
360
438
361
439
for _ , handler := range handlers {
0 commit comments