Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CCPA Phase 1: AMP Endpoint #1125

Merged
merged 4 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions privacy/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ type Policies struct {
CCPA ccpa.Policy
}

type policyWriter interface {
Write(req *openrtb.BidRequest) error
}

// Write mutates an OpenRTB bid request with the policies applied.
func (p Policies) Write(req *openrtb.BidRequest) error {
if err := p.GDPR.Write(req); err != nil {
return err
}
return writePolicies(req, []policyWriter{
p.GDPR, p.CCPA,
})
}

if err := p.CCPA.Write(req); err != nil {
return err
func writePolicies(req *openrtb.BidRequest, writers []policyWriter) error {
for _, writer := range writers {
if err := writer.Write(req); err != nil {
return err
}
}

return nil
Expand Down
89 changes: 53 additions & 36 deletions privacy/policies_test.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,77 @@
package privacy

import (
"encoding/json"
"errors"
"testing"

"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/privacy/ccpa"
"github.com/prebid/prebid-server/privacy/gdpr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestWrite(t *testing.T) {
polciies := Policies{
GDPR: gdpr.Policy{Consent: "anyConsent"},
CCPA: ccpa.Policy{Value: "anyValue"},
}
expectedRequest := &openrtb.BidRequest{
Regs: &openrtb.Regs{
Ext: json.RawMessage(`{"us_privacy":"anyValue"}`)},
User: &openrtb.User{
Ext: json.RawMessage(`{"consent":"anyConsent"}`)},
}

func TestWritePolicieNone(t *testing.T) {
request := &openrtb.BidRequest{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless nitpick: "TestWritePolicyNone"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha! Ooops.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed everywhere to TestWritePoliciesNone since its testing the private writePolicies func.

err := polciies.Write(request)
policyWriters := []policyWriter{}

err := writePolicies(request, policyWriters)

assert.NoError(t, err)
assert.Equal(t, expectedRequest, request)
}

func TestWriteWithErrorFromGDPR(t *testing.T) {
polciies := Policies{
GDPR: gdpr.Policy{Consent: "anyConsent"},
CCPA: ccpa.Policy{Value: "anyValue"},
}
request := &openrtb.BidRequest{
User: &openrtb.User{
Ext: json.RawMessage(`malformed`)},
func TestWritePolicieOne(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless nitpick: "TestWritePolicyOne"

request := &openrtb.BidRequest{}
mockWriter := new(mockPolicyWriter)
policyWriters := []policyWriter{
mockWriter,
}

err := polciies.Write(request)
mockWriter.On("Write", request).Return(nil).Once()

assert.Error(t, err)
err := writePolicies(request, policyWriters)

assert.NoError(t, err)
mockWriter.AssertExpectations(t)
}

func TestWriteWithErrorFromCCPA(t *testing.T) {
polciies := Policies{
GDPR: gdpr.Policy{Consent: "anyConsent"},
CCPA: ccpa.Policy{Value: "anyValue"},
func TestWritePolicieMany(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless nitpick: "TestWritePoliciesMany"

request := &openrtb.BidRequest{}
mockWriter1 := new(mockPolicyWriter)
mockWriter2 := new(mockPolicyWriter)
policyWriters := []policyWriter{
mockWriter1, mockWriter2,
}
request := &openrtb.BidRequest{
Regs: &openrtb.Regs{
Ext: json.RawMessage(`malformed`)},

mockWriter1.On("Write", request).Return(nil).Once()
mockWriter2.On("Write", request).Return(nil).Once()

err := writePolicies(request, policyWriters)

assert.NoError(t, err)
mockWriter1.AssertExpectations(t)
mockWriter2.AssertExpectations(t)
}

func TestWritePolicieError(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless nitpick: "TestWritePolicyError"

request := &openrtb.BidRequest{}
mockWriter := new(mockPolicyWriter)
policyWriters := []policyWriter{
mockWriter,
}

err := polciies.Write(request)
expectedErr := errors.New("anyError")
mockWriter.On("Write", request).Return(expectedErr).Once()

err := writePolicies(request, policyWriters)

assert.Error(t, err, expectedErr)
mockWriter.AssertExpectations(t)
}

type mockPolicyWriter struct {
mock.Mock
}

assert.Error(t, err)
func (m *mockPolicyWriter) Write(req *openrtb.BidRequest) error {
args := m.Called(req)
return args.Error(0)
}