| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
pub(crate) use base64::Engine as _; |
| 12 |
pub(crate) use chrono::Utc; |
| 13 |
pub(crate) use serde_json::json; |
| 14 |
pub(crate) use sha2::Digest as _; |
| 15 |
pub(crate) use std::sync::Arc; |
| 16 |
pub(crate) use std::time::Duration; |
| 17 |
pub(crate) use uuid::Uuid; |
| 18 |
pub(crate) use wiremock::ResponseTemplate; |
| 19 |
|
| 20 |
pub(crate) use crate::mockkit::MockKit; |
| 21 |
|
| 22 |
pub(crate) use synckit_client::{ |
| 23 |
AppId, ChangeEntry, ChangeOp, DeviceId, Hlc, SyncKitClient, SyncKitConfig, SyncKitError, UserId, |
| 24 |
}; |
| 25 |
|
| 26 |
pub(crate) fn fake_jwt(exp: i64) -> String { |
| 27 |
let header = |
| 28 |
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(r#"{"alg":"HS256","typ":"JWT"}"#); |
| 29 |
let payload = json!({ |
| 30 |
"sub": "550e8400-e29b-41d4-a716-446655440000", |
| 31 |
"app": "6ba7b810-9dad-11d1-80b4-00c04fd430c8", |
| 32 |
"exp": exp, |
| 33 |
"iat": exp - 3600, |
| 34 |
}); |
| 35 |
let payload_b64 = |
| 36 |
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(payload.to_string().as_bytes()); |
| 37 |
let sig = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(b"fake-signature"); |
| 38 |
format!("{header}.{payload_b64}.{sig}") |
| 39 |
} |
| 40 |
|
| 41 |
pub(crate) fn fresh_token() -> String { |
| 42 |
fake_jwt(Utc::now().timestamp() + 3600) |
| 43 |
} |
| 44 |
|
| 45 |
pub(crate) fn test_ids() -> (UserId, AppId) { |
| 46 |
( |
| 47 |
UserId::new(Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap()), |
| 48 |
AppId::new(Uuid::parse_str("6ba7b810-9dad-11d1-80b4-00c04fd430c8").unwrap()), |
| 49 |
) |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
pub(crate) fn ensure_crypto_provider() { |
| 56 |
static PROVIDER: std::sync::Once = std::sync::Once::new(); |
| 57 |
PROVIDER.call_once(|| { |
| 58 |
|
| 59 |
let _ = rustls::crypto::ring::default_provider().install_default(); |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
pub(crate) fn auth_response_json() -> serde_json::Value { |
| 64 |
let (user_id, app_id) = test_ids(); |
| 65 |
json!({ |
| 66 |
"token": fresh_token(), |
| 67 |
"user_id": user_id, |
| 68 |
"app_id": app_id, |
| 69 |
}) |
| 70 |
} |
| 71 |
|
| 72 |
pub(crate) fn device_json() -> serde_json::Value { |
| 73 |
let (user_id, app_id) = test_ids(); |
| 74 |
json!({ |
| 75 |
"id": Uuid::new_v4(), |
| 76 |
"app_id": app_id, |
| 77 |
"user_id": user_id, |
| 78 |
"device_name": "Test Device", |
| 79 |
"platform": "test", |
| 80 |
"last_seen_at": "2025-01-01T00:00:00Z", |
| 81 |
"created_at": "2025-01-01T00:00:00Z", |
| 82 |
}) |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
#[cfg(feature = "keychain")] |
| 100 |
pub(crate) fn ensure_mock_keystore() { |
| 101 |
static STORE: std::sync::Once = std::sync::Once::new(); |
| 102 |
STORE.call_once(|| { |
| 103 |
keyring_core::set_default_store( |
| 104 |
keyring_core::mock::Store::new().expect("the mock keychain store"), |
| 105 |
); |
| 106 |
}); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
#[cfg(not(feature = "keychain"))] |
| 112 |
pub(crate) fn ensure_mock_keystore() {} |
| 113 |
|