Skip to content

Commit 6ac4a6b

Browse files
committed
Add test for securejoin source stats
1 parent e4ce26c commit 6ac4a6b

File tree

2 files changed

+80
-8
lines changed

2 files changed

+80
-8
lines changed

src/securejoin.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,7 @@ async fn get_self_fingerprint(context: &Context) -> Result<Fingerprint> {
144144
///
145145
/// The function returns immediately and the handshake will run in background.
146146
pub async fn join_securejoin(context: &Context, qr: &str) -> Result<ChatId> {
147-
securejoin(context, qr).await.map_err(|err| {
148-
warn!(context, "Fatal joiner error: {:#}", err);
149-
// The user just scanned this QR code so has context on what failed.
150-
error!(context, "QR process failed");
151-
err
152-
})
147+
join_securejoin_with_source(context, qr, None).await
153148
}
154149

155150
/// Take a scanned QR-code and do the setup-contact/join-group/invite handshake.
@@ -163,7 +158,12 @@ pub async fn join_securejoin_with_source(
163158
qr: &str,
164159
source: Option<u32>,
165160
) -> Result<ChatId> {
166-
let res = join_securejoin(context, qr).await?;
161+
let res = securejoin(context, qr).await.map_err(|err| {
162+
warn!(context, "Fatal joiner error: {:#}", err);
163+
// The user just scanned this QR code so has context on what failed.
164+
error!(context, "QR process failed");
165+
err
166+
})?;
167167

168168
self_reporting::count_securejoin_source(context, source)
169169
.await

src/self_reporting/self_reporting_tests.rs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use super::*;
22
use crate::chat::Chat;
33
use crate::mimeparser::SystemMessage;
4-
use crate::test_utils::{get_chat_msg, TestContext, TestContextManager};
4+
use crate::securejoin::{get_securejoin_qr, join_securejoin, join_securejoin_with_source};
5+
use crate::test_utils::{TestContext, TestContextManager, get_chat_msg};
6+
use pretty_assertions::assert_eq;
57

68
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
79
async fn test_send_self_report() -> Result<()> {
@@ -79,3 +81,73 @@ async fn test_self_report_one_contact() -> Result<()> {
7981

8082
Ok(())
8183
}
84+
85+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
86+
async fn test_self_report_securejoin_source_stats() -> Result<()> {
87+
let mut tcm = TestContextManager::new();
88+
let alice = &tcm.alice().await;
89+
let bob = &tcm.bob().await;
90+
alice.set_config_bool(Config::SelfReporting, true).await?;
91+
92+
let mut expected = SecurejoinSourceStats {
93+
unknown: 0,
94+
external_link: 0,
95+
internal_link: 0,
96+
clipboard: 0,
97+
image_loaded: 0,
98+
scan: 0,
99+
};
100+
101+
check_securejoin_report(alice, &expected).await;
102+
103+
let qr = get_securejoin_qr(bob, None).await?;
104+
105+
join_securejoin(alice, &qr).await?;
106+
expected.unknown += 1;
107+
check_securejoin_report(alice, &expected).await;
108+
109+
join_securejoin(alice, &qr).await?;
110+
expected.unknown += 1;
111+
check_securejoin_report(alice, &expected).await;
112+
113+
join_securejoin_with_source(alice, &qr, Some(SecurejoinSource::Clipboard as u32)).await?;
114+
expected.clipboard += 1;
115+
check_securejoin_report(alice, &expected).await;
116+
117+
join_securejoin_with_source(alice, &qr, Some(SecurejoinSource::ExternalLink as u32)).await?;
118+
expected.external_link += 1;
119+
check_securejoin_report(alice, &expected).await;
120+
121+
join_securejoin_with_source(alice, &qr, Some(SecurejoinSource::InternalLink as u32)).await?;
122+
expected.internal_link += 1;
123+
check_securejoin_report(alice, &expected).await;
124+
125+
join_securejoin_with_source(alice, &qr, Some(SecurejoinSource::ImageLoaded as u32)).await?;
126+
expected.image_loaded += 1;
127+
check_securejoin_report(alice, &expected).await;
128+
129+
join_securejoin_with_source(alice, &qr, Some(SecurejoinSource::Scan as u32)).await?;
130+
expected.scan += 1;
131+
check_securejoin_report(alice, &expected).await;
132+
133+
join_securejoin_with_source(alice, &qr, Some(SecurejoinSource::Clipboard as u32)).await?;
134+
expected.clipboard += 1;
135+
check_securejoin_report(alice, &expected).await;
136+
137+
join_securejoin_with_source(alice, &qr, Some(SecurejoinSource::Clipboard as u32)).await?;
138+
expected.clipboard += 1;
139+
check_securejoin_report(alice, &expected).await;
140+
141+
Ok(())
142+
}
143+
144+
async fn check_securejoin_report(context: &TestContext, expected: &SecurejoinSourceStats) {
145+
let report = get_self_report(context).await.unwrap();
146+
let actual: serde_json::Value = serde_json::from_str(&report).unwrap();
147+
let actual = &actual["securejoin_source_stats"];
148+
149+
let expected = serde_json::to_string_pretty(&expected).unwrap();
150+
let expected: serde_json::Value = serde_json::from_str(&expected).unwrap();
151+
152+
assert_eq!(&expected, actual);
153+
}

0 commit comments

Comments
 (0)