autoconnect_common/
test_support.rs

1use bytestring::ByteString;
2use uuid::Uuid;
3
4use crate::protocol::MessageType;
5use autopush_common::{
6    db::{mock::MockDbClient, User},
7    util::timing::ms_since_epoch,
8};
9
10pub const UA: &str =
11    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/110.0";
12
13pub const DUMMY_UAID: Uuid = Uuid::from_u128(0xdeadbeef_0000_0000_deca_fbad00000000);
14pub const DUMMY_CHID: Uuid = Uuid::from_u128(0xdeadbeef_0000_0000_abad_1dea00000000);
15
16/// A minimal websocket Push "hello" message, used by an unregistered UA with
17/// no existing channel subscriptions
18pub fn hello_json() -> ByteString {
19    format!(
20        r#"{{"messageType": "{}", "use_webpush": true}}"#,
21        MessageType::Hello.as_ref()
22    )
23    .into()
24}
25
26pub fn hello_again_json() -> ByteString {
27    format!(
28        r#"{{"messageType": "{}", "use_webpush": true,
29                "uaid": "{}"}}"#,
30        MessageType::Hello.as_ref(),
31        DUMMY_UAID
32    )
33    .into()
34}
35
36pub const CURRENT_MONTH: &str = "message_2018_06";
37
38/// Return a simple MockDbClient that responds to hello (once) with a new uaid.
39pub fn hello_db() -> MockDbClient {
40    MockDbClient::new()
41}
42
43/// Return a simple MockDbClient that responds to hello (once) with the
44/// specified uaid.
45pub fn hello_again_db(uaid: Uuid) -> MockDbClient {
46    let mut db = MockDbClient::new();
47    db.expect_get_user().times(1).return_once(move |_| {
48        let user = User::builder()
49            .uaid(uaid)
50            .connected_at(ms_since_epoch() - (10 * 60 * 1000))
51            .build()
52            .unwrap();
53        Ok(Some(user))
54    });
55
56    db.expect_update_user().times(1).return_once(|_| Ok(true));
57    db.expect_fetch_topic_messages()
58        .times(1)
59        .return_once(|_, _| Ok(Default::default()));
60    db.expect_fetch_timestamp_messages()
61        .times(1)
62        .return_once(|_, _, _| Ok(Default::default()));
63    db
64}