This repository has been archived by the owner on Dec 22, 2022. It is now read-only.
forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CCPA Phase 1: AMP Endpoint (prebid#1125)
- Loading branch information
1 parent
ac248b9
commit 1795a05
Showing
9 changed files
with
452 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ccpa | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/buger/jsonparser" | ||
"github.com/mxmCherry/openrtb" | ||
) | ||
|
||
// Policy represents the CCPA regulation for an OpenRTB bid request. | ||
type Policy struct { | ||
Value string | ||
} | ||
|
||
// Write mutates an OpenRTB bid request with the context of the CCPA policy. | ||
func (p Policy) Write(req *openrtb.BidRequest) error { | ||
if p.Value == "" { | ||
return nil | ||
} | ||
|
||
if req.Regs == nil { | ||
req.Regs = &openrtb.Regs{} | ||
} | ||
|
||
if req.Regs.Ext == nil { | ||
req.Regs.Ext = json.RawMessage(`{"us_privacy":"` + p.Value + `"}`) | ||
return nil | ||
} | ||
|
||
var err error | ||
req.Regs.Ext, err = jsonparser.Set(req.Regs.Ext, []byte(`"`+p.Value+`"`), "us_privacy") | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package ccpa | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/mxmCherry/openrtb" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWrite(t *testing.T) { | ||
testCases := []struct { | ||
description string | ||
policy Policy | ||
request *openrtb.BidRequest | ||
expected *openrtb.BidRequest | ||
expectedError bool | ||
}{ | ||
{ | ||
description: "Disabled", | ||
policy: Policy{Value: ""}, | ||
request: &openrtb.BidRequest{}, | ||
expected: &openrtb.BidRequest{}, | ||
}, | ||
{ | ||
description: "Enabled With Nil Request Regs Object", | ||
policy: Policy{Value: "anyValue"}, | ||
request: &openrtb.BidRequest{}, | ||
expected: &openrtb.BidRequest{Regs: &openrtb.Regs{ | ||
Ext: json.RawMessage(`{"us_privacy":"anyValue"}`)}}, | ||
}, | ||
{ | ||
description: "Enabled With Nil Request Regs Ext Object", | ||
policy: Policy{Value: "anyValue"}, | ||
request: &openrtb.BidRequest{Regs: &openrtb.Regs{}}, | ||
expected: &openrtb.BidRequest{Regs: &openrtb.Regs{ | ||
Ext: json.RawMessage(`{"us_privacy":"anyValue"}`)}}, | ||
}, | ||
{ | ||
description: "Enabled With Existing Request Regs Ext Object - Doesn't Overwrite", | ||
policy: Policy{Value: "anyValue"}, | ||
request: &openrtb.BidRequest{Regs: &openrtb.Regs{ | ||
Ext: json.RawMessage(`{"existing":"any"}`)}}, | ||
expected: &openrtb.BidRequest{Regs: &openrtb.Regs{ | ||
Ext: json.RawMessage(`{"existing":"any","us_privacy":"anyValue"}`)}}, | ||
}, | ||
{ | ||
description: "Enabled With Existing Request Regs Ext Object - Overwrites", | ||
policy: Policy{Value: "anyValue"}, | ||
request: &openrtb.BidRequest{Regs: &openrtb.Regs{ | ||
Ext: json.RawMessage(`{"existing":"any","us_privacy":"toBeOverwritten"}`)}}, | ||
expected: &openrtb.BidRequest{Regs: &openrtb.Regs{ | ||
Ext: json.RawMessage(`{"existing":"any","us_privacy":"anyValue"}`)}}, | ||
}, | ||
{ | ||
description: "Enabled With Existing Malformed Request Regs Ext Object", | ||
policy: Policy{Value: "anyValue"}, | ||
request: &openrtb.BidRequest{Regs: &openrtb.Regs{ | ||
Ext: json.RawMessage(`malformed`)}}, | ||
expectedError: true, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
err := test.policy.Write(test.request) | ||
|
||
if test.expectedError { | ||
assert.Error(t, err, test.description) | ||
} else { | ||
assert.NoError(t, err, test.description) | ||
assert.Equal(t, test.expected, test.request, test.description) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package gdpr | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/buger/jsonparser" | ||
"github.com/mxmCherry/openrtb" | ||
) | ||
|
||
// Policy represents the GDPR regulation of an OpenRTB bid request. | ||
type Policy struct { | ||
Consent string | ||
} | ||
|
||
// Write mutates an OpenRTB bid request with the context of the GDPR policy. | ||
func (p Policy) Write(req *openrtb.BidRequest) error { | ||
if p.Consent == "" { | ||
return nil | ||
} | ||
|
||
if req.User == nil { | ||
req.User = &openrtb.User{} | ||
} | ||
|
||
if req.User.Ext == nil { | ||
req.User.Ext = json.RawMessage(`{"consent":"` + p.Consent + `"}`) | ||
return nil | ||
} | ||
|
||
var err error | ||
req.User.Ext, err = jsonparser.Set(req.User.Ext, []byte(`"`+p.Consent+`"`), "consent") | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package gdpr | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/mxmCherry/openrtb" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWrite(t *testing.T) { | ||
testCases := []struct { | ||
description string | ||
policy Policy | ||
request *openrtb.BidRequest | ||
expected *openrtb.BidRequest | ||
expectedError bool | ||
}{ | ||
{ | ||
description: "Disabled", | ||
policy: Policy{Consent: ""}, | ||
request: &openrtb.BidRequest{}, | ||
expected: &openrtb.BidRequest{}, | ||
}, | ||
{ | ||
description: "Enabled With Nil Request User Object", | ||
policy: Policy{Consent: "anyValue"}, | ||
request: &openrtb.BidRequest{}, | ||
expected: &openrtb.BidRequest{User: &openrtb.User{ | ||
Ext: json.RawMessage(`{"consent":"anyValue"}`)}}, | ||
}, | ||
{ | ||
description: "Enabled With Nil Request User Ext Object", | ||
policy: Policy{Consent: "anyValue"}, | ||
request: &openrtb.BidRequest{User: &openrtb.User{}}, | ||
expected: &openrtb.BidRequest{User: &openrtb.User{ | ||
Ext: json.RawMessage(`{"consent":"anyValue"}`)}}, | ||
}, | ||
{ | ||
description: "Enabled With Existing Request User Ext Object - Doesn't Overwrite", | ||
policy: Policy{Consent: "anyValue"}, | ||
request: &openrtb.BidRequest{User: &openrtb.User{ | ||
Ext: json.RawMessage(`{"existing":"any"}`)}}, | ||
expected: &openrtb.BidRequest{User: &openrtb.User{ | ||
Ext: json.RawMessage(`{"existing":"any","consent":"anyValue"}`)}}, | ||
}, | ||
{ | ||
description: "Enabled With Existing Request User Ext Object - Overwrites", | ||
policy: Policy{Consent: "anyValue"}, | ||
request: &openrtb.BidRequest{User: &openrtb.User{ | ||
Ext: json.RawMessage(`{"existing":"any","consent":"toBeOverwritten"}`)}}, | ||
expected: &openrtb.BidRequest{User: &openrtb.User{ | ||
Ext: json.RawMessage(`{"existing":"any","consent":"anyValue"}`)}}, | ||
}, | ||
{ | ||
description: "Enabled With Existing Malformed Request User Ext Object", | ||
policy: Policy{Consent: "anyValue"}, | ||
request: &openrtb.BidRequest{User: &openrtb.User{ | ||
Ext: json.RawMessage(`malformed`)}}, | ||
expectedError: true, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
err := test.policy.Write(test.request) | ||
|
||
if test.expectedError { | ||
assert.Error(t, err, test.description) | ||
} else { | ||
assert.NoError(t, err, test.description) | ||
assert.Equal(t, test.expected, test.request, test.description) | ||
} | ||
} | ||
} |
Oops, something went wrong.