Skip to content

Commit 4321f9b

Browse files
authored
Added publisher config
1 parent b33c96d commit 4321f9b

File tree

12 files changed

+166
-174
lines changed

12 files changed

+166
-174
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3030

3131
### Added
3232
- Added basic unit tests
33+
- Added publisher config
3334

3435
### Changed
3536
- Upgrade to rust 1.87.0

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ tokio = { version = "1.46", features = ["sync", "macros", "io-util", "rt", "time
2727
url = "2.4.1"
2828

2929
[dev-dependencies]
30+
regex = "1.1.1"
3031
temp-env = "0.3.6"

crates/common/src/cookies.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use cookie::{Cookie, CookieJar};
22
use fastly::http::header;
33
use fastly::Request;
44

5+
use crate::settings::Settings;
6+
57
const COOKIE_MAX_AGE: i32 = 365 * 24 * 60 * 60; // 1 year
68

79
// return empty cookie jar for unparsable cookies
@@ -31,15 +33,17 @@ pub fn handle_request_cookies(req: &Request) -> Option<CookieJar> {
3133
}
3234
}
3335

34-
pub fn create_synthetic_cookie(synthetic_id: &str) -> String {
36+
pub fn create_synthetic_cookie(settings: &Settings, synthetic_id: &str) -> String {
3537
format!(
36-
"synthetic_id={}; Domain=.auburndao.com; Path=/; Secure; SameSite=Lax; Max-Age={}",
37-
synthetic_id, COOKIE_MAX_AGE,
38+
"synthetic_id={}; Domain={}; Path=/; Secure; SameSite=Lax; Max-Age={}",
39+
synthetic_id, settings.publisher.cookie_domain, COOKIE_MAX_AGE,
3840
)
3941
}
4042

4143
#[cfg(test)]
4244
mod tests {
45+
use crate::test_support::tests::create_test_settings;
46+
4347
use super::*;
4448

4549
#[test]
@@ -113,10 +117,14 @@ mod tests {
113117

114118
#[test]
115119
fn test_create_synthetic_cookie() {
116-
let result = create_synthetic_cookie("12345");
120+
let settings = create_test_settings();
121+
let result = create_synthetic_cookie(&settings, "12345");
117122
assert_eq!(
118123
result,
119-
"synthetic_id=12345; Domain=.auburndao.com; Path=/; Secure; SameSite=Lax; Max-Age=31536000"
124+
format!(
125+
"synthetic_id=12345; Domain={}; Path=/; Secure; SameSite=Lax; Max-Age={}",
126+
settings.publisher.cookie_domain, COOKIE_MAX_AGE,
127+
)
120128
);
121129
}
122130
}

crates/common/src/gdpr.rs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ pub fn get_consent_from_request(req: &Request) -> Option<GdprConsent> {
5858
None
5959
}
6060

61-
pub fn create_consent_cookie(consent: &GdprConsent) -> String {
61+
pub fn create_consent_cookie(settings: &Settings, consent: &GdprConsent) -> String {
6262
format!(
63-
"gdpr_consent={}; Domain=.auburndao.com; Path=/; Secure; SameSite=Lax; Max-Age=31536000",
64-
serde_json::to_string(consent).unwrap_or_default()
63+
"gdpr_consent={}; Domain={}; Path=/; Secure; SameSite=Lax; Max-Age=31536000",
64+
serde_json::to_string(consent).unwrap_or_default(),
65+
settings.publisher.cookie_domain,
6566
)
6667
}
6768

68-
pub fn handle_consent_request(_settings: &Settings, req: Request) -> Result<Response, Error> {
69+
pub fn handle_consent_request(settings: &Settings, req: Request) -> Result<Response, Error> {
6970
match *req.get_method() {
7071
Method::GET => {
7172
// Return current consent status
@@ -81,7 +82,10 @@ pub fn handle_consent_request(_settings: &Settings, req: Request) -> Result<Resp
8182
.with_header(header::CONTENT_TYPE, "application/json")
8283
.with_body(serde_json::to_string(&consent)?);
8384

84-
response.set_header(header::SET_COOKIE, create_consent_cookie(&consent));
85+
response.set_header(
86+
header::SET_COOKIE,
87+
create_consent_cookie(settings, &consent),
88+
);
8589
Ok(response)
8690
}
8791
_ => {
@@ -132,23 +136,7 @@ mod tests {
132136
use super::*;
133137
use fastly::{Body, Request};
134138

135-
fn create_test_settings() -> Settings {
136-
Settings {
137-
ad_server: crate::settings::AdServer {
138-
ad_partner_url: "https://test.com".to_string(),
139-
sync_url: "https://sync.test.com".to_string(),
140-
},
141-
prebid: crate::settings::Prebid {
142-
server_url: "https://prebid.test.com".to_string(),
143-
},
144-
synthetic: crate::settings::Synthetic {
145-
counter_store: "test-counter".to_string(),
146-
opid_store: "test-opid".to_string(),
147-
secret_key: "test-secret".to_string(),
148-
template: "{{test}}".to_string(),
149-
},
150-
}
151-
}
139+
use crate::test_support::tests::create_test_settings;
152140

153141
#[test]
154142
fn test_gdpr_consent_default() {
@@ -196,6 +184,7 @@ mod tests {
196184

197185
#[test]
198186
fn test_create_consent_cookie() {
187+
let settings = create_test_settings();
199188
let consent = GdprConsent {
200189
analytics: true,
201190
advertising: true,
@@ -204,9 +193,9 @@ mod tests {
204193
version: "1.0".to_string(),
205194
};
206195

207-
let cookie = create_consent_cookie(&consent);
196+
let cookie = create_consent_cookie(&settings, &consent);
208197
assert!(cookie.starts_with("gdpr_consent="));
209-
assert!(cookie.contains("Domain=.auburndao.com"));
198+
assert!(cookie.contains(format!("Domain={}", settings.publisher.cookie_domain).as_str()));
210199
assert!(cookie.contains("Path=/"));
211200
assert!(cookie.contains("Secure"));
212201
assert!(cookie.contains("SameSite=Lax"));
@@ -297,7 +286,10 @@ mod tests {
297286
let set_cookie = response.get_header_str(header::SET_COOKIE);
298287
assert!(set_cookie.is_some());
299288
assert!(set_cookie.unwrap().contains("gdpr_consent="));
300-
assert!(set_cookie.unwrap().contains("Domain=.auburndao.com"));
289+
290+
assert!(set_cookie
291+
.unwrap()
292+
.contains(format!("Domain={}", settings.publisher.cookie_domain).as_str()));
301293

302294
// Check response body
303295
let body = response.into_body_str();

crates/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ pub mod privacy;
77
pub mod settings;
88
pub mod synthetic;
99
pub mod templates;
10+
pub mod test_support;
1011
pub mod why;

crates/common/src/prebid.rs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl PrebidRequest {
6464
.and_then(|o| url::Url::parse(o).ok())
6565
.and_then(|u| u.host_str().map(|h| h.to_string()))
6666
})
67-
.unwrap_or_else(|| "auburndao.com".to_string());
67+
.unwrap_or_else(|| settings.publisher.domain.clone());
6868

6969
log::info!("Detected domain: {}", domain);
7070

@@ -192,23 +192,7 @@ mod tests {
192192
use super::*;
193193
use fastly::Request;
194194

195-
fn create_test_settings() -> Settings {
196-
Settings {
197-
ad_server: crate::settings::AdServer {
198-
ad_partner_url: "https://test.com".to_string(),
199-
sync_url: "https://sync.test.com".to_string(),
200-
},
201-
prebid: crate::settings::Prebid {
202-
server_url: "https://prebid.test.com/openrtb2/auction".to_string(),
203-
},
204-
synthetic: crate::settings::Synthetic {
205-
counter_store: "test-counter".to_string(),
206-
opid_store: "test-opid".to_string(),
207-
secret_key: "test-secret-key".to_string(),
208-
template: "{{client_ip}}:{{user_agent}}:{{first_party_id}}:{{auth_user_id}}:{{publisher_domain}}:{{accept_language}}".to_string(),
209-
},
210-
}
211-
}
195+
use crate::test_support::tests::create_test_settings;
212196

213197
#[test]
214198
fn test_prebid_request_new_with_full_headers() {
@@ -258,31 +242,34 @@ mod tests {
258242
#[test]
259243
fn test_prebid_request_domain_fallback() {
260244
let settings = create_test_settings();
261-
let req = Request::get("https://example.com/test");
245+
let url = format!("https://{}", settings.publisher.domain);
246+
let req = Request::get(url.clone());
262247
// No referer or origin headers
263248

264249
let prebid_req = PrebidRequest::new(&settings, &req).unwrap();
265250

266-
assert_eq!(prebid_req.domain, "auburndao.com");
267-
assert_eq!(prebid_req.origin, "https://auburndao.com");
251+
assert_eq!(prebid_req.domain, settings.publisher.domain);
252+
assert_eq!(prebid_req.origin, url);
268253
}
269254

270255
#[test]
271256
fn test_prebid_request_invalid_url_in_referer() {
272257
let settings = create_test_settings();
273-
let mut req = Request::get("https://example.com/test");
258+
let url = format!("https://{}/test", settings.publisher.domain);
259+
let mut req = Request::get(url);
274260
req.set_header(header::REFERER, "not-a-valid-url");
275261

276262
let prebid_req = PrebidRequest::new(&settings, &req).unwrap();
277263

278264
// Should fallback to default domain
279-
assert_eq!(prebid_req.domain, "auburndao.com");
265+
assert_eq!(prebid_req.domain, settings.publisher.domain);
280266
}
281267

282268
#[test]
283269
fn test_prebid_request_x_forwarded_for_parsing() {
284270
let settings = create_test_settings();
285-
let mut req = Request::get("https://example.com/test");
271+
let url = format!("https://{}/test", settings.publisher.domain);
272+
let mut req = Request::get(url);
286273
req.set_header(HEADER_X_FORWARDED_FOR, "192.168.1.1, 10.0.0.1, 172.16.0.1");
287274

288275
let prebid_req = PrebidRequest::new(&settings, &req).unwrap();
@@ -330,18 +317,19 @@ mod tests {
330317
#[test]
331318
fn test_prebid_request_edge_cases() {
332319
let settings = create_test_settings();
320+
let url = format!("https://{}/test", settings.publisher.domain);
333321

334322
// Test with empty X-Forwarded-For
335-
let mut req = Request::get("https://example.com/test");
323+
let mut req = Request::get(url.clone());
336324
req.set_header(HEADER_X_FORWARDED_FOR, "");
337325
let prebid_req = PrebidRequest::new(&settings, &req).unwrap();
338326
assert!(!prebid_req.client_ip.is_empty() || prebid_req.client_ip.is_empty());
339327

340328
// Test with malformed origin
341-
let mut req2 = Request::get("https://example.com/test");
329+
let mut req2 = Request::get(url.clone());
342330
req2.set_header(header::ORIGIN, "://invalid");
343331
let prebid_req2 = PrebidRequest::new(&settings, &req2).unwrap();
344-
assert_eq!(prebid_req2.domain, "auburndao.com");
332+
assert_eq!(prebid_req2.domain, settings.publisher.domain);
345333
}
346334

347335
// Note: Testing send_bid_request would require mocking the Fastly backend,

0 commit comments

Comments
 (0)