Skip to content

Commit

Permalink
Add get-ad-selection-data command
Browse files Browse the repository at this point in the history
  • Loading branch information
cshmerling committed Aug 19, 2024
1 parent f7828e1 commit 236ba29
Show file tree
Hide file tree
Showing 19 changed files with 858 additions and 225 deletions.
16 changes: 16 additions & 0 deletions adservices_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,19 @@ command:
```
python3 main.py app-signals trigger-encoding
```

## Ad Selection

### Get Ad Selection Data for a buyer.

This command provides plaintext output on the console which can be used to test
the GetBids request on Buyer FrontEnd service of Bidding and Auction
architecture using the
[Secure Invoke tool](https://github.com/privacysandbox/bidding-auction-servers/blob/main/tools/secure_invoke/README.md#sending-getbidsrawrequest-to-bfe)

To generate the output run the following command:

```
python3 main.py ad-selection get-ad-selection-data \
--buyer <buyer>
```
62 changes: 47 additions & 15 deletions adservices_cli/ad_selection.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
"""
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Command for interacting with Protected Audience Ad Selection CLI Commands."""

import base64
import json

from google.protobuf.json_format import MessageToJson

import adb
import bidding_auction_servers_pb2
import utilities

_COMMAND_PREFIX = "ad-selection"
Expand All @@ -28,6 +30,8 @@
_CONSENTED_DEBUG_COMMAND_ENABLE_EXPIRY_IN_HOURS = "--expires-in-hours"
_CONSENTED_DEBUG_COMMAND_DISABLE = "disable"
_CONSENTED_DEBUG_COMMAND_VIEW = "view"
_GET_AD_SELECTION_DATA_COMMAND = "get-ad-selection-data"
_ARG_BUYER = "--buyer"


class AdSelection:
Expand Down Expand Up @@ -111,3 +115,31 @@ def disable_consented_debug(self) -> str:
{},
)
)

