Skip to content

Commit

Permalink
Refactor EnabledForIntegrationType methods on account privacy objects
Browse files Browse the repository at this point in the history
  • Loading branch information
bsardo committed Nov 17, 2020
1 parent 679cb47 commit c1b832c
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 121 deletions.
47 changes: 20 additions & 27 deletions config/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,7 @@ type AccountCCPA struct {
// EnabledForIntegrationType indicates whether CCPA is turned on at the account level for the specified integration type
// by using the integration type setting if defined or the general CCPA setting if defined; otherwise it returns nil
func (a *AccountCCPA) EnabledForIntegrationType(integrationType IntegrationType) *bool {
var integrationEnabled *bool

switch integrationType {
case IntegrationTypeAMP:
integrationEnabled = a.IntegrationEnabled.AMP
case IntegrationTypeApp:
integrationEnabled = a.IntegrationEnabled.App
case IntegrationTypeVideo:
integrationEnabled = a.IntegrationEnabled.Video
case IntegrationTypeWeb:
integrationEnabled = a.IntegrationEnabled.Web
}

if integrationEnabled != nil {
if integrationEnabled := a.IntegrationEnabled.GetByIntegrationType(integrationType); integrationEnabled != nil {
return integrationEnabled
}
if a.Enabled != nil {
Expand All @@ -62,20 +49,8 @@ type AccountGDPR struct {
// EnabledForIntegrationType indicates whether GDPR is turned on at the account level for the specified integration type
// by using the integration type setting if defined or the general GDPR setting if defined; otherwise it returns nil
func (a *AccountGDPR) EnabledForIntegrationType(integrationType IntegrationType) *bool {
var integrationEnabled *bool

switch integrationType {
case IntegrationTypeAMP:
integrationEnabled = a.IntegrationEnabled.AMP
case IntegrationTypeApp:
integrationEnabled = a.IntegrationEnabled.App
case IntegrationTypeVideo:
integrationEnabled = a.IntegrationEnabled.Video
case IntegrationTypeWeb:
integrationEnabled = a.IntegrationEnabled.Web
}

if integrationEnabled != nil {
if integrationEnabled := a.IntegrationEnabled.GetByIntegrationType(integrationType); integrationEnabled != nil {
return integrationEnabled
}
if a.Enabled != nil {
Expand All @@ -92,3 +67,21 @@ type AccountIntegration struct {
Video *bool `mapstructure:"video" json:"video,omitempty"`
Web *bool `mapstructure:"web" json:"web,omitempty"`
}

// GetByIntegrationType looks up the account integration enabled setting for the specified integration type
func (a *AccountIntegration) GetByIntegrationType(integrationType IntegrationType) *bool {
var integrationEnabled *bool

switch integrationType {
case IntegrationTypeAMP:
integrationEnabled = a.AMP
case IntegrationTypeApp:
integrationEnabled = a.App
case IntegrationTypeVideo:
integrationEnabled = a.Video
case IntegrationTypeWeb:
integrationEnabled = a.Web
}

return integrationEnabled
}
206 changes: 112 additions & 94 deletions config/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,26 @@ func TestAccountGDPREnabledForIntegrationType(t *testing.T) {
trueValue, falseValue := true, false

tests := []struct {
description string
giveIntegrationType IntegrationType
giveGDPREnabled *bool
giveAMPGDPREnabled *bool
giveAppGDPREnabled *bool
giveVideoGDPREnabled *bool
giveWebGDPREnabled *bool
wantEnabled *bool
description string
giveIntegrationType IntegrationType
giveGDPREnabled *bool
giveWebGDPREnabled *bool
wantEnabled *bool
}{
{
description: "GDPR AMP integration enabled, general GDPR disabled",
giveIntegrationType: IntegrationTypeAMP,
giveGDPREnabled: &falseValue,
giveAMPGDPREnabled: &trueValue,
wantEnabled: &trueValue,
},
{
description: "GDPR App integration enabled, general GDPR disabled",
giveIntegrationType: IntegrationTypeApp,
giveGDPREnabled: &falseValue,
giveAppGDPREnabled: &trueValue,
wantEnabled: &trueValue,
},
{
description: "GDPR Video integration enabled, general GDPR disabled",
giveIntegrationType: IntegrationTypeVideo,
giveGDPREnabled: &falseValue,
giveVideoGDPREnabled: &trueValue,
wantEnabled: &trueValue,
},
{
description: "GDPR Web integration enabled, general GDPR disabled",
giveIntegrationType: IntegrationTypeWeb,
giveGDPREnabled: &falseValue,
giveWebGDPREnabled: &trueValue,
wantEnabled: &trueValue,
},
{
description: "Web integration enabled, general GDPR unspecified",
giveIntegrationType: IntegrationTypeWeb,
giveGDPREnabled: nil,
giveWebGDPREnabled: &trueValue,
wantEnabled: &trueValue,
},
{
description: "GDPR Web integration disabled, general GDPR enabled",
giveIntegrationType: IntegrationTypeWeb,
giveGDPREnabled: &trueValue,
giveWebGDPREnabled: &falseValue,
wantEnabled: &falseValue,
},
{
description: "GDPR Web integration disabled, general GDPR unspecified",
giveIntegrationType: IntegrationTypeWeb,
giveGDPREnabled: nil,
giveWebGDPREnabled: &falseValue,
wantEnabled: &falseValue,
},
{
description: "GDPR Web integration unspecified, general GDPR disabled",
giveIntegrationType: IntegrationTypeWeb,
Expand Down Expand Up @@ -96,10 +58,7 @@ func TestAccountGDPREnabledForIntegrationType(t *testing.T) {
GDPR: AccountGDPR{
Enabled: tt.giveGDPREnabled,
IntegrationEnabled: AccountIntegration{
AMP: tt.giveAMPGDPREnabled,
App: tt.giveAppGDPREnabled,
Video: tt.giveVideoGDPREnabled,
Web: tt.giveWebGDPREnabled,
Web: tt.giveWebGDPREnabled,
},
},
}
Expand All @@ -119,64 +78,26 @@ func TestAccountCCPAEnabledForIntegrationType(t *testing.T) {
trueValue, falseValue := true, false

tests := []struct {
description string
giveIntegrationType IntegrationType
giveCCPAEnabled *bool
giveAMPCCPAEnabled *bool
giveAppCCPAEnabled *bool
giveVideoCCPAEnabled *bool
giveWebCCPAEnabled *bool
wantEnabled *bool
description string
giveIntegrationType IntegrationType
giveCCPAEnabled *bool
giveWebCCPAEnabled *bool
wantEnabled *bool
}{
{
description: "CCPA AMP integration enabled, general CCPA disabled",
giveIntegrationType: IntegrationTypeAMP,
giveCCPAEnabled: &falseValue,
giveAMPCCPAEnabled: &trueValue,
wantEnabled: &trueValue,
},
{
description: "CCPA App integration enabled, general CCPA disabled",
giveIntegrationType: IntegrationTypeApp,
giveCCPAEnabled: &falseValue,
giveAppCCPAEnabled: &trueValue,
wantEnabled: &trueValue,
},
{
description: "CCPA Video integration enabled, general CCPA disabled",
giveIntegrationType: IntegrationTypeVideo,
giveCCPAEnabled: &falseValue,
giveVideoCCPAEnabled: &trueValue,
wantEnabled: &trueValue,
},
{
description: "CCPA Web integration enabled, general CCPA disabled",
giveIntegrationType: IntegrationTypeWeb,
giveCCPAEnabled: &falseValue,
giveWebCCPAEnabled: &trueValue,
wantEnabled: &trueValue,
},
{
description: "Web integration enabled, general CCPA unspecified",
giveIntegrationType: IntegrationTypeWeb,
giveCCPAEnabled: nil,
giveWebCCPAEnabled: &trueValue,
wantEnabled: &trueValue,
},
{
description: "CCPA Web integration disabled, general CCPA enabled",
giveIntegrationType: IntegrationTypeWeb,
giveCCPAEnabled: &trueValue,
giveWebCCPAEnabled: &falseValue,
wantEnabled: &falseValue,
},
{
description: "CCPA Web integration disabled, general CCPA unspecified",
giveIntegrationType: IntegrationTypeWeb,
giveCCPAEnabled: nil,
giveWebCCPAEnabled: &falseValue,
wantEnabled: &falseValue,
},
{
description: "CCPA Web integration unspecified, general CCPA disabled",
giveIntegrationType: IntegrationTypeWeb,
Expand Down Expand Up @@ -205,10 +126,7 @@ func TestAccountCCPAEnabledForIntegrationType(t *testing.T) {
CCPA: AccountCCPA{
Enabled: tt.giveCCPAEnabled,
IntegrationEnabled: AccountIntegration{
AMP: tt.giveAMPCCPAEnabled,
App: tt.giveAppCCPAEnabled,
Video: tt.giveVideoCCPAEnabled,
Web: tt.giveWebCCPAEnabled,
Web: tt.giveWebCCPAEnabled,
},
},
}
Expand All @@ -223,3 +141,103 @@ func TestAccountCCPAEnabledForIntegrationType(t *testing.T) {
}
}
}

func TestAccountIntegrationGetByIntegrationType(t *testing.T) {
trueValue, falseValue := true, false

tests := []struct {
description string
giveAMPEnabled *bool
giveAppEnabled *bool
giveVideoEnabled *bool
giveWebEnabled *bool
giveIntegrationType IntegrationType
wantEnabled *bool
}{
{
description: "AMP integration setting unspecified, returns nil",
giveIntegrationType: IntegrationTypeAMP,
wantEnabled: nil,
},
{
description: "AMP integration disabled, returns false",
giveAMPEnabled: &falseValue,
giveIntegrationType: IntegrationTypeAMP,
wantEnabled: &falseValue,
},
{
description: "AMP integration enabled, returns true",
giveAMPEnabled: &trueValue,
giveIntegrationType: IntegrationTypeAMP,
wantEnabled: &trueValue,
},
{
description: "App integration setting unspecified, returns nil",
giveIntegrationType: IntegrationTypeApp,
wantEnabled: nil,
},
{
description: "App integration disabled, returns false",
giveAppEnabled: &falseValue,
giveIntegrationType: IntegrationTypeApp,
wantEnabled: &falseValue,
},
{
description: "App integration enabled, returns true",
giveAppEnabled: &trueValue,
giveIntegrationType: IntegrationTypeApp,
wantEnabled: &trueValue,
},
{
description: "Video integration setting unspecified, returns nil",
giveIntegrationType: IntegrationTypeVideo,
wantEnabled: nil,
},
{
description: "Video integration disabled, returns false",
giveVideoEnabled: &falseValue,
giveIntegrationType: IntegrationTypeVideo,
wantEnabled: &falseValue,
},
{
description: "Video integration enabled, returns true",
giveVideoEnabled: &trueValue,
giveIntegrationType: IntegrationTypeVideo,
wantEnabled: &trueValue,
},
{
description: "Web integration setting unspecified, returns nil",
giveIntegrationType: IntegrationTypeWeb,
wantEnabled: nil,
},
{
description: "Web integration disabled, returns false",
giveWebEnabled: &falseValue,
giveIntegrationType: IntegrationTypeWeb,
wantEnabled: &falseValue,
},
{
description: "Web integration enabled, returns true",
giveWebEnabled: &trueValue,
giveIntegrationType: IntegrationTypeWeb,
wantEnabled: &trueValue,
},
}

for _, tt := range tests {
accountIntegration := AccountIntegration{
AMP: tt.giveAMPEnabled,
App: tt.giveAppEnabled,
Video: tt.giveVideoEnabled,
Web: tt.giveWebEnabled,
}

result := accountIntegration.GetByIntegrationType(tt.giveIntegrationType)
if tt.wantEnabled == nil {
assert.Nil(t, result, tt.description)
} else {
assert.NotNil(t, result, tt.description)
assert.Equal(t, *tt.wantEnabled, *result, tt.description)
}
}
}

0 comments on commit c1b832c

Please sign in to comment.