Skip to content

Commit

Permalink
prc
Browse files Browse the repository at this point in the history
  • Loading branch information
pm-nilesh-chate committed Feb 22, 2024
1 parent 7767c3d commit f9969ab
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 81 deletions.
3 changes: 1 addition & 2 deletions endpoints/cookie_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,7 @@ func (c *cookieSyncEndpoint) setCookieDeprecationHeader(w http.ResponseWriter, r
HttpOnly: true,
Path: "/",
SameSite: http.SameSiteNoneMode,
// Partition: true,
Expires: c.time.Now().Add(time.Second * time.Duration(account.Privacy.PrivacySandbox.CookieDeprecation.TTLSec)),
Expires: c.time.Now().Add(time.Second * time.Duration(account.Privacy.PrivacySandbox.CookieDeprecation.TTLSec)),
}
setCookie(w, cookie)
}
Expand Down
134 changes: 59 additions & 75 deletions endpoints/cookie_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func TestCookieSyncHandle(t *testing.T) {
metrics: &mockMetrics,
pbsAnalytics: &mockAnalytics,
accountsFetcher: &fakeAccountFetcher,
time: &fakeTime{time: time.Now()},
time: &fakeTime{time: time.Date(2024, 2, 22, 9, 42, 4, 13, time.UTC)},
}
assert.NoError(t, endpoint.config.MarshalAccountDefaults())

