Skip to content

Commit 6110e2e

Browse files
committed
Merging master.
2 parents 5d6b284 + 70300ad commit 6110e2e

File tree

10 files changed

+185
-136
lines changed

10 files changed

+185
-136
lines changed

api/openapi-spec/openapi.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ paths:
4444
description: Unauthorized, invalid JWT
4545
'403':
4646
$ref: '#/components/responses/Forbidden'
47-
'404':
48-
description: Event does not exist
4947
requestBody:
5048
$ref: '#/components/requestBodies/TrackContext'
5149
/v1/activate:

pkg/handlers/activate.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ func Activate(w http.ResponseWriter, r *http.Request) {
8282
case "experimentKey-not-found":
8383
logger.Debug().Str("experimentKey", key).Msg("experimentKey not found")
8484
d = &optimizely.Decision{
85+
UserID: uc.ID,
8586
ExperimentKey: key,
8687
Error: "experimentKey not found",
8788
}
8889
err = nil
8990
case "featureKey-not-found":
9091
logger.Debug().Str("featureKey", key).Msg("featureKey not found")
9192
d = &optimizely.Decision{
93+
UserID: uc.ID,
9294
FeatureKey: key,
9395
Error: "featureKey not found",
9496
}

pkg/handlers/activate_test.go

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"bytes"
2222
"context"
2323
"encoding/json"
24-
"io"
2524
"net/http"
2625
"net/http/httptest"
2726
"testing"
@@ -40,7 +39,7 @@ type ActivateTestSuite struct {
4039
suite.Suite
4140
oc *optimizely.OptlyClient
4241
tc *optimizelytest.TestClient
43-
body io.Reader
42+
body []byte
4443
mux *chi.Mux
4544
}
4645

@@ -70,7 +69,7 @@ func (suite *ActivateTestSuite) SetupTest() {
7069
payload, err := json.Marshal(ab)
7170
suite.NoError(err)
7271

73-
suite.body = bytes.NewBuffer(payload)
72+
suite.body = payload
7473
suite.mux = mux
7574
suite.tc = testClient
7675
suite.oc = optlyClient
@@ -80,7 +79,7 @@ func (suite *ActivateTestSuite) TestGetFeatureWithFeatureTest() {
8079
feature := entities.Feature{Key: "one"}
8180
suite.tc.AddFeatureTest(feature)
8281

83-
req := httptest.NewRequest("POST", "/activate?featureKey=one&disableTracking=true", suite.body)
82+
req := httptest.NewRequest("POST", "/activate?featureKey=one&disableTracking=true", bytes.NewBuffer(suite.body))
8483
rec := httptest.NewRecorder()
8584
suite.mux.ServeHTTP(rec, req)
8685

@@ -92,6 +91,7 @@ func (suite *ActivateTestSuite) TestGetFeatureWithFeatureTest() {
9291
suite.NoError(err)
9392

9493
expected := optimizely.Decision{
94+
UserID: "testUser",
9595
FeatureKey: "one",
9696
Type: "feature",
9797
Enabled: true,
@@ -105,7 +105,7 @@ func (suite *ActivateTestSuite) TestTrackFeatureWithFeatureRollout() {
105105
feature := entities.Feature{Key: "one"}
106106
suite.tc.AddFeatureRollout(feature)
107107

108-
req := httptest.NewRequest("POST", "/activate?featureKey=one", suite.body)
108+
req := httptest.NewRequest("POST", "/activate?featureKey=one", bytes.NewBuffer(suite.body))
109109
rec := httptest.NewRecorder()
110110
suite.mux.ServeHTTP(rec, req)
111111

@@ -117,6 +117,7 @@ func (suite *ActivateTestSuite) TestTrackFeatureWithFeatureRollout() {
117117
suite.NoError(err)
118118

119119
expected := optimizely.Decision{
120+
UserID: "testUser",
120121
FeatureKey: "one",
121122
Enabled: true,
122123
Type: "feature",
@@ -130,7 +131,7 @@ func (suite *ActivateTestSuite) TestTrackFeatureWithFeatureTest() {
130131
feature := entities.Feature{Key: "one"}
131132
suite.tc.AddFeatureTest(feature)
132133

133-
req := httptest.NewRequest("POST", "/activate?featureKey=one", suite.body)
134+
req := httptest.NewRequest("POST", "/activate?featureKey=one", bytes.NewBuffer(suite.body))
134135
rec := httptest.NewRecorder()
135136
suite.mux.ServeHTTP(rec, req)
136137

@@ -142,6 +143,7 @@ func (suite *ActivateTestSuite) TestTrackFeatureWithFeatureTest() {
142143
suite.NoError(err)
143144

144145
expected := optimizely.Decision{
146+
UserID: "testUser",
145147
FeatureKey: "one",
146148
Type: "feature",
147149
Enabled: true,
@@ -157,7 +159,7 @@ func (suite *ActivateTestSuite) TestTrackFeatureWithFeatureTest() {
157159
}
158160

159161
func (suite *ActivateTestSuite) TestGetFeatureMissingFeature() {
160-
req := httptest.NewRequest("POST", "/activate?featureKey=feature-missing", suite.body)
162+
req := httptest.NewRequest("POST", "/activate?featureKey=feature-missing", bytes.NewBuffer(suite.body))
161163
rec := httptest.NewRecorder()
162164
suite.mux.ServeHTTP(rec, req)
163165
suite.Equal(http.StatusOK, rec.Code)
@@ -168,6 +170,7 @@ func (suite *ActivateTestSuite) TestGetFeatureMissingFeature() {
168170
suite.NoError(err)
169171

170172
expected := optimizely.Decision{
173+
UserID: "testUser",
171174
FeatureKey: "feature-missing",
172175
Error: "featureKey not found",
173176
}
@@ -176,34 +179,8 @@ func (suite *ActivateTestSuite) TestGetFeatureMissingFeature() {
176179
suite.Equal(expected, actual[0])
177180
}
178181

179-
func (suite *ActivateTestSuite) TestGetVariation() {
180-
testVariation := suite.tc.ProjectConfig.CreateVariation("variation_a")
181-
suite.tc.AddExperiment("one", []entities.Variation{testVariation})
182-
183-
req := httptest.NewRequest("POST", "/activate?experimentKey=one&disableTracking=true", nil)
184-
rec := httptest.NewRecorder()
185-
suite.mux.ServeHTTP(rec, req)
186-
187-
suite.Equal(http.StatusOK, rec.Code)
188-
189-
// Unmarshal response
190-
var actual []optimizely.Decision
191-
err := json.Unmarshal(rec.Body.Bytes(), &actual)
192-
suite.NoError(err)
193-
194-
expected := optimizely.Decision{
195-
ExperimentKey: "one",
196-
VariationKey: testVariation.Key,
197-
Type: "experiment",
198-
Enabled: true,
199-
}
200-
201-
suite.Equal(0, len(suite.tc.GetProcessedEvents()))
202-
suite.Equal(expected, actual[0])
203-
}
204-
205182
func (suite *ActivateTestSuite) TestGetVariationMissingExperiment() {
206-
req := httptest.NewRequest("POST", "/activate?experimentKey=experiment-missing", suite.body)
183+
req := httptest.NewRequest("POST", "/activate?experimentKey=experiment-missing", bytes.NewBuffer(suite.body))
207184
rec := httptest.NewRecorder()
208185
suite.mux.ServeHTTP(rec, req)
209186
suite.Equal(http.StatusOK, rec.Code)
@@ -214,6 +191,7 @@ func (suite *ActivateTestSuite) TestGetVariationMissingExperiment() {
214191
suite.NoError(err)
215192

216193
expected := optimizely.Decision{
194+
UserID: "testUser",
217195
ExperimentKey: "experiment-missing",
218196
Error: "experimentKey not found",
219197
}
@@ -226,7 +204,7 @@ func (suite *ActivateTestSuite) TestActivateExperiment() {
226204
testVariation := suite.tc.ProjectConfig.CreateVariation("variation_a")
227205
suite.tc.AddExperiment("one", []entities.Variation{testVariation})
228206

229-
req := httptest.NewRequest("POST", "/activate?experimentKey=one", suite.body)
207+
req := httptest.NewRequest("POST", "/activate?experimentKey=one", bytes.NewBuffer(suite.body))
230208
rec := httptest.NewRecorder()
231209
suite.mux.ServeHTTP(rec, req)
232210

@@ -238,6 +216,7 @@ func (suite *ActivateTestSuite) TestActivateExperiment() {
238216
suite.NoError(err)
239217

240218
expected := optimizely.Decision{
219+
UserID: "testUser",
241220
ExperimentKey: "one",
242221
VariationKey: testVariation.Key,
243222
Type: "experiment",
@@ -264,16 +243,19 @@ func (suite *ActivateTestSuite) TestActivateFeatures() {
264243

265244
expected := []optimizely.Decision{
266245
{
246+
UserID: "testUser",
267247
Enabled: true,
268248
FeatureKey: "featureA",
269249
Type: "feature",
270250
},
271251
{
252+
UserID: "testUser",
272253
Enabled: true,
273254
FeatureKey: "featureB",
274255
Type: "feature",
275256
},
276257
{
258+
UserID: "testUser",
277259
Enabled: true,
278260
FeatureKey: "featureC",
279261
Type: "feature",
@@ -285,7 +267,7 @@ func (suite *ActivateTestSuite) TestActivateFeatures() {
285267

286268
// Toggle between tracking and no tracking.
287269
for _, flag := range []string{"true", "false"} {
288-
req := httptest.NewRequest("POST", "/activate?type=feature&disableTracking="+flag, suite.body)
270+
req := httptest.NewRequest("POST", "/activate?type=feature&disableTracking="+flag, bytes.NewBuffer(suite.body))
289271
rec := httptest.NewRecorder()
290272
suite.mux.ServeHTTP(rec, req)
291273

@@ -315,18 +297,21 @@ func (suite *ActivateTestSuite) TestActivateExperiments() {
315297

316298
expected := []optimizely.Decision{
317299
{
300+
UserID: "testUser",
318301
ExperimentKey: "one",
319302
VariationKey: testVariationA.Key,
320303
Type: "experiment",
321304
Enabled: true,
322305
},
323306
{
307+
UserID: "testUser",
324308
ExperimentKey: "two",
325309
VariationKey: testVariationB.Key,
326310
Type: "experiment",
327311
Enabled: true,
328312
},
329313
{
314+
UserID: "testUser",
330315
ExperimentKey: "three",
331316
VariationKey: testVariationC.Key,
332317
Type: "experiment",
@@ -336,7 +321,7 @@ func (suite *ActivateTestSuite) TestActivateExperiments() {
336321

337322
// Toggle between tracking and no tracking.
338323
for _, flag := range []string{"true", "false"} {
339-
req := httptest.NewRequest("POST", "/activate?type=experiment&disableTracking="+flag, suite.body)
324+
req := httptest.NewRequest("POST", "/activate?type=experiment&disableTracking="+flag, bytes.NewBuffer(suite.body))
340325
rec := httptest.NewRecorder()
341326
suite.mux.ServeHTTP(rec, req)
342327

@@ -369,11 +354,13 @@ func (suite *ActivateTestSuite) TestEnabledFilter() {
369354

370355
expected := []optimizely.Decision{
371356
{
357+
UserID: "testUser",
372358
Enabled: true,
373359
FeatureKey: "featureA",
374360
Type: "feature",
375361
},
376362
{
363+
UserID: "testUser",
377364
Enabled: true,
378365
FeatureKey: "featureC",
379366
Type: "feature",
@@ -382,6 +369,7 @@ func (suite *ActivateTestSuite) TestEnabledFilter() {
382369
},
383370
},
384371
{
372+
UserID: "testUser",
385373
Enabled: false,
386374
FeatureKey: "featureB",
387375
Type: "feature",
@@ -407,7 +395,7 @@ func (suite *ActivateTestSuite) TestEnabledFilter() {
407395
}
408396

409397
for _, scenario := range scenarios {
410-
req := httptest.NewRequest("POST", "/activate?type=feature"+scenario.param, suite.body)
398+
req := httptest.NewRequest("POST", "/activate?type=feature"+scenario.param, bytes.NewBuffer(suite.body))
411399
rec := httptest.NewRecorder()
412400
suite.mux.ServeHTTP(rec, req)
413401

pkg/handlers/track.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
package handlers
1919

2020
import (
21-
"errors"
2221
"fmt"
2322
"net/http"
2423

2524
"github.com/go-chi/render"
2625
"github.com/optimizely/go-sdk/pkg/entities"
2726

2827
"github.com/optimizely/agent/pkg/middleware"
29-
"github.com/optimizely/agent/pkg/optimizely"
3028
)
3129

3230
type trackBody struct {
@@ -62,15 +60,12 @@ func TrackEvent(w http.ResponseWriter, r *http.Request) {
6260
Attributes: body.UserAttributes,
6361
}
6462

65-
if err = optlyClient.TrackEvent(eventKey, uc, body.EventTags); err != nil {
66-
if errors.Is(err, optimizely.ErrEntityNotFound) {
67-
RenderError(err, http.StatusNotFound, w, r)
68-
} else {
69-
RenderError(err, http.StatusInternalServerError, w, r)
70-
}
63+
track, err := optlyClient.TrackEvent(eventKey, uc, body.EventTags)
64+
if err != nil {
65+
RenderError(err, http.StatusInternalServerError, w, r)
7166
return
7267
}
7368

7469
middleware.GetLogger(r).Debug().Str("eventKey", eventKey).Msg("tracking event")
75-
render.NoContent(w, r)
70+
render.JSON(w, r, track)
7671
}

pkg/handlers/track_test.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,25 @@ func (suite *TrackTestSuite) TestTrackEventNoTags() {
133133
rec := httptest.NewRecorder()
134134
suite.mux.ServeHTTP(rec, req)
135135

136-
suite.Equal(http.StatusNoContent, rec.Code)
136+
suite.Equal(http.StatusOK, rec.Code)
137+
138+
// Unmarshal response
139+
var actual optimizely.Track
140+
suite.NoError(json.Unmarshal(rec.Body.Bytes(), &actual))
141+
142+
expected := optimizely.Track{
143+
UserID: "testUser",
144+
EventKey: eventKey,
145+
}
146+
147+
suite.Equal(expected, actual)
137148

138149
events := suite.tc.GetProcessedEvents()
139150
suite.Equal(1, len(events))
140151

141-
actual := events[0]
142-
suite.Equal(eventKey, actual.Conversion.Key)
143-
suite.Equal("testUser", actual.VisitorID)
152+
actualEvent := events[0]
153+
suite.Equal(eventKey, actualEvent.Conversion.Key)
154+
suite.Equal("testUser", actualEvent.VisitorID)
144155
}
145156

146157
func (suite *TrackTestSuite) TestTrackEventWithTags() {
@@ -161,15 +172,26 @@ func (suite *TrackTestSuite) TestTrackEventWithTags() {
161172
rec := httptest.NewRecorder()
162173
suite.mux.ServeHTTP(rec, req)
163174

164-
suite.Equal(http.StatusNoContent, rec.Code)
175+
suite.Equal(http.StatusOK, rec.Code)
176+
177+
// Unmarshal response
178+
var actual optimizely.Track
179+
suite.NoError(json.Unmarshal(rec.Body.Bytes(), &actual))
180+
181+
expected := optimizely.Track{
182+
UserID: "testUser",
183+
EventKey: eventKey,
184+
}
185+
186+
suite.Equal(expected, actual)
165187

166188
events := suite.tc.GetProcessedEvents()
167189
suite.Equal(1, len(events))
168190

169-
actual := events[0]
170-
suite.Equal(eventKey, actual.Conversion.Key)
171-
suite.Equal(tb.UserID, actual.VisitorID)
172-
suite.Equal(tb.EventTags, actual.Conversion.Tags)
191+
actualEvent := events[0]
192+
suite.Equal(eventKey, actualEvent.Conversion.Key)
193+
suite.Equal(tb.UserID, actualEvent.VisitorID)
194+
suite.Equal(tb.EventTags, actualEvent.Conversion.Tags)
173195
}
174196

175197
func (suite *TrackTestSuite) TestTrackEventWithInvalidTags() {
@@ -190,7 +212,7 @@ func (suite *TrackTestSuite) TestTrackEventParamError() {
190212
code int
191213
message string
192214
}{
193-
{"?eventKey=invalid", http.StatusNotFound, `eventKey: "invalid" not found`},
215+
{"?eventKey=invalid", http.StatusOK, "Event with key invalid not found"},
194216
{"?eventKey=", http.StatusBadRequest, "missing required path parameter: eventKey"},
195217
{"", http.StatusBadRequest, "missing required path parameter: eventKey"},
196218
}

0 commit comments

Comments
 (0)