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

Added GDPR to the app config #505

Merged
merged 4 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 20 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,32 @@ type Configuration struct {
MaxRequestSize int64 `mapstructure:"max_request_size"`
Analytics Analytics `mapstructure:"analytics"`
AMPTimeoutAdjustment int64 `mapstructure:"amp_timeout_adjustment_ms"`
GDPR GDPR `mapstructure:"gdpr"`
}

func (cfg *Configuration) validate() error {
if cfg.MaxRequestSize < 0 {
return fmt.Errorf("cfg.max_request_size must be a positive number. Got %d", cfg.MaxRequestSize)
}
return cfg.StoredRequests.validate()

if err := cfg.StoredRequests.validate(); err != nil {
return err
}

return cfg.GDPR.validate()
}

type GDPR struct {
HostVendorID int `mapstructure:"host_vendor_id"`
UsersyncIfAmbiguous bool `mapstructure:"usersync_if_ambiguous"`
}

func (cfg *GDPR) validate() error {
if cfg.HostVendorID < 0 || cfg.HostVendorID > 0xffff {
return fmt.Errorf("host_vendor_id must be in the range [0, %d]. Got %d", 0xffff, cfg.HostVendorID)
}

return nil
}

type Analytics struct {
Expand Down
36 changes: 36 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func TestDefaults(t *testing.T) {
}

var fullConfig = []byte(`
gdpr:
host_vendor_id: 15
usersync_if_ambiguous: true
host_cookie:
cookie_name: userid
family: prebid
Expand Down Expand Up @@ -95,6 +98,13 @@ func cmpInts(t *testing.T, key string, a int, b int) {
}
}

func cmpBools(t *testing.T, key string, a bool, b bool) {
t.Helper()
if a != b {
t.Errorf("%s: %t != %t", key, a, b)
}
}

func TestFullConfig(t *testing.T) {
v := newViperWithDefaults()
v.SetConfigType("yaml")
Expand All @@ -118,6 +128,8 @@ func TestFullConfig(t *testing.T) {
cmpStrings(t, "cache.scheme", cfg.CacheURL.Scheme, "http")
cmpStrings(t, "cache.host", cfg.CacheURL.Host, "prebidcache.net")
cmpStrings(t, "cache.query", cfg.CacheURL.Query, "uuid=%PBS_CACHE_UUID%")
cmpInts(t, "gdpr.host_vendor_id", cfg.GDPR.HostVendorID, 15)
cmpBools(t, "gdpr.usersync_if_ambiguous", cfg.GDPR.UsersyncIfAmbiguous, true)
cmpStrings(t, "recaptcha_secret", cfg.RecaptchaSecret, "asdfasdfasdfasdf")
cmpStrings(t, "metrics.influxdb.host", cfg.Metrics.Influxdb.Host, "upstream:8232")
cmpStrings(t, "metrics.influxdb.database", cfg.Metrics.Influxdb.Database, "metricsdb")
Expand Down Expand Up @@ -180,3 +192,27 @@ func TestNegativeRequestSize(t *testing.T) {
t.Error("cfg.max_request_size should prevent negative values, but it doesn't")
}
}

func TestNegativeVendorID(t *testing.T) {
cfg := Configuration{
GDPR: GDPR{
HostVendorID: -1,
},
}

if err := cfg.validate(); err == nil {
t.Error("cfg.gdpr.host_vendor_id should prevent negative values, but it doesn't")
}
}

func TestOverflowedVendorID(t *testing.T) {
cfg := Configuration{
GDPR: GDPR{
HostVendorID: (0xffff) + 1,
},
}

if err := cfg.validate(); err == nil {
t.Errorf("cfg.gdpr.host_vendor_id should prevent values over %d, but it doesn't", 0xffff)
}
}