Skip to content

Commit

Permalink
Test: geo lookup (#3062)
Browse files Browse the repository at this point in the history
  • Loading branch information
marki1an authored Mar 18, 2024
1 parent 31fa9eb commit 4970e4e
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AccountConfig {
AccountMetricsConfig metrics
AccountCookieSyncConfig cookieSync
AccountHooksConfiguration hooks
AccountSetting settings

static getDefaultAccountConfig() {
new AccountConfig(status: AccountStatus.ACTIVE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.prebid.server.functional.model.config

import com.fasterxml.jackson.databind.PropertyNamingStrategies
import com.fasterxml.jackson.databind.annotation.JsonNaming
import groovy.transform.ToString

@ToString(includeNames = true, ignoreNulls = true)
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy)
class AccountSetting {

Boolean geoLookup
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ import org.prebid.server.functional.util.privacy.model.State

enum Country {

USA("USA"),
CAN("CAN"),
MULTIPLE("*")
USA("USA","US"),
CAN("CAN","CA"),
MULTIPLE("*","*")

@JsonValue
final String value
final String ISOAlpha3

Country(String value) {
this.value = value
final String ISOAlpha2

Country(String ISOAlpha3,String ISOAlpha2) {
this.ISOAlpha3 = ISOAlpha3
this.ISOAlpha2 = ISOAlpha2
}

@Override
String toString() {
value
ISOAlpha3
}

String withState(State state) {
return "${value}.${state.abbreviation}".toString()
return "${ISOAlpha3}.${state.abbreviation}".toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.prebid.server.functional.model.request.auction

enum PublicCountryIp {

USA_IP("209.232.44.21", "d646:2414:17b2:f371:9b62:f176:b4c0:51cd"),
UKR_IP("193.238.111.14", "3080:f30f:e4bc:0f56:41be:6aab:9d0a:58e2"),
CAN_IP("70.71.245.39", "f9b2:c742:1922:7d4b:7122:c7fc:8b75:98c8")

final String v4
final String v6

PublicCountryIp(String v4, String ipV6) {
this.v4 = v4
this.v6 = ipV6
}
}
222 changes: 222 additions & 0 deletions src/test/groovy/org/prebid/server/functional/tests/GeoSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package org.prebid.server.functional.tests

import org.prebid.server.functional.model.config.AccountAuctionConfig
import org.prebid.server.functional.model.config.AccountConfig
import org.prebid.server.functional.model.db.Account
import org.prebid.server.functional.model.pricefloors.Country
import org.prebid.server.functional.model.request.auction.BidRequest
import org.prebid.server.functional.model.config.AccountSetting
import org.prebid.server.functional.model.request.auction.Device
import org.prebid.server.functional.model.request.auction.Geo
import org.prebid.server.functional.util.PBSUtils
import java.time.Instant

import static org.prebid.server.functional.model.AccountStatus.ACTIVE
import static org.prebid.server.functional.model.pricefloors.Country.USA
import static org.prebid.server.functional.model.request.auction.PublicCountryIp.USA_IP
import static org.prebid.server.functional.model.request.auction.TraceLevel.VERBOSE
import static org.prebid.server.functional.util.privacy.model.State.ALABAMA
import static org.prebid.server.functional.util.privacy.model.State.ONTARIO

class GeoSpec extends BaseSpec {

private static final String GEO_LOCATION_REQUESTS = "geolocation_requests"
private static final String GEO_LOCATION_FAIL = "geolocation_fail"
private static final String GEO_LOCATION_SUCCESSFUL = "geolocation_successful"
private static final Map<String, String> GEO_LOCATION = ["geolocation.type" : "configuration",
"geolocation.configurations.[0].address-pattern" : USA_IP.v4,
"geolocation.configurations.[0].geo-info.country": USA.ISOAlpha2,
"geolocation.configurations.[0].geo-info.region" : ALABAMA.abbreviation]

def "PBS should populate geo with country and region when geo location enabled in host and account config"() {
given: "PBS service with geolocation and default account configs"
def config = AccountConfig.defaultAccountConfig.tap {
settings = new AccountSetting(geoLookup: defaultAccountGeoLookup)
}
def defaultPbsService = pbsServiceFactory.getService(
["settings.default-account-config": encode(config),
"geolocation.enabled" : "true"] + GEO_LOCATION)

and: "Default bid request with device and geo data"
def bidRequest = BidRequest.defaultBidRequest.tap {
device = new Device(
ip: USA_IP.v4,
ipv6: USA_IP.v6,
geo: new Geo(
country: null,
region: null,
lat: PBSUtils.getRandomDecimal(0, 90),
lon: PBSUtils.getRandomDecimal(0, 90)))
ext.prebid.trace = VERBOSE
}

and: "Account in the DB"
def accountConfig = new AccountConfig(
auction: new AccountAuctionConfig(debugAllow: true),
settings: new AccountSetting(geoLookup: accountGeoLookup))
def account = new Account(status: ACTIVE, uuid: bidRequest.site.publisher.id, config: accountConfig)
accountDao.save(account)

when: "PBS processes auction request"
defaultPbsService.sendAuctionRequest(bidRequest)

then: "Bidder request should contain country and region"
def bidderRequests = bidder.getBidderRequest(bidRequest.id)
assert bidderRequests.device.geo.country == USA
assert bidderRequests.device.geo.region == ALABAMA.abbreviation

and: "Metrics processed across activities should be updated"
def metrics = defaultPbsService.sendCollectedMetricsRequest()
assert metrics[GEO_LOCATION_REQUESTS] == 1
assert metrics[GEO_LOCATION_SUCCESSFUL] == 1
assert !metrics[GEO_LOCATION_FAIL]

where:
defaultAccountGeoLookup | accountGeoLookup
false | true
true | true
true | null
}

def "PBS shouldn't populate geo with country and region when geo location disable in host and account config enabled"() {
given: "PBS service with geolocation and default account configs"
def config = AccountConfig.defaultAccountConfig.tap {
settings = new AccountSetting(geoLookup: defaultAccountGeoLookupConfig)
}
def defaultPbsService = pbsServiceFactory.getService(GEO_LOCATION +
["settings.default-account-config": encode(config),
"geolocation.enabled" : hostGeolocation])

and: "Default bid request with device and geo data"
def bidRequest = BidRequest.defaultBidRequest.tap {
device = new Device(
ip: USA_IP.v4,
ipv6: USA_IP.v6,
geo: new Geo(
country: null,
region: null,
lat: PBSUtils.getRandomDecimal(0, 90),
lon: PBSUtils.getRandomDecimal(0, 90)))
ext.prebid.trace = VERBOSE
}

and: "Account in the DB"
def accountConfig = new AccountConfig(
auction: new AccountAuctionConfig(debugAllow: true),
settings: new AccountSetting(geoLookup: accountGeoLookup))
def account = new Account(status: ACTIVE, uuid: bidRequest.site.publisher.id, config: accountConfig)
accountDao.save(account)

when: "PBS processes auction request"
defaultPbsService.sendAuctionRequest(bidRequest)

then: "Bidder request shouldn't contain country and region"
def bidderRequests = bidder.getBidderRequest(bidRequest.id)
assert !bidderRequests.device.geo.country
assert !bidderRequests.device.geo.region

and: "Metrics processed across geo location shouldn't be updated"
def metrics = defaultPbsService.sendCollectedMetricsRequest()
assert !metrics[GEO_LOCATION_REQUESTS]
assert !metrics[GEO_LOCATION_SUCCESSFUL]
assert !metrics[GEO_LOCATION_FAIL]

where:
defaultAccountGeoLookupConfig | hostGeolocation | accountGeoLookup
true | "true" | false
true | "false" | true
false | "false" | false
false | "true" | false
}

def "PBS shouldn't populate geo with country, region and emit error in log and metric when geo look up failed"() {
given: "Test start time"
def startTime = Instant.now()

and: "PBS service with geolocation"
def defaultPbsService = pbsServiceFactory.getService(GEO_LOCATION +
["geolocation.configurations.[0].address-pattern": PBSUtils.randomNumber as String,
"geolocation.enabled" : "true"])

and: "Default bid request with device and geo data"
def bidRequest = BidRequest.defaultBidRequest.tap {
device = new Device(
ip: USA_IP.v4,
ipv6: USA_IP.v6,
geo: new Geo(
country: null,
region: null,
lat: PBSUtils.getRandomDecimal(0, 90),
lon: PBSUtils.getRandomDecimal(0, 90)))
ext.prebid.trace = VERBOSE
}

and: "Account in the DB"
def accountConfig = new AccountConfig(
auction: new AccountAuctionConfig(debugAllow: true),
settings: new AccountSetting(geoLookup: true))
def account = new Account(status: ACTIVE, uuid: bidRequest.site.publisher.id, config: accountConfig)
accountDao.save(account)

when: "PBS processes auction request"
defaultPbsService.sendAuctionRequest(bidRequest)

then: "Bidder request shouldn't contain country and region"
def bidderRequests = bidder.getBidderRequest(bidRequest.id)
assert !bidderRequests.device.geo.country
assert !bidderRequests.device.geo.region

and: "Metrics processed across geo location should be updated"
def metrics = defaultPbsService.sendCollectedMetricsRequest()
assert metrics[GEO_LOCATION_REQUESTS] == 1
assert metrics[GEO_LOCATION_FAIL] == 1
assert !metrics[GEO_LOCATION_SUCCESSFUL]

and: "PBs should emit geo failed logs"
def logs = defaultPbsService.getLogsByTime(startTime)
def getLocation = getLogsByText(logs, "GeoLocationServiceWrapper")
assert getLocation.size() == 1
assert getLocation[0].contains("Geolocation lookup failed: " +
"ConfigurationGeoLocationService: Geo location lookup failed.")
}

def "PBS shouldn't populate country and region via geo when geo enabled in account and country and region specified in request"() {
given: "PBS service with geolocation"
def defaultPbsService = pbsServiceFactory.getService(
["geolocation.enabled": "true"] + GEO_LOCATION)

and: "Default bid request with device and geo data"
def bidRequest = BidRequest.defaultBidRequest.tap {
device = new Device(
ip: USA_IP.v4,
ipv6: USA_IP.v6,
geo: new Geo(
country: Country.CAN,
region: ONTARIO.abbreviation,
lat: PBSUtils.getRandomDecimal(0, 90),
lon: PBSUtils.getRandomDecimal(0, 90)))
ext.prebid.trace = VERBOSE
}

and: "Account in the DB"
def accountConfig = new AccountConfig(
auction: new AccountAuctionConfig(debugAllow: true),
settings: new AccountSetting(geoLookup: true))
def account = new Account(status: ACTIVE, uuid: bidRequest.site.publisher.id, config: accountConfig)
accountDao.save(account)

when: "PBS processes auction request"
defaultPbsService.sendAuctionRequest(bidRequest)

then: "Bidder request should contain country and region"
def bidderRequests = bidder.getBidderRequest(bidRequest.id)
assert bidderRequests.device.geo.country == Country.CAN
assert bidderRequests.device.geo.region == ONTARIO.abbreviation

and: "Metrics processed across activities shouldn't be updated"
def metrics = defaultPbsService.sendCollectedMetricsRequest()
assert !metrics[GEO_LOCATION_REQUESTS]
assert !metrics[GEO_LOCATION_SUCCESSFUL]
assert !metrics[GEO_LOCATION_FAIL]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ActivityTraceLogSpec extends PrivacyBaseSpec {
componentName: GENERIC.value,
componentType: BIDDER,
region: ALABAMA.abbreviation,
country: USA.value))
country: USA.ISOAlpha3))
assert fetchBidsActivity.ruleConfiguration.every { it == null }
assert fetchBidsActivity.allowByDefault.contains(activity.defaultAction)
assert fetchBidsActivity.result.contains("DISALLOW")
Expand Down Expand Up @@ -133,7 +133,7 @@ class ActivityTraceLogSpec extends PrivacyBaseSpec {
componentName: GENERIC.value,
componentType: BIDDER,
region: ALABAMA.abbreviation,
country: USA.value))
country: USA.ISOAlpha3))
assert fetchBidsActivity.allowByDefault.contains(activity.defaultAction)
assert fetchBidsActivity.ruleConfiguration.every { it == null }
assert fetchBidsActivity.result.contains("ALLOW")
Expand All @@ -148,7 +148,7 @@ class ActivityTraceLogSpec extends PrivacyBaseSpec {
componentName: GENERIC.value,
componentType: BIDDER,
region: ALABAMA.abbreviation,
country: USA.value))
country: USA.ISOAlpha3))
assert transmitUfpdActivity.allowByDefault.contains(activity.defaultAction)
assert transmitUfpdActivity.ruleConfiguration.every { it == null }
assert transmitUfpdActivity.result.every { it == null }
Expand All @@ -163,7 +163,7 @@ class ActivityTraceLogSpec extends PrivacyBaseSpec {
componentName: GENERIC.value,
componentType: BIDDER,
region: ALABAMA.abbreviation,
country: USA.value))
country: USA.ISOAlpha3))
assert transmitPreciseGeoActivity.allowByDefault.contains(activity.defaultAction)
assert transmitPreciseGeoActivity.ruleConfiguration.every { it == null }
assert transmitPreciseGeoActivity.result.every { it == null }
Expand All @@ -178,7 +178,7 @@ class ActivityTraceLogSpec extends PrivacyBaseSpec {
componentName: GENERIC.value,
componentType: BIDDER,
region: ALABAMA.abbreviation,
country: USA.value))
country: USA.ISOAlpha3))
assert transmitTidActivity.allowByDefault.contains(activity.defaultAction)
assert transmitTidActivity.ruleConfiguration.every { it == null }
assert transmitTidActivity.result.every { it == null }
Expand Down Expand Up @@ -227,7 +227,7 @@ class ActivityTraceLogSpec extends PrivacyBaseSpec {
componentType: BIDDER,
gpc: bidRequest.regs.ext.gpc,
region: ALABAMA.abbreviation,
country: USA.value))
country: USA.ISOAlpha3))
assert fetchBidsActivity.ruleConfiguration.contains(new RuleConfiguration(
componentNames: condition.componentName,
componentTypes: condition.componentType,
Expand Down Expand Up @@ -265,7 +265,7 @@ class ActivityTraceLogSpec extends PrivacyBaseSpec {
componentType: BIDDER,
gpc: bidRequest.regs.ext.gpc,
region: ALABAMA.abbreviation,
country: USA.value))
country: USA.ISOAlpha3))
assert transmitPreciseGeoActivity.ruleConfiguration.every { it == null }
assert transmitPreciseGeoActivity.allowByDefault.contains(activity.defaultAction)
assert transmitPreciseGeoActivity.result.every { it == null }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class GppFetchBidActivitiesSpec extends PrivacyBaseSpec {

where:
deviceGeo | conditionGeo
null | [USA.value]
null | [USA.ISOAlpha3]
new Geo(country: USA) | null
new Geo(region: ALABAMA.abbreviation) | [USA.withState(ALABAMA)]
new Geo(country: CAN, region: ALABAMA.abbreviation) | [USA.withState(ALABAMA)]
Expand Down Expand Up @@ -419,7 +419,7 @@ class GppFetchBidActivitiesSpec extends PrivacyBaseSpec {

where:
deviceGeo | conditionGeo
new Geo(country: USA) | [USA.value]
new Geo(country: USA) | [USA.ISOAlpha3]
new Geo(country: USA, region: ALABAMA.abbreviation) | [USA.withState(ALABAMA)]
new Geo(country: USA, region: ALABAMA.abbreviation) | [CAN.withState(ONTARIO), USA.withState(ALABAMA)]
}
Expand Down
Loading

0 comments on commit 4970e4e

Please sign in to comment.