Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
Added GDPR to the app config (prebid#505)
Browse files Browse the repository at this point in the history
* Added GDPR to the app config.

* Removed some accidental changes.

* Added some unit tests.

* Fixed a vet error.
  • Loading branch information
dbemiller authored May 14, 2018
1 parent f10540c commit def5ef8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
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)
}
}

0 comments on commit def5ef8

Please sign in to comment.