Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 16ae251

Browse files
committed
Add tests for changing nostr keys
1 parent 2001356 commit 16ae251

1 file changed

Lines changed: 64 additions & 1 deletion

File tree

mutiny-core/src/nostr/mod.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,7 @@ mod test {
22572257
use mockall::predicate::eq;
22582258
use nostr::prelude::rand;
22592259
use nostr::prelude::rand::prelude::SliceRandom;
2260+
use nostr::SubscriptionId;
22602261
use std::str::FromStr;
22612262

22622263
const EXPIRED_INVOICE: &str = "lnbc923720n1pj9nr6zpp5xmvlq2u5253htn52mflh2e6gn7pk5ht0d4qyhc62fadytccxw7hqhp5l4s6qwh57a7cwr7zrcz706qx0qy4eykcpr8m8dwz08hqf362egfscqzzsxqzfvsp5pr7yjvcn4ggrf6fq090zey0yvf8nqvdh2kq7fue0s0gnm69evy6s9qyyssqjyq0fwjr22eeg08xvmz88307yqu8tqqdjpycmermks822fpqyxgshj8hvnl9mkh6srclnxx0uf4ugfq43d66ak3rrz4dqcqd23vxwpsqf7dmhm";
@@ -2276,7 +2277,7 @@ mod test {
22762277

22772278
#[allow(unused_mut)] // need this because of mockall
22782279
let mut client = MockNostrClient::new();
2279-
client.expect_set_signer().return_const(());
2280+
client.expect_set_signer().once().return_const(());
22802281

22812282
NostrManager::from_mnemonic(
22822283
xprivkey,
@@ -3045,4 +3046,66 @@ mod test {
30453046
let list = nostr_manager.get_follow_list().unwrap();
30463047
assert_eq!(list.len(), 1);
30473048
}
3049+
3050+
#[tokio::test]
3051+
async fn test_change_nostr_keys() {
3052+
let mut nostr_manager = create_nostr_manager().await;
3053+
nostr_manager
3054+
.client
3055+
.expect_send_event()
3056+
.returning(|e| Ok(e.id));
3057+
nostr_manager
3058+
.client
3059+
.expect_send_event_builder()
3060+
.returning(|e| Ok(e.to_event(&Keys::generate()).unwrap().id));
3061+
// create follow list
3062+
nostr_manager
3063+
.follow_npub(Keys::generate().public_key())
3064+
.await
3065+
.unwrap();
3066+
// create a profile
3067+
nostr_manager
3068+
.primal_client
3069+
.expect_get_user_profile()
3070+
.return_once(|_| Ok(None));
3071+
nostr_manager
3072+
.edit_profile(Some("test profile".to_string()), None, None, None)
3073+
.await
3074+
.unwrap();
3075+
3076+
let new_keys = Keys::generate();
3077+
3078+
let xprivkey = ExtendedPrivKey::new_master(Network::Bitcoin, &[0; 32]).unwrap();
3079+
let source = NostrKeySource::Imported(new_keys.clone());
3080+
3081+
nostr_manager
3082+
.client
3083+
.expect_set_signer()
3084+
.once()
3085+
.return_once(|_| ());
3086+
3087+
nostr_manager
3088+
.client
3089+
.expect_subscribe()
3090+
.once()
3091+
.withf(|filters, opt| filters.len() == 2 && opt.is_none())
3092+
.return_once(|_, _| SubscriptionId::generate());
3093+
3094+
let new_pk = nostr_manager
3095+
.change_nostr_keys(source, xprivkey)
3096+
.await
3097+
.unwrap();
3098+
3099+
// easy tests
3100+
assert_eq!(new_pk, new_keys.public_key());
3101+
let npub = nostr_manager.get_npub().await;
3102+
assert_eq!(npub, new_keys.public_key());
3103+
3104+
// make sure we get a different follow list and profile
3105+
let list = nostr_manager.get_follow_list().unwrap();
3106+
assert!(list.is_empty());
3107+
let profile = nostr_manager.get_profile().unwrap();
3108+
assert_ne!(profile.name, Some("test profile".to_string()));
3109+
assert_eq!(profile, Metadata::default());
3110+
}
30483111
}

0 commit comments

Comments
 (0)