Expand All @@ -398,9 +398,9 @@ func TestCookieSyncHandle(t *testing.T) {
if test.expectedCookieDeprecationHeader {
wantCookieTTL := endpoint.time.Now().Add(time.Second * time.Duration(86400)).UTC().Format(http.TimeFormat)
wantCookie := fmt.Sprintf("receive-cookie-deprecation=1; Path=/; Expires=%v; HttpOnly; Secure; SameSite=None; Partitioned;", wantCookieTTL)
assert.Equal(t, wantCookie, gotCookie, ":set_cookie_deprecation_header")
assert.Equal(t, wantCookie, gotCookie, test.description)
} else {
assert.Equal(t, gotCookie, "")
assert.Empty(t, gotCookie, test.description)
}

mockMetrics.AssertExpectations(t)
Expand Down Expand Up @@ -2284,114 +2284,98 @@ func TestSetCookieDeprecationHeader(t *testing.T) {
return r
}

type args struct {
w http.ResponseWriter
r *http.Request
account *config.Account
}
tests := []struct {
name string
args args
responseWriter http.ResponseWriter
request *http.Request
account *config.Account
expectedCookieDeprecationHeader bool
}{
{
name: "receive-cookie-deprecation not present in request but account is nil",
args: args{
r: getTestRequest(false),
w: httptest.NewRecorder(),
},
name: "receive-cookie-deprecation not present in request but account is nil",
request: getTestRequest(false),
responseWriter: httptest.NewRecorder(),
expectedCookieDeprecationHeader: false,
},
{
name: "receive-cookie-deprecation not present in request but CookieDeprecation is disabled",
args: args{
r: getTestRequest(false),
w: httptest.NewRecorder(),
account: &config.Account{
Privacy: config.AccountPrivacy{
PrivacySandbox: config.PrivacySandbox{
CookieDeprecation: config.CookieDeprecation{
Enabled: false,
},
name: "receive-cookie-deprecation not present in request but CookieDeprecation is disabled",
request: getTestRequest(false),
responseWriter: httptest.NewRecorder(),
account: &config.Account{
Privacy: config.AccountPrivacy{
PrivacySandbox: config.PrivacySandbox{
CookieDeprecation: config.CookieDeprecation{
Enabled: false,
},
},
},
},
expectedCookieDeprecationHeader: false,
},
{
name: "receive-cookie-deprecation present in request and CookieDeprecation is disabled",
args: args{
r: getTestRequest(true),
w: httptest.NewRecorder(),
account: &config.Account{
Privacy: config.AccountPrivacy{
PrivacySandbox: config.PrivacySandbox{
CookieDeprecation: config.CookieDeprecation{
Enabled: false,
},
name: "receive-cookie-deprecation present in request and CookieDeprecation is disabled",
request: getTestRequest(true),
responseWriter: httptest.NewRecorder(),
account: &config.Account{
Privacy: config.AccountPrivacy{
PrivacySandbox: config.PrivacySandbox{
CookieDeprecation: config.CookieDeprecation{
Enabled: false,
},
},
},
},
expectedCookieDeprecationHeader: false,
},
{
name: "receive-cookie-deprecation present in request and CookieDeprecation is enabled",
args: args{
r: getTestRequest(true),
w: httptest.NewRecorder(),
account: &config.Account{
Privacy: config.AccountPrivacy{
PrivacySandbox: config.PrivacySandbox{
CookieDeprecation: config.CookieDeprecation{
Enabled: true,
TTLSec: 86400,
},
name: "receive-cookie-deprecation present in request and CookieDeprecation is enabled",
request: getTestRequest(true),
responseWriter: httptest.NewRecorder(),
account: &config.Account{
Privacy: config.AccountPrivacy{
PrivacySandbox: config.PrivacySandbox{
CookieDeprecation: config.CookieDeprecation{
Enabled: true,
TTLSec: 86400,
},
},
},
},

expectedCookieDeprecationHeader: false,
},
{
name: "receive-cookie-deprecation present in request and account is nil",
args: args{
r: getTestRequest(true),
w: httptest.NewRecorder(),
},
name: "receive-cookie-deprecation present in request and account is nil",
request: getTestRequest(true),
responseWriter: httptest.NewRecorder(),
expectedCookieDeprecationHeader: false,
},
{
name: "receive-cookie-deprecation not present in request and CookieDeprecation is enabled for account",
args: args{
r: getTestRequest(false),
w: httptest.NewRecorder(),
account: &config.Account{
Privacy: config.AccountPrivacy{
PrivacySandbox: config.PrivacySandbox{
CookieDeprecation: config.CookieDeprecation{
Enabled: true,
TTLSec: 86400,
},
name: "receive-cookie-deprecation not present in request and CookieDeprecation is enabled for account",
request: getTestRequest(false),
responseWriter: httptest.NewRecorder(),
account: &config.Account{
Privacy: config.AccountPrivacy{
PrivacySandbox: config.PrivacySandbox{
CookieDeprecation: config.CookieDeprecation{
Enabled: true,
TTLSec: 86400,
},
},
},
},
expectedCookieDeprecationHeader: true,
},
{
name: "failed to read receive-cookie-deprecation from request but CookieDeprecation is enabled",
args: args{
r: &http.Request{}, // nil cookie. error: http: named cookie not present
w: httptest.NewRecorder(),
account: &config.Account{
Privacy: config.AccountPrivacy{
PrivacySandbox: config.PrivacySandbox{
CookieDeprecation: config.CookieDeprecation{
Enabled: true,
TTLSec: 86400,
},
name: "failed to read receive-cookie-deprecation from request but CookieDeprecation is enabled",
request: &http.Request{}, // nil cookie. error: http: named cookie not present
responseWriter: httptest.NewRecorder(),
account: &config.Account{
Privacy: config.AccountPrivacy{
PrivacySandbox: config.PrivacySandbox{
CookieDeprecation: config.CookieDeprecation{
Enabled: true,
TTLSec: 86400,
},
},
},
Expand All @@ -2402,10 +2386,10 @@ func TestSetCookieDeprecationHeader(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &cookieSyncEndpoint{
time: &fakeTime{time: time.Now()},
time: &fakeTime{time: time.Date(2024, 2, 22, 9, 42, 4, 13, time.UTC)},
}
c.setCookieDeprecationHeader(tt.args.w, tt.args.r, tt.args.account)
gotCookie := tt.args.w.Header().Get("Set-Cookie")
c.setCookieDeprecationHeader(tt.responseWriter, tt.request, tt.account)
gotCookie := tt.responseWriter.Header().Get("Set-Cookie")
if tt.expectedCookieDeprecationHeader {
wantCookieTTL := c.time.Now().Add(time.Second * time.Duration(86400)).UTC().Format(http.TimeFormat)
wantCookie := fmt.Sprintf("receive-cookie-deprecation=1; Path=/; Expires=%v; HttpOnly; Secure; SameSite=None; Partitioned;", wantCookieTTL)
Expand Down
7 changes: 5 additions & 2 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -1943,9 +1943,12 @@ func validateOrFillCDep(httpReq *http.Request, req *openrtb_ext.RequestWrapper,
}
}

if deviceExt, err := req.GetDeviceExt(); err == nil {
deviceExt.SetCDep(secCookieDeprecation)
deviceExt, err := req.GetDeviceExt()
if err != nil {
return err
}

deviceExt.SetCDep(secCookieDeprecation)
return nil
}

Expand Down
32 changes: 30 additions & 2 deletions endpoints/openrtb2/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6253,7 +6253,7 @@ func TestValidateOrFillCDep(t *testing.T) {
},
},
},
wantDeviceExt: json.RawMessage(`{"foo":"bar","cdep":"example_label_1"}`),
wantDeviceExt: json.RawMessage(`{"cdep":"example_label_1","foo":"bar"}`),
wantErr: nil,
},
{
Expand Down Expand Up @@ -6285,6 +6285,34 @@ func TestValidateOrFillCDep(t *testing.T) {
WarningCode: errortypes.SecCookieDeprecationLenWarningCode,
},
},
{
name: "Sec-Cookie-Deprecation valid but invalid request.device.ext",
args: args{
httpReq: &http.Request{
Header: http.Header{secCookieDeprecation: []string{"example_label_1"}},
},
req: &openrtb_ext.RequestWrapper{
BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{
Ext: json.RawMessage(`{`),
},
},
},
account: config.Account{
Privacy: config.AccountPrivacy{
PrivacySandbox: config.PrivacySandbox{
CookieDeprecation: config.CookieDeprecation{
Enabled: true,
},
},
},
},
},
wantDeviceExt: json.RawMessage(`{`),
wantErr: &errortypes.FailedToUnmarshal{
Message: "expects \" or n, but found \x00",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -6299,7 +6327,7 @@ func TestValidateOrFillCDep(t *testing.T) {
assert.Nil(t, tt.args.req.Device.Ext, tt.name)
}
} else {
assert.JSONEq(t, string(tt.wantDeviceExt), string(tt.args.req.Device.Ext), tt.name)
assert.Equal(t, string(tt.wantDeviceExt), string(tt.args.req.Device.Ext), tt.name)
}
})
}
Expand Down

0 comments on commit f9969ab

Please sign in to comment.