Skip to content

Commit 7407161

Browse files
author
Mike Davis
authored
fix(activate): Reject empty userId with a 400 (#205)
Prior to this change, empty userIds would still yield a valid decision which might have masked a request error. If this becomes an sticking point with customers we will introduce a "generateUserId" config option that will allow users to control this behavior. https://optimizely.atlassian.net/browse/OASIS-6132
1 parent 961067f commit 7407161

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

pkg/handlers/activate.go

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package handlers
1919

2020
import (
21+
"errors"
2122
"fmt"
2223
"net/http"
2324

@@ -174,12 +175,19 @@ func filterDecisions(r *http.Request, decisions []*optimizely.Decision) []*optim
174175
return filtered
175176
}
176177

178+
// ErrEmptyUserID is a constant error if userId is omitted from the request
179+
var ErrEmptyUserID = errors.New(`missing "userId" in request payload`)
180+
177181
func getUserContext(r *http.Request) (entities.UserContext, error) {
178182
var body ActivateBody
179183
err := ParseRequestBody(r, &body)
180184
if err != nil {
181185
return entities.UserContext{}, err
182186
}
183187

188+
if body.UserID == "" {
189+
return entities.UserContext{}, ErrEmptyUserID
190+
}
191+
184192
return entities.UserContext{ID: body.UserID, Attributes: body.UserAttributes}, nil
185193
}

pkg/handlers/activate_test.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ func (suite *ActivateTestSuite) SetupTest() {
7575
suite.oc = optlyClient
7676
}
7777

78+
func (suite *ActivateTestSuite) TestInvalidPayload() {
79+
req := httptest.NewRequest("POST", "/activate", nil)
80+
rec := httptest.NewRecorder()
81+
suite.mux.ServeHTTP(rec, req)
82+
83+
suite.assertError(rec, `missing "userId" in request payload`, http.StatusBadRequest)
84+
}
85+
7886
func (suite *ActivateTestSuite) TestGetFeatureWithFeatureTest() {
7987
feature := entities.Feature{Key: "one"}
8088
suite.tc.AddFeatureTest(feature)
@@ -410,17 +418,11 @@ func (suite *ActivateTestSuite) TestEnabledFilter() {
410418
}
411419

412420
func (suite *ActivateTestSuite) TestInvalidFilter() {
413-
req := httptest.NewRequest("POST", "/activate?type=invalid", nil)
421+
req := httptest.NewRequest("POST", "/activate?type=invalid", bytes.NewBuffer(suite.body))
414422
rec := httptest.NewRecorder()
415423
suite.mux.ServeHTTP(rec, req)
416424

417-
suite.Equal(http.StatusBadRequest, rec.Code)
418-
419-
// Unmarshal response
420-
var actual ErrorResponse
421-
err := json.Unmarshal(rec.Body.Bytes(), &actual)
422-
suite.NoError(err)
423-
suite.Equal(`type "invalid" not supported`, actual.Error)
425+
suite.assertError(rec, `type "invalid" not supported`, http.StatusBadRequest)
424426
}
425427

426428
func (suite *ActivateTestSuite) assertError(rec *httptest.ResponseRecorder, msg string, code int) {

0 commit comments

Comments
 (0)