Skip to content

Commit e0c10f5

Browse files
committed
TestContext.get_pgp_chat()
1 parent 7f9a283 commit e0c10f5

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/securejoin/securejoin_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ async fn test_setup_contact_ex(case: SetupContactCase) {
141141
msg.get_header(HeaderDef::SecureJoin).unwrap(),
142142
"vc-auth-required"
143143
);
144-
let bob_chat = bob.get_chat(&alice).await;
144+
let bob_chat = bob.get_pgp_chat(&alice).await;
145145
assert_eq!(bob_chat.can_send(&bob).await.unwrap(), false);
146146
assert_eq!(
147147
bob_chat.why_cant_send(&bob).await.unwrap(),

src/test_utils.rs

+49-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::contact::{import_vcard, make_vcard, Contact, ContactId, Modifier, Ori
3333
use crate::context::Context;
3434
use crate::e2ee::EncryptHelper;
3535
use crate::events::{Event, EventEmitter, EventType, Events};
36-
use crate::key::{self, DcKey};
36+
use crate::key::{self, load_self_public_key, DcKey};
3737
use crate::message::{update_msg_state, Message, MessageState, MsgId, Viewtype};
3838
use crate::mimeparser::{MimeMessage, SystemMessage};
3939
use crate::peerstate::Peerstate;
@@ -706,12 +706,38 @@ impl TestContext {
706706
}
707707

708708
/// Returns the [`Contact`] for the other [`TestContext`], creating it if necessary.
709+
///
710+
/// If the contact does not exist yet, a new contact will be created
711+
/// with the correct fingerprint, but without the public key.
712+
pub async fn add_or_lookup_pgp_contact(&self, other: &TestContext) -> Contact {
713+
let primary_self_addr = other.ctx.get_primary_self_addr().await.unwrap();
714+
let addr = ContactAddress::new(&primary_self_addr).unwrap();
715+
let public_key = load_self_public_key(other).await.unwrap();
716+
let fingerprint = public_key.dc_fingerprint();
717+
718+
let (contact_id, _modified) = Contact::add_or_lookup_ex(
719+
self,
720+
"",
721+
&addr,
722+
&fingerprint.hex(),
723+
Origin::MailinglistAddress,
724+
)
725+
.await
726+
.expect("add_or_lookup");
727+
Contact::get_by_id(&self.ctx, contact_id).await.unwrap()
728+
}
729+
730+
/// Returns the [`Contact`] for the other [`TestContext`], creating it if necessary.
731+
///
732+
/// This function imports a vCard, so will transfer the public key
733+
/// as a side effect.
709734
pub async fn add_or_lookup_contact(&self, other: &TestContext) -> Contact {
710735
let contact_id = self.create_contact_id(other).await;
711736
Contact::get_by_id(&self.ctx, contact_id).await.unwrap()
712737
}
713738

714-
/// Returns 1:1 [`Chat`] with another account. Panics if it doesn't exist.
739+
/// Returns 1:1 [`Chat`] with another account email contact.
740+
/// Panics if it doesn't exist.
715741
/// May return a blocked chat.
716742
///
717743
/// This first creates a contact using the configured details on the other account, then
@@ -731,6 +757,27 @@ impl TestContext {
731757
Chat::load_from_db(&self.ctx, chat_id).await.unwrap()
732758
}
733759

760+
/// Returns 1:1 [`Chat`] with another account PGP-contact.
761+
/// Panics if the chat does not exist.
762+
///
763+
/// This first creates a contact, but does not import the key,
764+
/// so may create a PGP-contact with a fingerprint
765+
/// but without the key.
766+
pub async fn get_pgp_chat(&self, other: &TestContext) -> Chat {
767+
let contact = self.add_or_lookup_pgp_contact(other).await;
768+
769+
let chat_id = ChatIdBlocked::lookup_by_contact(&self.ctx, contact.id)
770+
.await
771+
.unwrap()
772+
.map(|chat_id_blocked| chat_id_blocked.id)
773+
.expect(
774+
"There is no chat with this contact. \
775+
Hint: Use create_chat() instead of get_chat() if this is expected.",
776+
);
777+
778+
Chat::load_from_db(&self.ctx, chat_id).await.unwrap()
779+
}
780+
734781
/// Creates a contact for another account.
735782
///
736783
/// This exports a vCard from the `other`

0 commit comments

Comments
 (0)