Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/ads-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ sql-support = { path = "../support/sql" }
mockall = "0.12"
mockito = { version = "0.31", default-features = false }
viaduct-dev = { path = "../support/viaduct-dev" }
viaduct-reqwest = { path = "../support/viaduct-reqwest" }

[build-dependencies]
uniffi = { version = "0.29.0", features = ["build"] }
22 changes: 21 additions & 1 deletion components/ads-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,32 @@ This component is currently still under construction.

## Tests

Tests are run with
### Unit Tests

Unit tests are run with

```shell
cargo test -p ads-client
```

### Integration Tests

Integration tests make real HTTP calls to the Mozilla Ads Routing Service (MARS) and are not run automatically in CI. They are marked with `#[ignore]` and must be run manually.

To run integration tests:

```shell
cargo test -p ads-client --test integration_test -- --ignored
```

To run a specific integration test:

```shell
cargo test -p ads-client --test integration_test -- --ignored test_mock_pocket_billboard_1_placement
```

**Note:** Integration tests require network access and will make real HTTP requests to the MARS API.

## Usage

Please refer to `./docs/usage.md` for information on using the component.
107 changes: 107 additions & 0 deletions components/ads-client/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

use ads_client::{MozAdsClient, MozAdsPlacementRequest, MozAdsPlacementRequestWithCount};

#[test]
#[ignore]
fn test_mock_pocket_billboard_1_placement() {
viaduct_reqwest::use_reqwest_backend();

let client = MozAdsClient::new(None);

let placement_request = MozAdsPlacementRequest {
placement_id: "mock_pocket_billboard_1".to_string(),
iab_content: None,
};

let result = client.request_ads(vec![placement_request], None);
println!("result: {:?}", result);

assert!(result.is_ok(), "Failed to request ads: {:?}", result.err());

let placements = result.unwrap();

assert!(
placements.contains_key("mock_pocket_billboard_1"),
"Response should contain placement_id 'mock_pocket_billboard_1'"
);

let placement = placements
.get("mock_pocket_billboard_1")
.expect("Placement should exist");

assert!(!placement.url.is_empty(), "Ad URL should not be empty");
assert!(
!placement.image_url.is_empty(),
"Ad image URL should not be empty"
);
assert!(
!placement.format.is_empty(),
"Ad format should not be empty"
);
assert!(
!placement.block_key.is_empty(),
"Ad block_key should not be empty"
);
}

#[test]
#[ignore]
fn test_request_ads_multiset_count() {
viaduct_reqwest::use_reqwest_backend();

let client = MozAdsClient::new(None);

let requested_count = 3;
let placement_request = MozAdsPlacementRequestWithCount {
placement_id: "mock_pocket_billboard_1".to_string(),
count: requested_count,
iab_content: None,
};

let result = client.request_ads_multiset(vec![placement_request], None);
println!("result: {:?}", result);

assert!(result.is_ok(), "Failed to request ads: {:?}", result.err());

let placements = result.unwrap();

assert!(
placements.contains_key("mock_pocket_billboard_1"),
"Response should contain placement_id 'mock_pocket_billboard_1'"
);

let ads = placements
.get("mock_pocket_billboard_1")
.expect("Placement should exist");

assert_eq!(
ads.len(),
requested_count as usize,
"Should have {} ads, but got {}",
requested_count,
ads.len()
);

for (index, ad) in ads.iter().enumerate() {
assert!(!ad.url.is_empty(), "Ad {} URL should not be empty", index);
assert!(
!ad.image_url.is_empty(),
"Ad {} image URL should not be empty",
index
);
assert!(
!ad.format.is_empty(),
"Ad {} format should not be empty",
index
);
assert!(
!ad.block_key.is_empty(),
"Ad {} block_key should not be empty",
index
);
}
}