def get_ad_selection_data(self, buyer: str) -> str:
"""Prints the JSON formatted input for GetBids request to BuyerFrontEnd service.
Args:
buyer: AdTech buyer.
Returns:
Textual output of get_ad_selection__data command.
"""
command_output = self._adb.execute_adservices_shell_command(
utilities.format_command(
_COMMAND_PREFIX,
_GET_AD_SELECTION_DATA_COMMAND,
"",
{_ARG_BUYER: buyer},
)
)
try:
proto_json = json.loads(command_output)
base64_decoded_str = base64.b64decode(proto_json.get("output_proto"))
return MessageToJson(
bidding_auction_servers_pb2.GetBidsRequest.GetBidsRawRequest.FromString(
base64_decoded_str
)
)
except ValueError:
return command_output
72 changes: 57 additions & 15 deletions adservices_cli/ad_selection_test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
"""
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json

Expand All @@ -36,6 +34,17 @@
"Successfully disabled consented debugging."
)

_GET_AD_SELECTION_DATA_SHELL_CMD_RESPONSE = '{"output_proto":"EvgBCngKCXdpbm5pbmdDQRIFc2hvZXMSIGJhLWJ1eWVyLTVqeXk1dWxhZ3EtdWMuYS5ydW4uYXBwEgRrZXkxEgRrZXkyGgExKgJ7fUIvY29tLmV4YW1wbGUuYWRzZXJ2aWNlcy5zYW1wbGVzLmZsZWRnZS5zYW1wbGVhcHAKfAoNc2hpcnRzX3NlcnZlchIFc2hvZXMSIGJhLWJ1eWVyLTVqeXk1dWxhZ3EtdWMuYS5ydW4uYXBwEgRrZXkxEgRrZXkyGgEyKgJ7fUIvY29tLmV4YW1wbGUuYWRzZXJ2aWNlcy5zYW1wbGVzLmZsZWRnZS5zYW1wbGVhcHAaAnt9IgJ7fSpAUGxhY2Vob2xkZXItU2hvdWxkLU1hdGNoLVdpdGgtU2VsbGVyLU9yaWdpbi1Eb21haW4tSW4tU0ZFLUNvbmZpZzIuUGxhY2Vob2xkZXItU2hvdWxkLU1hdGNoLVdpdGgtQXBwLVBhY2thZ2UtTmFtZTgBSg4IARIKMTIzNDU2Nzg5MFICCgBYAWJKUGxhY2Vob2xkZXItU2hvdWxkLU1hdGNoLVdpdGgtVG9wLUxldmVsLVNlbGxlci1PcmlnaW4tRG9tYWluLUluLVNGRS1Db25maWdoAHAB"}'


_GET_AD_SELECTION_DATA_EXPECTED_RESPONSE = json.loads(
'{"buyerInput":{"interestGroups":[{"name":"winningCA","biddingSignalsKeys":["shoes","ba-buyer-5jyy5ulagq-uc.a.run.app","key1","key2"],"adRenderIds":["1"],"userBiddingSignals":"{}","origin":"com.example.adservices.samples.fledge.sampleapp"},{"name":"shirts_server","biddingSignalsKeys":["shoes","ba-buyer-5jyy5ulagq-uc.a.run.app","key1","key2"],"adRenderIds":["2"],"userBiddingSignals":"{}","origin":"com.example.adservices.samples.fledge.sampleapp"}]},"auctionSignals":"{}","buyerSignals":"{}","seller":"Placeholder-Should-Match-With-Seller-Origin-Domain-In-SFE-Config","publisherName":"Placeholder-Should-Match-With-App-Package-Name","enableDebugReporting":true,"consentedDebugConfig":{"isConsented":true,"token":"1234567890"},"protectedAppSignalsBuyerInput":{"protectedAppSignals":{}},"clientType":"CLIENT_TYPE_ANDROID","topLevelSeller":"Placeholder-Should-Match-With-Top-Level-Seller-Origin-Domain-In-SFE-Config","buyerKvExperimentGroupId":0,"enableUnlimitedEgress":true}'
)

_GET_AD_SELECTION_DATA_SHELL_CMD_NO_DATA_RESPONSE = (
"could not find data for buyer: test-buyer"
)


class AdSelectionTest(absltest.TestCase):

Expand Down Expand Up @@ -120,6 +129,39 @@ def test_disable_consented_debug_happy_path(self):
)
self.assertEqual(output, _DISABLED_CONSENTED_DEBUG_RESPONSE)

def test_get_ad_selection_data_happy_path(self):
self.adb.set_shell_outputs([_GET_AD_SELECTION_DATA_SHELL_CMD_RESPONSE])
buyer = "test-buyer"

output = self.ad_selection.get_ad_selection_data(buyer)

self.assertContainsSubset(
utilities.split_adb_command(ad_selection._COMMAND_PREFIX)
+ [
ad_selection._GET_AD_SELECTION_DATA_COMMAND,
],
self.adb.shell_calls[0],
)
json_output = json.loads(output)
self.assertEqual(json_output, _GET_AD_SELECTION_DATA_EXPECTED_RESPONSE)

def test_get_ad_selection_data_no_data_for_buyer(self):
self.adb.set_shell_outputs(
[_GET_AD_SELECTION_DATA_SHELL_CMD_NO_DATA_RESPONSE]
)
buyer = "test-buyer"

output = self.ad_selection.get_ad_selection_data(buyer)

self.assertContainsSubset(
utilities.split_adb_command(ad_selection._COMMAND_PREFIX)
+ [
ad_selection._GET_AD_SELECTION_DATA_COMMAND,
],
self.adb.shell_calls[0],
)
self.assertEqual(output, _GET_AD_SELECTION_DATA_SHELL_CMD_NO_DATA_RESPONSE)


if __name__ == "__main__":
absltest.main()
28 changes: 13 additions & 15 deletions adservices_cli/adb.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
"""
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""ADB commands expressed as simple functions."""

Expand Down
28 changes: 13 additions & 15 deletions adservices_cli/adservices.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
"""
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Command for interacting with adservices."""

Expand Down
28 changes: 13 additions & 15 deletions adservices_cli/app_signals.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
"""
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Command for interacting with Protected App Signals CLI Commands."""

Expand Down
28 changes: 13 additions & 15 deletions adservices_cli/app_signals_test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
"""
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from absl.testing import absltest

Expand Down
Loading

0 comments on commit 236ba29

Please sign in to comment.